#GPT Functions return structured data
3 messages · Page 1 of 1 (latest)
this will do it 🙂 You dont have to define the function. all you need is the function description in this case. Pay attention that we are forcing gpt to call the function by setting function_call to the name of the function instead of "auto"
import openai
import json
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613", # use gpt-4-0613 for better results
messages=[{"role":"user", "content": "There are 8 sticks, and 3 cones"}],
functions = [
{
"name": "get_number_of_sticks_and_cones",
"description": "Get the number of sticks and cones in a given sentence",
"parameters": {
"type": "object",
"properties": {
"sticks": {
"type": "string",
"description": "The number of sticks",
},
"cones": {
"type": "string",
"description": "The number of cones",
},
},
"required": ["sticks", "cones"],
},
}
],
function_call={"name": "get_number_of_sticks_and_cones"},
)
response = response.choices[0].message
response_args = json.loads(response["function_call"]["arguments"])
print(response_args)
how long does it take to create json?