ask_human_tool: A tool that lets an agent ask the user a question

View source: R/ask_human.R

ask_human_toolR Documentation

A tool that lets an agent ask the user a question

Description

Returns an ellmer tool that an agent with tools, usually a [ReActAgent], can call in the middle of its run to ask the user a question, for example to clear up an ambiguous request or to let the user choose between options. The agent decides when to ask; the answer comes back as the tool's result and the agent carries on.

By default the question is asked in the console, with numbered options when the agent gives some. Pass 'ask' to get the answers elsewhere: from a Shiny app, a file, or a script.

When nobody can answer (a non-interactive session without 'ask', such as a script, a rendered document or a parallel worker), or when the user gives an empty answer, the agent is told so and asked to decide on its own and say what it assumed. It is never left waiting.

Usage

ask_human_tool(ask = NULL, name = "ask_user", description = NULL)

Arguments

ask

'[function | NULL]' A 'function(question, options)' that returns the user's answer as a string. 'options' is a character vector of suggested answers, possibly empty. Defaults to asking in the console. This is the same interface as the 'ask' argument of [ArchitectAgent]'s '$design()'.

name

'[character(1)]' The tool name. Defaults to '"ask_user"'.

description

'[character(1) | NULL]' When the agent should use the tool. The default tells it to ask only when it cannot go on without the user's input.

Value

An ellmer tool.

Examples

## Not run: 
planner <- ReActAgent$new(
  name = "planner",
  instruction = "You plan trips in Algeria. Ask the traveller when something is unclear.",
  llm_object = ellmer::chat("openai/gpt-4.1-mini", echo = "none"),
  tools = list(ask_human_tool())
)
planner$invoke("Plan a weekend in Oran.")

# Scripted answers, eg. for tests
always_history <- function(question, options) "History, on a small budget."
tool <- ask_human_tool(ask = always_history)

## End(Not run)

mini007 documentation built on Dec. 3, 2026, 5:10 p.m.