| from smolagents.tools import Tool | |
| class CalculateTool(Tool): | |
| name = "calculate" | |
| description = "Evaluates a mathematical expression safely." | |
| inputs = { | |
| "expression": { | |
| "type": "string", | |
| "description": "A mathematical expression (e.g., '2 * 3 + 5')", | |
| } | |
| } | |
| output_type = "string" | |
| def forward(self, expression: str) -> str: | |
| try: | |
| result = eval(expression, {"__builtins__": {}}, {}) | |
| return f"The result of '{expression}' is: {result}" | |
| except Exception as e: | |
| return f"Error calculating '{expression}': {str(e)}" | |