Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion relay/channel/deepseek/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"net/http"
"strings"

"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/dto"
"github.com/QuantumNous/new-api/relay/channel"
"github.com/QuantumNous/new-api/relay/channel/claude"
"github.com/QuantumNous/new-api/relay/channel/openai"
relaycommon "github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/relay/constant"
"github.com/QuantumNous/new-api/setting/reasoning"
"github.com/QuantumNous/new-api/types"
"github.com/gin-gonic/gin"
)
Expand All @@ -27,7 +29,18 @@ func (a *Adaptor) ConvertGeminiRequest(*gin.Context, *relaycommon.RelayInfo, *dt

func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *relaycommon.RelayInfo, req *dto.ClaudeRequest) (any, error) {
adaptor := claude.Adaptor{}
return adaptor.ConvertClaudeRequest(c, info, req)
convertedRequest, err := adaptor.ConvertClaudeRequest(c, info, req)
if err != nil {
return nil, err
}
claudeRequest, ok := convertedRequest.(*dto.ClaudeRequest)
if !ok {
return convertedRequest, nil
}
Comment on lines +36 to +39

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Silent fallthrough may leave the -none/-max suffix unhandled.

If claude.Adaptor.ConvertClaudeRequest ever returns a non-*dto.ClaudeRequest payload (e.g. a future refactor returns a raw []byte/map), this branch returns the converted value unchanged and the DeepSeek V4 suffix handling is silently skipped. The -none/-max-suffixed model name would then be forwarded upstream, producing an obscure "invalid model" error on /anthropic/v1/messages rather than a clear local error.

Consider either asserting the type or applying the suffix handling on the raw req.Model before calling the claude adaptor.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/deepseek/adaptor.go` around lines 36 - 39, The current branch
silently returns convertedRequest when the type assertion to *dto.ClaudeRequest
fails, which skips the DeepSeek V4 "-none"/"-max" suffix handling; update
ConvertClaudeRequest usage so the suffix is handled deterministically: either
perform the "-none"/"-max" model-name normalization on the original req.Model
before calling claude.Adaptor.ConvertClaudeRequest, or if you must inspect the
adaptor result, replace the silent fallthrough in the claudeRequest
type-assertion block with an explicit error return (or fallback that extracts
the model field) so that dto.ClaudeRequest, convertedRequest, or the original
request always goes through the suffix-stripping logic (referencing
ConvertClaudeRequest, convertedRequest, claudeRequest, and dto.ClaudeRequest).

if err := applyDeepSeekV4ClaudeThinkingSuffix(info, claudeRequest); err != nil {
return nil, err
}
return claudeRequest, nil
}

func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
Expand Down Expand Up @@ -71,9 +84,71 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
if request == nil {
return nil, errors.New("request is nil")
}
if err := applyDeepSeekV4OpenAIThinkingSuffix(info, request); err != nil {
return nil, err
}

return request, nil
}

func applyDeepSeekV4OpenAIThinkingSuffix(info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) error {
modelName := request.Model
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
if !ok {
return nil
}
thinking, err := common.Marshal(map[string]string{
"type": thinkingType,
})
if err != nil {
return fmt.Errorf("error marshalling thinking: %w", err)
}
request.Model = baseModel
request.THINKING = thinking
request.ReasoningEffort = effort
if info != nil {
if info.ChannelMeta != nil {
info.UpstreamModelName = baseModel
}
info.ReasoningEffort = effort
}
return nil
}

func applyDeepSeekV4ClaudeThinkingSuffix(info *relaycommon.RelayInfo, request *dto.ClaudeRequest) error {
modelName := request.Model
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
if !ok {
return nil
}
request.Model = baseModel
request.Thinking = &dto.Thinking{Type: thinkingType}
if effort == "" {
request.OutputConfig = nil
} else {
outputConfig, err := common.Marshal(map[string]string{
"effort": effort,
})
if err != nil {
return fmt.Errorf("error marshalling output_config: %w", err)
}
request.OutputConfig = outputConfig
}
if info != nil {
if info.ChannelMeta != nil {
info.UpstreamModelName = baseModel
}
info.ReasoningEffort = effort
}
return nil
}
Comment on lines +94 to +150

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Consider preserving caller-supplied Thinking / THINKING / OutputConfig fields.

Both helpers unconditionally clobber reasoning-related request fields whenever the suffix matches:

  • applyDeepSeekV4OpenAIThinkingSuffix (L103-111) replaces request.THINKING with {"type": thinkingType} and overwrites request.ReasoningEffort, even if the caller already set THINKING with budget_tokens or a custom ReasoningEffort.
  • applyDeepSeekV4ClaudeThinkingSuffix (L131-142) replaces request.Thinking with &dto.Thinking{Type: ...} (drops BudgetTokens etc.), and for the -none case sets request.OutputConfig = nil, silently discarding anything the caller put there; for -max it overwrites OutputConfig with just {"effort":"max"}.

If the intended semantic is "the suffix is the sole signal and overrides client input", this is fine but worth documenting. Otherwise, prefer merging (only set fields that are currently zero/nil, e.g. preserve existing BudgetTokens, only clear OutputConfig.effort rather than the whole struct).

♻️ Example: preserve existing Thinking fields
-	request.Model = baseModel
-	request.Thinking = &dto.Thinking{Type: thinkingType}
+	request.Model = baseModel
+	if request.Thinking == nil {
+		request.Thinking = &dto.Thinking{}
+	}
+	request.Thinking.Type = thinkingType
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func applyDeepSeekV4OpenAIThinkingSuffix(info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) error {
modelName := request.Model
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
if !ok {
return nil
}
thinking, err := common.Marshal(map[string]string{
"type": thinkingType,
})
if err != nil {
return fmt.Errorf("error marshalling thinking: %w", err)
}
request.Model = baseModel
request.THINKING = thinking
request.ReasoningEffort = effort
if info != nil {
if info.ChannelMeta != nil {
info.UpstreamModelName = baseModel
}
info.ReasoningEffort = effort
}
return nil
}
func applyDeepSeekV4ClaudeThinkingSuffix(info *relaycommon.RelayInfo, request *dto.ClaudeRequest) error {
modelName := request.Model
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
if !ok {
return nil
}
request.Model = baseModel
request.Thinking = &dto.Thinking{Type: thinkingType}
if effort == "" {
request.OutputConfig = nil
} else {
outputConfig, err := common.Marshal(map[string]string{
"effort": effort,
})
if err != nil {
return fmt.Errorf("error marshalling output_config: %w", err)
}
request.OutputConfig = outputConfig
}
if info != nil {
if info.ChannelMeta != nil {
info.UpstreamModelName = baseModel
}
info.ReasoningEffort = effort
}
return nil
}
func applyDeepSeekV4ClaudeThinkingSuffix(info *relaycommon.RelayInfo, request *dto.ClaudeRequest) error {
modelName := request.Model
if info != nil && info.ChannelMeta != nil && info.UpstreamModelName != "" {
modelName = info.UpstreamModelName
}
baseModel, thinkingType, effort, ok := reasoning.ParseDeepSeekV4ThinkingSuffix(modelName)
if !ok {
return nil
}
request.Model = baseModel
if request.Thinking == nil {
request.Thinking = &dto.Thinking{}
}
request.Thinking.Type = thinkingType
if effort == "" {
request.OutputConfig = nil
} else {
outputConfig, err := common.Marshal(map[string]string{
"effort": effort,
})
if err != nil {
return fmt.Errorf("error marshalling output_config: %w", err)
}
request.OutputConfig = outputConfig
}
if info != nil {
if info.ChannelMeta != nil {
info.UpstreamModelName = baseModel
}
info.ReasoningEffort = effort
}
return nil
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/deepseek/adaptor.go` around lines 94 - 150, The helpers
applyDeepSeekV4OpenAIThinkingSuffix and applyDeepSeekV4ClaudeThinkingSuffix
currently unconditionally overwrite reasoning fields; change them to merge
instead: in applyDeepSeekV4OpenAIThinkingSuffix only set request.THINKING.Type
when request.THINKING is nil or its Type is empty (preserve existing
budget_tokens and other keys) and only set request.ReasoningEffort when it is
empty; in applyDeepSeekV4ClaudeThinkingSuffix only set request.Thinking.Type
when request.Thinking is nil or its Type is empty (preserve existing
BudgetTokens), and instead of setting request.OutputConfig = nil or replacing it
entirely, update or clear only the "effort" entry (preserve other OutputConfig
fields) — keep updating info.UpstreamModelName and info.ReasoningEffort as
before. Ensure marshaling logic respects existing non-empty fields and avoids
discarding caller-supplied BudgetTokens or other custom fields.


func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
return nil, nil
}
Expand Down
2 changes: 2 additions & 0 deletions relay/channel/deepseek/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package deepseek

