Spaces:
Sleeping
Sleeping
import datetime | |
import math | |
import pytz | |
from langchain_community.tools import DuckDuckGoSearchRun | |
from langchain_core.tools import 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 | |
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 | |
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 | |
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 | |
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 | |
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 | |
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) | |
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 | |
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) | |
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) | |
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) | |
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) | |
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}" | |
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)}" | |