Integrating Function-Calling Tools
You've created a timezone conversion tool using the convert_timezone() function and defined it in OpenAI's tool format. Now you need to implement the complete function-calling workflow. The client is already initialized, and the tools list contains your timezone conversion tool definition. The convert_timezone() function is also ready to use.
A messages list has been started containing a user input that requires the timezone information from your convert_timezone tool.
Deze oefening maakt deel uit van de cursus
Working with the OpenAI Responses API
Oefeninstructies
- Loop through the response output items from the first Responses request to check if contains a
'function_call'to'convert_timezone'; then callconvert_timezone()on the unpacked arguments from the item, storing the result intimezone_result. - Add a message of type
'function_call_output'to the messages list containing the result fromconvert_timezone(). - Create the final Responses request with the messages containing the function result and, again, passing the
toolslist.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
messages = [{"role": "user", "content": "What time is 2:30pm on January 20th in New York in Tokyo time?"}]
response = client.responses.create(model="gpt-5-mini", input=messages, tools=tools)
messages += response.output
# Process function calls and execute the timezone conversion
for item in response.output:
if item.type == "____":
if item.name == "____":
timezone_result = ____(**json.loads(item.arguments))
# Append function output to messages
messages.append({"type": "____", "call_id": item.call_id, "output": json.dumps({"convert_timezone": ____})})
# Make second API request with function results
response = client.responses.create(model="gpt-5-mini", input=____, tools=____)
print(response.output_text)