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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
from abc import ABC, abstractmethod
from dataclasses import dataclass
@dataclass
class ToolResult:
content: str
class Tool(ABC):
@property
@abstractmethod
def name(self) -> str: ...
@property
@abstractmethod
def description(self) -> str: ...
@property
@abstractmethod
def schema(self) -> dict[str, object]: ...
@abstractmethod
def execute(self, arguments: dict[str, object]) -> ToolResult: ...
class ToolRegistry:
def __init__(self) -> None:
self.tools: dict[str, Tool] = {}
def register(self, tool: Tool) -> None:
self.tools[tool.name] = tool
def get(self, name: str) -> Tool | None:
return self.tools.get(name)
def all(self) -> list[Tool]:
return list(self.tools.values())
def schemas(self) -> list[dict[str, object]]:
return [
{
"type": "function",
"function": {
"name": tool.name,
"description": tool.description,
"parameters": tool.schema,
},
}
for tool in self.tools.values()
]
class HelloTool(Tool):
@property
def name(self) -> str:
return "hello"
@property
def description(self) -> str:
return "Say hello to someone."
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The person to greet.",
},
},
"required": ["name"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
name = arguments.get("name", "stranger")
return ToolResult(content=f"Hello, {name}!")
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 TypeError("a must be a number")
if not isinstance(b, (int, float)):
raise TypeError("b must be a number")
if not isinstance(operation, str):
raise TypeError("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 ValueError("cannot divide by zero")
result = a / b
else:
raise ValueError(f"unknown operation: {operation}")
return ToolResult(content=str(result))
if __name__ == "__main__":
registry = ToolRegistry()
registry.register(HelloTool())
print(registry.schemas())
tool = registry.get("hello")
if tool is not None:
result = tool.execute({"name": "Joseph"})
print(result.content)
|