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 {