diff --git a/dto/openai_request.go b/dto/openai_request.go index 25ef3a21aa51..8c104ddd242d 100644 --- a/dto/openai_request.go +++ b/dto/openai_request.go @@ -279,8 +279,8 @@ type Message struct { Content any `json:"content"` Name *string `json:"name,omitempty"` Prefix *bool `json:"prefix,omitempty"` - ReasoningContent string `json:"reasoning_content,omitempty"` - Reasoning string `json:"reasoning,omitempty"` + ReasoningContent *string `json:"reasoning_content,omitempty"` + Reasoning *string `json:"reasoning,omitempty"` ToolCalls json.RawMessage `json:"tool_calls,omitempty"` ToolCallId string `json:"tool_call_id,omitempty"` parsedContent []MediaContent @@ -431,6 +431,16 @@ const ( //ContentTypeAudioUrl = "audio_url" ) +func (m *Message) GetReasoningContent() string { + if m.ReasoningContent == nil && m.Reasoning == nil { + return "" + } + if m.ReasoningContent != nil { + return *m.ReasoningContent + } + return *m.Reasoning +} + func (m *Message) GetPrefix() bool { if m.Prefix == nil { return false diff --git a/relay/channel/claude/relay-claude.go b/relay/channel/claude/relay-claude.go index fa8234523c77..e177e56dab14 100644 --- a/relay/channel/claude/relay-claude.go +++ b/relay/channel/claude/relay-claude.go @@ -567,12 +567,14 @@ func ResponseClaude2OpenAI(claudeResponse *dto.ClaudeResponse) *dto.OpenAITextRe } choice.SetStringContent(responseText) if len(responseThinking) > 0 { - choice.ReasoningContent = responseThinking + choice.ReasoningContent = &responseThinking } if len(tools) > 0 { choice.Message.SetToolCalls(tools) } - choice.Message.ReasoningContent = thinkingContent + if thinkingContent != "" { + choice.Message.ReasoningContent = &thinkingContent + } fullTextResponse.Model = claudeResponse.Model choices = append(choices, choice) fullTextResponse.Choices = choices diff --git a/relay/channel/gemini/relay-gemini.go b/relay/channel/gemini/relay-gemini.go index 21641e483861..355c75d71b7c 100644 --- a/relay/channel/gemini/relay-gemini.go +++ b/relay/channel/gemini/relay-gemini.go @@ -1097,7 +1097,7 @@ func responseGeminiChat2OpenAI(c *gin.Context, response *dto.GeminiChatResponse) toolCalls = append(toolCalls, *call) } } else if part.Thought { - choice.Message.ReasoningContent = part.Text + choice.Message.ReasoningContent = &part.Text } else { if part.ExecutableCode != nil { texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```") diff --git a/relay/channel/ollama/stream.go b/relay/channel/ollama/stream.go index 2a264b27e467..43e024deafd7 100644 --- a/relay/channel/ollama/stream.go +++ b/relay/channel/ollama/stream.go @@ -273,7 +273,7 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R msg := dto.Message{Role: "assistant", Content: contentPtr(content)} if rc := reasoningBuilder.String(); rc != "" { - msg.ReasoningContent = rc + msg.ReasoningContent = &rc } full := dto.OpenAITextResponse{ Id: common.GetUUID(), diff --git a/relay/channel/openai/relay-openai.go b/relay/channel/openai/relay-openai.go index d33c5555f267..a85751844c0b 100644 --- a/relay/channel/openai/relay-openai.go +++ b/relay/channel/openai/relay-openai.go @@ -245,7 +245,7 @@ func OpenaiHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Respo completionTokens := simpleResponse.Usage.CompletionTokens if completionTokens == 0 { for _, choice := range simpleResponse.Choices { - ctkm := service.CountTextToken(choice.Message.StringContent()+choice.Message.ReasoningContent+choice.Message.Reasoning, info.UpstreamModelName) + ctkm := service.CountTextToken(choice.Message.StringContent()+choice.Message.GetReasoningContent(), info.UpstreamModelName) completionTokens += ctkm } }