-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
176 lines (146 loc) · 4.93 KB
/
tools.py
File metadata and controls
176 lines (146 loc) · 4.93 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
"""
What Are Tools?
Tools are methods (functions) provided to the model (or to an agent), making them available
for use when the agent needs to perform actions such as collecting data from the internet,
making calculations, retrieving emails from a mailbox, and more. Since the main goal is to
make agents increasingly autonomous, we cannot respond to every concern an agent may have.
Instead, we aim to equip the agent with tools they can use independently.
Let's explore how to build such tools and how to utilize tools that are integrated into external
Python libraries like Ollama and OpenAI.
"""
import httpx
import calendar
import datetime
import importlib
import traceback
from typing import Callable
registered_functions = {}
def register_function(func) -> Callable:
registered_functions[func.__name__] = func
return func
def run_callable(name: str, arguments: dict) -> Callable or dict:
try:
module = importlib.import_module("tools")
func = getattr(module, name, None)
func = registered_functions.get(name)
if func and callable(func):
return func(**arguments)
else:
return {"error": f"Function '{name}' is not callable or not found"}
except Exception as e:
traceback.print_exc()
return {"error": f"Error executing {name}: {str(e)}"}
# step 1: create a function & register it globally
@register_function
def weather(city: str) -> str:
"""
Use this tool to check updated weather for a given city.
Remember to replace diacritics with neutral consotants or vowels, e.g. Kraków -> Krakow You need to provide city name.
Arguments:
city (str): The city name.
Returns:
str: Response containing the weather data for the provided city, or in case of error: a str containint error message.
"""
base_url: str = f"http://wttr.in/{city}?format=j1"
response = httpx.Client(follow_redirects=True, default_encoding="utf-8").get(base_url)
response.raise_for_status()
if response.status_code == 200:
data = response.json()
temp = data["current_condition"][0]["temp_C"]
return f"The temperature in {city} is {temp} Celsius degree"
else:
return f"Could not retrieve weather data for {city}."
@register_function
def today_is() -> str:
"""
Use this tool to check today's time and date.
Remember to replace diacritics with neutral consotants or vowels, e.g. Kraków -> Krakow You need to provide city name.
Returns:
str: A string representing timestamp made of time and date in format 'YYYY-MM-DD HH:MM:SS'.
"""
return f"The time and date now is: {str(datetime.datetime.now().replace(microsecond=0))}"
@register_function
def day_of_week() -> str:
"""
Use to get name of today's day.
Arguments:
None
Returns: name of the day of the week for today.
"""
# d = datetime.date.today()
# weekday_index = calendar.weekday(d.year, d.month, d.day)
# weekday_names = list(calendar.day_name)
# return str(weekday_names[weekday_index].capitalize())
return 'monday'
@register_function
def add_numbers(a: int, b: int) -> int:
"""
Add the first and second numbers together
Arguments:
a (int): the first number to add
b (int): the second number to add
Returns (int): sum of a and b, if sum is less than 10, say "wow"
"""
c = a + b
# print(f"In the function call: Sum of {a} and {b} is {c}")
if c < 10:
return -1
else:
return c
# return a + b
# step 2: add tools schema so the agent is able to use it
today_is_tool = {
"type": "function",
"function": {
"name": "today_is",
"description": "Get today's date",
"parameters": {
"type": "object",
"properties": {},
},
},
}
weather_tool = {
"type": "function",
"function": {
"name": "weather",
"description": "Get current weather for a specific city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string", "description": "The city name"}},
"required": ["city"],
},
},
}
day_of_week_tool = {
"type": "function",
"function": {
"name": "day_of_week",
"description": "Get today's the day of the week",
"parameters": {
"type": "object",
"properties": {},
},
},
}
add_numbers_tool = {
"type": "function",
"function": {
"name": "add_numbers",
"description": "Add the first and second numbers together",
"parameters": {
"type": "object",
"properties": {
"a": {
"type": "int",
"description": "The 1st number to add",
},
"b": {
"type": "int",
"description": "The 2nd number to add",
}
},
"required": ["a", "b"],
},
},
}