feat: 支持 LLM 请求超时配置
This commit is contained in:
@@ -10,6 +10,22 @@ from wov_node_llm.__main__ import invoke, translate_lines
|
||||
from wov_sdk.models import InvokeRequest
|
||||
|
||||
|
||||
class FakeUrlOpenResponse:
|
||||
def __init__(self, content: str) -> None:
|
||||
self._payload = json.dumps(
|
||||
{"choices": [{"message": {"content": content}}]}
|
||||
).encode("utf-8")
|
||||
|
||||
def read(self) -> bytes:
|
||||
return self._payload
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *args) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _make_srt(tmp_path, count=5) -> Path:
|
||||
lines = []
|
||||
for index in range(count):
|
||||
@@ -83,6 +99,38 @@ def test_translate_lines_api_error(monkeypatch) -> None:
|
||||
pass
|
||||
|
||||
|
||||
def test_translate_lines_default_timeout(monkeypatch) -> None:
|
||||
captured = {}
|
||||
|
||||
def fake_open(request, timeout):
|
||||
captured["timeout"] = timeout
|
||||
return FakeUrlOpenResponse("译文一")
|
||||
|
||||
monkeypatch.setattr("wov_node_llm.__main__.urllib.request.urlopen", fake_open)
|
||||
monkeypatch.setenv("LLM_API_BASE", "http://fake/v1/chat/completions")
|
||||
|
||||
result = translate_lines(["一"], {})
|
||||
|
||||
assert result == ["译文一"]
|
||||
assert captured["timeout"] == 600
|
||||
|
||||
|
||||
def test_translate_lines_env_timeout(monkeypatch) -> None:
|
||||
captured = {}
|
||||
|
||||
def fake_open(request, timeout):
|
||||
captured["timeout"] = timeout
|
||||
return FakeUrlOpenResponse("译文一")
|
||||
|
||||
monkeypatch.setattr("wov_node_llm.__main__.urllib.request.urlopen", fake_open)
|
||||
monkeypatch.setenv("LLM_API_BASE", "http://fake/v1/chat/completions")
|
||||
monkeypatch.setenv("LLM_TIMEOUT_SECONDS", "45")
|
||||
|
||||
translate_lines(["一"], {})
|
||||
|
||||
assert captured["timeout"] == 45
|
||||
|
||||
|
||||
def test_invoke_success(tmp_path, monkeypatch) -> None:
|
||||
source = _make_srt(tmp_path)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user