-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathresponses-function-weather-aoai.py
More file actions
80 lines (71 loc) · 2.54 KB
/
responses-function-weather-aoai.py
File metadata and controls
80 lines (71 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import json
import requests
from openai import AzureOpenAI
from dotenv import load_dotenv
load_dotenv()
def get_weather(latitude: float, longitude: float) -> str:
"""Return a short natural language weather summary for given coordinates."""
r = requests.get(
"https://api.open-meteo.com/v1/forecast",
params={
"latitude": latitude,
"longitude": longitude,
"current": "temperature_2m,wind_speed_10m",
},
timeout=10,
)
c = r.json().get("current", {})
temp = c.get("temperature_2m")
wind = c.get("wind_speed_10m")
return f"Current temperature is {temp}°C with wind {wind} m/s." if temp is not None else "Weather data unavailable."
client = AzureOpenAI(
api_key = os.environ["AZURE_OPENAI_API_KEY"],
api_version = os.environ["AZURE_OPENAI_API_VERSION"],
azure_endpoint = os.environ["AZURE_OPENAI_API_ENDPOINT"]
)
tools = [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature (C) & wind for coordinates.",
"parameters": {
"type": "object",
"properties": {
"latitude": {"type": "number"},
"longitude": {"type": "number"},
},
"required": ["latitude", "longitude"],
},
}
]
# Running conversation state
conversation = [
{"role": "user", "content": "What's the weather in London right now?"}
]
# First model call – model decides whether to call the tool
resp1 = client.responses.create(
model=os.getenv("AZURE_OPENAI_API_MODEL"),
tools=tools,
input=conversation,
)
# Add model output messages (may include a function_call) to the conversation
conversation += resp1.output
# Execute any function calls and append outputs
for item in resp1.output:
if item.type == "function_call" and item.name == "get_weather":
args = json.loads(item.arguments)
weather_text = get_weather(args["latitude"], args["longitude"])
conversation.append({
"type": "function_call_output",
"call_id": item.call_id,
"output": json.dumps({"weather": weather_text}),
})
# Second model call – model incorporates tool output and answers user
final_resp = client.responses.create(
model=os.getenv("AZURE_OPENAI_API_MODEL"),
tools=tools,
instructions="Answer using the weather tool output only; be concise.",
input=conversation,
)
print(final_resp.output_text)