You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
2.0 KiB

from typing import Callable, Dict, Any, List
class ToolRegistry:
"""
A registry for managing and accessing tools (functions).
This class provides methods to register functions as tools and retrieve them by name.
Attributes:
_tools (Dict[str, Callable]): A dictionary mapping tool names to their corresponding functions.
Methods:
register(func: Callable) -> Callable:
Registers a function as a tool. The function's name is used as the key in the registry.
get_tools(tools: List[str] = None) -> List[Callable]:
Retrieves a list of registered tools. If a list of tool names is provided, only the tools
with those names are returned. If no list is provided, all registered tools are returned.
"""
_tools: Dict[str, Callable] = {}
@classmethod
def register(cls, func: Callable):
"""
Registers a function as a tool in the class.
This method adds the given function to the class's `_tools` dictionary,
using the function's name as the key.
Args:
func (Callable): The function to be registered.
Returns:
Callable: The same function that was passed in, allowing for decorator usage.
"""
cls._tools[func.__name__] = func
return func
@classmethod
def get_tools(cls, tools: List[str] = None) -> List[Callable]:
"""
Retrieve a list of tool callables.
This method returns a list of tool callables based on the provided tool names.
If no tool names are provided, it returns all available tool callables.
Args:
tools (List[str], optional): A list of tool names to retrieve. Defaults to None.
Returns:
List[Callable]: A list of tool callables.
"""
print(tools)
if tools:
print(cls._tools)
return [cls._tools[name] for name in tools if name in cls._tools]
else:
return list(cls._tools.values())