Skip to content

ToolRegistry

llmfy.llmfy_core.tools.tool_registry

ToolRegistry

TollRegistry class.

Use this ToolRegistry to using tools in LLMfyPipe.

Example:

# register tools
registry = ToolRegistry(['some_tool','other_tool'])

# execute
registry.execute_tool(name='some_tool', arguments:{"arg1": "value"})

Source code in llmfy/llmfy_core/tools/tool_registry.py
class ToolRegistry:
    """
    TollRegistry class.

    Use this ToolRegistry to using tools in `LLMfyPipe`.

    Example:
    ```python
    # register tools
    registry = ToolRegistry(['some_tool','other_tool'])

    # execute
    registry.execute_tool(name='some_tool', arguments:{"arg1": "value"})

    ```

    """

    def __init__(
        self,
        funcs: List[Callable],
        model: BaseAIModel,
    ):
        self._tools: Dict[str, Callable] = {}
        self._tool_definitions: Dict[str, Dict[str, Any]] = {}

        for func in funcs:
            if not hasattr(func, "_is_tool"):
                raise LLMfyException("Function must be decorated with @Tool")

            tool_def = Tool._get_tool_definition(func, model.provider)
            self._tools[func.__name__] = func
            self._tool_definitions[func.__name__] = tool_def
            # print(f"`{func.__name__}` registered ✅")

    def get_tool_definitions(self) -> List[Dict[str, Any]]:
        """Get all tool definitions registered with this framework."""
        return list(self._tool_definitions.values())

    def execute_tool(self, name: str, arguments: Dict[str, Any]) -> Any:
        """Execute a registered tool."""
        if name not in self._tools:
            raise LLMfyException(f"Tool not found: {name}")
        return self._tools[name](**arguments)

__init__(funcs, model)

Source code in llmfy/llmfy_core/tools/tool_registry.py
def __init__(
    self,
    funcs: List[Callable],
    model: BaseAIModel,
):
    self._tools: Dict[str, Callable] = {}
    self._tool_definitions: Dict[str, Dict[str, Any]] = {}

    for func in funcs:
        if not hasattr(func, "_is_tool"):
            raise LLMfyException("Function must be decorated with @Tool")

        tool_def = Tool._get_tool_definition(func, model.provider)
        self._tools[func.__name__] = func
        self._tool_definitions[func.__name__] = tool_def

get_tool_definitions()

Get all tool definitions registered with this framework.

Source code in llmfy/llmfy_core/tools/tool_registry.py
def get_tool_definitions(self) -> List[Dict[str, Any]]:
    """Get all tool definitions registered with this framework."""
    return list(self._tool_definitions.values())

execute_tool(name, arguments)

Execute a registered tool.

Source code in llmfy/llmfy_core/tools/tool_registry.py
def execute_tool(self, name: str, arguments: Dict[str, Any]) -> Any:
    """Execute a registered tool."""
    if name not in self._tools:
        raise LLMfyException(f"Tool not found: {name}")
    return self._tools[name](**arguments)