Skip to contents

Generic method for generating text or structured output from LLMs and Agents.

Usage

generate(
  x,
  prompt,
  temperature = NULL,
  top_p = NULL,
  max_tokens = NULL,
  stop = NULL,
  image_path = NULL,
  think = NULL,
  output_schema = NULL,
  verbosity = 1L,
  validate_output = TRUE,
  on_validation_failure = c("warn", "collect", "abort"),
  ...
)

Arguments

x

An object of class LLM or Agent.

prompt

Character: The prompt to pass to the model or agent.

temperature

Optional numeric [0, 2]: Per-call sampling temperature.

top_p

Optional numeric [0, 1]: Nucleus sampling cutoff.

max_tokens

Optional integer [1, Inf): Maximum tokens to generate. For Anthropic, this overrides the config-level value (which is required); for Ollama this maps to options.num_predict; for OpenAI-compatible backends this maps to max_tokens.

stop

Optional character: Stop sequence(s). Mapped to stop_sequences on Anthropic and options.stop on Ollama.

image_path

Optional character: Paths to one or more local image files (PNG, JPEG, GIF, or WebP) to send with the prompt. Each file is checked and read before the request is sent. The model must accept image input; a model that does not is reported as an error by its server.

think

Optional logical or character: Whether to enable model thinking (reasoning trace) for this call. Character values target gpt-oss-style local models.

output_schema

Optional Schema: Output schema to enforce on this call's response. If omitted, the object's default schema (if any) is used.

verbosity

Integer: Verbosity level.

validate_output

Logical: Validate final output locally when a schema is supplied. Disabling this does not disable the schema sent to the model.

on_validation_failure

Character {"warn", "collect", "abort"}: Preserve invalid output and report a styled rtemis.core message (not an R warning), collect silently, or abort with an error carrying output and validation. See validate_output.

...

Additional backend-specific per-call arguments. See Details.

Value

Message object or list of Message objects (for Agent). With a schema, validation_results retrieves the attached validation report. Invalid output is retained unless explicitly configured to abort. Agent validation checks only the final answer, before committing that answer to memory.

Details

The system prompt is set once at agent (or LLM) construction time and is not overridable per call. Construct a new agent if you need a different system prompt.

Backend-specific extra arguments accepted via ...:

  • Ollama: top_k (integer), seed (integer), num_ctx (integer, mapped to options.num_ctx), keep_alive (character duration such as "10m", or seconds as a number, controlling how long the model stays loaded after the request), logprobs (logical), top_logprobs (integer)

  • OpenAI: seed (integer), logprobs (logical), top_logprobs (integer [0, 20])

  • Anthropic: top_k (integer)

Any argument set to NULL (the default) falls back to the value baked into the underlying LLMConfig at construction time.

Author

EDG

Examples

# Requires running Ollama server and gemma4:e4b model
if (FALSE) { # \dontrun{
  agent <- create_agent(
    config_Ollama(
      model_name = "gemma4:e4b",
      temperature = 0.2
    )
  )
  generate(agent, "What is your name?", temperature = 0.7)
} # }