View source: R/answer_using_tools.R
tools_add_docs | R Documentation |
This function adds documentation to a custom function. This documentation
is used to extract information about the function's name, description, arguments,
and return value. This information is used to provide an LLM with information
about the functions, so that the LLM can call R functions. The intended
use of this function is to add documentation to custom functions that do not
have help files; tools_get_docs()
may generate documentation from a
help file when the function is part of base R or a package.
If a function already has documentation, the documentation added by this
function may overwrite it. If you wish to modify existing documentation,
you may make a call to tools_get_docs()
to extract the existing documentation,
modify it, and then call tools_add_docs()
to add the modified documentation.
tools_add_docs(func, docs)
func |
A function object |
docs |
A list with the following elements:
|
The function object with the documentation added as an attribute ('tidyprompt_tool_docs')
Other tools:
answer_using_tools()
,
tools_get_docs()
## Not run:
# When using functions from base R or R packages,
# documentation is automatically extracted from help files:
"What are the files in my current directory?" |>
answer_using_tools(dir) |> # 'dir' function is from base R
send_prompt()
## End(Not run)
# Custom functions may also be provided;
# in this case, some documentation is extracted from the function's formals;
# descriptions may be added manually. See below
# Example fake weather function to add to the prompt:
temperature_in_location <- function(
location = c("Amsterdam", "Utrecht", "Enschede"),
unit = c("Celcius", "Fahrenheit")
) {
location <- match.arg(location)
unit <- match.arg(unit)
temperature_celcius <- switch(
location,
"Amsterdam" = 32.5,
"Utrecht" = 19.8,
"Enschede" = 22.7
)
if (unit == "Celcius") {
return(temperature_celcius)
} else {
return(temperature_celcius * 9/5 + 32)
}
}
# Generate documentation for a function
# (based on formals, & help file if available)
docs <- tools_get_docs(temperature_in_location)
# The types get inferred from the function's formals
# However, descriptions are still missing as the function is not from a package
# We can modify the documentation object to add descriptions:
docs$description <- "Get the temperature in a location"
docs$arguments$unit$description <- "Unit in which to return the temperature"
docs$arguments$location$description <- "Location for which to return the temperature"
docs$return$description <- "The temperature in the specified location and unit"
# (See `?tools_add_docs` for more details on the structure of the documentation)
# When we are satisfied with the documentation, we can add it to the function:
temperature_in_location <- tools_add_docs(temperature_in_location, docs)
## Not run:
# Now the LLM can use the function:
"Hi, what is the weather in Enschede? Give me Celcius degrees" |>
answer_using_tools(temperature_in_location) |>
send_prompt()
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.