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

Error when using model 'gpt-4o' with multiple tools in chat-with-tools example #1623

Closed
rossanodr opened this issue May 16, 2024 · 3 comments

Comments

@rossanodr
Copy link

Description

Description:
The error occurs when following the example: link but using the model const model = 'gpt-4o';. When making a call with 2 or more tools, the following error happens:
Error calling experimental_onToolCall: BadRequestError: 400 An assistant message with 'tool_calls' must be followed by tool messages responding to each 'tool_call_id'. The following tool_call_ids did not have response messages: call_vg71gjwxV7clGEZU9t664SrO

Code example

const response = await openai.chat.completions.create({
model: 'gpt-4o',
stream: true,
messages,
tools,
tool_choice: "auto",
});

const data = new StreamData();
const stream = OpenAIStream(response, {
    experimental_onToolCall: async (
        call: ToolCallPayload,
        appendToolCallMessage
    ) => {
        for (const toolCall of call.tools) {...

Additional context

Works fine if the const response = await openai.chat.completions.create({ model: 'gpt-3.5-turbo-0613, stream: true, messages, tools, tool_choice: 'auto', });
and the
return openai.chat.completions.create({ messages: [...messages, ...newMessages], model: 'gpt-4o', stream: true, tools, tool_choice: 'auto', });

@rossanodr
Copy link
Author

Fixed:
The error was using the
return openai.chat.completions.create({ messages: [...messages, ...newMessages], model: "gpt-4o", stream: true, tools, tool_choice: "auto", });
inside the loop for (const toolCall of call.tools)

@zineanteoh
Copy link

@rossanodr how did you end up fixing this?

@rossanodr
Copy link
Author

rossanodr commented May 29, 2024

@rossanodr how did you end up fixing this?

sorry for the late response
I just put the
return openai.chat.completions.create({ messages: [...messages, ...newMessages], model: "gpt-4o", stream: true, tools, tool_choice: "auto", });
outside the for (const toolCall of call.tools) {}loop
so before the code was something like
` let newMessages;
for (const toolCall of call.tools) {
if (toolCall.func.name === "search") {
// Call a CNPJ search API here
if (typeof toolCall.func.arguments.keyword === "string") {
const resultData = await searchFunction(
toolCall.func.arguments.keyword as string
);

                    newMessages = appendToolCallMessage({
                        tool_call_id: toolCall.id,
                        function_name: "search",
                        tool_call_result: resultData,
                    });
                }
                  } else if (toolCall.func.name === "taxas_search") {
                // Call a taxas search API here
                const resultData = await taxasSearchFunction();

                newMessages = appendToolCallMessage({
                    tool_call_id: toolCall.id,
                    function_name: "taxas_search",
                    tool_call_result: resultData,
                });
                  return openai.chat.completions.create({
                messages: [...messages, ...newMessages],
                model: "gpt-4o",
                stream: true,
                tools,
                tool_choice: "auto",
                temperature: 0,
            });
            } `
            
            then 

             `  let newMessages;
        for (const toolCall of call.tools) {
            if (toolCall.func.name === "search") {
                // Call a CNPJ search API here
                if (typeof toolCall.func.arguments.keyword === "string") {
                    const resultData = await searchFunction(
                        toolCall.func.arguments.keyword as string
                    );

                    newMessages = appendToolCallMessage({
                        tool_call_id: toolCall.id,
                        function_name: "search",
                        tool_call_result: resultData,
                    });
                }
                  } else if (toolCall.func.name === "taxas_search") {
                // Call a taxas search API here
                const resultData = await taxasSearchFunction();

                newMessages = appendToolCallMessage({
                    tool_call_id: toolCall.id,
                    function_name: "taxas_search",
                    tool_call_result: resultData,
                });
              
            }
                  if (newMessages) {
            return openai.chat.completions.create({
                messages: [...messages, ...newMessages],
                model: "gpt-4o",
                stream: true,
                tools,
                tool_choice: "auto",
                temperature: 0,
            });
        }`

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

2 participants