GPT function return all relevant from enum

I am using GPTs function calling to help classify specific things, in this regard I am also using enum as there are a strict set of categories I want it to return within. However, I was wondering if there is a way to make it return all relevant ones instead of just one, e.g. say I categories a news article with the title “Startups and medium sized business are both doing amazing”, and my tags are [“Startups”, “Small business”, “Medium business”, …] I would it to return both "Startups and “Medium business”

I havent attached any code as I dont know if the above is possible.

I’m pretty sure a function argument can be an array of strings :thinking:

Does that solve your issue?

Like so? This doesnt seem to make a difference for me

function_name: {
“type”: “array”,
“enum”:[“apple”,“pear”,“watermelon”,“strawberries”,“raspberries”,“cherries”,“yogurt”]
},
},

Here’s a contrived example:

            "function": {
                "name": "get_multiple_current_weathers",
                "description": "Get the current weather in given locations",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "locations": {
                            "type": "array",
                            "description": "An array of city strings, e.g. ['SF', 'LA', 'Paris']",
                            "items": {
                                "type": "string", "enum": ["SF", "LA", "NY", "Paris", "London"]
                            }
                        }
                    },
                    "required": ["locations"]
                }
            }

this is the response you’ll get:

{'id': 'chatcmpl-...',
 'object': 'chat.completion',
 'created': 1705187563,
 'model': 'gpt-3.5-turbo-1106',
 'choices': [{'index': 0,
   'message': {'role': 'assistant',
    'content': None,
    'tool_calls': [{'id': '...',
      'type': 'function',
      'function': {'name': 'get_multiple_current_weathers',
       'arguments': '{"locations": ["SF"]}'}},
     {'id': '...',
      'type': 'function',
      'function': {'name': 'get_multiple_current_weathers',
       'arguments': '{"locations": ["Paris"]}'}}]},
   'logprobs': None,
   'finish_reason': 'tool_calls'}],
 'usage': {'prompt_tokens': 106, 'completion_tokens': 50, 'total_tokens': 156},
 'system_fingerprint': '...'}
full example
import requests
import json
import os

# Ensure you have your OpenAI API key set in the environment variables
openai_api_key = os.getenv("OPENAI_API_KEY")
if openai_api_key is None:
    raise ValueError("OpenAI API key is not set in environment variables.")

url = "https://api.openai.com/v1/chat/completions"

headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {openai_api_key}"
}

data = {
    "model": "gpt-3.5-turbo-1106",
    "messages": [
        {
            "role": "system",
            "content": "You are a helpful assistant."
        },
        {
            "role": "user",
            "content": "Hello! What's the weather like in SF and paris?"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_multiple_current_weathers",
                "description": "Get the current weather in given locations",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "locations": {
                            "type": "array",
                            "description": "An array of city strings, e.g. ['SF', 'LA', 'Paris']",
                            "items": {
                                "type": "string", "enum": ["SF", "LA", "NY", "Paris", "London"]
                            }
                        }
                    },
                    "required": ["locations"]
                }
            }
        }
    ],
    "tool_choice":"auto"
}

response = requests.post(url, headers=headers, json=data)

# Check if the request was successful
if response.status_code == 200:
    print("Response from OpenAI:", response.json())
    print('\n')
    print(response.json()['choices'][0]['message']['content'])
else:
    print("Error:", response.status_code, response.text)

interesting - it seems to prefer calling the method multiple times…

well, functions are trash :laughing: (personal opinion)

1 Like

This works, really great, and even without multiple function calling when i use it

1 Like