var ModelList = []string{
"deepseek-chat", "deepseek-reasoner",
"deepseek-v4-flash", "deepseek-v4-flash-none", "deepseek-v4-flash-max",
"deepseek-v4-pro", "deepseek-v4-pro-none", "deepseek-v4-pro-max",
}

var ChannelName = "deepseek"
20 changes: 3 additions & 17 deletions relay/channel/openai/adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
relayconstant "github.com/QuantumNous/new-api/relay/constant"
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/setting/model_setting"
"github.com/QuantumNous/new-api/setting/reasoning"
"github.com/QuantumNous/new-api/types"
"github.com/samber/lo"

Expand All @@ -39,21 +40,6 @@ type Adaptor struct {
ResponseFormat string
}

// parseReasoningEffortFromModelSuffix 从模型名称中解析推理级别
// support OAI models: o1-mini/o3-mini/o4-mini/o1/o3 etc...
// minimal effort only available in gpt-5
func parseReasoningEffortFromModelSuffix(model string) (string, string) {
effortSuffixes := []string{"-high", "-minimal", "-low", "-medium", "-none", "-xhigh"}
for _, suffix := range effortSuffixes {
if strings.HasSuffix(model, suffix) {
effort := strings.TrimPrefix(suffix, "-")
originModel := strings.TrimSuffix(model, suffix)
return effort, originModel
}
}
return "", model
}

