Hi there! I’m building an scheduling application that uses chatgpt under the hood with functions, and I’ve observed that sometimes chat gpt calls the same function multiple times in a row…
[ {
“role”:“user”,
“content”:“I want a dermaplaning appointment for next Monday in Exton.”
},
{
“role”:“assistant”,
“content”:“”,
“function_call”:{
“name”:“get_schedule_for_appointment_id”,
“arguments”:“{\n "appointment_id": 517\n}”
}
},
{
“role”:“function”,
“content”:“{"availabilities": }”,
“name”:“get_schedule_for_appointment_id”
},
{
“role”:“assistant”,
“content”:“”,
“function_call”:{
“name”:“get_schedule_for_appointment_id”,
“arguments”:“{\n "appointment_id": 517\n}”
}
},
{
“role”:“function”,
“content”:“{"availabilities": }”,
“name”:“get_schedule_for_appointment_id”
},
{
“role”:“assistant”,
“content”:“I apologize, but it seems that there are no available appointments for dermaplaning at the Exton location next Monday. However, I can offer you some alternative dates and times. Would you like me to provide you with the available options?”
}]
I don’t like that it calls the same function multiple times. Is there a way to prevent this behavior? Thanks!
The AI needs a chat history to see what it called that did or didn’t work for ideal understanding of what to do next.
Here is a pattern for creating the additional “assistant called a function” + “function replies back to AI” messages in chat history after the user question, which should be maintained until at least all iterations are done.
# Check if the model wants to call a function
if response_message.get("function_call"):
# Call the function. The JSON response may not always be valid so make sure to handle errors
function_name = response_message["function_call"]["name"]
available_functions = {
"search_hotels": search_hotels,
}
function_to_call = available_functions[function_name]
function_args = json.loads(response_message["function_call"]["arguments"])
function_response = function_to_call(**function_args)
# Add the assistant response and function response to the messages
messages.append( # adding assistant response to messages
{
"role": response_message["role"],
"function_call": {
"name": function_name,
"arguments": response_message["function_call"]["arguments"],
},
"content": None
}
)
messages.append( # adding function response to messages
{
"role": "function",
"name": function_name,
"content": function_response,
}
)
@_j I’m pretty sure that’s exactly what I’m doing…what I pasted above is what my messages list looks like…it’s just that chatgpt seems to calls two of the same functions back to back, which is what I’m trying to avoid
I am also facing the same issue. In my case the api is calling the same function 5 times or more and on each function call I can see that the function output has been submitted to the run and the run state changes to ‘in_progress’, then ‘queue’ and then back to ‘requires_action’. I have set a time limit of 30s and each time the assistant keep on calling the same function until the timeout exception is raised.
I am facing the same issue as well. The function output is submitted and the AI requests the call multiple times. No errors in the responses. Looking at the messages once the run is completed, it mentions that it could not get the information and that it is retrying.