Skip to content

Implement ToolRegistry #1635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/smolagents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
Monitor,
)
from .remote_executors import DockerExecutor, E2BExecutor, WasmExecutor
from .tools import BaseTool, Tool, validate_tool_arguments
from .tools import BaseTool, Tool, ToolRegistry, validate_tool_arguments
from .utils import (
AgentError,
AgentExecutionError,
Expand Down Expand Up @@ -361,7 +361,7 @@ def _setup_tools(self, tools, add_base_tools):
assert all(isinstance(tool, BaseTool) for tool in tools), (
"All elements must be instance of BaseTool (or a subclass)"
)
self.tools = {tool.name: tool for tool in tools}
self.tools = ToolRegistry(tools)
if add_base_tools:
self.tools.update(
{
Expand Down
15 changes: 15 additions & 0 deletions src/smolagents/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,21 @@ def __call__(self, *args, **kwargs) -> Any:
pass


class ToolRegistry(dict):
"""Registry for tools that provides dict-like access."""

def __init__(self, tools: list[BaseTool]):
super().__init__({tool.name: tool for tool in tools})

def __setitem__(self, key: str, value: BaseTool):
"""Add or update a tool."""
super().__setitem__(key, value)

def __repr__(self) -> str:
"""String representation of the registry."""
return f"ToolRegistry({list(self.keys())})"


class Tool(BaseTool):
"""
A base class for the functions used by the agent. Subclass this and implement the `forward` method as well as the
Expand Down