第三十三轮:模型接口加入上下文参数(context_window / max_output_tokens)
老板 2026-09-24 要求:「文档里有写明支持上下文多少吗?有的话就修改 ai 网关, 接口加入上下文参数」+「Agnes 先加上,opencode go 看下 v2/docs/console/go/ 文档, 没有的话就看接口里有没有,这个也要加入同步脚本里」。 数据来源(逐一核查过): - Agnes:官方**每个文本模型详情页**的「限制与价格」表写明 `上下文窗口` / `最大输出` (定价页没有这两项)。3.0-flash = 512K/65,536、2.5-flash = 512K/65.5K、 2.5-pro 与 2.5-pro-beta = 1M/65,536。图片/视频页无此表 → 留 0。 - OpenCode:官方文档(Zen 页、v2/docs/console/go/)**都不写**上下文;`/zen/go/v1/models` 实测只回 id/object/created/owned_by(单模型详情 404、include 参数被忽略)。 唯一机器可读来源是 **models.dev**(OpenCode 团队维护),其 `opencode-go` / `opencode` 两供应商每条模型都带 `limit.context` / `limit.output`。 口径(老板拍板):只用于**更新网关已有模型**,不拿它当增删依据(models.dev 收录 比官方 Go 文档多,含 omen-alpha 等未收录的);跨源不一致时取**保守下限** (minimax-m3 Go 1M / Zen 512K → 512,000;qwen3.6-plus → 262,144)。 改动: - app/config.py:ModelInfo 新增 context_window / max_output_tokens(默认 0 = 未标注), 解析 + to_dict 序列化一并带上。 - app/main.py:GET /models 与 /v1/models 输出这两个字段;别名继承目标模型的值。 - app/settings.py:「供应商与模型」面板往返不丢字段(默认 0 → 未配置的模型不会被 补出 `context_window: 0` 污染 YAML);提交了就以此为准,没提交则沿用文件原文。 - data/gateway.yaml:37 个模型落盘(33 OpenCode + 4 Agnes 文本),文本级插入保住 全部手写注释(注释行 162 → 236)。 - scripts/sync_models.py:新增 fetch_models_dev()(拉失败返回 None → 只跳过上下文 同步、不当成漂移)、fetch_agnes_context()(逐模型页解析「限制与价格」表)、 diff() 产出 ctx_drift、build_plan/apply_plan(_set_context 块内改值,保留行尾注释、 幂等)、verify() 回读校验、render_plan/render 显示漂移;_DRIFT_KEYS 与 plan_is_empty 认 ctx_set。 - tests:新增 3 个(models.dev 保守下限合并 + 抓失败返回 None;_set_context 幂等与 保注释;/models 带出字段且未标注不瞎补)→ **201 passed / 0 failed**;ci_check.sh 全绿。 上线:supervisorctl restart api-gateway:api-gateway_00(pid 3492804)后实测 /v1/models 45 个模型、38 个带 context_window(含 wjd 别名继承 = 1,000,000/384,000), 图片模型正确为 0。
This commit is contained in:
1 parent
1c4bc9fb1e
commit
64054ad920
6 files changed
+428
-7
No files matched your search
@@ -5099,6 +5099,110 @@ def test_sync_plan_is_empty_drills_into_provider_buckets():
|
||||
assert sm.plan_is_empty({**empty, "price_set": {"m": {"old": None, "new": None}}}) is False
|
||||
assert sm.plan_is_empty({**empty, "proto_add": {"m": "messages"}}) is False
|
||||
assert sm.plan_is_empty({**empty, "free_set": {"m": True}}) is False
|
||||
# 第三十三轮: ctx_set 也必须是真动作(否则上下文漂移不会被落盘)
|
||||
assert sm.plan_is_empty({**empty, "ctx_set": {"m": {"context": 1, "output": 2}}}) is False
|
||||
|
||||
|
||||
def test_sync_models_dev_context_merges_conservatively(monkeypatch=None):
|
||||
"""models.dev 上下文合并:两供应商同名模型取**保守下限**(第三十三轮)。
|
||||
|
||||
背景:minimax-m3 在 opencode-go 是 1M、opencode(Zen) 是 512K;qwen3.6-plus
|
||||
Go 1M / Zen 262144。同一模型跨源不一致时取小的那个 —— 宁可早压缩,不能超窗口。
|
||||
"""
|
||||
sys.path.insert(0, str(_ROOT / "scripts"))
|
||||
import sync_models as sm
|
||||
|
||||
fake = json.dumps({
|
||||
"opencode-go": {"models": {
|
||||
"minimax-m3": {"limit": {"context": 1_000_000, "output": 131_072}},
|
||||
"qwen3.6-plus": {"limit": {"context": 1_000_000, "output": 65_536}},
|
||||
"only-in-go": {"limit": {"context": 500_000, "output": 128_000}},
|
||||
}},
|
||||
"opencode": {"models": {
|
||||
"minimax-m3": {"limit": {"context": 512_000, "output": 128_000}},
|
||||
"qwen3.6-plus": {"limit": {"context": 262_144, "output": 65_536}},
|
||||
}},
|
||||
})
|
||||
monkey_ok = lambda url, timeout=30: fake # noqa: E731
|
||||
orig = sm._get
|
||||
sm._get = monkey_ok
|
||||
try:
|
||||
got = sm.fetch_models_dev()
|
||||
finally:
|
||||
sm._get = orig
|
||||
assert got is not None
|
||||
assert got["minimax-m3"]["context"] == 512_000 # 保守下限
|
||||
assert got["qwen3.6-plus"]["context"] == 262_144
|
||||
assert got["only-in-go"]["context"] == 500_000 # 单源照样收
|
||||
# 抓不到时必须返回 None(调用方据此跳过上下文同步,而不是当成"全部漂移")
|
||||
sm._get = lambda url, timeout=60: (_ for _ in ()).throw(RuntimeError("boom"))
|
||||
try:
|
||||
assert sm.fetch_models_dev() is None
|
||||
finally:
|
||||
sm._get = orig
|
||||
|
||||
|
||||
def test_sync_context_apply_is_idempotent_and_keeps_comments():
|
||||
"""上下文写盘:插在 type: 之后、保住手写注释、重复执行不叠加(第三十三轮)。"""
|
||||
sys.path.insert(0, str(_ROOT / "scripts"))
|
||||
import sync_models as sm
|
||||
|
||||
text = (
|
||||
'providers:\n'
|
||||
' opencode:\n'
|
||||
' name: "OpenCode Go"\n'
|
||||
' models:\n'
|
||||
' - id: "glm-5.3"\n'
|
||||
' display_name: "GLM-5.3"\n'
|
||||
' type: "text"\n'
|
||||
' protocols: ["chat/completions"]\n'
|
||||
)
|
||||
seg = text.splitlines(keepends=True)[4:] # 从这个模型块起
|
||||
once = sm._set_context(list(seg), 1_000_000, 131_072, "2026-09-24")
|
||||
joined = "".join(once)
|
||||
assert "context_window: 1000000" in joined
|
||||
assert "max_output_tokens: 131072" in joined
|
||||
# 插在 type: 之后、protocols 之前
|
||||
assert joined.index("type:") < joined.index("context_window") < joined.index("protocols")
|
||||
# 幂等:再跑一次值不变、不叠加第二组
|
||||
twice = sm._set_context(list(once), 1_000_000, 131_072, "2026-09-24")
|
||||
assert "".join(twice).count("context_window:") == 1
|
||||
# 改值:旧值被替换、不新增行
|
||||
changed = sm._set_context(list(once), 512_000, 128_000, "2026-09-25")
|
||||
cj = "".join(changed)
|
||||
assert cj.count("context_window:") == 1 and "context_window: 512000" in cj
|
||||
assert cj.count("max_output_tokens:") == 1 and "max_output_tokens: 128000" in cj
|
||||
|
||||
|
||||
def test_models_payload_exposes_context_window():
|
||||
"""GET /models 与 /v1/models 必须带出 context_window / max_output_tokens。
|
||||
|
||||
第三十三轮(老板要求「接口加入上下文参数」):上下文窗口不只在 YAML 里,
|
||||
客户端读接口时也得能看到,否则这个字段等于白加。
|
||||
"""
|
||||
import app.main as m
|
||||
from app.config import ModelInfo, ProviderConfig, AuthConfig
|
||||
|
||||
cfg = m._cfg()
|
||||
key = "__ctx_test__"
|
||||
try:
|
||||
cfg.providers[key] = ProviderConfig(
|
||||
key=key, name="CTX", base_url="https://example.com/v1", auth=AuthConfig(),
|
||||
models=[
|
||||
ModelInfo(id="ctx-with", type="text",
|
||||
context_window=1_000_000, max_output_tokens=131_072),
|
||||
ModelInfo(id="ctx-without", type="image"), # 未标注 → 0
|
||||
])
|
||||
m._state.pop("_models_cache", None) # 绕开目录缓存
|
||||
rows = {x["id"]: x for x in m._models_payload()["data"]}
|
||||
assert rows["ctx-with"]["context_window"] == 1_000_000
|
||||
assert rows["ctx-with"]["max_output_tokens"] == 131_072
|
||||
# 未标注的模型不得瞎补数字(图片/视频模型天然没有上下文)
|
||||
assert rows["ctx-without"]["context_window"] == 0
|
||||
assert rows["ctx-without"]["max_output_tokens"] == 0
|
||||
finally:
|
||||
cfg.providers.pop(key, None)
|
||||
m._state.pop("_models_cache", None)
|
||||
|
||||
|
||||
def test_agnes_free_map_rules():
|
||||
|
||||
Reference in new issue
Block a user