From c39b2e9f3ee338057ce574eb868dfd651252120f Mon Sep 17 00:00:00 2001 From: zizi Date: Wed, 15 Jul 2026 14:44:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6:=20chat()=E6=92=9E=E6=A8=A1?= =?UTF-8?q?=E5=9E=8Bmax=5Ftokens=E4=B8=8A=E9=99=90=E8=87=AA=E9=80=82?= =?UTF-8?q?=E5=BA=94=E9=99=8D=E6=A1=A3=E9=87=8D=E5=8F=91(M2.7=E4=B8=8A?= =?UTF-8?q?=E9=99=90196608=E5=AE=9E=E6=B5=8B,=E4=BF=AE=E9=99=8D=E7=BA=A7?= =?UTF-8?q?=E9=93=BE400=E6=96=AD=E9=93=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/skills/llm/scripts/llm.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.claude/skills/llm/scripts/llm.py b/.claude/skills/llm/scripts/llm.py index 7aa03bc..287c302 100644 --- a/.claude/skills/llm/scripts/llm.py +++ b/.claude/skills/llm/scripts/llm.py @@ -59,6 +59,17 @@ def chat(prompt, model=DEFAULT_MODEL, max_tokens=512000, temperature=0.2, r = s.post(f"{BASE}/v1/chat/completions", headers={"Authorization": f"Bearer {TOKEN}"}, json=payload, timeout=timeout) + # 各模型 max_tokens 上限不一(实测 M3 收 512k、M2.7 上限 196608 报 400): + # 撞上限自适应降档重发,保证降级链换模型时不被参数掀翻。 + # 必须在敏感检查之前处理——重发后的响应仍要完整走敏感/429/5xx 判定链 + if r.status_code == 400 and "max tokens" in r.text.lower(): + cur = payload.get("max_tokens", 0) + payload["max_tokens"] = 196608 if cur > 196608 else 12000 + print(f"[llm] {model} max_tokens={cur} 超模型上限,降为 {payload['max_tokens']} 重发", + file=sys.stderr) + r = s.post(f"{BASE}/v1/chat/completions", + headers={"Authorization": f"Bearer {TOKEN}"}, + json=payload, timeout=timeout) # 内容安全拦截:同模型退避重试必再敏感,立即抛 SensitiveError 交上层 # 降级换模型,不在此浪费退避(否则一敏感章空烧 3 次,实测占放量请求 23%) if r.status_code >= 500 and "sensitive" in r.text.lower():