mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-18 22:42:00 +00:00
c02ef189bb
起因是一条没人看得懂的报错:
工具 db_query__shop 执行失败:
_make_query_tool.<locals>._run() got an unexpected keyword argument 'query'
参数名写成了 query(它叫 sql),而 call_tool 对动态工具是裸 **args 展开的,
跳过了 args_schema —— pydantic 那句"缺少 sql 字段"漏成了只剩内部闭包名的
TypeError,既指不到是哪个工具,也说不出该填什么。同一个函数里内置工具那条
分支本来是校验的,两条路径不一致。
registry 里补上 prepare_args / describe_args,两条路径共用:
- 候选唯一时(多一个不认识的、少一个必填的)改名重试,并发一条 warn 说破
—— 配置里那个错名还在,替它跑通一次却不吭声,下次照样踩
- 有歧义就不猜,报错改成"参数不对:query 不是它的参数;缺少必填参数 sql。
它接受:sql(必填,字符串)— 要执行的 SQL…",模型照着这句就能改对。
这才是失败之后真正能自愈的重试
- 顺带堵了一类静默失败:pydantic 默认把认不出的键悄悄丢掉,全可选 schema 上
{"tabel": "orders"} 会"成功"执行一次什么都没查的调用
agent 循环默认改成串行:一轮只调一个工具,看到结果再想下一步。并行更快,但
一批调用中间没有任何新的思考——第二个是照着"还没看到第一个结果"时的判断发出
去的。节点上留了「并行执行工具」开关。两个必须守住的点:没执行的调用绝不发
tool.start(前端靠 call_id 配对,发了没结尾的 start 会永远转圈),但必须各有
一条配对的 tool_result(少一条下次调用直接 400)。
交错思考跟着补上。4.6 起 adaptive thinking 自带交错,不需要也不该挂 beta
header(Opus 4.6 上手动模式压根没有交错);4/4.5 那一代要手动开 thinking +
interleaved-thinking beta,光挂 header 是一条什么都不做的空设置。开了 thinking
就不能再传 temperature,这一代本来是收的,漏了就是 400。
还有两处是串行带出来的,一并治了:
- 步数用完时收尾取的是 messages[-1],而那往往是给模型看的 ToolMessage。
真漏到用户面前当过答案(run dd9927e6):一句"本轮只执行了第一个工具"冒充
结论。改成只取模型自己说过的话,并把"步数用完了、去调大最大步数"附在后面
- db_schema 一次能问多张表。一次调用就是一步,逐张问 6 张表就是 6 步——
实测 8 步的运行里 7 步花在这上面,最后只剩一步真查数据
228 lines
8.3 KiB
Python
228 lines
8.3 KiB
Python
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
# --------------------------------------------------------------------------
|
||
# 模型目录
|
||
#
|
||
# pricing 单位是「美元 / 百万 token」,用于 run 的成本估算。
|
||
# 新模型的价格会变,这份表只是开箱即用的默认值,用户可以在设置页改。
|
||
# --------------------------------------------------------------------------
|
||
|
||
ANTHROPIC_MODELS: list[dict[str, Any]] = [
|
||
{"id": "claude-opus-5", "label": "Claude Opus 5", "context": 1_000_000,
|
||
"pricing": {"input": 5.0, "output": 25.0}, "thinking": "adaptive"},
|
||
{"id": "claude-fable-5-1", "label": "Claude Fable 5.1", "context": 1_000_000,
|
||
"pricing": {"input": 10.0, "output": 50.0}, "thinking": "always"},
|
||
{"id": "claude-opus-4-8", "label": "Claude Opus 4.8", "context": 1_000_000,
|
||
"pricing": {"input": 5.0, "output": 25.0}, "thinking": "adaptive"},
|
||
{"id": "claude-sonnet-5", "label": "Claude Sonnet 5", "context": 1_000_000,
|
||
"pricing": {"input": 2.0, "output": 10.0}, "thinking": "adaptive"},
|
||
{"id": "claude-haiku-4-5", "label": "Claude Haiku 4.5", "context": 200_000,
|
||
"pricing": {"input": 1.0, "output": 5.0}, "thinking": "budget"},
|
||
]
|
||
|
||
OPENAI_MODELS: list[dict[str, Any]] = [
|
||
{"id": "gpt-5.2", "label": "GPT-5.2", "context": 400_000,
|
||
"pricing": {"input": 1.25, "output": 10.0}},
|
||
{"id": "gpt-5.2-mini", "label": "GPT-5.2 mini", "context": 400_000,
|
||
"pricing": {"input": 0.25, "output": 2.0}},
|
||
{"id": "o4", "label": "o4", "context": 200_000,
|
||
"pricing": {"input": 3.0, "output": 12.0}},
|
||
]
|
||
|
||
# 常见的 OpenAI 兼容服务,一键添加用
|
||
COMPATIBLE_PRESETS: list[dict[str, Any]] = [
|
||
{
|
||
"key": "deepseek",
|
||
"label": "DeepSeek",
|
||
"base_url": "https://api.deepseek.com/v1",
|
||
"models": [
|
||
{"id": "deepseek-chat", "label": "DeepSeek Chat", "context": 128_000,
|
||
"pricing": {"input": 0.27, "output": 1.1}},
|
||
{"id": "deepseek-reasoner", "label": "DeepSeek Reasoner", "context": 128_000,
|
||
"pricing": {"input": 0.55, "output": 2.19}},
|
||
],
|
||
},
|
||
{
|
||
"key": "moonshot",
|
||
"label": "Moonshot / Kimi",
|
||
"base_url": "https://api.moonshot.cn/v1",
|
||
"models": [
|
||
{"id": "kimi-k2-turbo-preview", "label": "Kimi K2 Turbo", "context": 256_000,
|
||
"pricing": {"input": 0.6, "output": 2.5}},
|
||
{"id": "moonshot-v1-128k", "label": "Moonshot v1 128k", "context": 128_000,
|
||
"pricing": {"input": 0.8, "output": 3.0}},
|
||
],
|
||
},
|
||
{
|
||
"key": "dashscope",
|
||
"label": "阿里云百炼 / 通义千问",
|
||
"base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||
"models": [
|
||
{"id": "qwen-max", "label": "Qwen Max", "context": 32_000,
|
||
"pricing": {"input": 0.34, "output": 1.37}},
|
||
{"id": "qwen-plus", "label": "Qwen Plus", "context": 131_000,
|
||
"pricing": {"input": 0.11, "output": 0.28}},
|
||
],
|
||
},
|
||
{
|
||
"key": "zhipu",
|
||
"label": "智谱 GLM",
|
||
"base_url": "https://open.bigmodel.cn/api/paas/v4",
|
||
"models": [
|
||
{"id": "glm-4.6", "label": "GLM-4.6", "context": 200_000,
|
||
"pricing": {"input": 0.6, "output": 2.2}},
|
||
],
|
||
},
|
||
{
|
||
"key": "siliconflow",
|
||
"label": "硅基流动",
|
||
"base_url": "https://api.siliconflow.cn/v1",
|
||
"models": [
|
||
{"id": "deepseek-ai/DeepSeek-V3", "label": "DeepSeek V3", "context": 128_000,
|
||
"pricing": {"input": 0.27, "output": 1.1}},
|
||
],
|
||
},
|
||
{
|
||
"key": "openrouter",
|
||
"label": "OpenRouter",
|
||
"base_url": "https://openrouter.ai/api/v1",
|
||
"models": [
|
||
{"id": "anthropic/claude-opus-5", "label": "Claude Opus 5", "context": 1_000_000,
|
||
"pricing": {"input": 5.0, "output": 25.0}},
|
||
],
|
||
},
|
||
{
|
||
"key": "ollama",
|
||
"label": "Ollama(本地)",
|
||
"base_url": "http://localhost:11434/v1",
|
||
"api_key_optional": True,
|
||
"models": [
|
||
{"id": "qwen3:8b", "label": "Qwen3 8B (本地)", "context": 32_000,
|
||
"pricing": {"input": 0.0, "output": 0.0}},
|
||
{"id": "llama3.2", "label": "Llama 3.2 (本地)", "context": 128_000,
|
||
"pricing": {"input": 0.0, "output": 0.0}},
|
||
],
|
||
},
|
||
]
|
||
|
||
MOCK_MODELS: list[dict[str, Any]] = [
|
||
{"id": "mock-fast", "label": "Mock(不花钱,用来试编排)", "context": 128_000,
|
||
"pricing": {"input": 0.0, "output": 0.0}},
|
||
]
|
||
|
||
PROVIDER_KINDS = [
|
||
{
|
||
"kind": "anthropic",
|
||
"label": "Anthropic",
|
||
"base_url_hint": "留空走官方;填了就是自定义网关(读 ANTHROPIC_BASE_URL)",
|
||
"models": ANTHROPIC_MODELS,
|
||
"default_model": "claude-opus-5",
|
||
},
|
||
{
|
||
"kind": "openai",
|
||
"label": "OpenAI",
|
||
"base_url_hint": "留空走官方",
|
||
"models": OPENAI_MODELS,
|
||
"default_model": "gpt-5.2",
|
||
},
|
||
{
|
||
"kind": "openai_compatible",
|
||
"label": "OpenAI 兼容(DeepSeek / Kimi / 通义 / Ollama…)",
|
||
"base_url_hint": "必填,例如 https://api.deepseek.com/v1",
|
||
"models": [],
|
||
"presets": COMPATIBLE_PRESETS,
|
||
"default_model": "",
|
||
},
|
||
{
|
||
"kind": "mock",
|
||
"label": "Mock(无需 API Key)",
|
||
"base_url_hint": "",
|
||
"models": MOCK_MODELS,
|
||
"default_model": "mock-fast",
|
||
},
|
||
]
|
||
|
||
|
||
# --------------------------------------------------------------------------
|
||
# 模型能力
|
||
# --------------------------------------------------------------------------
|
||
|
||
# Claude 4.6 及以后的模型移除了采样参数,传 temperature/top_p 会直接 400。
|
||
_NO_SAMPLING_PREFIXES = (
|
||
"claude-fable-5",
|
||
"claude-mythos-5",
|
||
"claude-opus-5",
|
||
"claude-opus-4-8",
|
||
"claude-opus-4-7",
|
||
"claude-opus-4-6",
|
||
"claude-sonnet-5",
|
||
"claude-sonnet-4-6",
|
||
)
|
||
|
||
# 支持 adaptive thinking(而不是老的 budget_tokens)的模型
|
||
_ADAPTIVE_THINKING_PREFIXES = _NO_SAMPLING_PREFIXES
|
||
|
||
# 支持 output_config.effort
|
||
_EFFORT_PREFIXES = _NO_SAMPLING_PREFIXES + ("claude-opus-4-5",)
|
||
|
||
# 交错思考(工具结果回来之后再想一段,而不是一轮开头想完就只剩执行)。
|
||
#
|
||
# 4.6 及以后走 adaptive thinking,交错是自带的,不需要也不该再挂 beta header
|
||
# ——在 Opus 4.6 上手动模式压根没有交错思考,只有 adaptive 有。上面那张
|
||
# _ADAPTIVE_THINKING_PREFIXES 覆盖的都属于这一档。
|
||
#
|
||
# 需要显式开的是 Claude 4 / 4.5 这一代。目录里没有列它们,但模型名是放行的
|
||
# (用户可以手填),所以这条路径要留着。Haiku 4.5 不支持,前缀也就不包含它。
|
||
_INTERLEAVED_BETA_PREFIXES = ("claude-opus-4", "claude-sonnet-4")
|
||
INTERLEAVED_BETA = "interleaved-thinking-2025-05-14"
|
||
|
||
|
||
def needs_interleaved_beta(model: str) -> bool:
|
||
"""这个模型要不要靠 beta header 才会在工具之间思考。"""
|
||
name = _norm(model)
|
||
if supports_adaptive_thinking(name):
|
||
return False # adaptive 自带,多挂一个 header 只会是噪音
|
||
return name.startswith(_INTERLEAVED_BETA_PREFIXES)
|
||
|
||
|
||
def _norm(model: str) -> str:
|
||
"""去掉 openrouter 之类的前缀,只看模型名本身。"""
|
||
return (model or "").split("/")[-1].strip()
|
||
|
||
|
||
def supports_sampling(model: str) -> bool:
|
||
"""这个模型还能不能传 temperature / top_p。"""
|
||
return not _norm(model).startswith(_NO_SAMPLING_PREFIXES)
|
||
|
||
|
||
def supports_adaptive_thinking(model: str) -> bool:
|
||
return _norm(model).startswith(_ADAPTIVE_THINKING_PREFIXES)
|
||
|
||
|
||
def supports_effort(model: str) -> bool:
|
||
return _norm(model).startswith(_EFFORT_PREFIXES)
|
||
|
||
|
||
def find_pricing(model: str) -> dict[str, float] | None:
|
||
target = _norm(model)
|
||
pools = [ANTHROPIC_MODELS, OPENAI_MODELS, MOCK_MODELS]
|
||
pools += [p["models"] for p in COMPATIBLE_PRESETS]
|
||
for pool in pools:
|
||
for entry in pool:
|
||
if _norm(entry["id"]) == target:
|
||
return entry.get("pricing")
|
||
return None
|
||
|
||
|
||
def estimate_cost(model: str, input_tokens: int, output_tokens: int) -> float:
|
||
"""按目录价估算一次调用的花费(美元)。"""
|
||
pricing = find_pricing(model)
|
||
if not pricing:
|
||
return 0.0
|
||
return (
|
||
input_tokens / 1_000_000 * pricing.get("input", 0.0)
|
||
+ output_tokens / 1_000_000 * pricing.get("output", 0.0)
|
||
)
|