From 56353904a3cc0bb2f582e969c9712da2a04211c8 Mon Sep 17 00:00:00 2001 From: nanako <469449812@qq.com> Date: Wed, 12 Nov 2025 11:20:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D"reasoning"=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/api/openai_handlers.go | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/backend/api/openai_handlers.go b/backend/api/openai_handlers.go index 2af18b3..bdcd198 100644 --- a/backend/api/openai_handlers.go +++ b/backend/api/openai_handlers.go @@ -73,6 +73,25 @@ func (h *APIHandler) ChatCompletions(c *gin.Context) { modelName, _ := jsonBody["model"].(string) isStream, _ := jsonBody["stream"].(bool) + // 处理 reasoning 字段,将对象转换为字符串 + if messagesRaw, ok := jsonBody["messages"].([]interface{}); ok { + for _, msgRaw := range messagesRaw { + if msgMap, ok := msgRaw.(map[string]interface{}); ok { + if reasoning, exists := msgMap["reasoning"]; exists { + // 如果 reasoning 是对象,提取 content 字段作为字符串 + if reasoningMap, ok := reasoning.(map[string]interface{}); ok { + if reasoningContent, ok := reasoningMap["content"].(string); ok { + msgMap["reasoning"] = reasoningContent + } else { + // 如果无法提取 content,移除 reasoning 字段 + delete(msgMap, "reasoning") + } + } + } + } + } + } + // 提取 messages 并转换为计算 token 所需的格式 var messages []billing.ChatCompletionMessage if messagesRaw, ok := jsonBody["messages"].([]interface{}); ok { @@ -206,6 +225,25 @@ func (h *APIHandler) ResponsesCompletions(c *gin.Context) { // 从 map 中提取需要的字段 modelName, _ := jsonBody["model"].(string) + // 处理 input 中的 reasoning 字段,将对象转换为字符串 + if inputRaw, ok := jsonBody["input"].([]interface{}); ok { + for _, msgRaw := range inputRaw { + if msgMap, ok := msgRaw.(map[string]interface{}); ok { + if reasoning, exists := msgMap["reasoning"]; exists { + // 如果 reasoning 是对象,提取 content 字段作为字符串 + if reasoningMap, ok := reasoning.(map[string]interface{}); ok { + if reasoningContent, ok := reasoningMap["content"].(string); ok { + msgMap["reasoning"] = reasoningContent + } else { + // 如果无法提取 content,移除 reasoning 字段 + delete(msgMap, "reasoning") + } + } + } + } + } + } + // 提取 input 并转换为计算 token 所需的格式 var messages []billing.ChatCompletionMessage if inputRaw, ok := jsonBody["input"].([]interface{}); ok {