This function adds a prompt wrap to a tidyprompt()
that instructs the
LLM to answer the prompt with R code. There are various options to customize
the behavior of this prompt wrap, concerning the evaluation of the R code,
the packages that may be used, the objects that already exist in the R
session, and if the console output that should be sent back to the LLM.
Usage
answer_as_code(
prompt,
add_text = "You must code in the programming language 'R' to answer this prompt.",
pkgs_to_use = c(),
objects_to_use = list(),
list_packages = TRUE,
list_objects = TRUE,
skim_dataframes = TRUE,
evaluate_code = TRUE,
output_as_tool = FALSE,
return_mode = c("full", "code", "console", "object", "formatted_output", "llm_answer")
)
Arguments
- prompt
A single string or a
tidyprompt()
object- add_text
Character string which will be added to the current prompt text, informing the LLM that they must code in R to answer the prompt
- pkgs_to_use
A character vector of package names that may be used in the R code that the LLM will generate. If evaluating the R code, these packages will be pre-loaded in the R session
- objects_to_use
A named list of objects that may be used in the R code that the LLM will generate. If evaluating the R code, these objects will be pre-loaded in the R session. The names of the list will be used as the object names in the R session
- list_packages
Logical indicating whether the LLM should be informed about the packages that may be used in their R code (if TRUE, a list of the loaded packages will be shown in the initial prompt)
- list_objects
Logical indicating whether the LLM should be informed about the existence of 'objects_to_use' (if TRUE, a list of the objects plus their types will be shown in the initial prompt)
- skim_dataframes
Logical indicating whether the LLM should be informed about the structure of dataframes present in 'objects_to_use' (if TRUE, a skim summary of each
data.frame
type object will be shown in the initial prompt). This uses the functionskim_with_labels_and_levels()
- evaluate_code
Logical indicating whether the R code should be evaluated. If TRUE, the R code will be evaluated in a separate R session (using the 'callr' package)
- output_as_tool
Logical indicating whether the console output of the evaluated R code should be sent back to the LLM, meaning the LLM will use R code as a tool to formulate an answer to the prompt. If TRUE, the LLM can decide if they can answer the prompt with the output, or if they need to modify their R code. Once the LLM does not provide new R code (i.e., the prompt is being answered) this prompt wrap will end (it will continue for as long as the LLM provides R code). When this option is enabled, the resulting
prompt_wrap()
will be of type 'tool' (meaning it will be applied first in the order of prompt wraps)- return_mode
Character string indicating the return mode. One of 'full', 'code', 'console', 'object', 'formatted_output', or 'llm_answer'. If 'full', the function will return a list with the original LLM answer, the extracted R code, and (if evaluated) the output of the R code. If 'code', the function will return the extracted R code. If 'console', the function will return the console output of the evaluated R code. If 'object', the function will return the object produced by the evaluated R code. If 'formatted_output', the function will return a formatted string with the extracted R code, its console output, and a print of the last object (identical to how it would be presented to the LLM if 'output_as_tool' is TRUE). If 'llm_answer', the function will return only the original LLM answer. When choosing 'console' or 'object', an additional instruction will be added to the prompt text to inform the LLM about the expected output of the R code.
Value
A tidyprompt()
object with the prompt_wrap()
added to it, which
will handle R code generation and possibly evaluation
Details
For the evaluation of the R code, the 'callr' package is required. Please note: automatic evaluation of generated R code may be dangerous to your system; you must use this function with caution.
See also
Other pre_built_prompt_wraps:
add_text()
,
add_tools()
,
answer_as_boolean()
,
answer_as_integer()
,
answer_as_list()
,
answer_as_named_list()
,
answer_as_regex()
,
answer_by_chain_of_thought()
,
answer_by_react()
,
prompt_wrap()
,
quit_if()
,
set_system_prompt()
Other answer_as_prompt_wraps:
answer_as_boolean()
,
answer_as_integer()
,
answer_as_list()
,
answer_as_named_list()
,
answer_as_regex()
Other llm_tools:
add_tools()
Examples
if (FALSE) { # \dontrun{
plot <- paste0(
"Create a scatter plot of miles per gallon (mpg) versus",
" horsepower (hp) for the cars in the mtcars dataset.",
" Use different colors to represent the number of cylinders (cyl).",
" Be very creative and make the plot look nice but also a little crazy!"
) |>
answer_as_code(
pkgs_to_use = c("ggplot2"),
objects_to_use = list("mtcars" = mtcars),
evaluate_code = TRUE,
return_mode = "object"
) |>
send_prompt(llm_provider_openai())
plot
} # }