Define an R function for use by a chatbot. The function will always be run in the current R instance.
Learn more in vignette("tool-calling")
.
Arguments
- .fun
The function to be invoked when the tool is called.
- .description
A detailed description of what the function does. Generally, the more information that you can provide here, the better.
- ...
Name-type pairs that define the arguments accepted by the function. Each element should be created by a
type_*()
function.- .name
The name of the function.
Examples
# First define the metadata that the model uses to figure out when to
# call the tool
tool_rnorm <- tool(
rnorm,
"Drawn numbers from a random normal distribution",
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.")
)
chat <- chat_openai()
#> Using model = "gpt-4o".
# 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.
")
#> To provide you with five numbers from a random normal distribution, I
#> need the mean and standard deviation values for the distribution. Could
#> you please specify these parameters?
# Look at the chat history to see how tool calling works:
# Assistant sends a tool request which is evaluated locally and
# results are send back in a tool result.