-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
233 lines (210 loc) · 10.4 KB
/
tools.py
File metadata and controls
233 lines (210 loc) · 10.4 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import time
import random
import json
# ---------------------------------------------------------------------------
# Tool implementations
# ---------------------------------------------------------------------------
def get_weather(city: str) -> str:
print(f" ⚡ [get_weather] Fetching weather for {city}... (instant)")
data = {
"mumbai": "32°C, humid and partly cloudy",
"amsterdam": "9°C, overcast with light drizzle",
"tokyo": "18°C, rainy",
"paris": "14°C, sunny",
}
return data.get(city.lower(), f"25°C, clear skies in {city}")
def get_hotels(city: str) -> str:
delay = random.uniform(15, 30)
print(f" ⏳ [get_hotels] Fetching hotels for {city}... ({delay:.1f}s)")
time.sleep(delay)
hotels = {
"mumbai": [
{"name": "The Taj Mahal Palace", "area": "Colaba", "cost_per_night": "$280"},
{"name": "Oberoi Trident", "area": "Nariman Point", "cost_per_night": "$220"},
{"name": "ITC Grand Central", "area": "Parel", "cost_per_night": "$160"},
{"name": "Hotel Sea Princess", "area": "Juhu", "cost_per_night": "$110"},
{"name": "Residency Hotel", "area": "Fort", "cost_per_night": "$70"},
],
"amsterdam": [
{"name": "Hotel V Nesplein", "area": "City Centre", "cost_per_night": "$210"},
{"name": "The Dylan", "area": "Jordaan", "cost_per_night": "$350"},
{"name": "citizenM Amsterdam", "area": "Museum Quarter","cost_per_night": "$160"},
{"name": "Generator Amsterdam", "area": "East", "cost_per_night": "$60"},
{"name": "INK Hotel", "area": "De Pijp", "cost_per_night": "$130"},
],
}
results = hotels.get(city.lower())
if not results:
return f"No hotel data available for {city}."
lines = [f"Hotels in {city.title()}:"]
for h in results:
lines.append(f" • {h['name']} — {h['area']} — {h['cost_per_night']}/night")
return "\n".join(lines)
def get_activities(city: str, tag: str = None) -> str:
"""
city: mumbai | amsterdam
tag: couple | family | solo (optional — returns all if omitted)
"""
delay = random.uniform(15, 30)
print(f" ⏳ [get_activities] Fetching activities for {city} (tag={tag})... ({delay:.1f}s)")
time.sleep(delay)
activities = {
"mumbai": [
{"name": "Gateway of India sunset walk", "area": "Colaba", "tags": ["couple", "solo"]},
{"name": "Dharavi slum tour", "area": "Dharavi", "tags": ["solo", "family"]},
{"name": "Juhu Beach cricket & snacks", "area": "Juhu", "tags": ["family", "solo"]},
{"name": "Bollywood studio tour", "area": "Goregaon", "tags": ["family", "couple"]},
{"name": "Elephanta Caves ferry trip", "area": "Harbour", "tags": ["family", "couple", "solo"]},
{"name": "Bandra street food walk", "area": "Bandra", "tags": ["couple", "solo"]},
{"name": "Marine Drive evening stroll", "area": "Nariman Point", "tags": ["couple", "solo"]},
],
"amsterdam": [
{"name": "Canal boat tour", "area": "City Centre", "tags": ["couple", "family"]},
{"name": "Rijksmuseum visit", "area": "Museum Quarter","tags": ["family", "solo", "couple"]},
{"name": "Vondelpark picnic", "area": "Museum Quarter","tags": ["couple", "family"]},
{"name": "Anne Frank House", "area": "Jordaan", "tags": ["solo", "family"]},
{"name": "Heineken Experience", "area": "De Pijp", "tags": ["couple", "solo"]},
{"name": "Cycling through Jordaan", "area": "Jordaan", "tags": ["couple", "solo"]},
{"name": "NEMO Science Museum", "area": "Waterfront", "tags": ["family"]},
],
}
city_activities = activities.get(city.lower())
if not city_activities:
return f"No activity data for {city}."
if tag:
city_activities = [a for a in city_activities if tag.lower() in a["tags"]]
if not city_activities:
return f"No activities tagged '{tag}' found in {city}."
label = f"Activities in {city.title()}" + (f" (tag: {tag})" if tag else "")
lines = [label + ":"]
for a in city_activities:
lines.append(f" • {a['name']} — {a['area']} [{', '.join(a['tags'])}]")
return "\n".join(lines)
def get_flights(origin: str, destination: str) -> str:
delay = random.uniform(8, 25)
print(f" ⏳ [get_flights] Searching flights {origin}→{destination}... ({delay:.1f}s)")
time.sleep(delay)
flights = {
("tokyo", "mumbai"): [
{"airline": "Air India", "duration": "10h 05m", "price": "$420", "stops": "Nonstop"},
{"airline": "IndiGo", "duration": "11h 20m", "price": "$310", "stops": "1 stop (Delhi)"},
{"airline": "JAL", "duration": "10h 15m", "price": "$530", "stops": "Nonstop"},
],
("tokyo", "amsterdam"): [
{"airline": "KLM", "duration": "12h 40m", "price": "$680", "stops": "Nonstop"},
{"airline": "Lufthansa", "duration": "15h 10m", "price": "$510", "stops": "1 stop (Frankfurt)"},
{"airline": "ANA", "duration": "13h 00m", "price": "$720", "stops": "Nonstop"},
],
}
key = (origin.lower(), destination.lower())
options = flights.get(key)
if not options:
return f"No flight data for {origin} → {destination}."
lines = [f"Flights from {origin.title()} to {destination.title()}:"]
for f in options:
lines.append(f" • {f['airline']} — {f['duration']} — {f['price']} — {f['stops']}")
return "\n".join(lines)
# ---------------------------------------------------------------------------
# Registry — maps tool name → callable(args_dict)
# ---------------------------------------------------------------------------
# Tools in this set run async (PENDING). Everything else runs synchronously.
SLOW_TOOLS = {"get_hotels", "get_activities", "get_flights"}
TOOL_FUNCTIONS = {
"get_weather": lambda args: get_weather(args["city"]),
"get_hotels": lambda args: get_hotels(args["city"]),
"get_activities": lambda args: get_activities(args["city"], args.get("tag")),
"get_flights": lambda args: get_flights(args["origin"], args["destination"]),
}
# ---------------------------------------------------------------------------
# Schema — sent to OpenAI so it knows what tools exist
# ---------------------------------------------------------------------------
TOOLS_SCHEMA = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
},
{
"type": "function",
"function": {
"name": "get_hotels",
"description": "Get a list of hotels with area and cost per night. Supports: mumbai, amsterdam.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string", "description": "mumbai or amsterdam"}},
"required": ["city"],
},
},
},
{
"type": "function",
"function": {
"name": "get_activities",
"description": "Get activities for a city, optionally filtered by tag (couple, family, solo). Supports: mumbai, amsterdam.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "mumbai or amsterdam"},
"tag": {"type": "string", "enum": ["couple", "family", "solo"], "description": "Optional filter"},
},
"required": ["city"],
},
},
},
{
"type": "function",
"function": {
"name": "get_flights",
"description": "Get flight options. Supports routes: tokyo→mumbai, tokyo→amsterdam.",
"parameters": {
"type": "object",
"properties": {
"origin": {"type": "string", "description": "Departure city, e.g. tokyo"},
"destination": {"type": "string", "description": "Arrival city, e.g. mumbai or amsterdam"},
},
"required": ["origin", "destination"],
},
},
},
{
"type": "function",
"function": {
"name": "await_job",
"description": (
"Call this immediately after starting a slow tool (get_hotels, get_activities, get_flights) "
"if you already know the next step. Pass the exact job_id from that tool's response and "
"describe what to do with the result. "
"Example: after firing get_flights(origin='tokyo', destination='amsterdam'), call "
"await_job(job_id='<id>', followup_hint='call get_hotels(city=amsterdam) for 2 nights'). "
"Do NOT call the follow-up tool now with guessed args — await_job ensures it runs with "
"real data. If the job has already completed (you see '(System) Job X completed' in the "
"conversation), call the follow-up tool directly instead of using await_job."
),
"parameters": {
"type": "object",
"properties": {
"job_id": {
"type": "string",
"description": "The job_id from the still-running background job to wait for.",
},
"followup_hint": {
"type": "string",
"description": (
"Natural language: what tool to call and how to map the result to its args. "
"Example: 'call get_activities with destination city and tag=couple'. "
"If this is an intermediate step in a chain, also note the step after it."
),
},
},
"required": ["job_id", "followup_hint"],
},
},
},
]