import math from typing import Any, Dict from smolagents import tool @tool def perform_calculation(expression: str) -> Dict[str, Any]: """ Safely evaluate a mathematical expression. Args: expression: Mathematical expression to evaluate Returns: Dictionary containing the result or error message """ try: # Define allowed names allowed_names = { "abs": abs, "round": round, "min": min, "max": max, "sum": sum, "len": len, "pow": pow, "math": math, } # Clean the expression cleaned_expr = expression.strip() # Evaluate using safer methods (this is still a simplified example) # In a real implementation, use a proper math expression parser result = eval(cleaned_expr, {"__builtins__": {}}, allowed_names) return {"result": result} except Exception as e: return {"error": str(e)}