Chatgpt with functions calls the same function twice

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!

2 Likes

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,
        }
    ) 
1 Like

@_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

1 Like

Maybe the AI can’t believe that a function would just return empty brackets and not tell it text like “no matching appointment times” or such.

1 Like

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.

Checkout my code here: Assistant API - Function Calling

Let me know if someone finds the solution for this problem.

1 Like

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.

To solve this issue, specify the name of your function in : tool_choice={“type”: “function”, “function”: {“name”: “name of your function”}}

@oumaima.elmouelhi I notice three different ways of invoking function call: as parameter in chat.completion, as a role in message, and as an entry inside parameter tool_choice or tools.
what are the differences?

The AI is the one doing the “invoking”. You as a developer:

  1. provide the specification of what functions are available, the functions API parameter
  2. after a request to use a function is emitted, you fulfill that, and return the value as a function role message (along with a special version of what the assistant wrote to get that return value.

Tools is a newer version for specifying functions, which requires an ID of the tool that was called be repeated in the assistant and tool return that you send back.