import json
from openai import OpenAI
client = OpenAI()
def get_current_weather(location, unit="fahrenheit"):
weather_info = {
"location": location,
"temperature": "72",
"unit": unit,
"forecast": ["sunny", "windy"],
}
return json.dumps(weather_info)
tools = [{
"type": "function",
"name": "get_current_weather",
"description": "Get the current weather for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": [
"location"
],
"additionalProperties": False
}
}]
input_messages=[{"role": "user", "content": "What is the weather like in Orlando, Florida today?"}]
response = client.responses.create(
model="gpt-4o",
input=input_messages,
tools=tools
)
print("Step 1: Initial result for weather in Orlando:")
print(response.output)
print("\nStep 2: Call the tool it wants")
tool_call = response.output[0]
args = json.loads(tool_call.arguments)
result = get_current_weather(args["location"], args["unit"])
print(result)
print("\nStep 3: Incorporate results into prompt")
input_messages.append(tool_call) input_messages.append({ "type": "function_call_output",
"call_id": tool_call.call_id,
"output": str(result)
})
response_2 = client.responses.create(
model="gpt-4o",
input=input_messages,
tools=tools,
)
print(response_2.output_text)