EtienneB
starting final assignment, added some tools
7da4369
raw
history blame
4.15 kB
import math
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)