mirror of
https://github.com/alibaba/open-code-review.git
synced 2026-07-09 17:28:58 +00:00
fix(llm): handle Anthropic thinking content blocks in multi-turn conversations
Anthropic models with extended thinking return type:"thinking" content blocks which were silently dropped, causing empty assistant content in conversation history. Extract thinking blocks into ReasoningContent so the existing Content() fallback works correctly.
This commit is contained in:
parent
5fad9ce2fa
commit
1b3c0ab19f
1 changed files with 14 additions and 3 deletions
|
|
@ -653,12 +653,17 @@ func buildToolInputSchema(params map[string]any) anthropic.ToolInputSchemaParam
|
|||
// mapAnthropicResponse converts the SDK response into ChatResponse.
|
||||
func (c *AnthropicClient) mapAnthropicResponse(sdkResp *anthropic.Message) *ChatResponse {
|
||||
var textParts []string
|
||||
var thinkingParts []string
|
||||
var toolCalls []ToolCall
|
||||
|
||||
for _, block := range sdkResp.Content {
|
||||
switch block.Type {
|
||||
case "text":
|
||||
textParts = append(textParts, block.Text)
|
||||
case "thinking":
|
||||
if block.Thinking != "" {
|
||||
thinkingParts = append(thinkingParts, block.Thinking)
|
||||
}
|
||||
case "tool_use":
|
||||
toolCalls = append(toolCalls, ToolCall{
|
||||
ID: block.ID,
|
||||
|
|
@ -677,6 +682,11 @@ func (c *AnthropicClient) mapAnthropicResponse(sdkResp *anthropic.Message) *Chat
|
|||
contentStr = &s
|
||||
}
|
||||
|
||||
var reasoningContent string
|
||||
if len(thinkingParts) > 0 {
|
||||
reasoningContent = strings.Join(thinkingParts, "\n")
|
||||
}
|
||||
|
||||
finishReason := string(sdkResp.StopReason)
|
||||
if finishReason == "" {
|
||||
finishReason = "stop"
|
||||
|
|
@ -701,9 +711,10 @@ func (c *AnthropicClient) mapAnthropicResponse(sdkResp *anthropic.Message) *Chat
|
|||
Model: string(sdkResp.Model),
|
||||
Choices: []Choice{{
|
||||
Message: ResponseMessage{
|
||||
Role: "assistant",
|
||||
Content: contentStr,
|
||||
ToolCalls: toolCalls,
|
||||
Role: "assistant",
|
||||
Content: contentStr,
|
||||
ReasoningContent: reasoningContent,
|
||||
ToolCalls: toolCalls,
|
||||
},
|
||||
FinishReason: finishReason,
|
||||
}},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue