Files

36 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""客户端公共基类:推理指标与错误类型
Ollama 原生客户端与 OpenAI 兼容客户端共享同一数据模型,
测试用例通过统一接口(generate/chat)调用,无需关心后端差异。
"""
from dataclasses import dataclass
class ClientError(Exception):
"""推理服务调用相关错误(统一异常基类)"""
@dataclass
class InferenceResult:
"""单次推理的完整指标(两套后端共用)"""
ttft_ms: float = 0.0 # 首 Token 延迟(毫秒)
prefill_ms: float = 0.0 # 预填充/处理 prompt 时间(毫秒)
decode_speed_tok_s: float = 0.0 # 解码速度(tokens/s)
total_tokens: int = 0 # 总 token 数
prompt_tokens: int = 0 # prompt token 数
completion_tokens: int = 0 # 生成 token 数
e2e_ms: float = 0.0 # 端到端耗时(毫秒)
response: str = "" # 完整回复文本
def to_dict(self) -> dict:
return {
"ttft_ms": round(self.ttft_ms, 2),
"prefill_ms": round(self.prefill_ms, 2),
"decode_speed_tok_s": round(self.decode_speed_tok_s, 2),
"total_tokens": self.total_tokens,
"prompt_tokens": self.prompt_tokens,
"completion_tokens": self.completion_tokens,
"e2e_ms": round(self.e2e_ms, 2),
"response_length": len(self.response),
}