The “function” role is for returning the response back to the AI, and actually now you should migrate to tools, which requires you to pass a matching ID in the assistant tool output and the tool function return.
The “function” alone doesn’t let the AI imagine what function that text came from, and no spec = no function call trained model.
For passing the specification, I whipped up a bit of code with demo parsing of the API response, and using the with_raw_response, so headers and other request object info is also available.
from openai import OpenAI
client = OpenAI()
tools=[{
"type": "function",
"function": {
"name": "get_weather_forecast",
"description": "Get weather forecast",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state.",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use.",
},
"time_period": {
"type": "number",
"description": "length in days or portions of day",
}
},
"required": ["location", "format", "num_days"]
},
}
}]
params = {
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful AI assistant."},
{"role": "user", "content": "How hot will it be today in Seattle?"}
],
"tools": tools
}
c = client.chat.completions.with_raw_response.create(**params)
# print any response always
if c.parse().choices[0].message.content:
print(c.parse().choices[0].message.content)
# print the first function invoked
if c.parse().choices[0].finish_reason:
print(c.parse().choices[0].message.tool_calls[0].function)