Spaces:
Sleeping
Sleeping
File size: 6,251 Bytes
4e8db54 7da4369 4e8db54 7da4369 ffa4ee4 7da4369 ffa4ee4 4e8db54 ffa4ee4 4e8db54 ffa4ee4 4e8db54 ffa4ee4 4e8db54 ffa4ee4 4e8db54 ffa4ee4 4e8db54 ffa4ee4 |
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 |
import datetime
import math
import pytz
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_core.tools import tool
@tool
def multiply(a: int, b:int) -> int:
"""Multiplies two integers and returns the product.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The product of the two input integers.
"""
return a * b
@tool
def add(a: int, b:int) -> int:
"""Adds two integers and returns the sum.
Args:
a (int): The first integer.
b (int): The second integer.
Returns:
int: The sum of the two input integers.
"""
return a + b
@tool
def power(a: float, b: float) -> float:
"""Raises a number to the power of another.
Args:
a (float): The base number.
b (float): The exponent.
Returns:
float: The result of raising `a` to the power of `b`.
"""
return a ** b
@tool
def subtract(a: float, b: float) -> float:
"""Subtracts the second number from the first.
Args:
a (float): The number from which to subtract.
b (float): The number to subtract.
Returns:
float: The result of `a` minus `b`.
"""
return a - b
@tool
def divide(a: float, b: float) -> float:
"""Divides one number by another.
Args:
a (float): The numerator.
b (float): The denominator.
Returns:
float: The result of `a` divided by `b`.
Raises:
ValueError: If `b` is zero.
"""
if b == 0:
raise ValueError("Divide by zero is not allowed")
return a / b
@tool
def modulus(a: int, b: int) -> int:
"""Returns the remainder of the division of two integers.
Args:
a (int): The dividend.
b (int): The divisor.
Returns:
int: The remainder when `a` is divided by `b`.
Raises:
ValueError: If `b` is zero.
"""
if b == 0:
raise ValueError("Modulus by zero is not allowed")
return a % b
@tool
def square_root(x: float) -> float:
"""Returns the square root of a number.
Args:
x (float): The input number. Must be non-negative.
Returns:
float: The square root of `x`.
Raises:
ValueError: If `x` is negative.
"""
if x < 0:
raise ValueError("Square root of negative number is not allowed")
return math.sqrt(x)
@tool
def floor_divide(a: int, b: int) -> int:
"""Performs integer division (floor division) of two numbers.
Args:
a (int): The dividend.
b (int): The divisor.
Returns:
int: The floor of the quotient.
Returns the quotient rounded down to the nearest integer.
Raises:
ValueError: If `b` is zero.
"""
if b == 0:
raise ValueError("Division by zero is not allowed")
return a // b
@tool
def absolute(x: float) -> float:
"""Returns the absolute value of a number.
Args:
x (float): The input number.
Returns:
float: The absolute value of `x`.
"""
return abs(x)
@tool
def logarithm(x: float, base: float = math.e) -> float:
"""Returns the logarithm of a number with a given base.
Args:
x (float): The number to take the logarithm of. Must be positive.
base (float): The logarithmic base. Must be positive and not equal to 1.
Returns:
float: The logarithm of `x` to the given base.
Raises:
ValueError: If `x <= 0` or `base <= 0` or `base == 1`.
"""
if x <= 0 or base <= 0 or base == 1:
raise ValueError("Invalid input for logarithm")
return math.log(x, base)
@tool
def exponential(x: float) -> float:
"""Returns e raised to the power of `x`.
Args:
x (float): The exponent.
Returns:
float: The value of e^x.
"""
return math.exp(x)
@tool
def web_search(query: str) -> str:
"""Performs a DuckDuckGo search for the given query and returns the results.
Args:
query (str): The search query.
Returns:
str: The top search results as a string.
"""
search_tool = DuckDuckGoSearchRun()
return search_tool.invoke(query)
@tool
def roman_calculator_converter(value1: int, value2: int, oper: str) -> str:
"""A tool that performs an operator on 2 numbers to calculate the result
Args:
value1: the first value
value2: the second value
oper: operator for the calculation, like "add", "subtract", "multiply", "divide"
"""
roman_numerals = {
1000: "M", 900: "CM", 500: "D", 400: "CD",
100: "C", 90: "XC", 50: "L", 40: "XL",
10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I"
}
roman_string = ""
if oper == "add":
result = value1 + value2
elif oper == "subtract":
result = value1 - value2 # Fixed: was value2 - value1
elif oper == "divide":
if value2 == 0:
return "Error: Division by zero is not allowed"
result = int(value1 / value2) # Convert to int for Roman numerals
elif oper == "multiply":
result = value1 * value2
else:
return "Unsupported operation. Please use 'add', 'subtract', 'multiply', or 'divide'."
# Handle negative results
if result <= 0:
return f"Error: Roman numerals cannot represent zero or negative numbers. Result was: {result}"
for value, numeral in roman_numerals.items():
while result >= value:
roman_string += numeral
result -= value
return f"The result of {oper} on the values {value1} and {value2} is the Roman numeral: {roman_string}"
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""A tool that fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'America/New_York').
"""
try:
# Create timezone object
tz = pytz.timezone(timezone)
# Get current time in that timezone
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The current local time in {timezone} is: {local_time}"
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|