File size: 659 Bytes
1bbca12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import ast
from typing import Dict

class CalculatorTool:
    def __init__(self):
        self.name = "calculator"
        self.description = "Evaluates mathematical expressions."
        self.inputs = {
            "expression": {"type": "string", "description": "Mathematical expression to evaluate"}
        }
        self.output_type = str

    async def aparse(self, expression: str) -> str:
        try:
            result = eval(expression, {"__builtins__": {}}, {"abs": abs, "round": round})
            return str(result)
        except Exception as e:
            return f"Error calculating expression: {str(e)}"

calculator_tool = CalculatorTool()