fix: claude thinking for non-stream mode

This commit is contained in:
Laisky.Cai
2025-02-25 03:13:21 +00:00
parent 3a8924d7af
commit c61d6440f9
2 changed files with 16 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package anthropic
import ( import (
"bufio" "bufio"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
@@ -235,9 +236,17 @@ func ResponseClaude2OpenAI(claudeResponse *Response) *openai.TextResponse {
tools := make([]model.Tool, 0) tools := make([]model.Tool, 0)
for _, v := range claudeResponse.Content { for _, v := range claudeResponse.Content {
reasoningText += v.Text switch v.Type {
if v.Thinking != nil { case "thinking":
reasoningText += *v.Thinking if v.Thinking != nil {
reasoningText += *v.Thinking
} else {
logger.Errorf(context.Background(), "thinking is nil in response")
}
case "text":
responseText += v.Text
default:
logger.Warnf(context.Background(), "unknown response type %q", v.Type)
} }
if v.Type == "tool_use" { if v.Type == "tool_use" {
@@ -252,6 +261,7 @@ func ResponseClaude2OpenAI(claudeResponse *Response) *openai.TextResponse {
}) })
} }
} }
choice := openai.TextResponseChoice{ choice := openai.TextResponseChoice{
Index: 0, Index: 0,
Message: model.Message{ Message: model.Message{
@@ -374,6 +384,8 @@ func Handler(c *gin.Context, resp *http.Response, promptTokens int, modelName st
return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil return openai.ErrorWrapper(err, "close_response_body_failed", http.StatusInternalServerError), nil
} }
logger.Debugf(c.Request.Context(), "response <- %s\n", string(responseBody))
var claudeResponse Response var claudeResponse Response
err = json.Unmarshal(responseBody, &claudeResponse) err = json.Unmarshal(responseBody, &claudeResponse)
if err != nil { if err != nil {