CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Working with the OpenAI Responses API

Afficher le cours

Instructions

  • Loop through the response output items from the first Responses request to check if contains a 'function_call' to 'convert_timezone'; then call convert_timezone() on the unpacked arguments from the item, storing the result in timezone_result.
  • Add a message of type 'function_call_output' to the messages list containing the result from convert_timezone().
  • Create the final Responses request with the messages containing the function result and, again, passing the tools list.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

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)
Modifier et exécuter le code