feat: engine 透传 error_code(与 error 同源取最后一次异常)
This commit is contained in:
@@ -81,6 +81,65 @@ def test_chat_all_failed_returns_failed():
|
||||
assert r.status == "failed" and r.error
|
||||
|
||||
|
||||
# ---------- error_code 透传(与 error 同源取最后一次异常) ----------
|
||||
|
||||
def test_chat_failed_error_code_timeout():
|
||||
# 主模型超时 + 备用也失败:error_code 与 error 同源(取最后一次异常)
|
||||
client = FakeLLMClient([("raise_timeout", ""), ("raise_timeout", "")])
|
||||
eng = InferenceEngine(client=client, models=Models())
|
||||
r = eng.chat(session_id="s1", prompt=Prompt(name="p", version="v1", template="t"), variables={})
|
||||
assert r.status == "failed"
|
||||
assert r.error_code == "LLM_TIMEOUT"
|
||||
|
||||
|
||||
def test_chat_failed_error_code_network_last():
|
||||
# 主模型超时(第一次)、备用网络失败(最后一次)→ error_code 取最后一次 = LLM_NETWORK_ERROR
|
||||
client = FakeLLMClient([("raise_timeout", ""), ("raise_network", "")])
|
||||
eng = InferenceEngine(client=client, models=Models())
|
||||
r = eng.chat(session_id="s1", prompt=Prompt(name="p", version="v1", template="t"), variables={})
|
||||
assert r.status == "failed"
|
||||
assert r.error_code == "LLM_NETWORK_ERROR"
|
||||
|
||||
|
||||
def test_chat_failed_error_code_not_configured():
|
||||
# 备用模型 Key 缺失(最后一次)→ LLM_NOT_CONFIGURED
|
||||
client = FakeLLMClient([("raise_network", ""), ("raise_timeout", "")])
|
||||
# 用自定义异常客户端模拟 NotConfigured
|
||||
class NotConfiguredClient:
|
||||
def __init__(self):
|
||||
self.calls = []
|
||||
def chat(self, *, model, messages, temperature, max_tokens):
|
||||
self.calls.append(model)
|
||||
from genesis.inference.exceptions import LLMNotConfiguredError
|
||||
raise LLMNotConfiguredError("no key")
|
||||
eng = InferenceEngine(client=NotConfiguredClient(), models=Models())
|
||||
r = eng.chat(session_id="s1", prompt=Prompt(name="p", version="v1", template="t"), variables={})
|
||||
assert r.status == "failed"
|
||||
assert r.error_code == "LLM_NOT_CONFIGURED"
|
||||
|
||||
|
||||
def test_chat_structured_parse_error_code():
|
||||
client = FakeLLMClient([("parse_fail", ""), ("parse_fail", "")])
|
||||
eng = InferenceEngine(client=client, models=Models())
|
||||
r = eng.chat_structured(
|
||||
session_id="s1", prompt=Prompt(name="p", version="v1", template="提取"),
|
||||
variables={}, schema={}, retry_count=1,
|
||||
)
|
||||
assert r.status == "parse_error"
|
||||
assert r.error_code == "LLM_PARSE_ERROR"
|
||||
|
||||
|
||||
def test_chat_structured_failed_error_code_network():
|
||||
client = FakeLLMClient([("raise_network", "")])
|
||||
eng = InferenceEngine(client=client, models=Models())
|
||||
r = eng.chat_structured(
|
||||
session_id="s1", prompt=Prompt(name="p", version="v1", template="提取"),
|
||||
variables={}, schema={},
|
||||
)
|
||||
assert r.status == "failed"
|
||||
assert r.error_code == "LLM_NETWORK_ERROR"
|
||||
|
||||
|
||||
def test_chat_plain_string_prompt():
|
||||
eng = make_engine(FakeLLMClient([("ok", "hi")]))
|
||||
r = eng.chat(session_id="s1", prompt="直接文本", variables={})
|
||||
|
||||
Reference in New Issue
Block a user