added basic math functions as tools
Browse files
agent.py
CHANGED
@@ -1,13 +1,137 @@
|
|
|
|
|
|
1 |
|
|
|
2 |
|
|
|
3 |
|
|
|
|
|
|
|
4 |
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
|
|
6 |
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
8 |
|
9 |
-
|
|
|
|
|
|
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.tools import Tool
|
2 |
+
import math
|
3 |
|
4 |
+
## ----- TOOLS DEFINITION ----- ##
|
5 |
|
6 |
+
# ** Math Tools ** #
|
7 |
|
8 |
+
def add_numbers(a: float, b: float) -> float:
|
9 |
+
"""
|
10 |
+
Add two floating-point numbers.
|
11 |
|
12 |
+
Args:
|
13 |
+
a (float): The first number.
|
14 |
+
b (float): The second number.
|
15 |
|
16 |
+
Returns:
|
17 |
+
float: The result of the addition.
|
18 |
+
"""
|
19 |
+
return a + b
|
20 |
|
21 |
+
def subtract_numbers(a: float, b: float) -> float:
|
22 |
+
"""
|
23 |
+
Subtract the second floating-point number from the first.
|
24 |
|
25 |
+
Args:
|
26 |
+
a (float): The first number.
|
27 |
+
b (float): The second number.
|
28 |
|
29 |
+
Returns:
|
30 |
+
float: The result of the subtraction.
|
31 |
+
"""
|
32 |
+
return a - b
|
33 |
|
34 |
+
def multiply_numbers(a: float, b: float) -> float:
|
35 |
+
"""
|
36 |
+
Multiply two floating-point numbers.
|
37 |
+
|
38 |
+
Args:
|
39 |
+
a (float): The first number.
|
40 |
+
b (float): The second number.
|
41 |
+
|
42 |
+
Returns:
|
43 |
+
float: The result of the multiplication.
|
44 |
+
"""
|
45 |
+
return a * b
|
46 |
+
|
47 |
+
def divide_numbers(a: float, b: float) -> float:
|
48 |
+
"""
|
49 |
+
Divide the first floating-point number by the second.
|
50 |
+
|
51 |
+
Args:
|
52 |
+
a (float): The numerator.
|
53 |
+
b (float): The denominator.
|
54 |
+
|
55 |
+
Returns:
|
56 |
+
float: The result of the division.
|
57 |
+
|
58 |
+
Raises:
|
59 |
+
ValueError: If division by zero is attempted.
|
60 |
+
"""
|
61 |
+
if b == 0:
|
62 |
+
raise ValueError("Division by zero")
|
63 |
+
return a / b
|
64 |
+
|
65 |
+
def power(a: float, b: float) -> float:
|
66 |
+
"""
|
67 |
+
Raise the first number to the power of the second.
|
68 |
+
|
69 |
+
Args:
|
70 |
+
a (float): The base.
|
71 |
+
b (float): The exponent.
|
72 |
+
|
73 |
+
Returns:
|
74 |
+
float: The result of the exponentiation.
|
75 |
+
"""
|
76 |
+
return a ** b
|
77 |
+
|
78 |
+
def modulus(a: float, b: float) -> float:
|
79 |
+
"""
|
80 |
+
Compute the modulus (remainder) of the division of a by b.
|
81 |
+
|
82 |
+
Args:
|
83 |
+
a (float): The dividend.
|
84 |
+
b (float): The divisor.
|
85 |
+
|
86 |
+
Returns:
|
87 |
+
float: The remainder after division.
|
88 |
+
"""
|
89 |
+
return a % b
|
90 |
+
|
91 |
+
def square_root(a: float) -> float:
|
92 |
+
"""
|
93 |
+
Compute the square root of a number.
|
94 |
+
|
95 |
+
Args:
|
96 |
+
a (float): The number.
|
97 |
+
|
98 |
+
Returns:
|
99 |
+
float: The square root.
|
100 |
+
|
101 |
+
Raises:
|
102 |
+
ValueError: If a is negative.
|
103 |
+
"""
|
104 |
+
if a < 0:
|
105 |
+
raise ValueError("Cannot compute square root of a negative number")
|
106 |
+
return math.sqrt(a)
|
107 |
+
|
108 |
+
def logarithm(a: float, base: float = math.e) -> float:
|
109 |
+
"""
|
110 |
+
Compute the logarithm of a number with a specified base.
|
111 |
+
|
112 |
+
Args:
|
113 |
+
a (float): The number.
|
114 |
+
base (float, optional): The logarithmic base (default is natural log).
|
115 |
+
|
116 |
+
Returns:
|
117 |
+
float: The logarithm.
|
118 |
+
|
119 |
+
Raises:
|
120 |
+
ValueError: If a or base is not positive.
|
121 |
+
"""
|
122 |
+
if a <= 0 or base <= 0:
|
123 |
+
raise ValueError("Logarithm arguments must be positive")
|
124 |
+
return math.log(a, base)
|
125 |
+
|
126 |
+
## ----- TOOLS LIST ----- ##
|
127 |
|
128 |
+
tools = [
|
129 |
+
add_numbers,
|
130 |
+
subtract_numbers,
|
131 |
+
multiply_numbers,
|
132 |
+
divide_numbers,
|
133 |
+
power,
|
134 |
+
modulus,
|
135 |
+
square_root,
|
136 |
+
logarithm
|
137 |
+
]
|