-
Notifications
You must be signed in to change notification settings - Fork 135
Problems when using toolcalls in streaming #164
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
Comments
@yp05327 Did you figure this out? |
It's undocumented, but here's the solution for anyone else dropping in. You just need to add an additional Go playground link: https://go.dev/play/p/35kQ-Zx6jV2 And inline: func main() {
var ctx context.Context
var system, user, functionName, toolCallId, arguments, result string
client := openai.NewClient()
stream := client.Chat.Completions.NewStreaming(ctx, openai.ChatCompletionNewParams{
Messages: openai.F([]openai.ChatCompletionMessageParamUnion{
openai.SystemMessage(system),
openai.UserMessage(user),
openai.ChatCompletionMessage{
Role: "assistant",
ToolCalls: []openai.ChatCompletionMessageToolCall{
{
ID: toolCallId,
Type: "function",
Function: openai.ChatCompletionMessageToolCallFunction{
Name: functionName,
Arguments: arguments,
},
},
},
},
openai.ToolMessage(toolCallId, result),
}),
Model: openai.F(openai.ChatModelGPT4oMini),
})
defer func(stream *ssestream.Stream[openai.ChatCompletionChunk]) {
err := stream.Close()
if err != nil {
panic(fmt.Errorf("error closing stream: %v", err))
}
}(stream)
} |
The tool call id has been added to the accumulator in the upcoming V2 release. Thanks for sharing a solution! |
@jacobzim-stl |
@dlo
Also, there's no |
Ok, I figured it out. But in this version, |
acc.JustFinishedToolCall()
-FinishedChatCompletionToolCall
does not contain toolCallID, which is is required foropenai.ToolMessage
, so users need to record it by themselves.Message
to the message list used in the API call.For non-streaming, you can simply append
chat.choices[0].message
like:https://github.com/openai/openai-[go/blob/952d231c9d5fce675262a6a639a99a62249d4a7f/examples/chat-completion-tool-calling/main.go#L62](https://www.golinks.io/blob/952d231c9d5fce675262a6a639a99a62249d4a7f/examples/chat-completion-tool-calling/main.go#L62?trackSource=github)
For streaming, it seems that there's not implements about it, maybe something like
openai.ToolCallsMessage
? Or, simply useFinishedChatCompletionToolCall
as the Message?Also, I could not find any examples about it. I only found this:
https://github.com/openai/openai-[go/blob/952d231c9d5fce675262a6a639a99a62249d4a7f/examples/chat-completion-accumulating/main.go#L65-L68](https://www.golinks.io/blob/952d231c9d5fce675262a6a639a99a62249d4a7f/examples/chat-completion-accumulating/main.go#L65-L68?trackSource=github)
If you don't append the origin tool call
Message
with only oneopenai.ToolMessage
, you will get this error:The text was updated successfully, but these errors were encountered: