R/ask_human.R

Defines functions ask_human_tool

Documented in ask_human_tool

#' 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.
#'
#' @param 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()`.
#' @param name `[character(1)]` The tool name. Defaults to `"ask_user"`.
#' @param 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.
#' @return An ellmer tool.
#' @examples
#' \dontrun{
#' 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)
#' }
#' @export
ask_human_tool <- function(ask = NULL, name = "ask_user", description = NULL) {
  if (!is.null(ask)) checkmate::assert_function(ask)
  checkmate::assert_string(name, min.chars = 1)
  if (is.null(description)) {
    description <- paste(
      "Ask the user a question and get their answer. This is the only way to",
      "ask the user something: never ask in your final answer, which ends the",
      "conversation. Use it only when you",
      "cannot go on without the user's input, for example when the request is",
      "ambiguous or when they must choose between options. Ask as few",
      "questions as possible, usually one, and make reasonable assumptions",
      "about the details. Ask one clear question at a time, and suggest a few",
      "answers in `options` when that helps. Once you have the answer, carry",
      "on with the task: do not stop at acknowledging it."
    )
  }
  checkmate::assert_string(description, min.chars = 1)

  run <- function(question, options = NULL) {
    options <- as.character(unlist(options))
    answer_with <- ask
    if (is.null(answer_with)) {
      if (!interactive()) {
        return(paste(
          "No one can answer questions in this session. Decide on your own,",
          "and say in your answer what you assumed."
        ))
      }
      answer_with <- function(question, options) console_question(question, options)
    }
    answer <- answer_with(question, options)
    answer <- trimws(paste(as.character(unlist(answer)), collapse = "\n"))
    if (!nzchar(answer)) {
      return(paste(
        "The user gave no answer. Decide on your own, and say in your answer",
        "what you assumed."
      ))
    }
    answer
  }

  ellmer::tool(
    run,
    name = tool_name_for(name),
    description = description,
    arguments = list(
      question = ellmer::type_string("The question to ask the user."),
      options = ellmer::type_array(
        ellmer::type_string(),
        "A few suggested answers the user can pick from. Leave it out for an open question.",
        required = FALSE
      )
    )
  )
}

Try the mini007 package in your browser

Any scripts or data that you put into this service are public.

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