Function Calling - Return Multiple Objects

When applying Function Calling, is there a way to have ChatCompletions model return multiple objects?

Use the get_current_weather() function as an example (as in OpenAI official guide).
When the user query is: "What's the weather like in Boston, and what's the weather in Houston?"

How can I let GPT return [{'location': 'Boston, MA'}, {'location': 'Houston, TX'}]?

Accept an array or csv list of cities and give the json accordingly? Should work.

1 Like

Try this function

{
        name: 'get_weather', 
        description: 'Get weather from given locations and datetimes', 
        parameters: {
            type: 'object', 
            properties: {
                locations: {
                    type: 'array', 
                    items: {
                        type: "object",
                        properties: {
                            name: { type: "string", description: "Name of location, e.g. San Francisco, CA" },
                            datetime: { type: "string", description: "Date or time, e.g. today, tomorrow, 2023-06-29" },
                        }
                    }
                }
            }, 
            required: ['locations']
        }
 }

Using your sample inquiry, "What's the weather like in Boston, and what's the weather in Houston?", will get you:

{
  role: 'assistant',
  content: null,
  function_call: {
    name: 'get_weather',
    arguments: '{\n' +
      '  "locations": [\n' +
      '    {\n' +
      '      "name": "Boston",\n' +
      '      "datetime": "2022-01-01T12:00:00"\n' +
      '    },\n' +
      '    {\n' +
      '      "name": "Houston",\n' +
      '      "datetime": "2022-01-01T12:00:00"\n' +
      '    }\n' +
      '  ]\n' +
      '}'
  }
}

If we modify the inquiry by adding time, "What's the weather like in Boston today, and what's the weather in Houston tomorrow?", will get you:

{
  role: 'assistant',
  content: null,
  function_call: {
    name: 'get_weather',
    arguments: '{\n' +
      '  "locations": [\n' +
      '    {\n' +
      '      "name": "Boston",\n' +
      '      "datetime": "today"\n' +
      '    },\n' +
      '    {\n' +
      '      "name": "Houston",\n' +
      '      "datetime": "tomorrow"\n' +
      '    }\n' +
      '  ]\n' +
      '}'
  }
}
3 Likes

This works!! Thank you so much!!

like this:
curl OPENAI_API -u :$OPENAI_KEY -H ‘Content-Type: application/json’ -d ‘{
“model”: “gpt-3.5-turbo-0613”,
“messages”: [
{“role”: “user”, “content”: “Change the text to Hello World and top to 10 and left to 30”}
],
“functions”: [
{
“name”: “change_value”,
“description”: “Update value of object key”,
“parameters”: {
“type”: “object”,
“properties”: {
“props”: {
“type”: “array”,
“items”: {
“type”: “object”,
“properties”: {
“key”: { “type”: “string”, “description”: “Key that needs to be changed” },
“value”: { “type”: “string”, “description”: “value that needs to be updated” }
}
}
}
},
“required”: [“props”]
}
}
]
}’