e10ai commited on
Commit
178d6c2
·
verified ·
1 Parent(s): 6a98175

Upload tools.py

Browse files
Files changed (1) hide show
  1. tools.py +246 -0
tools.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ from langchain_community.tools import DuckDuckGoSearchRun
4
+ from langchain_core.tools import tool
5
+
6
+
7
+ @tool
8
+ def multiply(a: int, b:int) -> int:
9
+ """Multiplies two integers and returns the product.
10
+
11
+ Args:
12
+ a (int): The first integer.
13
+ b (int): The second integer.
14
+
15
+ Returns:
16
+ int: The product of the two input integers.
17
+ """
18
+ return a * b
19
+
20
+
21
+ @tool
22
+ def add(a: int, b:int) -> int:
23
+ """Adds two integers and returns the sum.
24
+
25
+ Args:
26
+ a (int): The first integer.
27
+ b (int): The second integer.
28
+
29
+ Returns:
30
+ int: The sum of the two input integers.
31
+ """
32
+ return a + b
33
+
34
+
35
+ @tool
36
+ def power(a: float, b: float) -> float:
37
+ """Raises a number to the power of another.
38
+
39
+ Args:
40
+ a (float): The base number.
41
+ b (float): The exponent.
42
+
43
+ Returns:
44
+ float: The result of raising `a` to the power of `b`.
45
+ """
46
+ return a ** b
47
+
48
+
49
+ @tool
50
+ def subtract(a: float, b: float) -> float:
51
+ """Subtracts the second number from the first.
52
+
53
+ Args:
54
+ a (float): The number from which to subtract.
55
+ b (float): The number to subtract.
56
+
57
+ Returns:
58
+ float: The result of `a` minus `b`.
59
+ """
60
+ return a - b
61
+
62
+
63
+ @tool
64
+ def divide(a: float, b: float) -> float:
65
+ """Divides one number by another.
66
+
67
+ Args:
68
+ a (float): The numerator.
69
+ b (float): The denominator.
70
+
71
+ Returns:
72
+ float: The result of `a` divided by `b`.
73
+
74
+ Raises:
75
+ ValueError: If `b` is zero.
76
+ """
77
+ if b == 0:
78
+ raise ValueError("Divide by zero is not allowed")
79
+ return a / b
80
+
81
+
82
+ @tool
83
+ def modulus(a: int, b: int) -> int:
84
+ """Returns the remainder of the division of two integers.
85
+
86
+ Args:
87
+ a (int): The dividend.
88
+ b (int): The divisor.
89
+
90
+ Returns:
91
+ int: The remainder when `a` is divided by `b`.
92
+
93
+ Raises:
94
+ ValueError: If `b` is zero.
95
+ """
96
+ if b == 0:
97
+ raise ValueError("Modulus by zero is not allowed")
98
+ return a % b
99
+
100
+
101
+ @tool
102
+ def square_root(x: float) -> float:
103
+ """Returns the square root of a number.
104
+
105
+ Args:
106
+ x (float): The input number. Must be non-negative.
107
+
108
+ Returns:
109
+ float: The square root of `x`.
110
+
111
+ Raises:
112
+ ValueError: If `x` is negative.
113
+ """
114
+ if x < 0:
115
+ raise ValueError("Square root of negative number is not allowed")
116
+ return math.sqrt(x)
117
+
118
+
119
+ @tool
120
+ def floor_divide(a: int, b: int) -> int:
121
+ """Performs integer division (floor division) of two numbers.
122
+
123
+ Args:
124
+ a (int): The dividend.
125
+ b (int): The divisor.
126
+
127
+ Returns:
128
+ int: The floor of the quotient.
129
+ Returns the quotient rounded down to the nearest integer.
130
+
131
+ Raises:
132
+ ValueError: If `b` is zero.
133
+ """
134
+ if b == 0:
135
+ raise ValueError("Division by zero is not allowed")
136
+ return a // b
137
+
138
+
139
+ @tool
140
+ def absolute(x: float) -> float:
141
+ """Returns the absolute value of a number.
142
+
143
+ Args:
144
+ x (float): The input number.
145
+
146
+ Returns:
147
+ float: The absolute value of `x`.
148
+ """
149
+ return abs(x)
150
+
151
+
152
+ @tool
153
+ def logarithm(x: float, base: float = math.e) -> float:
154
+ """Returns the logarithm of a number with a given base.
155
+
156
+ Args:
157
+ x (float): The number to take the logarithm of. Must be positive.
158
+ base (float): The logarithmic base. Must be positive and not equal to 1.
159
+
160
+ Returns:
161
+ float: The logarithm of `x` to the given base.
162
+
163
+ Raises:
164
+ ValueError: If `x <= 0` or `base <= 0` or `base == 1`.
165
+ """
166
+ if x <= 0 or base <= 0 or base == 1:
167
+ raise ValueError("Invalid input for logarithm")
168
+ return math.log(x, base)
169
+
170
+
171
+ @tool
172
+ def exponential(x: float) -> float:
173
+ """Returns e raised to the power of `x`.
174
+
175
+ Args:
176
+ x (float): The exponent.
177
+
178
+ Returns:
179
+ float: The value of e^x.
180
+ """
181
+ return math.exp(x)
182
+
183
+
184
+ @tool
185
+ def web_search(query: str) -> str:
186
+ """Performs a DuckDuckGo search for the given query and returns the results.
187
+
188
+ Args:
189
+ query (str): The search query.
190
+
191
+ Returns:
192
+ str: The top search results as a string.
193
+ """
194
+ search_tool = DuckDuckGoSearchRun()
195
+ return search_tool.invoke(query)
196
+
197
+
198
+ @tool
199
+ def roman_calculator_converter(value1:int, value2:int, oper:str) -> str:
200
+ """A tool that performs an operator on 2 numbers to calculate the result
201
+ Args:
202
+ value1: the first value
203
+ value2: the second value
204
+ oper: operator for the calculation, like "add", "substract", "multiply", "divide"
205
+ """
206
+ roman_numerals = {
207
+ 1000: "M", 900: "CM", 500: "D", 400: "CD",
208
+ 100: "C", 90: "XC", 50: "L", 40: "XL",
209
+ 10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I"
210
+ }
211
+ roman_string = ""
212
+
213
+ if oper == "add":
214
+ result = value1 + value2
215
+ elif oper == "subtract":
216
+ result = value2 - value1
217
+ elif oper == "divide":
218
+ result = value1 / value2
219
+ elif oper == "multiply":
220
+ result = value1 * value2
221
+
222
+ else:
223
+ return "Unsupported operation. Please use 'add' or 'subtract'."
224
+
225
+ for value, numeral in roman_numerals.items():
226
+ while result >= value:
227
+ roman_string += numeral
228
+ result -= value
229
+ return f"The result of {oper} on the values {value1} and {value2} is the Roman numeral: {roman_string}"
230
+
231
+
232
+ @tool
233
+ def get_current_time_in_timezone(timezone: str) -> str:
234
+ """A tool that fetches the current local time in a specified timezone.
235
+ Args:
236
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
237
+ """
238
+ try:
239
+ # Create timezone object
240
+ tz = pytz.timezone(timezone)
241
+ # Get current time in that timezone
242
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
243
+ return f"The current local time in {timezone} is: {local_time}"
244
+ except Exception as e:
245
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
246
+