Fix tool calling
This commit is contained in:
@@ -85,7 +85,23 @@ func (p *Provider) Generate(ctx context.Context, messages []api.Message, req *ap
|
||||
case "user":
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewUserMessage(anthropic.NewTextBlock(content)))
|
||||
case "assistant":
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewAssistantMessage(anthropic.NewTextBlock(content)))
|
||||
// Build content blocks including text and tool calls
|
||||
var contentBlocks []anthropic.ContentBlockParamUnion
|
||||
if content != "" {
|
||||
contentBlocks = append(contentBlocks, anthropic.NewTextBlock(content))
|
||||
}
|
||||
// Add tool use blocks
|
||||
for _, tc := range msg.ToolCalls {
|
||||
var input map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(tc.Arguments), &input); err != nil {
|
||||
// If unmarshal fails, skip this tool call
|
||||
continue
|
||||
}
|
||||
contentBlocks = append(contentBlocks, anthropic.NewToolUseBlock(tc.ID, input, tc.Name))
|
||||
}
|
||||
if len(contentBlocks) > 0 {
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewAssistantMessage(contentBlocks...))
|
||||
}
|
||||
case "tool":
|
||||
// Tool results must be in user message with tool_result blocks
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewUserMessage(
|
||||
@@ -213,7 +229,23 @@ func (p *Provider) GenerateStream(ctx context.Context, messages []api.Message, r
|
||||
case "user":
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewUserMessage(anthropic.NewTextBlock(content)))
|
||||
case "assistant":
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewAssistantMessage(anthropic.NewTextBlock(content)))
|
||||
// Build content blocks including text and tool calls
|
||||
var contentBlocks []anthropic.ContentBlockParamUnion
|
||||
if content != "" {
|
||||
contentBlocks = append(contentBlocks, anthropic.NewTextBlock(content))
|
||||
}
|
||||
// Add tool use blocks
|
||||
for _, tc := range msg.ToolCalls {
|
||||
var input map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(tc.Arguments), &input); err != nil {
|
||||
// If unmarshal fails, skip this tool call
|
||||
continue
|
||||
}
|
||||
contentBlocks = append(contentBlocks, anthropic.NewToolUseBlock(tc.ID, input, tc.Name))
|
||||
}
|
||||
if len(contentBlocks) > 0 {
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewAssistantMessage(contentBlocks...))
|
||||
}
|
||||
case "tool":
|
||||
// Tool results must be in user message with tool_result blocks
|
||||
anthropicMsgs = append(anthropicMsgs, anthropic.NewUserMessage(
|
||||
|
||||
@@ -86,7 +86,32 @@ func (p *Provider) Generate(ctx context.Context, messages []api.Message, req *ap
|
||||
case "user":
|
||||
oaiMessages = append(oaiMessages, openai.UserMessage(content))
|
||||
case "assistant":
|
||||
oaiMessages = append(oaiMessages, openai.AssistantMessage(content))
|
||||
// If assistant message has tool calls, include them
|
||||
if len(msg.ToolCalls) > 0 {
|
||||
toolCalls := make([]openai.ChatCompletionMessageToolCallUnionParam, len(msg.ToolCalls))
|
||||
for i, tc := range msg.ToolCalls {
|
||||
toolCalls[i] = openai.ChatCompletionMessageToolCallUnionParam{
|
||||
OfFunction: &openai.ChatCompletionMessageFunctionToolCallParam{
|
||||
ID: tc.ID,
|
||||
Function: openai.ChatCompletionMessageFunctionToolCallFunctionParam{
|
||||
Name: tc.Name,
|
||||
Arguments: tc.Arguments,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
msgParam := openai.ChatCompletionAssistantMessageParam{
|
||||
ToolCalls: toolCalls,
|
||||
}
|
||||
if content != "" {
|
||||
msgParam.Content.OfString = openai.String(content)
|
||||
}
|
||||
oaiMessages = append(oaiMessages, openai.ChatCompletionMessageParamUnion{
|
||||
OfAssistant: &msgParam,
|
||||
})
|
||||
} else {
|
||||
oaiMessages = append(oaiMessages, openai.AssistantMessage(content))
|
||||
}
|
||||
case "system":
|
||||
oaiMessages = append(oaiMessages, openai.SystemMessage(content))
|
||||
case "developer":
|
||||
@@ -194,7 +219,32 @@ func (p *Provider) GenerateStream(ctx context.Context, messages []api.Message, r
|
||||
case "user":
|
||||
oaiMessages = append(oaiMessages, openai.UserMessage(content))
|
||||
case "assistant":
|
||||
oaiMessages = append(oaiMessages, openai.AssistantMessage(content))
|
||||
// If assistant message has tool calls, include them
|
||||
if len(msg.ToolCalls) > 0 {
|
||||
toolCalls := make([]openai.ChatCompletionMessageToolCallUnionParam, len(msg.ToolCalls))
|
||||
for i, tc := range msg.ToolCalls {
|
||||
toolCalls[i] = openai.ChatCompletionMessageToolCallUnionParam{
|
||||
OfFunction: &openai.ChatCompletionMessageFunctionToolCallParam{
|
||||
ID: tc.ID,
|
||||
Function: openai.ChatCompletionMessageFunctionToolCallFunctionParam{
|
||||
Name: tc.Name,
|
||||
Arguments: tc.Arguments,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
msgParam := openai.ChatCompletionAssistantMessageParam{
|
||||
ToolCalls: toolCalls,
|
||||
}
|
||||
if content != "" {
|
||||
msgParam.Content.OfString = openai.String(content)
|
||||
}
|
||||
oaiMessages = append(oaiMessages, openai.ChatCompletionMessageParamUnion{
|
||||
OfAssistant: &msgParam,
|
||||
})
|
||||
} else {
|
||||
oaiMessages = append(oaiMessages, openai.AssistantMessage(content))
|
||||
}
|
||||
case "system":
|
||||
oaiMessages = append(oaiMessages, openai.SystemMessage(content))
|
||||
case "developer":
|
||||
|
||||
Reference in New Issue
Block a user