Closed Crayon-ShinChan closed 3 months ago
I would suggest going through the docs and properly setting up function calling
I would suggest going through the docs and properly setting up function calling
Hi, do you mean add experimental_onFunctionCall
? But I don't need the experimental_onFunctionCall
, because I just want to extract the student name and gpa in the input message. I don't need to call any external api like weather api.
For example, if the input
is "I'm a student named Joe with 3.7 GPA", then the message
from useChat hook already returns the right answer
{
"name": "Joe",
"GPA": 3.7,
}
But it just infinite loop the process and message was streamed (from empty json to the correct json string) again and again.
I create functionCallHandler
const functionCallHandler: FunctionCallHandler = async (
chatMessages,
functionCall
) => {
if (functionCall.name === "student_info") {
console.log("chatMessages: ", chatMessages);
console.log("functionCall: ", functionCall);
}
};
then this infinite loop problem solved. However, I think in my situation, I don't really need such a functionCallHandler in my code logic because I don't call external API or something.
Is there any plan to support functional call without functionCallHandler?
Try using experimental_onFunctionCall
const stream = OpenAIStream(response, {
experimental_onFunctionCall: async (
{ name, arguments: args },
createFunctionCallMessages,
) => {
// if you skip the function call and return nothing, the `function_call`
// message will be sent to the client for it to handle
if (name === 'get_current_weather') {
// Call a weather API here
const weatherData = {
temperature: 20,
unit: args.format === 'celsius' ? 'C' : 'F',
};
// `createFunctionCallMessages` constructs the relevant "assistant" and "function" messages for you
const newMessages = createFunctionCallMessages(weatherData);
return openai.chat.completions.create({
messages: [...messages, ...newMessages],
stream: true,
model: 'gpt-3.5-turbo-0613',
// see "Recursive Function Calls" below
functions,
});
}
},
});
Use the functionCallHandler (client side) and process your logic from the functionCall message inside it, But don't return a functionResponse like the docs tells you to. Typescript will throw errors, but you can ignore them considering your use case.
Please check out our new tool calling with AI SDK Core and useChat: https://sdk.vercel.ai/docs/ai-sdk-ui/chatbot-with-tool-calling#example-server-side-tool-execution-with-roundtrips
Description
I use
useChat
in client side. when I didn't use function call, everything was fine. but when I used function call, it called the api again and again in a infinite loop.Code example
Additional context
No response