feat(glm): max_tokens 可选透传(site-import 前置)

max_tokens 探针结论: 8192 可用(glm-4-flash 实测 max_tokens=8192 调用成功返回,finish_reason=stop;Task 5 可按 8192 取值)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-10-20 17:23:36 +08:00
parent 29953b9fc8
commit 0d6a82ba14
2 changed files with 43 additions and 1 deletions
+9 -1
View File
@@ -13,9 +13,16 @@ DEFAULT_MODEL = "glm-4-flash"
class GLMProvider(AIProvider):
def __init__(self, api_key: str, model: str = "glm-4-flash", base_url: str = ""):
def __init__(
self,
api_key: str,
model: str = "glm-4-flash",
base_url: str = "",
max_tokens: int | None = None,
):
self.client = ZhipuAI(api_key=api_key, base_url=base_url or None)
self.model = model
self.max_tokens = max_tokens
async def chat(self, messages: list[dict], system_prompt: str) -> str:
full_messages = [{"role": "system", "content": system_prompt}] + messages
@@ -74,6 +81,7 @@ class GLMProvider(AIProvider):
model=self.model,
messages=messages,
temperature=temperature,
**({"max_tokens": self.max_tokens} if self.max_tokens is not None else {}),
)
return resp.choices[0].message.content
+34
View File
@@ -0,0 +1,34 @@
"""GLMProvider max_tokens 透传(site-import spec §3.2)。"""
from unittest.mock import MagicMock, patch
from app.engine.providers.glm import GLMProvider
def _provider(**kw):
with patch("app.engine.providers.glm.ZhipuAI"):
return GLMProvider(api_key="k", **kw)
def _ok(cli):
cli.chat.completions.create.return_value.choices = [
MagicMock(message=MagicMock(content="ok"))]
class TestMaxTokensPassthrough:
def test_默认不传_调用无max_tokens参数(self):
import asyncio
p = _provider()
with patch.object(p, "client") as cli:
_ok(cli)
asyncio.run(p.chat([{"role": "user", "content": "hi"}], "sys"))
_, kwargs = cli.chat.completions.create.call_args
assert "max_tokens" not in kwargs
def test_传入max_tokens_透传到create(self):
import asyncio
p = _provider(max_tokens=4096)
with patch.object(p, "client") as cli:
_ok(cli)
asyncio.run(p.chat([{"role": "user", "content": "hi"}], "sys"))
_, kwargs = cli.chat.completions.create.call_args
assert kwargs.get("max_tokens") == 4096