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
endInline 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
Types
Callbacks
Functions
Convert a module implementing the LLM.Tool behaviour to a tool struct.
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 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