Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

anthropic: improve handling of multipart tool responses #1011

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 75 additions & 37 deletions llms/anthropic/anthropicllm.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,35 +272,52 @@
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for human message", ErrInvalidContentType)
}

func handleAIMessage(msg llms.MessageContent) (anthropicclient.ChatMessage, error) {
if toolCall, ok := msg.Parts[0].(llms.ToolCall); ok {
var inputStruct map[string]interface{}
err := json.Unmarshal([]byte(toolCall.FunctionCall.Arguments), &inputStruct)
if err != nil {
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: failed to unmarshal tool call arguments: %w", err)
}
toolUse := anthropicclient.ToolUseContent{
Type: "tool_use",
ID: toolCall.ID,
Name: toolCall.FunctionCall.Name,
Input: inputStruct,
}
func getTextPart(part llms.TextContent) *anthropicclient.TextContent {
return &anthropicclient.TextContent{
Type: "text",
Text: part.Text,
}
}

return anthropicclient.ChatMessage{
Role: RoleAssistant,
Content: []anthropicclient.Content{toolUse},
}, nil
func getToolPart(part llms.ToolCall) (*anthropicclient.ToolUseContent, error) {
var inputStruct map[string]interface{}
err := json.Unmarshal([]byte(part.FunctionCall.Arguments), &inputStruct)
if err != nil {
return nil, fmt.Errorf("anthropic: failed to unmarshal tool call arguments: %w", err)
}
if textContent, ok := msg.Parts[0].(llms.TextContent); ok {
return anthropicclient.ChatMessage{
Role: RoleAssistant,
Content: []anthropicclient.Content{&anthropicclient.TextContent{
Type: "text",
Text: textContent.Text,
}},
}, nil
return &anthropicclient.ToolUseContent{
Type: "tool_use",
ID: part.ID,
Name: part.FunctionCall.Name,
Input: inputStruct,
}, nil
}

func handleAIMessage(msg llms.MessageContent) (anthropicclient.ChatMessage, error) {
cm := anthropicclient.ChatMessage{
Role: RoleAssistant,
Content: make([]anthropicclient.Content, 0),
}
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for AI message", ErrInvalidContentType)

contentArr := make([]anthropicclient.Content, 0)

for _, part := range msg.Parts {
switch part.(type) {

Check failure on line 305 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

typeSwitchVar: 2 cases can benefit from type switch with assignment (gocritic)
case llms.TextContent:
contentArr = append(contentArr, getTextPart(part.(llms.TextContent)))

Check failure on line 307 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

type assertion must be checked (forcetypeassert)
case llms.ToolCall:
tp, err := getToolPart(part.(llms.ToolCall))

Check failure on line 309 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

S1034(related information): could eliminate this type assertion (gosimple)
if err != nil {
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for AI message %T", err, part)
}
contentArr = append(contentArr, tp)
default:
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for AI message %T", ErrInvalidContentType, part)
}
}

cm.Content = contentArr
return cm, nil
}

type ToolResult struct {
Expand All @@ -309,18 +326,39 @@
Content string `json:"content"`
}

func getToolResponse(part llms.ToolCallResponse) (*anthropicclient.ToolResultContent, error) {

Check failure on line 329 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

getToolResponse - result 1 (error) is always nil (unparam)
return &anthropicclient.ToolResultContent{
Type: "tool_result",
ToolUseID: part.ToolCallID,
Content: part.Content,
}, nil

Check failure on line 335 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)
}

Check failure on line 336 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary trailing newline (whitespace)

func handleToolMessage(msg llms.MessageContent) (anthropicclient.ChatMessage, error) {
if toolCallResponse, ok := msg.Parts[0].(llms.ToolCallResponse); ok {
toolContent := anthropicclient.ToolResultContent{
Type: "tool_result",
ToolUseID: toolCallResponse.ToolCallID,
Content: toolCallResponse.Content,
}
cm := anthropicclient.ChatMessage{
Role: RoleUser,
Content: make([]anthropicclient.Content, 0),
}

return anthropicclient.ChatMessage{
Role: RoleUser,
Content: []anthropicclient.Content{toolContent},
}, nil
contentArr := make([]anthropicclient.Content, 0)

for _, part := range msg.Parts {
switch part.(type) {

Check failure on line 347 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

typeSwitchVar: 2 cases can benefit from type switch with assignment (gocritic)
case llms.TextContent:
contentArr = append(contentArr, getTextPart(part.(llms.TextContent)))

Check failure on line 349 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

type assertion must be checked (forcetypeassert)
case llms.ToolCallResponse:
tp, err := getToolResponse(part.(llms.ToolCallResponse))

Check failure on line 351 in llms/anthropic/anthropicllm.go

View workflow job for this annotation

GitHub Actions / lint

S1034(related information): could eliminate this type assertion (gosimple)
if err != nil {
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for tool part response message %T", err, part)
}
contentArr = append(contentArr, tp)
default:
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for AI message %T", ErrInvalidContentType, part)
}
}
return anthropicclient.ChatMessage{}, fmt.Errorf("anthropic: %w for tool message", ErrInvalidContentType)

cm.Content = contentArr

return cm, nil
}
Loading