Spaces:
Sleeping
Sleeping
import math | |
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) | |