Function Calling doesn't do multiple calls unless tool_choice="auto"

I have the following function:

    tools = [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "Get the current weather in a given location",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
                    },
                    "required": ["location"],
                },
            },
        }

User Message:

    messages = [{"role": "user", "content": "What's the weather like in San Francisco, Tokyo, and Paris?"}]

If I have tool_choice as auto I get multiple tool_calls and the response I am looking for (albeit everyone in San Francisco is burning alive):

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        tools=tools,
        tool_choice="auto"
        #tool_choice={
        #    "type": "function",
        #    "function": {"name": "get_current_weather"},
        #},    
    )

{"location": "San Francisco", "temperature": "72", "unit": "celsius"}
{"location": "Tokyo", "temperature": "10", "unit": "celcius"}
{"location": "Paris", "temperature": "22", "unit": "celsius"}

When I am explicate on the function that I want to use, I only get one function call, and only get the weather for San Francisco:

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        tools=tools,
        #tool_choice="auto"
        tool_choice={
            "type": "function",
            "function": {"name": "get_current_weather"},
        },    
    ) 

{"location": "San Francisco", "temperature": "72", "unit": "celsius"}

Auto is not an option in my use case. Neither is only one function call for most prompts.

Has anyone experienced this behavior? How did you work around it?

1 Like

I found a working solution. I first make a call to gpt to process the user message:

            {
                "type": "function",
                "function": {
                    "name": "process_user_message",
                    "description": "Analyzes user message and adds content to it that will help GPT understand how to process it in the next call.",
                    "parameters": {
                        "type": "object",
                        "properties": {
                            "num_segments":{
                                "type":"integer",
                                "description": "Number of cities in the request"
                            },
                            
                        },
                    "required": ["num_cities"]
                    }
                }
            },

    def process_user_message(self, num_cities=1):
        # create a dictionary with user_message and num_cities
        data = {
            "user_message": self.user_message,
            "num_cities": num_cities
        }

        # convert the dictionary to a JSON string and return it
        json_string = json.dumps(data)
        print(json_string)
        return json.dumps(data)

I then use the num_cities value to loop over chat completions:

    for i in range(0,num_cities):
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=messages,
            tools=tools,
            #tool_choice="auto"
            tool_choice={
                "type": "function",
                "function": {"name": "get_current_weather"},
            },    
        )
1 Like