Skip to content

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

Closed
yp05327 opened this issue Jan 13, 2025 · 6 comments
Closed

Problems when using toolcalls in streaming #164

yp05327 opened this issue Jan 13, 2025 · 6 comments

Comments

@yp05327
Copy link

yp05327 commented Jan 13, 2025

  1. The return of acc.JustFinishedToolCall() - FinishedChatCompletionToolCall does not contain toolCallID, which is is required for openai.ToolMessage, so users need to record it by themselves.
  2. We should append the origin tool call 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 use FinishedChatCompletionToolCall 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 one openai.ToolMessage, you will get this error:

Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.

@dlo
Copy link

dlo commented Mar 5, 2025

@yp05327 Did you figure this out?

@dlo
Copy link

dlo commented Mar 5, 2025

It's undocumented, but here's the solution for anyone else dropping in.

You just need to add an additional ChatCompletionMessage to your message list in the follow-up API call and a ToolMessage message.

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)
}

@jacobzim-stl
Copy link
Collaborator

The tool call id has been added to the accumulator in the upcoming V2 release. Thanks for sharing a solution!

@yp05327
Copy link
Author

yp05327 commented Apr 5, 2025

@jacobzim-stl
Thanks for the work.
I found there are three branchs including v2:
Image
which one is the 'main' branch for v2?

@yp05327
Copy link
Author

yp05327 commented Apr 5, 2025

@dlo
Which version are you using. I'm using 0.1.0-beta6, and I got this error:

cannot use openai.ChatCompletionMessage{…} (value of struct type openai.ChatCompletionMessage) as openai.ChatCompletionMessageParamUnion value in argument to append

Also, there's no openai.F in it.

@yp05327
Copy link
Author

yp05327 commented Apr 5, 2025

Ok, I figured it out.
The correct version is v0.1.0-alpha.67 in v2/next branch.

But in this version, openai.F is not exsist and you can not add openai.ChatCompletionMessage into []openai.ChatCompletionMessageParamUnion directly.
You should use openai.ChatCompletionMessage.ToParam() to convert it into openai.ChatCompletionMessageParamUnion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants