Andrei Sferdian
Add tools to load excel files and perform calculations
929e339
Raw History Blame Contribute Delete
3.35 kB
from langchain.tools import tool
@tool(parse_docstring=True)
def add(numbers: list[float]) -> float:
"""
Calculate the sum of a list of numbers.
Args:
numbers (list[float]): A list of floating-point numbers to be summed.
Returns:
float: The sum of the numbers in the list.
"""
return sum(numbers)
@tool(parse_docstring=True)
def subtract(numbers: list[float]) -> float:
"""
Subtracts a variable number of numbers in sequence.
Args:
numbers (list[float]): Numbers to be subtracted in sequence.
Returns:
float: The result of sequentially subtracting the numbers.
Raises:
ValueError: If less than two numbers are provided.
"""
if len(numbers) < 2:
raise ValueError("At least two numbers are required to perform subtraction.")
result = numbers[0]
for num in numbers[1:]:
result -= num
return result
@tool(parse_docstring=True)
def multiply(numbers: list[float]) -> float:
"""
Multiplies a variable number of numbers together and returns the result.
Args:
numbers (list[float]): Numbers to be multiplied.
Returns:
float: The product of all the numbers.
Raises:
ValueError: If no numbers are provided.
"""
if not numbers:
raise ValueError("At least one number is required to perform multiplication.")
result = 1
for num in numbers:
result *= num
return result
@tool(parse_docstring=True)
def divide(numbers: list[float]) -> float:
"""
Divides a sequence of numbers in order and returns the result.
Args:
numbers (list[float]): Numbers to be divided in sequence.
Returns:
float: The result of sequentially dividing the numbers.
Raises:
ValueError: If less than two numbers are provided or if division by zero occurs.
"""
if len(numbers) < 2:
raise ValueError("At least two numbers are required to perform division.")
result = numbers[0]
for num in numbers[1:]:
if num == 0:
raise ValueError("Cannot divide by zero.")
result /= num
return result
@tool(parse_docstring=True)
def modulus(a: float, b: float) -> float:
"""
Calculates the modulus (remainder) of the division of two numbers.
Args:
a (float): The dividend.
b (float): The divisor. Must not be zero.
Returns:
float: The remainder when `a` is divided by `b`.
Raises:
ZeroDivisionError: If `b` is zero.
"""
return a % b
@tool(parse_docstring=True)
def power(a: float, b: float) -> float:
"""
Calculates the result of raising a number to the power of another number.
Args:
a (float): The base number.
b (float): The exponent to which the base number is raised.
Returns:
float: The result of `a` raised to the power of `b`.
"""
return a**b
@tool(parse_docstring=True)
def floor_divide(a: int, b: int) -> int:
"""
Performs floor division between two numbers.
Args:
a (int): The dividend.
b (int): The divisor.
Returns:
int: The result of the floor division.
Raises:
ValueError: If the divisor (`b`) is zero.
"""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a // b