feat(v2): 架构与算法优化二轮——推理机不变量外提、知识库匹配预编译、协作循环增量索引
- inference.py(/chat/legacy 热路径):kb.match 循环不变量外提(原每步全量重扫+重排序, 最坏 O(steps×rules×patterns));fired 查重 list→set - knowledge.py:Rule patterns 注册侧懒缓存小写副本(原每条规则每次匹配重复 lower); match() 文本只 lower 一次(原逐规则重复);load() 的 yaml 文件名集合提到循环外 - worker.py:本地端点生成器 httpx.AsyncClient 懒建复用(原每步新建/销毁连接, 对齐 ArchitectClient 惯用法;协作循环最多 10 次生成免重复建连) - pipeline.py(协作循环):plan_by_id O(1) 步定义查找;done 集合增量维护 (原每轮重建 progress+archive 扫描);领域只解析一次(原 _artifact_name 每步 全领域 kb.match);_deps_done 支持传入预填集合(保持旧签名兼容) - v2stats.py:回合数分布改增量聚合(sum/max/分桶计数),summary() O(n)→O(1), 不再持有无界 list(修长时运行内存增长) - gateway/agent.py + api.py:AgentService 运行计数 O(1) 化(原 register 全量扫描), 状态迁移收敛到 _transition_state 单一入口(api.py cancel/异常两处绕过点一并接入, 消除计数与状态脱节隐患);21 项 agent 测试全绿(两轮全量 230 passed 复核)
This commit is contained in:
+17
-9
@@ -188,25 +188,33 @@ def _mock_generate() -> Callable[[str], Awaitable[str]]:
|
||||
|
||||
def _make_llama_generate(cfg: Dict[str, Any],
|
||||
default_base_url: Optional[str] = None) -> Callable[[str], Awaitable[str]]:
|
||||
"""返回调用本地 OpenAI 兼容端点(llama-server / Ollama / vLLM)的生成器。"""
|
||||
"""返回调用本地 OpenAI 兼容端点(llama-server / Ollama / vLLM)的生成器。
|
||||
|
||||
连接复用:httpx.AsyncClient 懒建一次、跨步骤复用(与 ArchitectClient 一致),
|
||||
避免协作循环每步重新 TCP 建连。
|
||||
"""
|
||||
if default_base_url is None:
|
||||
default_base_url = f"http://127.0.0.1:{cfg.get('port', 8901)}/v1"
|
||||
base_url = cfg.get("base_url") or default_base_url
|
||||
model = cfg.get("model") or "local"
|
||||
temperature = float(cfg.get("temperature", 0.3))
|
||||
timeout_s = float(cfg.get("per_step_timeout_s", 300))
|
||||
client_holder: Dict[str, Any] = {"client": None}
|
||||
|
||||
async def _gen(prompt: str) -> str:
|
||||
try:
|
||||
import httpx
|
||||
async with httpx.AsyncClient(timeout=timeout_s) as client:
|
||||
resp = await client.post(
|
||||
f"{base_url}/chat/completions",
|
||||
json={"model": model, "messages": [{"role": "user", "content": prompt}],
|
||||
"temperature": temperature, "max_tokens": 4096},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()["choices"][0]["message"]["content"]
|
||||
client = client_holder["client"]
|
||||
if client is None or client.is_closed:
|
||||
client = httpx.AsyncClient(timeout=timeout_s)
|
||||
client_holder["client"] = client
|
||||
resp = await client.post(
|
||||
f"{base_url}/chat/completions",
|
||||
json={"model": model, "messages": [{"role": "user", "content": prompt}],
|
||||
"temperature": temperature, "max_tokens": 4096},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()["choices"][0]["message"]["content"]
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 连不上本地模型 -> 优雅降级(不抛 500),提示用户检查模型端点
|
||||
return ("(本地降级)无法连接本地模型端点,未能生成该步骤内容。"
|
||||
|
||||
Reference in New Issue
Block a user