feat: 推理引擎数据模型与异常层(types/exceptions)及 httpx/jinja2 依赖

This commit is contained in:
lhl
2026-08-09 05:53:55 +08:00
parent 2fbff07d3f
commit c2fad77698
6 changed files with 159 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
import pytest
from genesis.inference.exceptions import (
LLMError,
LLMNetworkError,
LLMNotConfiguredError,
LLMResponseError,
LLMTimeoutError,
)
def test_error_hierarchy():
assert issubclass(LLMNetworkError, LLMError)
assert issubclass(LLMTimeoutError, LLMError)
assert issubclass(LLMNotConfiguredError, LLMError)
assert issubclass(LLMResponseError, LLMError)
def test_error_message_roundtrip():
e = LLMTimeoutError("timeout!")
assert str(e) == "timeout!"
+40
View File
@@ -0,0 +1,40 @@
from genesis.inference.types import ChatMessage, ChatResult, Prompt, StructuredResult, TokenUsage
def test_token_usage_defaults():
u = TokenUsage()
assert u.input_tokens == 0 and u.output_tokens == 0
def test_chat_message_roles():
assert ChatMessage(role="system", content="x").content == "x"
def test_chat_result_defaults():
r = ChatResult(
text="t", model="m", prompt_version="v1", usage=TokenUsage(),
duration_ms=10, status="ok",
)
assert r.status == "ok" and r.error is None
def test_structured_result_status_ok():
r = StructuredResult(
data={"a": 1}, raw_text='{"a":1}', parse_attempts=1, model="m",
prompt_version="v1", usage=TokenUsage(), duration_ms=10, status="ok",
)
assert r.data == {"a": 1} and r.raw_text == '{"a":1}'
def test_structured_result_status_parse_error():
r = StructuredResult(
data={}, raw_text="NOT JSON", parse_attempts=3, model="m",
prompt_version="v1", usage=TokenUsage(), duration_ms=50,
status="parse_error", error="bad json",
)
assert r.status == "parse_error" and r.error == "bad json"
def test_prompt_fields():
p = Prompt(name="writer", version="v2", template="章节 {{chapter}}")
assert p.name == "writer" and p.version == "v2"