LLM.Tool behaviour (LLM v0.1.0)

Copy Markdown View Source

Tool definition behaviour and inline tool creation.

Tools can be defined as modules implementing the behaviour, or inline using new/1.

Module Tool

defmodule MyApp.Tools.ReadFile do
  @behaviour LLM.Tool

  @impl true
  def name, do: "read_file"

  @impl true
  def description, do: "Read the contents of a file"

  @impl true
  def input_schema do
    %{
      "type" => "object",
      "properties" => %{
        "path" => %{"type" => "string", "description" => "File path"}
      },
      "required" => ["path"]
    }
  end

  @impl true
  def execute(%{"path" => path}, _context) do
    File.read(path)
  end
end

Inline Tool

LLM.Tool.new(%{
  name: "shell",
  description: "Execute a shell command",
  input_schema: %{
    "type" => "object",
    "properties" => %{
      "command" => %{"type" => "string"}
    },
    "required" => ["command"]
  },
  run: fn %{"command" => cmd} ->
    {output, _} = System.cmd("sh", ["-c", cmd])
    {:ok, output}
  end
})

Summary

Functions

Convert a module implementing the LLM.Tool behaviour to a tool struct.

Create an inline tool from a map.

Normalize a tool specification to a LLM.Tool.t() struct.

Types

t()

@type t() :: %LLM.Tool{
  description: String.t(),
  execute: (map(), map() -> {:ok, term()} | {:error, term()}),
  input_schema: map(),
  name: String.t()
}

Callbacks

description()

@callback description() :: String.t()

execute(map, map)

@callback execute(map(), map()) :: {:ok, term()} | {:error, term()}

input_schema()

@callback input_schema() :: map()

name()

@callback name() :: String.t()

Functions

from_module(module)

Convert a module implementing the LLM.Tool behaviour to a tool struct.

new(attrs)

Create an inline tool from a map.

Options

  • :name - tool name (required)
  • :description - tool description (required)
  • :input_schema - JSON Schema for tool input (required)
  • :run - function to execute. Receives input map. Can be arity 1 or 2.

normalize(tool)

Normalize a tool specification to a LLM.Tool.t() struct.

Accepts:

  • %LLM.Tool{} — returned as-is
  • A module — converted via from_module/1
  • A {module, opts} tuple — module tool with extra context merged into execution