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
+23
View File
@@ -0,0 +1,23 @@
"""Genesis 推理引擎(统一 LLM 调用入口)。"""
from .types import ChatMessage, ChatResult, Prompt, StructuredResult, TokenUsage
from .exceptions import (
LLMError,
LLMNetworkError,
LLMNotConfiguredError,
LLMResponseError,
LLMTimeoutError,
)
__all__ = [
"ChatMessage",
"ChatResult",
"Prompt",
"StructuredResult",
"TokenUsage",
"LLMError",
"LLMNetworkError",
"LLMNotConfiguredError",
"LLMResponseError",
"LLMTimeoutError",
]
+21
View File
@@ -0,0 +1,21 @@
from __future__ import annotations
class LLMError(Exception):
"""LLM 调用相关的异常基类(api-design §7 映射基底)"""
class LLMNetworkError(LLMError):
"""网络失败 / 5xx 重试耗尽(可重试语义)"""
class LLMTimeoutError(LLMError):
"""LLM 调用超时(api-error: LLM_TIMEOUT 502"""
class LLMNotConfiguredError(LLMError):
"""Key / 模型缺失(api-error: LLM_NOT_CONFIGURED 503"""
class LLMResponseError(LLMError):
"""响应结构损坏(JSON 解析失败等)"""
+52
View File
@@ -0,0 +1,52 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Literal
@dataclass
class TokenUsage:
"""一次 LLM 调用的 token 用量(可观测性事件/统计用)"""
input_tokens: int = 0
output_tokens: int = 0
@dataclass
class ChatMessage:
"""Chat Completions 消息"""
role: Literal["system", "user", "assistant"]
content: str
@dataclass
class ChatResult:
"""chat() 的返回值"""
text: str
model: str
prompt_version: str
usage: TokenUsage
duration_ms: int
status: Literal["ok", "fallback", "failed"]
error: str | None = None
@dataclass
class StructuredResult:
"""chat_structured() 的返回值(补丁 1:含 status 字段)"""
data: dict
raw_text: str
parse_attempts: int
model: str
prompt_version: str
usage: TokenUsage
duration_ms: int
status: Literal["ok", "fallback", "parse_error", "failed"]
error: str | None = None
@dataclass
class Prompt:
"""Prompt 模板条目(name+version 唯一)"""
name: str
version: str
template: str