ToolStorage
The ToolStorage
module provides a structured and efficient way to manage and utilize various tool functions. It is designed to store tool functions, manage settings, and ensure smooth registration and retrieval of tools. This module is particularly useful in applications that require dynamic management of a collection of functions, such as plugin systems, modular software, or any application where functions need to be registered and called dynamically.
Class: ToolStorage
The ToolStorage
class is the core component of the module. It provides functionalities to add, retrieve, and list tool functions as well as manage settings.
Attributes
verbose
bool
A flag to enable verbose logging.
tools
List[Callable]
A list of tool functions.
_tools
Dict[str, Callable]
A dictionary that stores the tools, where the key is the tool name and the value is the tool function.
_settings
Dict[str, Any]
A dictionary that stores the settings, where the key is the setting name and the value is the setting value.
Methods
__init__
Initializes the ToolStorage
instance.
verbose
bool
None
A flag to enable verbose logging.
tools
List[Callable]
None
A list of tool functions to initialize the storage with.
*args
tuple
None
Additional positional arguments.
**kwargs
dict
None
Additional keyword arguments.
add_tool
Adds a tool to the storage.
func
Callable
The tool function to be added.
Raises: - ValueError
: If a tool with the same name already exists.
get_tool
Retrieves a tool by its name.
name
str
The name of the tool to retrieve.
Returns: - Callable
: The tool function.
Raises: - ValueError
: If no tool with the given name is found.
set_setting
Sets a setting in the storage.
key
str
The key for the setting.
value
Any
The value for the setting.
get_setting
Gets a setting from the storage.
key
str
The key for the setting.
Returns: - Any
: The value of the setting.
Raises: - KeyError
: If the setting is not found.
list_tools
Lists all registered tools.
Returns: - List[str]
: A list of tool names.
Decorator: tool_registry
The tool_registry
decorator registers a function as a tool in the storage.
storage
ToolStorage
The storage instance to register the tool in.
Returns: - Callable
: The decorator function.
Usage Examples
Full Example
Basic Usage
Example 1: Initializing ToolStorage and Adding a Tool
Advanced Usage
Example 2: Managing Settings
Error Handling
Example 3: Handling Errors in Tool Retrieval
Example 4: Handling Duplicate Tool Addition
Last updated