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 (personal opinion)