Skip to contents

agentapply is the lapply-style entry point for running a single prompt against an Agent repeatedly over a vector of inputs. Pass either a model name (in which case an Agent is built on the fly using backend, system_prompt, tools, use_memory, max_tool_rounds, output_schema) or a pre-built Agent object.

Usage

agentapply(
  x,
  model_or_agent,
  backend = c("ollama", "openai", "anthropic"),
  system_prompt = SYSTEM_PROMPT_DEFAULT,
  tools = NULL,
  use_memory = FALSE,
  max_tool_rounds = 3L,
  output_schema = NULL,
  verbosity = 1L,
  extract_responses = TRUE,
  ...
)

Arguments

x

Character or list: Values to iterate over.

model_or_agent

Character or Agent: Either the name of a model (a string) or a pre-built Agent object from create_agent.

backend

Character {"ollama", "openai", "anthropic"}: Backend to use when model_or_agent is a string. Ignored when model_or_agent is an Agent object.

system_prompt

Character: System prompt for the on-the-fly Agent.

tools

Optional list of Tool objects: Tools available to the on-the-fly Agent.

use_memory

Logical: Whether the on-the-fly Agent should keep conversation memory.

max_tool_rounds

Integer [1, Inf): Maximum number of tool call rounds per query.

output_schema

Optional Schema: Output schema for the on-the-fly Agent.

verbosity

Integer [0, Inf): Verbosity level.

extract_responses

Logical: If TRUE, return a character vector of assistant responses. If FALSE, return the raw list of lists of Message objects.

...

Additional per-call arguments forwarded to generate.

Value

If extract_responses = TRUE, a character vector the same length as x. Otherwise, a list of lists of Message objects.

Details

Unlike llmapply, this function can carry tools and memory. The default is use_memory = FALSE because the common case for vectorized calls is independent queries.

Author

EDG

Examples

# Requires running Ollama server and gemma4:e4b model
if (FALSE) { # \dontrun{
  agentapply(
    c("today", "yesterday", "tomorrow"),
    "gemma4:e4b",
    system_prompt = "Return the date in ISO format",
    tools = list(tool_datetime),
    temperature = 0.2
  )
} # }