func (a *Adaptor) ConvertGeminiRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeminiChatRequest) (any, error) {
// 使用 service.GeminiToOpenAIRequest 转换请求格式
openaiRequest, err := service.GeminiToOpenAIRequest(request, info)
Expand Down Expand Up @@ -342,7 +328,7 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
}

// 转换模型推理力度后缀
effort, originModel := parseReasoningEffortFromModelSuffix(info.UpstreamModelName)
effort, originModel := reasoning.ParseOpenAIReasoningEffortFromModelSuffix(info.UpstreamModelName)
if effort != "" {
request.ReasoningEffort = effort
info.UpstreamModelName = originModel
Expand Down Expand Up @@ -587,7 +573,7 @@ func detectImageMimeType(filename string) string {

func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
// 转换模型推理力度后缀
effort, originModel := parseReasoningEffortFromModelSuffix(request.Model)
effort, originModel := reasoning.ParseOpenAIReasoningEffortFromModelSuffix(request.Model)
if effort != "" {
if request.Reasoning == nil {
request.Reasoning = &dto.Reasoning{
Expand Down
33 changes: 32 additions & 1 deletion setting/reasoning/suffix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,44 @@ import (

var EffortSuffixes = []string{"-max", "-xhigh", "-high", "-medium", "-low", "-minimal"}

var OpenAIEffortSuffixes = []string{"-high", "-minimal", "-low", "-medium", "-none", "-xhigh"}

var DeepSeekV4EffortSuffixes = []string{"-none", "-max"}

// TrimEffortSuffix -> modelName level(low) exists
func TrimEffortSuffix(modelName string) (string, string, bool) {
suffix, found := lo.Find(EffortSuffixes, func(s string) bool {
return TrimEffortSuffixWithSuffixes(modelName, EffortSuffixes)
}

func TrimEffortSuffixWithSuffixes(modelName string, suffixes []string) (string, string, bool) {
suffix, found := lo.Find(suffixes, func(s string) bool {
return strings.HasSuffix(modelName, s)
})
if !found {
return modelName, "", false
}
return strings.TrimSuffix(modelName, suffix), strings.TrimPrefix(suffix, "-"), true
}

func ParseOpenAIReasoningEffortFromModelSuffix(modelName string) (string, string) {
baseModel, effort, ok := TrimEffortSuffixWithSuffixes(modelName, OpenAIEffortSuffixes)
if !ok {
return "", modelName
}
return effort, baseModel
}

func ParseDeepSeekV4ThinkingSuffix(modelName string) (baseModel string, thinkingType string, effort string, ok bool) {
baseModel, suffix, ok := TrimEffortSuffixWithSuffixes(modelName, DeepSeekV4EffortSuffixes)
if !ok || !strings.HasPrefix(baseModel, "deepseek-v4-") {
return modelName, "", "", false
}
switch suffix {
case "none":
return baseModel, "disabled", "", true
case "max":
return baseModel, "enabled", "max", true
default:
return modelName, "", "", false
}
}