Fix double reply callback on tool calls
This commit is contained in:
parent
ec1f326c2a
commit
62f07dd240
@ -197,6 +197,8 @@ func (c *AnthropicClient) CreateChatCompletionStream(
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
sb := strings.Builder{}
|
||||
|
||||
isToolCall := false
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
line = strings.TrimSpace(line)
|
||||
@ -271,6 +273,8 @@ func (c *AnthropicClient) CreateChatCompletionStream(
|
||||
return content, fmt.Errorf("reached </function_calls> stop sequence but no opening tag found")
|
||||
}
|
||||
|
||||
isToolCall = true
|
||||
|
||||
funcCallXml := content[start:]
|
||||
funcCallXml += FUNCTION_STOP_SEQUENCE
|
||||
|
||||
@ -316,11 +320,13 @@ func (c *AnthropicClient) CreateChatCompletionStream(
|
||||
case "message_stop":
|
||||
// return the completed message
|
||||
if callback != nil {
|
||||
if !isToolCall {
|
||||
callback(model.Message{
|
||||
Role: model.MessageRoleAssistant,
|
||||
Content: sb.String(),
|
||||
})
|
||||
}
|
||||
}
|
||||
return sb.String(), nil
|
||||
case "error":
|
||||
return sb.String(), fmt.Errorf("an error occurred: %s", event["error"])
|
||||
|
@ -204,7 +204,7 @@ func (c *OpenAIClient) CreateChatCompletionStream(
|
||||
ctx context.Context,
|
||||
params model.RequestParameters,
|
||||
messages []model.Message,
|
||||
callbback provider.ReplyCallback,
|
||||
callback provider.ReplyCallback,
|
||||
output chan<- string,
|
||||
) (string, error) {
|
||||
client := openai.NewClient(c.APIKey)
|
||||
@ -256,23 +256,23 @@ func (c *OpenAIClient) CreateChatCompletionStream(
|
||||
return content.String(), err
|
||||
}
|
||||
|
||||
if callbback != nil {
|
||||
if callback != nil {
|
||||
for _, result := range results {
|
||||
callbback(result)
|
||||
callback(result)
|
||||
}
|
||||
}
|
||||
|
||||
// Recurse into CreateChatCompletionStream with the tool call replies
|
||||
messages = append(messages, results...)
|
||||
return c.CreateChatCompletionStream(ctx, params, messages, callbback, output)
|
||||
}
|
||||
|
||||
if callbback != nil {
|
||||
callbback(model.Message{
|
||||
return c.CreateChatCompletionStream(ctx, params, messages, callback, output)
|
||||
} else {
|
||||
if callback != nil {
|
||||
callback(model.Message{
|
||||
Role: model.MessageRoleAssistant,
|
||||
Content: content.String(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return content.String(), err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user