Skip to content
20 changes: 19 additions & 1 deletion ai/liii/pi/ai/api/openai-completions/thinking.scm
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; OpenAI Chat Completions 请求侧思考参数:
;; - 从 models-store 取原始模型条目(compat/thinkingLevelMap 不在 model-metadata 中)
;; - 按 thinkingFormat 生成 thinking / reasoning_effort 字段
;; 目前覆盖 openai 与 deepseek 两种格式;其他格式保持不注入,避免猜错字段。
;; 目前覆盖 openai、deepseek 与 qwen 三种格式;其他格式保持不注入,避免猜错字段。
(define-library (liii pi ai api openai-completions thinking)
(import (scheme base) (liii string) (liii list))
(export openai-resolve-model-entry openai-thinking-request-params)
Expand Down Expand Up @@ -113,13 +113,31 @@
) ;if
) ;define

;; qwen 风格:enable_thinking(布尔)+ 可选 reasoning_effort
;; 对齐上游 openai-completions.ts 的 qwen 分支:
;; enable_thinking = !!reasoningEffort(off 与未开启为 false)
;; reasoning_effort 仅当 supportsReasoningEffort 为真且 level 非 off 时注入
;; (thinkingLevelMap 映射缺失/null 时回退原始级别,与上游 ?? 语义一致)
(define (%qwen-thinking-params model level)
(if (%thinking-off? level)
(list (cons "enable_thinking" #f))
(if (%json-true? (%compat-ref model "supportsReasoningEffort" #t))
(list (cons "enable_thinking" #t)
(cons "reasoning_effort" (%mapped-effort model level))
) ;list
(list (cons "enable_thinking" #t))
) ;if
) ;if
) ;define

;; 仅注入已实现且显式声明的格式;未知格式保持原行为(不猜字段)。
(define (openai-thinking-request-params model level)
(if (not (%model-supports-reasoning? model))
'()
(let ((format (%compat-ref model "thinkingFormat" "openai")))
(cond ((equal? format "deepseek") (%deepseek-thinking-params model level))
((equal? format "openai") (%openai-thinking-params model level))
((equal? format "qwen") (%qwen-thinking-params model level))
(else '())
) ;cond
) ;let
Expand Down
5 changes: 5 additions & 0 deletions ai/liii/pi/ai/auth.scm
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@
(%env-var-non-empty "OPENCODE_GO_API_KEY" opt-env)
) ;or
) ;
((string=? p-str "qwen-token-plan-cn")
(or (%env-var-non-empty "QWEN_TOKEN_PLAN_TEAM_CN_API_KEY" opt-env)
(%env-var-non-empty "QWEN_TOKEN_PLAN_CN_API_KEY" opt-env)
) ;or
) ;
(else #f)
) ;cond
) ;define
Expand Down
6 changes: 5 additions & 1 deletion ai/liii/pi/ai/models/meta.scm
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
(is-mm? (string=? p-str "minimax-cn"))
(is-zai? (or (string=? p-str "zai-coding-cn") (string=? p-str "zai")))
(is-opencode? (or (string=? p-str "opencode") (string=? p-str "opencode-go")))
(is-qwen? (string=? p-str "qwen-token-plan-cn"))
(is-aio? (string=? p-str "all-in-one"))
(def-url (cond (is-ant? "https://api.anthropic.com")
(is-kimi? "https://api.kimi.com/coding/v1")
Expand All @@ -124,13 +125,14 @@
(is-mm? "https://api.minimaxi.com/anthropic")
(is-zai? "https://open.bigmodel.cn/api/coding/paas/v4")
(is-opencode? "https://opencode.ai/zen/go/v1")
(is-qwen? "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
((string=? p-str "ollama") "http://localhost:11434/v1")
(else "https://generativelanguage.googleapis.com/v1beta")
) ;cond
) ;def-url
(m
(or (%find-model-in-list (%get-provider-models models-store p-str) model-id)
(and (or is-ant? is-mm? is-zai? is-opencode? is-aio?)
(and (or is-ant? is-mm? is-zai? is-opencode? is-qwen? is-aio?)
(%find-model-in-list (%dict-ref PI_PROVIDER_BUILTIN_MODELS p-str '()) model-id)
) ;and
) ;or
Expand All @@ -152,6 +154,7 @@
(is-mm? 1000000)
(is-zai? 1000000)
(is-opencode? 1000000)
(is-qwen? 1000000)
(else 128000)
) ;cond
) ;def-cw
Expand All @@ -163,6 +166,7 @@
(is-mm? 512000)
(is-zai? 131072)
(is-opencode? 131072)
(is-qwen? 131072)
(else 16384)
) ;cond
) ;def-mt
Expand Down
4 changes: 4 additions & 0 deletions ai/liii/pi/ai/models/route.scm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
((or (equal? p "minimax") (equal? p "minimax-cn")) "MiniMax-M3.1-Flash")
((equal? p "ollama") "qwen2.5-coder:latest")
((or (equal? p "opencode") (equal? p "opencode-go")) "deepseek-v4.1-flash")
((equal? p "qwen-token-plan-cn") "qwen3.8-max")
(else "gemini-3.8-flash")
) ;cond
) ;let
Expand Down Expand Up @@ -191,6 +192,9 @@
((equal? provider "deepseek") (%safe-lookup-env "DEEPSEEK_MODEL" opt-env))
((equal? provider "ollama") (%safe-lookup-env "OLLAMA_MODEL" opt-env))
((equal? provider "minimax-cn") (%safe-lookup-env "MINIMAX_CN_MODEL" opt-env))
((equal? provider "qwen-token-plan-cn")
(%safe-lookup-env "QWEN_TOKEN_PLAN_CN_MODEL" opt-env)
) ;
(else #f)
) ;cond
) ;define
Expand Down
229 changes: 229 additions & 0 deletions ai/liii/pi/ai/providers/qwen-token-plan-cn.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
;;
;; Copyright (C) 2026 The Goldfish Scheme Authors
;;
;; Licensed under the MIT License
;;

;; 阿里云百炼 QwenCloud Token Plan 团队订阅(中国区)
;;
;; - 端点:https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1
;; (对齐上游 pi qwenTokenPlanCnProvider 的 baseUrl)
;; - 协议:openai-completions(OpenAI Chat Completions 兼容)
;; - env 角色对齐上游 env-api-keys.ts:
;; 团队订阅变量 QWEN_TOKEN_PLAN_TEAM_CN_API_KEY 优先,回退区域共享变量
;; QWEN_TOKEN_PLAN_CN_API_KEY
;; - 思考格式为 qwen:注入 enable_thinking(布尔)+ 可选 reasoning_effort
;; - 内置模型清单以上游 qwen-token-plan-models.test.ts 固定的团队版 TEXT_MODELS
;; 快照为基础(仅保留 qwen3.8 系列,不含已退役模型),并补充阿里云百炼新上线
;; 的 qwen4-max / deepseek-v4.1-flash / glm-5.3-prime / kimi-k3.1-pro-prime
;; (models.dev 尚未收录前三者,其 reasoning effort 与上下文规格对齐同族基线)
;; - 额度耗尽识别:上游未注册专门 quota handler,初版同样不识别,429 仍按可
;; 重试处理(待拿到真实 429 业务码后再补码表)

(define-library (liii pi ai providers qwen-token-plan-cn)
(import (scheme base))
(export PI_QWEN_TOKEN_PLAN_CN_PROVIDER
PI_QWEN_TOKEN_PLAN_CN_BUILTIN_MODELS
PI_QWEN_TOKEN_PLAN_CN_QUOTA_ERROR
) ;export
(begin

;; Provider 配置:api 为协议实现标识(OpenAI Chat Completions 兼容),
;; baseUrl 为中国区端点,envKeys 为依次尝试的 API key 环境变量
;; (团队订阅变量优先,回退区域共享变量,对齐上游 env-api-keys.ts 的角色)
(define PI_QWEN_TOKEN_PLAN_CN_PROVIDER
'((api . "openai-completions")
(baseUrl
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
(envKeys "QWEN_TOKEN_PLAN_TEAM_CN_API_KEY" "QWEN_TOKEN_PLAN_CN_API_KEY"))
) ;define

;; 内置模型目录:团队订阅中国区 TEXT_MODELS 快照(仅保留 qwen3.8 系列)。
;; 每条记录含 id/name/baseUrl/contextWindow/maxTokens/reasoning/input/compat
;; /thinkingLevelMap 字段,供 openai-thinking-request-params 取思考格式与映射。
(define PI_QWEN_TOKEN_PLAN_CN_BUILTIN_MODELS
'((("id" . "deepseek-v4-pro")
("name" . "DeepSeek V4 Pro")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 384000)
("reasoning" . #t)
("input" "text")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . null)
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
;; 阿里云百炼新上线:DeepSeek V4.1 Flash(对齐 models.dev
;; alibaba-token-plan-cn 的 reasoning effort ['low','high','max'] 与上下文规格)
(("id" . "deepseek-v4.1-flash")
("name" . "DeepSeek V4.1 Flash")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 384000)
("reasoning" . #t)
("input" "text" "image")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
;; 阿里云百炼新版快照:DeepSeek V4.1 Flash 1017(与 deepseek-v4.1-flash
;; 同规格:1M 上下文 / 384K maxTokens / 多模态,reasoning effort
;; ['low','high','max'],其余档位映射为 null)
(("id" . "deepseek-v4.1-flash-1017")
("name" . "DeepSeek V4.1 Flash 1017")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 384000)
("reasoning" . #t)
("input" "text" "image")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
(("id" . "glm-5.2")
("name" . "GLM-5.2")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 131072)
("reasoning" . #t)
("input" "text")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . null)
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
;; 阿里云百炼上线:GLM-5.3(普通团队订阅可用,reasoning effort 对齐
;; 同族 ['low','high','max'],上下文 1M / maxTokens 131072)
(("id" . "glm-5.3")
("name" . "GLM-5.3")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 131072)
("reasoning" . #t)
("input" "text")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
;; 阿里云百炼新上线:GLM-5.3 Prime(Prime 通道,需更高订阅,reasoning effort
;; 对齐同族 glm-5.3 的 ['low','high','max'],上下文规格沿用 glm-5.3)
(("id" . "glm-5.3-prime")
("name" . "GLM-5.3 Prime")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 131072)
("reasoning" . #t)
("input" "text")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
;; 阿里云百炼新上线:Kimi K3.1 Pro Prime(Prime 通道,always-on 思考;
;; reasoning effort ['low','high','max'],1M 上下文 + 多模态输入)
(("id" . "kimi-k3.1-pro-prime")
("name" . "Kimi K3.1 Pro Prime")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1048576)
("maxTokens" . 32768)
("reasoning" . #t)
("input" "text" "image")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . null)
("high" . "high")
("xhigh" . null)
("max" . "max")))
(("id" . "qwen3.8-flash")
("name" . "Qwen3.8 Flash")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 131072)
("reasoning" . #t)
("input" "text" "image")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . "medium")
("high" . null)
("xhigh" . "xhigh")
("max" . null)))
(("id" . "qwen3.8-max")
("name" . "Qwen3.8 Max")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 131072)
("reasoning" . #t)
("input" "text" "image")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . "medium")
("high" . null)
("xhigh" . "xhigh")
("max" . null)))
;; 阿里云百炼新上线:Qwen4 Max 旗舰(qwen4-max-preview 的正式版,
;; reasoning effort ['low','medium','xhigh'],对齐 qwen3.8 系列映射)
(("id" . "qwen4-max")
("name" . "Qwen4 Max")
("baseUrl"
. "https://token-plan.maas.qianwenaiapi.com/compatible-mode/v1")
("contextWindow" . 1000000)
("maxTokens" . 262144)
("reasoning" . #t)
("input" "text" "image")
("compat" ("thinkingFormat" . "qwen") ("supportsReasoningEffort" . #t))
("thinkingLevelMap"
("minimal" . null)
("low" . "low")
("medium" . "medium")
("high" . null)
("xhigh" . "xhigh")
("max" . null))))
) ;define

;; 「额度耗尽」识别:
;; 上游 src/providers/qwen-token-plan-cn.ts 未注册专门 quota handler,
;; 本任务初版同样不识别,429 仍按可重试处理。
;; TODO: 待拿到该端点真实 429 业务码后再补码表,避免凭空猜测误杀瞬时限流。
(define (PI_QWEN_TOKEN_PLAN_CN_QUOTA_ERROR status-code body-str)
#f
) ;define

) ;begin
) ;define-library
5 changes: 4 additions & 1 deletion ai/liii/pi/ai/providers/registry.scm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
(liii pi ai providers zai-coding-cn)
(liii pi ai providers ollama)
(liii pi ai providers opencode)
(liii pi ai providers qwen-token-plan-cn)
(liii pi ai providers quota)
) ;import
(export PI_PROVIDERS PI_SUPPORTED_PROVIDERS PI_PROVIDER_BUILTIN_MODELS
Expand All @@ -41,14 +42,15 @@
(cons "ollama" PI_OLLAMA_PROVIDER)
(cons "opencode" PI_OPENCODE_PROVIDER)
(cons "opencode-go" PI_OPENCODE_PROVIDER)
(cons "qwen-token-plan-cn" PI_QWEN_TOKEN_PLAN_CN_PROVIDER)
) ;list
) ;define

;; 实际可用的 provider。anthropic 为官方 API;glm 已移除(智谱由
;; zai-coding-cn 覆盖);需要其它端点时用自定义 provider:models/<id>.json
(define PI_SUPPORTED_PROVIDERS
'("google" "anthropic" "kimi-coding" "deepseek" "minimax-cn"
"zai-coding-cn" "ollama" "opencode")
"zai-coding-cn" "ollama" "opencode" "qwen-token-plan-cn")
) ;define

