Learn R Programming

aisdk (version 1.1.0)

generate_text: Generate Text

Description

Generate text using a language model. This is the primary high-level function for non-streaming text generation.

When tools are provided and max_steps > 1, the function will automatically execute tool calls and feed results back to the LLM in a ReAct-style loop until the LLM produces a final response or max_steps is reached.

Usage

generate_text(
  model = NULL,
  prompt,
  system = NULL,
  temperature = 0.7,
  max_tokens = NULL,
  tools = NULL,
  max_steps = 1,
  sandbox = FALSE,
  skills = NULL,
  session = NULL,
  hooks = NULL,
  registry = NULL,
  ...
)

Value

A GenerateResult object with text and optionally tool_calls. When max_steps > 1 and tools are used, the result includes:

  • steps: Number of steps taken

  • all_tool_calls: List of all tool calls made across all steps

Arguments

model

Either a LanguageModelV1 object, or a string ID like "openai:gpt-4o".

prompt

A character string prompt, or a list of messages.

system

Optional system prompt.

temperature

Sampling temperature (0-2). Default 0.7.

max_tokens

Maximum tokens to generate.

tools

Optional list of Tool objects for function calling.

max_steps

Maximum number of generation steps (tool execution loops). Default 1 (single generation, no automatic tool execution). Set to higher values (e.g., 5) to enable automatic tool execution.

sandbox

Logical. If TRUE, enables R-native programmatic sandbox mode. All tools are bound into an isolated R environment and replaced by a single execute_r_code meta-tool. The LLM writes R code to batch-invoke tools, filter data with dplyr/purrr, and return only summary results, dramatically reducing token usage and latency. Default FALSE.

skills

Optional path to skills directory, or a SkillRegistry object. When provided, skill tools are auto-injected and skill summaries are added to the system prompt.

session

Optional ChatSession object. When provided, tool executions run in the session's environment, enabling cross-agent data sharing.

hooks

Optional HookHandler object for intercepting events.

registry

Optional ProviderRegistry to use (defaults to global registry).

...

Additional arguments passed to the model.

Examples

Run this code
# \donttest{
if (interactive()) {
  # Using hooks
  my_hooks <- create_hooks(
    on_generation_start = function(model, prompt, tools) message("Starting..."),
    on_tool_start = function(tool, args) message("Calling tool ", tool$name)
  )
  result <- generate_text(model, "...", hooks = my_hooks)
}
# }

Run the code above in your browser using DataLab