-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_tools.py
More file actions
382 lines (324 loc) · 11.5 KB
/
example_tools.py
File metadata and controls
382 lines (324 loc) · 11.5 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
"""
Example tools that both agents can use.
These tools demonstrate various scenarios for benchmarking.
"""
import json
from typing import Dict, List, Any, Callable
# Tool implementations
def get_weather(location: str, unit: str = "celsius") -> str:
"""Get the current weather for a location."""
# Simulated weather data
weather_data = {
"san francisco": {"temp": 18, "condition": "Partly cloudy"},
"new york": {"temp": 22, "condition": "Sunny"},
"london": {"temp": 12, "condition": "Rainy"},
"tokyo": {"temp": 25, "condition": "Clear"},
}
location_lower = location.lower()
if location_lower not in weather_data:
return f"Weather data not available for {location}"
data = weather_data[location_lower]
temp = data["temp"]
if unit.lower() == "fahrenheit":
temp = (temp * 9/5) + 32
return json.dumps({
"location": location,
"temperature": temp,
"unit": unit,
"condition": data["condition"]
})
def calculate(expression: str) -> str:
"""Safely evaluate a mathematical expression."""
try:
# Simple safe evaluation (in production, use a proper parser)
allowed_chars = set('0123456789+-*/.() ')
if not all(c in allowed_chars for c in expression):
return json.dumps({"error": "Invalid characters in expression"})
result = eval(expression, {"__builtins__": {}}, {})
return json.dumps({"expression": expression, "result": result})
except Exception as e:
return json.dumps({"error": str(e)})
def search_database(query: str, category: str = "all") -> str:
"""Search a simulated database."""
database = {
"products": [
{"id": 1, "name": "Laptop", "price": 999},
{"id": 2, "name": "Mouse", "price": 25},
{"id": 3, "name": "Keyboard", "price": 75},
],
"users": [
{"id": 1, "name": "Alice", "email": "alice@example.com"},
{"id": 2, "name": "Bob", "email": "bob@example.com"},
]
}
query_lower = query.lower()
results = []
categories = [category] if category != "all" else database.keys()
for cat in categories:
if cat in database:
for item in database[cat]:
if any(query_lower in str(v).lower() for v in item.values()):
results.append({**item, "category": cat})
return json.dumps({"query": query, "results": results})
def send_email(to: str, subject: str, body: str) -> str:
"""Simulate sending an email."""
return json.dumps({
"status": "sent",
"to": to,
"subject": subject,
"message": "Email sent successfully (simulated)"
})
# Tool registry
TOOLS: Dict[str, Callable] = {
"get_weather": get_weather,
"calculate": calculate,
"search_database": search_database,
"send_email": send_email,
}
def get_tools() -> Dict[str, Callable]:
"""Return the dictionary of available tools."""
return TOOLS
def get_tool_schemas() -> List[Dict[str, Any]]:
"""
Return tool schemas in the format expected by LLM APIs.
This is for the regular agent using function calling.
"""
return [
{
"name": "get_weather",
"description": "Get the current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit",
"default": "celsius"
}
},
"required": ["location"]
}
},
{
"name": "calculate",
"description": "Safely evaluate a mathematical expression",
"input_schema": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": "The mathematical expression to evaluate"
}
},
"required": ["expression"]
}
},
{
"name": "search_database",
"description": "Search a simulated database",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
},
"category": {
"type": "string",
"enum": ["all", "products", "users"],
"description": "Category to search in",
"default": "all"
}
},
"required": ["query"]
}
},
{
"name": "send_email",
"description": "Simulate sending an email",
"input_schema": {
"type": "object",
"properties": {
"to": {
"type": "string",
"description": "Recipient email address"
},
"subject": {
"type": "string",
"description": "Email subject"
},
"body": {
"type": "string",
"description": "Email body content"
}
},
"required": ["to", "subject", "body"]
}
}
]
def get_code_mode_api() -> str:
"""
Return Python API definitions for Code Mode.
This is the interface that the LLM will write code against.
"""
return '''
from typing import TypedDict, List, Literal, Dict
# ============================================================================
# Response Type Definitions
# ============================================================================
# These TypedDicts show the exact structure of JSON responses from each tool.
# All tools return JSON strings that must be parsed with json.loads().
class WeatherResponse(TypedDict):
"""Response from get_weather"""
location: str
temperature: float # In the requested unit
unit: str # "celsius" or "fahrenheit"
condition: str # Weather condition description
class CalculateSuccessResponse(TypedDict):
"""Successful response from calculate"""
expression: str
result: float
class CalculateErrorResponse(TypedDict):
"""Error response from calculate"""
error: str
class DatabaseItemDict(TypedDict):
"""Structure of a database item (product or user)"""
id: int
category: str # "products" or "users"
# Additional fields vary by category:
# Products: name (str), price (int)
# Users: name (str), email (str)
class SearchResponse(TypedDict):
"""Response from search_database"""
query: str
results: List[Dict] # List of items matching the query, structure varies
class EmailResponse(TypedDict):
"""Response from send_email"""
status: Literal["sent"]
to: str
subject: str
message: str
# ============================================================================
# Tools API
# ============================================================================
class Tools:
"""Available tools for the agent to use."""
def get_weather(
self,
location: str,
unit: Literal["celsius", "fahrenheit"] = "celsius"
) -> str:
"""
Get the current weather for a location.
Args:
location: The city name (e.g., "San Francisco", "New York", "London", "Tokyo")
unit: Temperature unit ('celsius' or 'fahrenheit')
Returns:
JSON string that parses to WeatherResponse.
Example parsed structure:
{
"location": "San Francisco",
"temperature": 18.0,
"unit": "celsius",
"condition": "Partly cloudy"
}
Or error message string if location not found:
"Weather data not available for {location}"
Usage:
result_str = tools.get_weather("San Francisco", "celsius")
result: WeatherResponse = json.loads(result_str)
temp: float = result["temperature"]
condition: str = result["condition"]
"""
pass
def calculate(self, expression: str) -> str:
"""
Safely evaluate a mathematical expression.
Args:
expression: The mathematical expression to evaluate (supports +, -, *, /, parentheses)
Returns:
JSON string that parses to CalculateSuccessResponse or CalculateErrorResponse.
Success example:
{
"expression": "10 + 5 * 2",
"result": 20.0
}
Error example:
{
"error": "Invalid characters in expression"
}
Usage:
result_str = tools.calculate("10 + 5 * 2")
result = json.loads(result_str)
if "error" in result:
print(f"Error: {result['error']}")
else:
answer: float = result["result"]
print(f"Result: {answer}")
"""
pass
def search_database(
self,
query: str,
category: Literal["all", "products", "users"] = "all"
) -> str:
"""
Search a simulated database.
Args:
query: The search query (case-insensitive, searches all fields)
category: Category to search in ('all', 'products', or 'users')
Returns:
JSON string that parses to SearchResponse.
Example parsed structure:
{
"query": "laptop",
"results": [
{
"id": 1,
"name": "Laptop",
"price": 999,
"category": "products"
}
]
}
Product items have: id, name, price, category
User items have: id, name, email, category
Usage:
result_str = tools.search_database("laptop", "products")
result: SearchResponse = json.loads(result_str)
results: List[Dict] = result["results"]
for item in results:
if item["category"] == "products":
print(f"Product: {item['name']} - ${item['price']}")
"""
pass
def send_email(self, to: str, subject: str, body: str) -> str:
"""
Simulate sending an email.
Args:
to: Recipient email address
subject: Email subject
body: Email body content
Returns:
JSON string that parses to EmailResponse.
Example parsed structure:
{
"status": "sent",
"to": "recipient@example.com",
"subject": "Hello",
"message": "Email sent successfully (simulated)"
}
Usage:
result_str = tools.send_email("user@example.com", "Hello", "Message body")
result: EmailResponse = json.loads(result_str)
print(result["message"])
"""
pass
# Available instance
tools = Tools()
'''