Rag.Agent.Registry (rag v0.3.4)

View Source

Tool registry for managing available agent tools.

The registry maintains a collection of tools that can be used by agents. It provides functions for registering, looking up, and executing tools.

Usage

# Create a registry with initial tools
registry = Registry.new(tools: [SearchTool, ReadFileTool])

# Register additional tools
registry = Registry.register(registry, AnalyzeTool)

# Execute a tool
{:ok, result} = Registry.execute(registry, "search", %{"query" => "test"}, context)

Summary

Functions

Returns the number of registered tools.

Executes a tool by name.

Filters tools by a predicate function.

Formats all tools for LLM function calling.

Gets a tool by name.

Checks if a tool is registered.

Lists all registered tool modules.

Lists all registered tool names.

Creates a new tool registry.

Registers a tool in the registry.

Registers multiple tools at once.

Unregisters a tool by name.

Types

t()

@type t() :: %Rag.Agent.Registry{tools: %{required(String.t()) => module()}}

Functions

count(registry)

@spec count(t()) :: non_neg_integer()

Returns the number of registered tools.

Examples

iex> Registry.count(registry)
2

execute(registry, name, args, context)

@spec execute(t(), String.t(), map(), map()) :: {:ok, term()} | {:error, term()}

Executes a tool by name.

Examples

iex> Registry.execute(registry, "my_tool", %{"input" => "test"}, %{})
{:ok, "result"}

iex> Registry.execute(registry, "unknown", %{}, %{})
{:error, {:tool_not_found, "unknown"}}

filter(registry, predicate)

@spec filter(t(), (module() -> boolean())) :: t()

Filters tools by a predicate function.

Examples

iex> Registry.filter(registry, fn tool -> tool.name() == "my_tool" end)
%Registry{...}

format_for_llm(registry)

@spec format_for_llm(t()) :: [map()]

Formats all tools for LLM function calling.

Examples

iex> Registry.format_for_llm(registry)
[%{name: "tool1", description: "...", parameters: %{...}}, ...]

get(registry, name)

@spec get(t(), String.t()) :: {:ok, module()} | {:error, :not_found}

Gets a tool by name.

Examples

iex> Registry.get(registry, "my_tool")
{:ok, MyTool}

iex> Registry.get(registry, "unknown")
{:error, :not_found}

has_tool?(registry, name)

@spec has_tool?(t(), String.t()) :: boolean()

Checks if a tool is registered.

Examples

iex> Registry.has_tool?(registry, "my_tool")
true

list(registry)

@spec list(t()) :: [module()]

Lists all registered tool modules.

Examples

iex> Registry.list(registry)
[Tool1, Tool2]

names(registry)

@spec names(t()) :: [String.t()]

Lists all registered tool names.

Examples

iex> Registry.names(registry)
["tool1", "tool2"]

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new tool registry.

Options

  • :tools - List of tool modules to register initially

Examples

iex> Registry.new()
%Registry{tools: %{}}

iex> Registry.new(tools: [MyTool])
%Registry{tools: %{"my_tool" => MyTool}}

register(registry, tool_module)

@spec register(t(), module()) :: t()

Registers a tool in the registry.

If a tool with the same name already exists, it will be replaced.

Examples

iex> registry |> Registry.register(MyTool)
%Registry{tools: %{"my_tool" => MyTool}}

register_all(registry, tool_modules)

@spec register_all(t(), [module()]) :: t()

Registers multiple tools at once.

Examples

iex> Registry.register_all(registry, [Tool1, Tool2])
%Registry{tools: %{"tool1" => Tool1, "tool2" => Tool2}}

unregister(registry, name)

@spec unregister(t(), String.t()) :: t()

Unregisters a tool by name.

Examples

iex> Registry.unregister(registry, "my_tool")
%Registry{...}