I need help with the function calling API, this is the basic sequence that OpenAI documentations says:
1 Call the model with the user query and a set of functions defined in the functions parameter.
2 The model can choose to call one or more functions; if so, the content will be a stringified JSON object adhering to your custom schema (note: the model may hallucinate parameters).
3 Parse the string into JSON in your code, and call your function with the provided arguments if they exist.
4 Call the model again by appending the function response as a new message, and let the model summarize the results back to the user.
And an example:
// Step 2: check if the model wanted to call a function
const toolCalls = responseMessage.tool_calls;
if (responseMessage.tool_calls) {
// Step 3: call the function
// Note: the JSON response may not always be valid; be sure to handle errors
const availableFunctions = {
get_current_weather: getCurrentWeather,
}; // only one function in this example, but you can have multiple
messages.push(responseMessage); // extend conversation with assistant's reply
for (const toolCall of toolCalls) {
......
messages.push({
tool_call_id: toolCall.id,
role: "tool",
name: functionName,
content: functionResponse,
}); // extend conversation with function response
}
const secondResponse = await openai.chat.completions.create({
model: "gpt-3.5-turbo-0125",
messages: messages,
}); // get a new response from the model where it can see the function response
return secondResponse.choices;
}
}
I have a question, if i have a chatting website, with the AI, it means that the first response saved in the messages should not be seen and neither the message with the tool role, but only the last message generated with the second response, right?