| import os |
| import requests |
| import cmath |
| import pytesseract |
| from PIL import Image |
| from typing import Optional |
| from smolagents import tool, FinalAnswerTool, DuckDuckGoSearchTool |
|
|
|
|
| DuckDuckGoSearchTool = DuckDuckGoSearchTool() |
| FinalAnswer = FinalAnswerTool() |
|
|
| |
| @tool |
| def multiply(a: float, b: float) -> float: |
| """ |
| Multiplies two numbers. |
| Args: |
| a (float): the first number |
| b (float): the second number |
| """ |
| return a * b |
|
|
|
|
| @tool |
| def add(a: float, b: float) -> float: |
| """ |
| Adds two numbers. |
| Args: |
| a (float): the first number |
| b (float): the second number |
| """ |
| return a + b |
|
|
|
|
| @tool |
| def subtract(a: float, b: float) -> int: |
| """ |
| Subtracts two numbers. |
| Args: |
| a (float): the first number |
| b (float): the second number |
| """ |
| return a - b |
|
|
|
|
| @tool |
| def divide(a: float, b: float) -> float: |
| """ |
| Divides two numbers. |
| Args: |
| a (float): the first float number |
| b (float): the second float number |
| """ |
| if b == 0: |
| raise ValueError("Cannot divided by zero.") |
| return a / b |
|
|
| @tool |
| def modulus(a: int, b: int) -> int: |
| """ |
| Get the modulus of two numbers. |
| Args: |
| a (int): the first number |
| b (int): the second number |
| """ |
| return a % b |
|
|
|
|
| @tool |
| def power(a: float, b: float) -> float: |
| """ |
| Get the power of two numbers. |
| Args: |
| a (float): the first number |
| b (float): the second number |
| """ |
| return a**b |
|
|
|
|
| @tool |
| def square_root(a: float) -> float | complex: |
| """ |
| Get the square root of a number. |
| Args: |
| a (float): the number to get the square root of |
| """ |
| if a >= 0: |
| return a**0.5 |
| return cmath.sqrt(a) |
|
|
|
|
| |
|
|
| @tool |
| def download_file_from_url(url: str, filename: Optional[str] = None) -> str: |
| """ |
| Download a file from a URL and save it to a temporary location. |
| Args: |
| url (str): the URL of the file to download. |
| filename (str, optional): the name of the file. If not provided, a random name file will be created. |
| """ |
| try: |
| |
| if not filename: |
| path = urlparse(url).path |
| filename = os.path.basename(path) |
| if not filename: |
| filename = f"downloaded_{uuid.uuid4().hex[:8]}" |
|
|
| |
| temp_dir = tempfile.gettempdir() |
| filepath = os.path.join(temp_dir, filename) |
|
|
| |
| response = requests.get(url, stream=True) |
| response.raise_for_status() |
|
|
| |
| with open(filepath, "wb") as f: |
| for chunk in response.iter_content(chunk_size=8192): |
| f.write(chunk) |
|
|
| return f"File downloaded to {filepath}. You can read this file to process its contents." |
| except Exception as e: |
| return f"Error downloading file: {str(e)}" |
|
|
|
|
| all_tools = [DuckDuckGoSearchTool, FinalAnswer, |
| multiply, add, subtract, divide, modulus, power, square_root, |
| download_file_from_url] |
|
|