Download tools/calculator.py from andrei-sferdian/Agents_Course_Final_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/tools/calculator.py
- Command line
-
hf download hf://spaces/andrei-sferdian/Agents_Course_Final_Assignment/tools/calculator.py
-
curl -L -o calculator.py https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/tools/calculator.py
3.35 kB
| from langchain.tools import tool | |
| 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) | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |