依据 DevDocs/design/04 §3.2「官方核对后的修正」9 条 + 本地 DevDocs/reference/deepseek/facts-for-cyrene.md(官方原句/URL)。 - 🔴 reasoning_effort 白名单改为官方集合 [none, low, high, max] + 别名映射(minimal→low, medium→high, xhigh→high, ultra→max);**删除就近降级**,非法值一律丢弃+WARN。此前 none(关思考) 被降级成 low(开思考) 属语义反转 - 🔴 DeepSeek thinking 开关真发顶层 {"thinking":{"type":"enabled|disabled"}}(官方默认 enabled,不传≠关);此前只打 WARN 导致 mode:off 静默无效 - DeepSeek 的 /anthropic 端点忽略 budget_tokens → 不再对其抬 max_tokens(会误导预算/日志),改为按官方用 output_config.effort - 注释纠正:penalty 属接口级 deprecated(非仅思考模式);top_p 仅思考模式生效且下限 0.95(不加该字段) - tool_choice 雷登记:思考模式下 required/指定 tool 会 400(思考默认开) - 旧模型名清理(deepseek-chat/reasoner 已于 2026-07-24 停用) - 请求体级测试 + 变异验证(把 none 改回 low / 把 thinking 改回只 WARN → 对应断言 FAIL)
292 lines
12 KiB
Go
292 lines
12 KiB
Go
package model
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"strings"
|
|
|
|
"git.yeij.top/AskaEth/Cyrene_v2/internal/logger"
|
|
)
|
|
|
|
// discardLogger returns a logger that drops every record but still carries the
|
|
// adapter's module source, so an adapter built without an injected logger can
|
|
// never emit an unsourced record.
|
|
func discardLogger(source string) *slog.Logger {
|
|
return logger.WithSource(slog.New(slog.NewTextHandler(io.Discard, nil)), source)
|
|
}
|
|
|
|
// Reasoning wire values shared by the OpenAI-compatible and Anthropic adapters
|
|
// (design/04 §3.2, checked against the provider documents on 2026-10-03).
|
|
const (
|
|
// thinkingTypeEnabled and thinkingTypeDisabled are the values of the
|
|
// {"thinking":{"type":...}} switch. DeepSeek's default is enabled, so omitting
|
|
// the field leaves thinking ON — "do not intervene" is not a way to switch it
|
|
// off; only an explicit disabled (or reasoning_effort "none") is.
|
|
thinkingTypeEnabled = "enabled"
|
|
thinkingTypeDisabled = "disabled"
|
|
// effortNone is the reasoning_effort value that switches thinking off. It is a
|
|
// switch, not a strength tier, so it must never be replaced by a tier: mapping
|
|
// it onto "low" would invert the caller's intent (thinking off → thinking on).
|
|
effortNone = "none"
|
|
)
|
|
|
|
// reasoningVendor identifies the vendor behind an OpenAI-compatible endpoint.
|
|
// The wire protocol is the same for all of them, but the accepted
|
|
// reasoning_effort values are not, so a value must be checked against the right
|
|
// whitelist instead of being forwarded blindly.
|
|
type reasoningVendor string
|
|
|
|
const (
|
|
vendorOpenAI reasoningVendor = "openai"
|
|
vendorDeepSeek reasoningVendor = "deepseek"
|
|
vendorQwen reasoningVendor = "qwen"
|
|
vendorUnknown reasoningVendor = "unknown"
|
|
)
|
|
|
|
// reasoningEffortAliases is DeepSeek's documented compatibility mapping: the
|
|
// service accepts these aliases and applies the mapped tier. The mapping is
|
|
// applied on our side too, so the request body always carries the tier the
|
|
// provider documents as the effective one (design/04 §3.2 fact 1).
|
|
var reasoningEffortAliases = map[string]string{
|
|
"minimal": "low",
|
|
"medium": "high",
|
|
"xhigh": "high",
|
|
"ultra": "max",
|
|
}
|
|
|
|
// reasoningEffortAllowed is the per-vendor set of accepted reasoning_effort
|
|
// values. A vendor absent from this table has no reasoning_effort support at all,
|
|
// which is reported rather than guessed.
|
|
//
|
|
// There is deliberately no "round down to the nearest tier" fallback: an
|
|
// unsupported value is dropped and reported instead, because silently changing it
|
|
// can invert the caller's intent — the original bug mapped DeepSeek's "none"
|
|
// (thinking off) onto "low" (thinking on) exactly that way.
|
|
var reasoningEffortAllowed = map[reasoningVendor]map[string]bool{
|
|
vendorOpenAI: tierSet("none", "minimal", "low", "medium", "high", "xhigh", "max"),
|
|
vendorDeepSeek: tierSet("none", "low", "high", "max"),
|
|
}
|
|
|
|
// tierSet builds a membership set from the given tiers.
|
|
func tierSet(tiers ...string) map[string]bool {
|
|
set := make(map[string]bool, len(tiers))
|
|
for _, t := range tiers {
|
|
set[t] = true
|
|
}
|
|
return set
|
|
}
|
|
|
|
// reasoningVendorFor classifies the vendor of an OpenAI-compatible request from
|
|
// its model name, falling back to the base URL when the name does not identify
|
|
// one. An unrecognized pair yields vendorUnknown so the caller reports "not
|
|
// supported" instead of assuming OpenAI semantics. The Anthropic adapter uses the
|
|
// same classification to tell a DeepSeek /anthropic endpoint (which ignores
|
|
// budget_tokens) from the Anthropic endpoint.
|
|
func reasoningVendorFor(model, baseURL string) reasoningVendor {
|
|
m := strings.ToLower(strings.TrimSpace(model))
|
|
switch {
|
|
case strings.Contains(m, "deepseek"):
|
|
return vendorDeepSeek
|
|
case strings.Contains(m, "qwen"):
|
|
return vendorQwen
|
|
case strings.Contains(m, "gpt-"), strings.Contains(m, "chatgpt"),
|
|
strings.HasPrefix(m, "o1"), strings.HasPrefix(m, "o3"), strings.HasPrefix(m, "o4"):
|
|
return vendorOpenAI
|
|
}
|
|
u := strings.ToLower(strings.TrimSpace(baseURL))
|
|
switch {
|
|
case strings.Contains(u, "deepseek"):
|
|
return vendorDeepSeek
|
|
case strings.Contains(u, "dashscope"), strings.Contains(u, "aliyuncs"):
|
|
return vendorQwen
|
|
case strings.Contains(u, "openai"):
|
|
return vendorOpenAI
|
|
}
|
|
return vendorUnknown
|
|
}
|
|
|
|
// thinkingSwitchValue maps an explicit on/off switch onto its wire value.
|
|
func thinkingSwitchValue(enable bool) string {
|
|
if enable {
|
|
return thinkingTypeEnabled
|
|
}
|
|
return thinkingTypeDisabled
|
|
}
|
|
|
|
// normalizeDeepSeekEffort maps an effort onto the tier DeepSeek applies and
|
|
// reports whether the value is one the service documents: the documented set
|
|
// (none/low/high/max) passes through, the compatibility aliases map to their
|
|
// official tier.
|
|
func normalizeDeepSeekEffort(effort string) (string, bool) {
|
|
if tier, ok := reasoningEffortAliases[effort]; ok {
|
|
return tier, true
|
|
}
|
|
if reasoningEffortAllowed[vendorDeepSeek][effort] {
|
|
return effort, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// resolveReasoningEffort validates effort against the vendor's whitelist and
|
|
// returns the value to send; "" drops the field. An unrecognized value is never
|
|
// turned into a different tier — it is dropped and reported with provider, field,
|
|
// value and model, because changing it can invert the caller's intent. A DeepSeek
|
|
// compatibility alias is normalized to the tier the service applies and reported.
|
|
func resolveReasoningEffort(log *slog.Logger, vendor reasoningVendor, effort, model string) string {
|
|
effort = strings.TrimSpace(effort)
|
|
if effort == "" {
|
|
return ""
|
|
}
|
|
allowed, ok := reasoningEffortAllowed[vendor]
|
|
if !ok {
|
|
log.Warn("reasoning_effort ignored: provider does not support it",
|
|
"provider", string(vendor), "field", "reasoning_effort", "value", effort, "model", model)
|
|
return ""
|
|
}
|
|
if allowed[effort] {
|
|
return effort
|
|
}
|
|
if vendor == vendorDeepSeek {
|
|
if tier, ok := normalizeDeepSeekEffort(effort); ok {
|
|
log.Warn("reasoning_effort alias normalized to the tier DeepSeek applies",
|
|
"provider", "deepseek", "field", "reasoning_effort",
|
|
"value", effort, "sent", tier, "model", model)
|
|
return tier
|
|
}
|
|
}
|
|
log.Warn("reasoning_effort dropped: unrecognized value",
|
|
"provider", string(vendor), "field", "reasoning_effort", "value", effort, "model", model)
|
|
return ""
|
|
}
|
|
|
|
// resolveOpenAIReasoning maps the caller's reasoning knobs onto the DeepSeek
|
|
// OpenAI-compatible wire fields and returns the top-level thinking switch (nil =
|
|
// omitted) together with the reasoning_effort to send ("" = omitted). A
|
|
// non-DeepSeek vendor has no portable field for the switch, so its directive is
|
|
// only reported.
|
|
//
|
|
// When both knobs are given and disagree, the "off" side wins and the conflict is
|
|
// reported: the provider documents no precedence, and letting a strength tier
|
|
// re-open thinking is exactly the inversion this resolution exists to prevent
|
|
// (design/04 §3.2 fact 2).
|
|
func resolveOpenAIReasoning(log *slog.Logger, vendor reasoningVendor, model string, th *Thinking, effort string) (*openAIThinking, string) {
|
|
if vendor != vendorDeepSeek {
|
|
reportUnmappedThinking(log, vendor, model, th)
|
|
return nil, effort
|
|
}
|
|
var block *openAIThinking
|
|
if th != nil {
|
|
if th.BudgetTokens > 0 {
|
|
log.Warn("thinking.budget_tokens ignored: the DeepSeek chat-completions protocol takes reasoning_effort, not a token budget",
|
|
"provider", "deepseek", "field", "thinking.budget_tokens",
|
|
"budget_tokens", th.BudgetTokens, "model", model)
|
|
}
|
|
if th.Enable != nil {
|
|
block = &openAIThinking{Type: thinkingSwitchValue(*th.Enable)}
|
|
}
|
|
}
|
|
if block != nil && block.Type == thinkingTypeDisabled {
|
|
if effort != "" {
|
|
log.Warn("reasoning_effort dropped: thinking is explicitly disabled",
|
|
"provider", "deepseek", "field", "reasoning_effort", "value", effort, "model", model)
|
|
}
|
|
return block, ""
|
|
}
|
|
if effort == effortNone && block != nil {
|
|
log.Warn("reasoning_effort none contradicts thinking enabled: thinking disabled wins",
|
|
"provider", "deepseek", "field", "reasoning_effort", "value", effort, "model", model)
|
|
return &openAIThinking{Type: thinkingTypeDisabled}, ""
|
|
}
|
|
return block, effort
|
|
}
|
|
|
|
// openAIThinkingOn reports the effective thinking state of a resolved DeepSeek
|
|
// OpenAI-compatible request: an omitted switch leaves the provider default in
|
|
// place, and DeepSeek's default is enabled.
|
|
func openAIThinkingOn(th *openAIThinking, effort string) bool {
|
|
if th != nil {
|
|
return th.Type == thinkingTypeEnabled
|
|
}
|
|
return effort != effortNone
|
|
}
|
|
|
|
// toolChoiceForces reports whether a tool_choice directive obliges the model to
|
|
// call a tool ("required", Anthropic's "any", or a named tool). auto/none do not.
|
|
func toolChoiceForces(toolChoice any) bool {
|
|
switch v := toolChoice.(type) {
|
|
case nil:
|
|
return false
|
|
case string:
|
|
return strings.EqualFold(strings.TrimSpace(v), "required")
|
|
case map[string]any:
|
|
if forcedToolChoiceType(v["type"]) {
|
|
return true
|
|
}
|
|
_, named := v["function"]
|
|
return named
|
|
case map[string]string:
|
|
if forcedToolChoiceType(v["type"]) {
|
|
return true
|
|
}
|
|
_, named := v["function"]
|
|
return named
|
|
}
|
|
return false
|
|
}
|
|
|
|
// forcedToolChoiceType reports whether a tool_choice "type" value forces a call.
|
|
func forcedToolChoiceType(value any) bool {
|
|
t, _ := value.(string)
|
|
switch strings.ToLower(strings.TrimSpace(t)) {
|
|
case "required", "any", "tool":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// reportForcedToolChoiceWithThinking warns about the DeepSeek red line that a
|
|
// forced tool call — tool_choice "required"/"any"/a named tool — and thinking are
|
|
// not compatible: while thinking is on (its default) the API rejects the forcing
|
|
// directive with a 400, so the caller must switch thinking off explicitly first
|
|
// (design/04 §3.2 fact 5). It only reports: resolution must never switch thinking
|
|
// off on its own. On the OpenAI-compatible adapter the directive is not on the
|
|
// wire yet (M2), so the warning is prospective there — it still records that the
|
|
// requested combination can never be honoured.
|
|
func reportForcedToolChoiceWithThinking(log *slog.Logger, model string, toolChoice any, thinkingOn bool) {
|
|
if !thinkingOn || !toolChoiceForces(toolChoice) {
|
|
return
|
|
}
|
|
log.Warn("tool_choice forcing cannot be combined with DeepSeek thinking (forcing is rejected with a 400): disable thinking explicitly before forcing a tool call",
|
|
"provider", "deepseek", "field", "tool_choice", "model", model)
|
|
}
|
|
|
|
// reportUnmappedThinking logs why a Thinking directive cannot be carried by a
|
|
// non-DeepSeek OpenAI-compatible request, naming the provider and the field that
|
|
// was ignored. Qwen3 does have enable_thinking, but the protocol has no portable
|
|
// field for it (vLLM wants chat_template_kwargs.enable_thinking, DashScope's
|
|
// compatible mode wants a top-level enable_thinking), and sending the wrong shape
|
|
// can 400 the request — so this version reports it instead of guessing. DeepSeek's
|
|
// switch is mapped by resolveOpenAIReasoning, not here.
|
|
func reportUnmappedThinking(log *slog.Logger, vendor reasoningVendor, model string, th *Thinking) {
|
|
if th == nil {
|
|
return
|
|
}
|
|
if th.Enable != nil {
|
|
switch vendor {
|
|
case vendorQwen:
|
|
log.Warn("enable_thinking not forwarded: the OpenAI-compatible protocol has no portable field for it",
|
|
"provider", "qwen", "field", "extra_body.chat_template_kwargs.enable_thinking",
|
|
"enable", *th.Enable, "model", model)
|
|
default:
|
|
log.Warn("thinking switch ignored: provider has no boolean thinking switch",
|
|
"provider", string(vendor), "field", "thinking.enable", "enable", *th.Enable, "model", model)
|
|
}
|
|
return
|
|
}
|
|
if th.BudgetTokens > 0 {
|
|
log.Warn("thinking budget ignored: OpenAI-compatible providers take reasoning_effort, not a token budget",
|
|
"provider", string(vendor), "field", "thinking.budget_tokens",
|
|
"budget_tokens", th.BudgetTokens, "model", model)
|
|
}
|
|
}
|