Spaces:
Configuration error
Configuration error
| import logging | |
| from langchain_core.tools import StructuredTool | |
| from pydantic import BaseModel, Field | |
| logger = logging.getLogger(__name__) | |
| class CalculatorInput(BaseModel): | |
| expression: str = Field(description="Mathematical expression to evaluate") | |
| async def calculator_func(expression: str) -> str: | |
| """ | |
| Evaluate a mathematical expression and return the result as a string. | |
| Args: | |
| expression (str): Mathematical expression (e.g., '2 + 2'). | |
| Returns: | |
| str: Result of the expression. | |
| """ | |
| try: | |
| logger.info(f"Evaluating expression: {expression}") | |
| result = eval(expression, {"__builtins__": {}}, {}) # Safe eval | |
| if isinstance(result, float): | |
| return f"{result:.2f}" if "USD" in expression else str(result) | |
| return str(result) | |
| except Exception as e: | |
| logger.error(f"Calculator error: {e}") | |
| return f"Error: {e}" | |
| calculator_tool = StructuredTool.from_function( | |
| func=calculator_func, | |
| name="calculator_tool", | |
| args_schema=CalculatorInput, | |
| coroutine=calculator_func | |
| ) |