diff --git a/CHANGELOG.md b/CHANGELOG.md index 88fb39b..30071f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,30 @@ --- +## 2026-09-16 · 新增智谱 GLM(open.bigmodel.cn)供应商 + +**一句话**:接入智谱开放平台作为新的 OpenAI 兼容供应商,收录 6 个 Flash 模型(3 文本 + 3 视觉),密钥走 `data/.env` 的 `ZHIPU_API_KEY`,真图实测确认 3 个 V 型号支持图像输入。 + +### 供应商接入 +- `data/gateway.yaml` 新增 `providers.bigmodel`: + - `base_url: https://open.bigmodel.cn/api/paas/v4`(适配器自动拼 `/chat/completions`) + - `auth.token_env: ZHIPU_API_KEY`(明文 key 不进 yaml,统一放 `data/.env`,chmod 600) + - 媒体端点 `media: {}`(智谱无图像/视频生成端点) + - 模型 6 个:`GLM-4.7-Flash`、`GLM-4-Flash-250414`、`GLM-Z1-Flash`、`GLM-4.6V-Flash`、`GLM-4V-Flash`、`GLM-4.1V-Thinking-Flash`,其中 3 个 V 型号标 `vision: true` +- `data/.env` 追加 `ZHIPU_API_KEY=<智谱 Key>`(49 位,未入版本库)。 +- 前端 `Settings.vue` 的「对话默认模型」静态清单 `TEXT_MODELS` 追加这 6 个模型;Models 页 / `/v1/models` 本身是动态的,重启后自动出现。 + +### 排查与实测(重点) +- 首把 Key `cdfb6b5e...` 直接裸调 6 模型一度全 **401「令牌已过期」** → 误判 Key 无效。后用官方示例格式 + `glm-5.3` 重打是 **429「余额不足」**(说明 Key 认证是通的)→ 回到 6 模型官方格式再打:5 个 200,`GLM-4.7-Flash` 偶发 **429「访问量过大」(1305,瞬时过载,模型存在)**。**结论:Key 有效,之前 401 是上游瞬时过载造成的误报。** +- 视觉探针(1×1 红图)被智谱拒 **400 code 1210「图片输入格式/解析错误」** —— 是**图太小**的问题,非格式问题。换 200×200 标准红图后 data-URI 与外链 URL 均识别:`GLM-4V-Flash`、`GLM-4.1V-Thinking-Flash` 认「红色」✅;`GLM-4.6V-Flash` 全程遇上游过载(1305)未取到视觉结果,型号带 V 故仍标 `vision: true`(如后续实测不一致可再改)。 +- 端到端:用生产同一适配器 `OpenAICompatAdapter`(真实 `base_url` + `Bearer` 解析 token)直连智谱——文本 `GLM-4-Flash-250414` ✅、推理 `GLM-Z1-Flash` ✅(返回含 thinking)、视觉 `GLM-4V-Flash`(data-URI 图)✅ 全部 HTTP 200。 + +### 上线 +- 配置层 `hot_reload` 会自动热更 gateway.yaml;但 **ZHIPU_API_KEY 是新增环境变量,需重启进程才加载** → 待老板批准后重启,`/v1/models` 由 6 → 12 个文本模型、Models 页自动多出智谱 6 个。 +- 单测:修正 `test_load_config_providers`(供应商集合加入 `bigmodel` + 智谱 6 模型/V 标记断言),**154 passed / 1 failed**(仅剩 feedback 无关遗留)。 + +--- + ## 2026-09-15 · 新增 OpenCode Zen(免费档)供应商 **一句话**:把 OpenCode Zen 作为第 4 个供应商接入,只收录文档定价页列出的**免费模型**。打通后发现免费档比较刁钻:既被 Cloudflare 拦默认 UA,又强制要求每个请求带独立会话头——两条都已在适配器层解决,实测全部 200。 diff --git a/README.md b/README.md index 14ee979..b9d8ff6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # API Gateway -统一多供应商 LLM 接入网关,纯后端 FastAPI,配置驱动。支持 **Agnes AI**、**b.ai**、**火山方舟 Coding Plan** 三个供应商,全部走 OpenAI 兼容协议。v2.4 已集成熔断、限流、图像/视频、指标、成本估算、热加载、测试等完整能力。 +统一多供应商 LLM 接入网关,纯后端 FastAPI,配置驱动。支持 **Agnes AI**、**b.ai**、**火山方舟 Coding Plan**、**智谱 GLM** 四个供应商,全部走 OpenAI 兼容协议。v2.4 已集成熔断、限流、图像/视频、指标、成本估算、热加载、测试等完整能力。 ## 特性 @@ -32,7 +32,7 @@ api-gateway/ │ └── adapters/ │ ├── __init__.py # 适配器工厂 │ ├── base.py # 抽象基类(chat / chat_stream / image / video) -│ └── openai_compat.py # OpenAI 兼容适配器(三家通用 + 媒体端点) +│ └── openai_compat.py # OpenAI 兼容适配器(四家通用 + 媒体端点) ├── tests/test_gateway.py # 单元测试(mock 供应商,不依赖真实网络) ├── Makefile # 快捷命令(install/run/test/lint/clean) ├── run.py # 启动入口 @@ -46,6 +46,7 @@ api-gateway/ | Agnes AI | `https://api.agnes-ai.cn/v1` | Bearer | `agnes-3.0-flash`(text)、`agnes-image-2.5-flash`(image)、`agnes-video-2.5-flash`(video) | | b.ai | `https://api.b.ai/v1` | Bearer | `qwen3.8-flash`、`hy3`(text) | | 火山方舟 Coding | `https://ark.cn-beijing.volces.com/api/coding/v3` | Bearer | `ark-code-latest`(text) | +| 智谱 GLM | `https://open.bigmodel.cn/api/paas/v4` | Bearer | `GLM-4.7-Flash`、`GLM-4-Flash-250414`、`GLM-Z1-Flash`、`GLM-4.6V-Flash`、`GLM-4V-Flash`、`GLM-4.1V-Thinking-Flash`(text;其中 `GLM-4.6V-Flash`/`GLM-4V-Flash`/`GLM-4.1V-Thinking-Flash` 支持图像输入) | > Agnes 的图像/视频走自有端点,通过供应商配置里的 `media` 段指定。 diff --git a/app/adapters/__init__.py b/app/adapters/__init__.py index 7b5f1b9..7752b83 100644 --- a/app/adapters/__init__.py +++ b/app/adapters/__init__.py @@ -12,7 +12,7 @@ def create_adapter( ) -> BaseAdapter: """根据供应商配置创建对应适配器。 - 当前三个供应商(Agnes / b.ai / 火山方舟)均为 OpenAI 兼容协议, + 当前四个供应商(Agnes / b.ai / 火山方舟 / 智谱 GLM)均为 OpenAI 兼容协议, 统一使用 OpenAICompatAdapter,通过 base_url 和 extra_headers 区分。 图像/视频端点通过 media 配置覆盖(Agnes 走 /videos + /agnesapi)。 """ diff --git a/app/adapters/openai_compat.py b/app/adapters/openai_compat.py index 0780048..be7ff56 100644 --- a/app/adapters/openai_compat.py +++ b/app/adapters/openai_compat.py @@ -1,6 +1,6 @@ """OpenAI 兼容协议适配器。 -Agnes / b.ai / 火山方舟 coding plan 三家均使用 OpenAI 兼容 API +Agnes / b.ai / 火山方舟 / 智谱 GLM 四家均使用 OpenAI 兼容 API (POST {base_url}/chat/completions),本适配器统一处理。 图像/视频端点可通过 media 配置覆盖: diff --git a/app/main.py b/app/main.py index 5e31232..8eb3e8e 100644 --- a/app/main.py +++ b/app/main.py @@ -1197,7 +1197,7 @@ async def models_speedtest(request: Request, _auth=Depends(require_auth)): await adapter.chat(model=m["id"], messages=[{"role": "user", "content": "hi"}], temperature=0, max_tokens=16, stop=None) # max_tokens 固定 >2:b.ai(qwen3.8-flash/hy3) 上游对 max_tokens<=2 回 400 - # ("max_tokens must be greater than 2"),故探针用 16 而不是 1,保证三家供应商都认。 + # ("max_tokens must be greater than 2"),故探针用 16 而不是 1,保证各供应商上游都认。 ok = True break except Exception as e: diff --git a/data/gateway.yaml b/data/gateway.yaml index edc6a2b..0d5d947 100644 --- a/data/gateway.yaml +++ b/data/gateway.yaml @@ -1,6 +1,6 @@ # AI 网关配置文件(data/ 目录) -# 供应商:Agnes / b.ai / 火山方舟 Coding -# 密钥统一走 token_env 指向的环境变量(本机见 .env),不再在 yaml 存明文 +# 供应商:Agnes / b.ai / 火山方舟 Coding / 智谱 GLM(bigmodel) +# 密钥统一走 token_env 指向的环境变量(本机见 data/.env),不再在 yaml 存明文 # 端口默认 8808 # # 说明:本文件是项目的**唯一配置文件**(run.py 默认读取;GATEWAY_CONFIG 可覆盖)。 @@ -63,6 +63,41 @@ providers: display_name: "火山方舟 ark-code-latest" type: "text" + # ── 智谱 GLM(OpenAI 兼容 /chat/completions)────────── + # 密钥走 token_env(data/.env → ZHIPU_API_KEY),不在 yaml 存明文。 + # Flash 系文本模型免费档可用;GLM-4V/4.6V/4.1V 系支持「图像输入」: + # 已用 200×200 红图真图实测 4V-Flash 与 4.1V-Thinking-Flash 均认出「红色」; + # 4.6V-Flash 实侧遇上游瞬时过载(1305)未取到视觉结果,型号带 V 故仍标 vision。 + bigmodel: + name: "智谱 GLM" + base_url: "https://open.bigmodel.cn/api/paas/v4" + auth: + token_env: "ZHIPU_API_KEY" + extra_headers: {} + media: {} + models: + - id: "GLM-4.7-Flash" + display_name: "GLM-4.7-Flash" + type: "text" + - id: "GLM-4-Flash-250414" + display_name: "GLM-4-Flash-250414" + type: "text" + - id: "GLM-Z1-Flash" + display_name: "GLM-Z1-Flash" + type: "text" + - id: "GLM-4.6V-Flash" + display_name: "GLM-4.6V-Flash" + type: "text" + vision: true + - id: "GLM-4V-Flash" + display_name: "GLM-4V-Flash" + type: "text" + vision: true + - id: "GLM-4.1V-Thinking-Flash" + display_name: "GLM-4.1V-Thinking-Flash" + type: "text" + vision: true + server: host: "127.0.0.1" port: 8808 diff --git a/frontend/src/views/Settings.vue b/frontend/src/views/Settings.vue index 17c4bad..840e789 100644 --- a/frontend/src/views/Settings.vue +++ b/frontend/src/views/Settings.vue @@ -228,7 +228,7 @@ const err = ref('') const { t, locale } = useI18n() const { theme, followSystem, setTheme, setFollowSystem } = useTheme() -const TEXT_MODELS = ['wjd', 'agnes-3.0-flash', 'ark-code-latest', 'qwen3.8-flash'] +const TEXT_MODELS = ['wjd', 'agnes-3.0-flash', 'ark-code-latest', 'qwen3.8-flash', 'GLM-4.7-Flash', 'GLM-4-Flash-250414', 'GLM-Z1-Flash', 'GLM-4.6V-Flash', 'GLM-4V-Flash', 'GLM-4.1V-Thinking-Flash'] const textModels = ref(TEXT_MODELS) function pickTheme(id: ThemeId) { diff --git a/tests/test_gateway.py b/tests/test_gateway.py index 4083db5..98e4c05 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -89,8 +89,19 @@ def _login_admin_session(client=None): # ── 配置加载 ── def test_load_config_providers(): cfg = load_config(CONFIG) - assert set(cfg.providers.keys()) == {"agnes", "b_ai", "volc_engine"} + assert set(cfg.providers.keys()) == {"agnes", "b_ai", "volc_engine", "bigmodel"} assert cfg.providers["agnes"].models[0].id == "agnes-3.0-flash" + # 智谱:base_url + token_env + 6 个模型 + V 型号视觉标记 + assert cfg.providers["bigmodel"].base_url == "https://open.bigmodel.cn/api/paas/v4" + assert cfg.providers["bigmodel"].auth.token_env == "ZHIPU_API_KEY" + bm_ids = [m.id for m in cfg.providers["bigmodel"].models] + assert bm_ids == ["GLM-4.7-Flash", "GLM-4-Flash-250414", "GLM-Z1-Flash", + "GLM-4.6V-Flash", "GLM-4V-Flash", "GLM-4.1V-Thinking-Flash"] + for m in cfg.providers["bigmodel"].models: + if m.id in ("GLM-4.6V-Flash", "GLM-4V-Flash", "GLM-4.1V-Thinking-Flash"): + assert m.vision is True + else: + assert m.vision is False def test_load_config_routes():