feat: enhance Gemini response handling by adding reasoning content and updating JSON decoding method

This commit is contained in:
CaIon
2025-05-22 16:11:50 +08:00
parent 66bdfe180c
commit e18001299b

View File

@@ -539,6 +539,8 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
if call := getResponseToolCall(&part); call != nil { if call := getResponseToolCall(&part); call != nil {
toolCalls = append(toolCalls, *call) toolCalls = append(toolCalls, *call)
} }
} else if part.Thought {
choice.Message.ReasoningContent = part.Text
} else { } else {
if part.ExecutableCode != nil { if part.ExecutableCode != nil {
texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```") texts = append(texts, "```"+part.ExecutableCode.Language+"\n"+part.ExecutableCode.Code+"\n```")
@@ -556,7 +558,6 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
choice.Message.SetToolCalls(toolCalls) choice.Message.SetToolCalls(toolCalls)
isToolCall = true isToolCall = true
} }
choice.Message.SetStringContent(strings.Join(texts, "\n")) choice.Message.SetStringContent(strings.Join(texts, "\n"))
} }
@@ -724,8 +725,11 @@ func GeminiChatHandler(c *gin.Context, resp *http.Response, info *relaycommon.Re
if err != nil { if err != nil {
return service.OpenAIErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil return service.OpenAIErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
} }
if common.DebugEnabled {
println(string(responseBody))
}
var geminiResponse GeminiChatResponse var geminiResponse GeminiChatResponse
err = json.Unmarshal(responseBody, &geminiResponse) err = common.DecodeJson(responseBody, &geminiResponse)
if err != nil { if err != nil {
return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
} }