| tool | R Documentation |
Annotate a function for use in tool calls, by providing a name, description, and type definition for the arguments.
Learn more in vignette("tool-calling").
tool(
fun,
description,
...,
arguments = list(),
name = NULL,
convert = TRUE,
annotations = list(),
.name = deprecated(),
.description = deprecated(),
.convert = deprecated(),
.annotations = deprecated()
)
fun |
The function to be invoked when the tool is called. The return value of the function is sent back to the chatbot. Expert users can customize the tool result by returning a ContentToolResult object. |
description |
A detailed description of what the function does. Generally, the more information that you can provide here, the better. |
... |
|
arguments |
A named list that defines the arguments accepted by the
function. Each element should be created by a |
name |
The name of the function. This can be omitted if |
convert |
Should JSON inputs be automatically convert to their
R data type equivalents? Defaults to |
annotations |
Additional properties that describe the tool and its
behavior. Usually created by |
.name, .description, .convert, .annotations |
An S7 ToolDef object.
In ellmer 0.3.0, the definition of the tool() function changed quite
a bit. To make it easier to update old versions, you can use an LLM with
the following system prompt
Help the user convert an ellmer 0.2.0 and earlier tool definition into a
ellmer 0.3.0 tool definition. Here's what changed:
* All arguments, apart from the first, should be named, and the argument
names no longer use `.` prefixes. The argument order should be function,
name (as a string), description, then arguments, then anything
* Previously `arguments` was passed as `...`, so all type specifications
should now be moved into a named list and passed to the `arguments`
argument. It can be omitted if the function has no arguments.
```R
# old
tool(
add,
"Add two numbers together"
x = type_number(),
y = type_number()
)
# new
tool(
add,
name = "add",
description = "Add two numbers together",
arguments = list(
x = type_number(),
y = type_number()
)
)
```
Don't respond; just let the user provide function calls to convert.
Other tool calling helpers:
tool_annotations(),
tool_reject()
# First define the metadata that the model uses to figure out when to
# call the tool
tool_rnorm <- tool(
rnorm,
description = "Draw numbers from a random normal distribution",
arguments = list(
n = type_integer("The number of observations. Must be a positive integer."),
mean = type_number("The mean value of the distribution."),
sd = type_number("The standard deviation of the distribution. Must be a non-negative number.")
)
)
tool_rnorm(n = 5, mean = 0, sd = 1)
chat <- chat_openai()
# Then register it
chat$register_tool(tool_rnorm)
# Then ask a question that needs it.
chat$chat("Give me five numbers from a random normal distribution.")
# Look at the chat history to see how tool calling works:
chat
# Assistant sends a tool request which is evaluated locally and
# results are sent back in a tool result.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.