;; 各 provider 的内置模型目录(id -> models),与 PI_PROVIDERS 同源
Expand All @@ -62,6 +64,7 @@
(cons "zai-coding-cn" PI_ZAI_CODING_CN_BUILTIN_MODELS)
(cons "ollama" PI_OLLAMA_BUILTIN_MODELS)
(cons "opencode" PI_OPENCODE_BUILTIN_MODELS)
(cons "qwen-token-plan-cn" PI_QWEN_TOKEN_PLAN_CN_BUILTIN_MODELS)
) ;list
) ;define

Expand Down
4 changes: 2 additions & 2 deletions ai/tests/liii/pi/ai/api/openai-completions/thinking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

`(liii pi ai api openai-completions thinking)` 负责请求侧思考参数:
从 `models-store` 定位原始模型条目,并按 `thinkingFormat`、`thinkingLevelMap`
生成 `thinking` / `reasoning_effort` 字段。
生成 `thinking` / `reasoning_effort` / `enable_thinking` 字段。

## 运行测试

Expand All @@ -13,4 +13,4 @@ bin/gf test library/tests/liii/pi/ai/api/openai-completions/thinking/
## 测试文件索引

- `openai-resolve-model-entry-test.scm` — 从 models-store 定位模型条目
- `openai-thinking-request-params-test.scm` — deepseek/openai 两种格式的思考字段生成
- `openai-thinking-request-params-test.scm` — deepseek/openai/qwen 三种格式的思考字段生成
Loading
Loading