- Issue9: client.py 由同步 httpx.Client 全异步化 - LLMClient Protocol / HttpLLMClient.chat → async;httpx.AsyncClient + asyncio.sleep 退避 - __enter__/__exit__ → __aenter__/__aexit__(async with 生命周期闭环) - engine.py chat/chat_structured/_call 全部 async + await - FakeLLMClient.chat → async;测试用 anyio pytest 插件转换(engine 32 + client 10 用例) - 同步 inference-engine spec 与 milestone3 review 的 httpx 描述 - 全量 182 passed / 100.00%(987 stmts/252 br)
31 lines
1.2 KiB
Python
31 lines
1.2 KiB
Python
from __future__ import annotations
|
||
|
||
from genesis.inference.types import TokenUsage
|
||
|
||
|
||
class FakeLLMClient:
|
||
"""可编程的假 LLM 客户端:记录调用,按脚本返回(离线)。"""
|
||
|
||
def __init__(self, script=None):
|
||
# script: list[(status, content)];status: "ok" | "raise_timeout" | "raise_network" | "parse_fail"
|
||
self.script = script or [("ok", "hello")]
|
||
self.calls: list[dict] = []
|
||
|
||
async def chat(self, *, model, messages, temperature, max_tokens):
|
||
# 与真实 HttpLLMClient 的 payload 结构一致:{role, content}(T4 防护断言 role)
|
||
self.calls.append({
|
||
"model": model,
|
||
"messages": [{"role": m.role, "content": m.content} for m in messages],
|
||
})
|
||
status, content = self.script.pop(0)
|
||
if status == "raise_timeout":
|
||
from genesis.inference.exceptions import LLMTimeoutError
|
||
|
||
raise LLMTimeoutError("timeout")
|
||
if status == "raise_network":
|
||
from genesis.inference.exceptions import LLMNetworkError
|
||
|
||
raise LLMNetworkError("network")
|
||
if status == "parse_fail":
|
||
content = "NOT JSON"
|
||
return content, TokenUsage(input_tokens=10, output_tokens=2) |