File size: 623 Bytes
72e59f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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)}"