First_agent_template / tools /calculate_tool.py
tanmaydesai07's picture
fully functional
72e59f2
Raw
History Blame Contribute Delete
623 Bytes
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)}"