Update calculator.py
Browse files- calculator.py +136 -0
calculator.py
CHANGED
|
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_core.tools import tool
|
| 2 |
+
import cmath
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def add(a: int, b: int) -> int:
|
| 7 |
+
"""
|
| 8 |
+
Adds two numbers.
|
| 9 |
+
Args:
|
| 10 |
+
a (integer): the first number
|
| 11 |
+
b (integer): the second number
|
| 12 |
+
"""
|
| 13 |
+
return a + b
|
| 14 |
+
|
| 15 |
+
@tool
|
| 16 |
+
def sub(a: int, b: int) -> int:
|
| 17 |
+
"""
|
| 18 |
+
Subracts two numbers.
|
| 19 |
+
Args:
|
| 20 |
+
a (integer): the first number
|
| 21 |
+
b (integer): the second number
|
| 22 |
+
"""
|
| 23 |
+
return a - b
|
| 24 |
+
|
| 25 |
+
@tool
|
| 26 |
+
def mul(a: int, b: int) -> int:
|
| 27 |
+
"""
|
| 28 |
+
multiplies two numbers.
|
| 29 |
+
Args:
|
| 30 |
+
a (integer): the first number
|
| 31 |
+
b (integer): the second number
|
| 32 |
+
"""
|
| 33 |
+
return a * b
|
| 34 |
+
|
| 35 |
+
@tool
|
| 36 |
+
def div(a: int, b: int) -> float:
|
| 37 |
+
"""
|
| 38 |
+
divides two numbers and gave float as a result
|
| 39 |
+
Args:
|
| 40 |
+
a (integer): the first number
|
| 41 |
+
b (integer): the second number
|
| 42 |
+
"""
|
| 43 |
+
return a / b
|
| 44 |
+
|
| 45 |
+
@tool
|
| 46 |
+
def floor_div(a: int, b: int) -> int:
|
| 47 |
+
"""
|
| 48 |
+
divides two numbers and gave integr as a result
|
| 49 |
+
Args:
|
| 50 |
+
a (integer): the first number
|
| 51 |
+
b (integer): the second number
|
| 52 |
+
"""
|
| 53 |
+
return a // b
|
| 54 |
+
|
| 55 |
+
@tool
|
| 56 |
+
def square(a: int) -> int:
|
| 57 |
+
"""
|
| 58 |
+
returns square of the number
|
| 59 |
+
Args:
|
| 60 |
+
a (integer): the number
|
| 61 |
+
"""
|
| 62 |
+
return a * a
|
| 63 |
+
|
| 64 |
+
@tool
|
| 65 |
+
def mod(a: int, b: int) -> int:
|
| 66 |
+
"""
|
| 67 |
+
Modulus of two numbers.
|
| 68 |
+
Args:
|
| 69 |
+
a (integer): the first number
|
| 70 |
+
b (integer): the second number
|
| 71 |
+
"""
|
| 72 |
+
return a % b
|
| 73 |
+
|
| 74 |
+
@tool
|
| 75 |
+
def pow(a: int, b: int) -> int:
|
| 76 |
+
"""
|
| 77 |
+
Get the power of two numbers
|
| 78 |
+
Args:
|
| 79 |
+
a (integer): the first number
|
| 80 |
+
b (integer): the second number
|
| 81 |
+
"""
|
| 82 |
+
return a ** b
|
| 83 |
+
|
| 84 |
+
@tool
|
| 85 |
+
def square_root(a: int):
|
| 86 |
+
"""
|
| 87 |
+
Square root of the number
|
| 88 |
+
Args:
|
| 89 |
+
a (integer): the number
|
| 90 |
+
"""
|
| 91 |
+
if a < 0:
|
| 92 |
+
return cmath.sqrt(a)
|
| 93 |
+
else:
|
| 94 |
+
return a ** 0.5
|
| 95 |
+
|
| 96 |
+
@tool
|
| 97 |
+
def absolute(a: int) -> int:
|
| 98 |
+
"""
|
| 99 |
+
returns absolute value of the number
|
| 100 |
+
Args:
|
| 101 |
+
a (integer): the number
|
| 102 |
+
"""
|
| 103 |
+
return a if a >= 0 else -a
|
| 104 |
+
|
| 105 |
+
@tool
|
| 106 |
+
def gcd(a: int, b: int) -> int:
|
| 107 |
+
"""
|
| 108 |
+
returns gcd of two numbers using recursion
|
| 109 |
+
Args:
|
| 110 |
+
a (integer): the first number
|
| 111 |
+
b (integer): the second number
|
| 112 |
+
"""
|
| 113 |
+
if b == 0:
|
| 114 |
+
return a
|
| 115 |
+
return gcd(b, a % b)
|
| 116 |
+
|
| 117 |
+
@tool
|
| 118 |
+
def lcm(a: int, b: int) -> int:
|
| 119 |
+
"""
|
| 120 |
+
returns lcm of two numbers
|
| 121 |
+
Args:
|
| 122 |
+
a (integer): the first number
|
| 123 |
+
b (integer): the second number
|
| 124 |
+
"""
|
| 125 |
+
return absolute(a * b) // gcd(a, b)
|
| 126 |
+
|
| 127 |
+
@tool
|
| 128 |
+
def factorial(a: int) -> int:
|
| 129 |
+
"""
|
| 130 |
+
returns factorial of a number
|
| 131 |
+
Args:
|
| 132 |
+
a (integer): the number
|
| 133 |
+
"""
|
| 134 |
+
if a <= 1:
|
| 135 |
+
return 1
|
| 136 |
+
return a * factorial(a - 1)
|