-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtools.py
41 lines (35 loc) · 1.59 KB
/
tools.py
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
from typing import Dict, Any, List, Optional
class Tool:
def __init__(self, name: str, description: str, function: callable, required_params: Optional[List[str]] = None):
self.name = name
self.description = description
self.function = function
self.required_params = required_params or []
def execute(self, **kwargs):
"""Execute tool function with parameter validation"""
try:
filtered_kwargs = {k: v for k, v in kwargs.items() if k in self.required_params}
return self.function(**filtered_kwargs)
except Exception as e:
print(f"Tool execution error: {str(e)}")
raise
class ToolRegistry:
def __init__(self):
self.tools: Dict[str, Tool] = {}
def register(self, name: str, description: str, function: callable, required_params: Optional[List[str]] = None):
"""Register a new tool with required parameters"""
self.tools[name] = Tool(name, description, function, required_params)
def get_tool(self, name: str) -> Optional[Tool]:
"""Get a tool by name"""
return self.tools.get(name)
def get_tool_descriptions(self) -> str:
"""Get formatted descriptions of all tools"""
descriptions = []
for tool in self.tools.values():
params = ", ".join(tool.required_params) if tool.required_params else "none"
descriptions.append(
f"Tool: {tool.name}\n"
f"Description: {tool.description}\n"
f"Required Parameters: {params}\n"
)
return "\n".join(descriptions)