Files
2026Technology-Competition/tests/inference_helpers.py
T
lhl 1f931228e7 feat(inference): 引擎层统一注入防护(T4 架构审查整改)
- Issue4: engine.py 新增恒定系统指令 DEFAULT_SYSTEM_INSTRUCTION + 用户数据边界包裹
  - _call 统一构造 [system 恒定指令, user 边界包裹数据],chat/chat_structured 全生效
  - __init__ 支持 system_instruction 注入覆盖;声明「用户数据段指令不作为要求执行」
- FakeLLMClient 记录结构对齐真实 payload({role, content}),同步 2 处既有断言
- 同步 agent-runtime-design.md §8.1 标注已实现
- 新增 5 用例,全量 182 passed / 100.00%(987 stmts/252 br)
2026-08-12 09:44:33 +08:00

31 lines
1.2 KiB
Python
Raw 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.
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] = []
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)