1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
from personal_agent.tools.base import Tool, ToolError, ToolResult
class CalculatorTool(Tool):
@property
def name(self) -> str:
return "calculator"
@property
def description(self) -> str:
return "Perform basic arithemtic"
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"a": {
"type": "number",
"description": "The first number.",
},
"b": {
"type": "number",
"description": "The second number.",
},
"operation": {
"type": "string",
"enum": ["add", "subtract", "multiply", "divide"],
"description": "The arithemtic operation to perform",
},
},
"required": ["a", "b", "operation"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
a = arguments["a"]
b = arguments["b"]
operation = arguments["operation"]
if not isinstance(a, (int, float)):
raise ToolError("a must be a number")
if not isinstance(b, (int, float)):
raise ToolError("b must be a number")
if not isinstance(operation, str):
raise ToolError("operation must be a string")
if operation == "add":
result = a + b
elif operation == "subtract":
result = a - b
elif operation == "multiply":
result = a * b
elif operation == "divide":
if b == 0:
raise ToolError("cannot divide by zero")
result = a / b
else:
raise ToolError(f"unknown operation: {operation}")
return ToolResult(content=str(result))
|