-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path02-tools-openai.py
113 lines (94 loc) · 3.31 KB
/
02-tools-openai.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import json
import sys
import requests
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
# Initialize the OpenAI client
client = OpenAI()
# Define a simple tool for getting the current weather
def get_weather(latitude, longitude):
base_url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": latitude,
"longitude": longitude,
"current": "temperature_2m,wind_speed_10m,relative_humidity_2m",
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status() # Raise an exception for bad status codes
return response.text
except requests.RequestException as e:
return f"Error fetching weather data: {str(e)}"
# Define the tool for the API
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a location using latitude and longitude",
"parameters": {
"type": "object",
"properties": {
"latitude": {
"type": "number",
"description": "The latitude coordinate",
},
"longitude": {
"type": "number",
"description": "The longitude coordinate",
},
},
"required": ["latitude", "longitude"],
},
},
}
]
def process_conversation(messages):
while True:
response = client.chat.completions.create(
model="gpt-4o", # Make sure to use a model that supports function calling
messages=messages,
tools=tools,
tool_choice="auto",
)
message = response.choices[0].message
messages.append(message.model_dump())
if not message.tool_calls:
# If there are no tool calls, we're done
return message.content
# Process all tool calls
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
if function_name == "get_weather":
latitude = function_args.get("latitude")
longitude = function_args.get("longitude")
content = get_weather(latitude, longitude)
else:
# If the function is unknown, return an error message
# and also log to stderr
content = f"Unknown function: {function_name}"
print(f"Unknown function: {function_name}", file=sys.stderr)
messages.append(
{
"role": "tool",
"tool_call_id": tool_call.id,
"name": function_name,
"content": content,
}
)
# Initial conversation
messages = [
{
"role": "system",
"content": "You are a helpful assistant that can check the weather. Report results in imperial units.",
},
{
"role": "user",
"content": "What's the weather like in Seattle?",
},
]
# Start the conversation and process any tool calls
final_response = process_conversation(messages)
print("Final response:", final_response)