ReqLLM.Tool (ReqLLM v1.0.0-rc.5)
View SourceTool definition for AI model function calling.
Tools enable AI models to call external functions, perform actions, and retrieve information. Each tool has a name, description, parameters schema, and a callback function to execute.
Basic Usage
# Create a simple tool
{:ok, tool} = ReqLLM.Tool.new(
name: "get_weather",
description: "Get current weather for a location",
parameter_schema: [
location: [type: :string, required: true, doc: "City name"]
],
callback: {WeatherService, :get_current_weather}
)
# Execute the tool
{:ok, result} = ReqLLM.Tool.execute(tool, %{location: "San Francisco"})
# Get provider-specific schema
anthropic_schema = ReqLLM.Tool.to_schema(tool, :anthropic)
Parameters Schema
Parameters are defined using NimbleOptions-compatible keyword lists:
parameter_schema: [
location: [type: :string, required: true, doc: "City name"],
units: [type: :string, default: "celsius", doc: "Temperature units"]
]
Callback Formats
Multiple callback formats are supported:
# Module and function (args passed as single argument)
callback: {MyModule, :my_function}
# Module, function, and additional args (prepended to input)
callback: {MyModule, :my_function, [:extra, :args]}
# Anonymous function
callback: fn args -> {:ok, "result"} end
Provider Schema Formats
Tools can be converted to provider-specific formats:
# Anthropic tool format
anthropic_schema = ReqLLM.Tool.to_schema(tool, :anthropic)
Functions
new/1
- Creates a new Tool from the given optionsnew!/1
- Creates a new Tool from the given options, raising on errorexecute/2
- Executes a tool with the given input parametersto_schema/2
- Converts a Tool to provider-specific schema formatto_json_schema/1
- Converts a Tool to JSON Schema format for LLM integrationvalid_name?/1
- Validates a tool name for compliance with function calling standards
Summary
Types
A tool definition for AI model function calling
Functions
Executes a tool with the given input parameters.
Creates a new Tool from the given options.
Creates a new Tool from the given options, raising on error.
Converts a Tool to JSON Schema format for LLM integration.
Converts a Tool to provider-specific schema format.
Validates a tool name for compliance with function calling standards.
Types
@type callback() :: callback_mfa() | callback_fun()
@type t() :: %ReqLLM.Tool{ callback: callback(), compiled: term() | nil, description: String.t(), name: String.t(), parameter_schema: keyword(), strict: boolean() }
A tool definition for AI model function calling
Functions
Executes a tool with the given input parameters.
Validates input parameters against the tool's schema and calls the callback function.
The callback is expected to return {:ok, result}
or {:error, reason}
.
Parameters
tool
- Tool structinput
- Input parameters as map
Examples
{:ok, result} = ReqLLM.Tool.execute(tool, %{location: "San Francisco"})
#=> {:ok, %{temperature: 72, conditions: "sunny"}}
{:error, reason} = ReqLLM.Tool.execute(tool, %{invalid: "params"})
#=> {:error, %ReqLLM.Error.Validation.Error{...}}
Creates a new Tool from the given options.
Parameters
opts
- Tool options as keyword list
Options
:name
- Tool name (required, must be valid identifier):description
- Tool description for AI model (required):parameter_schema
- Parameter schema as NimbleOptions keyword list (optional):callback
- Callback function or MFA tuple (required)
Examples
{:ok, tool} = ReqLLM.Tool.new(
name: "get_weather",
description: "Get current weather",
parameter_schema: [
location: [type: :string, required: true]
],
callback: {WeatherService, :get_weather}
)
Creates a new Tool from the given options, raising on error.
See new/1
for details.
Examples
tool = ReqLLM.Tool.new!(
name: "get_weather",
description: "Get current weather",
callback: {WeatherService, :get_weather}
)
Converts a Tool to JSON Schema format for LLM integration.
Backward compatibility function that defaults to OpenAI format.
Use to_schema/2
for explicit provider selection.
Examples
json_schema = ReqLLM.Tool.to_json_schema(tool)
# Equivalent to: ReqLLM.Tool.to_schema(tool, :openai)
Converts a Tool to provider-specific schema format.
Returns a map containing the provider's expected tool format with tool name, description, and parameter definitions.
Parameters
tool
- Tool structprovider
- Provider atom (:anthropic
)
Examples
# Anthropic tool format
anthropic_schema = ReqLLM.Tool.to_schema(tool, :anthropic)
#=> %{
# "name" => "get_weather",
# "description" => "Get current weather",
# "input_schema" => %{...}
# }
Validates a tool name for compliance with function calling standards.
Tool names must be valid identifiers (alphanumeric + underscores, start with letter/underscore).
Examples
ReqLLM.Tool.valid_name?("get_weather")
#=> true
ReqLLM.Tool.valid_name?("123invalid")
#=> false