Skip to contents

Wraps an R function with the name, description, and JSON-Schema argument specification that providers need for native tool calling. Pass a list of tools to call_llm_tools() (or to a chat_session() via that function), which executes the calls the model makes and returns the model's final answer.

Usage

llm_tool(fn, name, description, parameters = NULL, required = NULL)

Arguments

fn

The R function to expose. It is called with the model's arguments matched by name, so use the same parameter names as in parameters.

name

Tool name shown to the model (letters, digits, _, -).

description

One or two sentences telling the model what the tool does and when to use it. Write it for the model, not for a human reader; it is the only documentation the model sees.

parameters

Either a named list of JSON-Schema property definitions, e.g. list(city = list(type = "string", description = "City name")), or a complete JSON-Schema object (a list with type = "object" and properties). A tool with no arguments may omit it.

required

Character vector of required argument names. Defaults to all parameter names when parameters is a property list.

Value

An object of class llmr_tool.

Examples

weather <- llm_tool(
  fn = function(city) paste0("22C and clear in ", city),
  name = "get_weather",
  description = "Current weather for a city.",
  parameters = list(city = list(type = "string", description = "City name"))
)