From cc4406738ccf0970f4abc70c1d378a07c223aeba Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 15 Sep 2026 20:40:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(upstreams):=20base=5Furl=20=E5=BC=BA?= =?UTF-8?q?=E5=88=B6=E5=B8=A6=20/v1=EF=BC=88OpenAI=20SDK=20=E9=9C=80?= =?UTF-8?q?=E8=A6=81=EF=BC=89=20(1.0.37)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit OpenAI Python SDK 用法:`client = OpenAI(base_url=...)` 内部拼 `/chat/completions`,**不会自动加 /v1**。 但网关 location 只接 `//v1/...`(slot 拆完路径必须是 `/v1/chat/completions`,上游 base `https://api.deepseek.com` 也要求 带 /v1)。 旧 base_url 函数:上游 base 无路径时,输出 `https://lm.mayishangshu.cn:88/openai`(**无 /v1**)——SDK 拼出 `/openai/chat/completions` → 网关拆 slot 后路径是 /chat/completions → 转发到上游 → 404 且响应体为空。 修: - base_url 出口保证末尾是 `/v1`(上游已有 /v1 时不加,避免 /v1/v1) - anthropic_base_url 末尾保证 `/anthropic/v1` - e2e 测试断言同步改:base_url 必须带 /v1 + anthropic_base_url 必须带 /v1 回归全绿。 --- rust/crates/plugin-llm/src/upstreams.rs | 21 ++++++++++++++++++--- tests/llm/e2e.sh | 16 ++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/rust/crates/plugin-llm/src/upstreams.rs b/rust/crates/plugin-llm/src/upstreams.rs index 4afe0b8..3f4c505 100644 --- a/rust/crates/plugin-llm/src/upstreams.rs +++ b/rust/crates/plugin-llm/src/upstreams.rs @@ -647,7 +647,16 @@ pub fn base_url(slot: &str, up: &Upstream, port: &str) -> String { Some(i) => &rest[i..], None => "", }; - format!("https://{LLM_PUBLIC_HOST}{port}/{slot}{path}") + let mut out = format!("https://{LLM_PUBLIC_HOST}{port}/{slot}{path}"); + // **1.0.37**:客户端 OpenAI SDK 不会自动在 base_url 后拼 `/v1`, + // 而我们的网关 location 只接 `//v1/...`(slot 拆完就是 /v1/chat/completions)。 + // 上游 base 若已是 `…/v1`(厂商文档常见写法),path 已经带 /v1, + // 不能再无脑追加 —— join_target 的去重逻辑负责上游拼装,这里只看出口 URL。 + // 出口 URL 必须保证末尾是 `/v1`(覆盖无 path 的 base 与已有 path 但不是 v1 的两种情况)。 + if !out.ends_with("/v1") { + out.push_str("/v1"); + } + out } /// Anthropic 客户端(Claude Code / Claude SDK)应当使用的 `ANTHROPIC_BASE_URL`。 @@ -655,12 +664,18 @@ pub fn base_url(slot: &str, up: &Upstream, port: &str) -> String { /// 协议段由**客户端**负责表达(网关不猜协议),但这段路径是网关替客户端拼好的, /// 所以要处理重合:上游 `base` 若已钉在 `…/anthropic`(协议钉死型记录),其 /// `base_url` 末尾已经带 `/anthropic`,再无脑追加会得到 `…/anthropic/anthropic`。 +/// +/// **1.0.37**:Anthropic 协议的 path 段是 `/v1/messages`,需要保证出口 URL +/// 末尾是 `/anthropic/v1`——既覆盖无 path 的 base,也避免与厂商 `…/anthropic` +/// 路径重叠。 pub fn anthropic_base_url(slot: &str, up: &Upstream, port: &str) -> String { let b = base_url(slot, up, port); + // base_url 已保证末尾是 /v1,现在把 /v1 切掉拼 /anthropic/v1 + let b = b.trim_end_matches("/v1"); if b.ends_with("/anthropic") { - b + format!("{b}/v1") } else { - format!("{b}/anthropic") + format!("{b}/anthropic/v1") } } diff --git a/tests/llm/e2e.sh b/tests/llm/e2e.sh index b0ef16c..c683670 100755 --- a/tests/llm/e2e.sh +++ b/tests/llm/e2e.sh @@ -289,10 +289,18 @@ ck " 模型清单里带 base_url(lm 域名 + 端口 + slot)" yes \ # 搞错这一条会让"base 自带 /v1"的上游变成 /v1/v1/... → 上游 404 且响应体为空,极难排查。 # (校验 base_url 字段的**完整值**而不是整份响应的子串——后者会被同段新增的 # anthropic_base_url(…/openai/anthropic)误伤。) -ck "★base_url 不凭空拼 /v1(base 无路径就不带)" yes \ - "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "\"base_url\":\"https://lm.mayishangshu.cn:$API_PORT/openai\"")" -ck "★base_url 保留上游 base 的路径 /llm" yes \ - "$(curl -s -H "X-API-Key: $BOB_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/other/llm")" +# +# **1.0.37 修正**:base_url **必须保证末尾是 /v1**(OpenAI SDK 不会自动拼 /v1), +# 否则 SDK 用 base_url 拼 `/chat/completions` 会得到 `/openai/chat/completions`, +# 网关拆出 slot 后上游路径只剩 `/chat/completions` → 上游 404 且响应体为空。 +# 上游 base 自带 /v1 时(如 `https://api.openai.com/v1`)也不能凭空再加一次, +# 走 join_target 去重;出口 URL 一定以 /v1 结尾。 +ck "★base_url 强制带 /v1(base 无路径时补上)" yes \ + "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "\"base_url\":\"https://lm.mayishangshu.cn:$API_PORT/openai/v1\"")" +ck "★base_url 保留上游 base 的路径 /llm(不重复加 /v1)" yes \ + "$(curl -s -H "X-API-Key: $BOB_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/other/llm/v1")" +ck "★anthropic_base_url 强制带 /v1" yes \ + "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "anthropic_base_url\":\"https://lm.mayishangshu.cn:$API_PORT/openai/anthropic/v1")" # ★ 端口自适应:Host 不带端口(走 443 反代)时 base_url 也不该带端口。 # 写死 88 → 443 部署下客户端连到另一个端口的服务;裸域名 → 88 部署下被出口设备抢答 # (实测 HTTP 200 + 一个 HTML 登录页,是最难看出问题的一种失败)。