Thanos's picture
Update tools
356e874
Raw History Blame Contribute Delete
4.23 kB
import cmath
from langchain_core.tools import tool
@tool("math-add-tool", parse_docstring=True)
def add(a: float | int, b: float | int) -> float | int:
"""Adds two numbers.
Args:
a (float | int): The first number.
b (float | int): The second number.
Returns:
float | int: A number representing the result of the addition
between a and b.
"""
return a + b
@tool("math-subtract-tool", parse_docstring=True)
def subtract(a: float | int, b: float | int) -> float | int:
"""Subtracts two numbers.
Args:
a (float | int): The first number (minuend).
b (float | int): The second number (subtrahend).
Returns:
float | int: A number representing the result of substracting b from a.
"""
return a - b
@tool("math-multiply-tool", parse_docstring=True)
def multiply(a: float | int, b: float | int) -> float | int:
"""Multiplies two numbers.
Args:
a (float | int): The first number.
b (float | int): The second number.
Returns:
float | int: A number representing the result of the multiplication
between a and b.
"""
return a * b
@tool("math-divide-tool", parse_docstring=True)
def divide(a: float | int, b: float | int) -> float | int:
"""Divides two numbers.
This function divides `a` by `b`. If `b` is zero, then it raises
a ValueError.
Args:
a (float | int): The first number (dividend).
b (float | int): The second number (divisor).
Returns:
float | int: A number representing the result of dividing a by b.
"""
if b == 0:
raise ValueError("Cannot divided by zero.")
return a / b
@tool("math-modulus-tool", parse_docstring=True)
def modulus(a: int, b: int) -> int:
"""Calculates the modulus of two numbers.
This function calculates the expression `a` modulo `b`. If `b` is zero,
then it raises a ValueError.
Args:
a (int): The first number (dividend).
b (int): The second number (divisor).
Returns:
int: A number representing the reminder of a modulo b.
"""
if b == 0:
raise ValueError("Cannot divided by zero.")
return a % b
@tool("math-power-tool", parse_docstring=True)
def power(a: float | int, b: float | int) -> float | int:
"""Calculates the result of a to the power of b.
Args:
a (float | int): The first number.
b (float | int): The second number.
Returns:
float | int: A number representing the result of a to the power of b.
"""
return a**b
@tool("math-square-root-tool", parse_docstring=True)
def square_root(a: float | int) -> float | complex:
"""Calculates the square root of a number.
Args:
a (float | int): The number of which to get the square root.
Returns:
float | complex: A number representing the result of the square root
of a.
"""
if a >= 0:
return a**0.5
return cmath.sqrt(a)
@tool("math-is-greater-tool", parse_docstring=True)
def is_greater(a: float | int, b: float | int) -> bool:
"""Checks if a > b.
Args:
a (float | int): The number on the left of the greater (>) operator.
b (float | int): The number on the right of the greater (>) operator.
Returns:
bool: A boolean indicating whether a is greater than b.
"""
return a > b
@tool("math-is-less-tool", parse_docstring=True)
def is_less(a: float | int, b: float | int) -> bool:
"""Checks if a < b.
Args:
a (float | int): The number on the left of the less (<) operator.
b (float | int): The number on the right of the less (<) operator.
Returns:
bool: A boolean indicating whether a is less than b.
"""
return a < b
@tool("math-is-equal-tool", parse_docstring=True)
def is_equal(a: float | int, b: float | int) -> bool:
"""Checks if a == b.
Args:
a (float | int): The number on the left of the equal (==) operator.
b (float | int): The number on the right of the equal (==) operator.
Returns:
bool: A boolean indicating whether a is equal to b.
"""
return a == b