answer_by_react: Set ReAct mode for a prompt

View source: R/answer_by_react.R

answer_by_reactR Documentation

Set ReAct mode for a prompt

Description

This function enables ReAct mode for the evaluation of a prompt or a tidyprompt(). In ReAct mode, the large language model (LLM) is asked to think step by step, each time detailing a thought, action, and observation, to eventually arrive at a final answer. It is hypothesized that this may increase LLM performance at solving complex tasks. ReAct mode is inspired by the method described in Yao et al. (2022).

Usage

answer_by_react(
  prompt,
  extract_from_finish_brackets = TRUE,
  extraction_lenience = TRUE
)

Arguments

prompt

A single string or a tidyprompt() object

extract_from_finish_brackets

A logical indicating whether the final answer should be extracted from the text inside the "FINISH..." brackets

extraction_lenience

A logical indcating whether the extraction function should be lenient. If TRUE, the extraction function will attempt to extract the final answer even if it cannot be extracted from within the brackets, by extracting everything after the final occurence of 'FINISH' (if present). This may be useful for smaller LLMs which may not follow the output format as strictly

Details

Please note that ReAct mode may be most useful if in combination with tools that the LLM can use. See, for example, 'add_tools()' for enabling R function calling, or, for example, 'answer_as_code()' with 'output_as_tool = TRUE' for enabling R code evaluation as a tool.

Value

A tidyprompt() with an added prompt_wrap() which will ensure that the LLM follows the ReAct mode in answering the prompt

References

Yao, S., Wu, Y., Cheung, W., Wang, Z., Narasimhan, K., & Kong, L. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. doi:10.48550/arXiv.2210.03629

See Also

answer_using_tools() answer_using_r()

Other pre_built_prompt_wraps: add_text(), answer_as_boolean(), answer_as_integer(), answer_as_json(), answer_as_list(), answer_as_named_list(), answer_as_regex_match(), answer_as_text(), answer_by_chain_of_thought(), answer_using_r(), answer_using_sql(), answer_using_tools(), prompt_wrap(), quit_if(), set_system_prompt()

Other answer_by_prompt_wraps: answer_by_chain_of_thought()

Examples

## Not run: 
  "What is 5 + 5?" |>
    answer_by_react() |>
    answer_as_integer() |>
    send_prompt()
  # --- Sending request to LLM provider (llama3.1:8b): ---
  # You are given a user's prompt.
  # To answer the user's prompt, you need to think step by step,
  # take an action if needed, and then return a final answer.
  #
  # ----- START OF USER'S PROMPT -----
  # What is 5 + 5?
  #
  # You must answer with only an integer (use no other characters).
  # ----- END OF USER'S PROMPT -----
  #
  # Use the following structure:
  #   Thought: <describe your thought process>
  #   Action: <if needed, describe the action you take (e.g., look up information)>
  #   Observation: <describe the result or observation from the action>
  # (Repeat Thought -> Action -> Observation as necessary)
  #
  # When you are done, you must type:
  #   FINISH[<put here your final answer to the user's prompt>]
  #
  # Ensure your final answer aligns with your reasoning and observations.
  # --- Receiving response from LLM provider: ---
  # Thought: The problem is asking for the sum of two numbers, 5 and 5.
  #
  # Action: None needed, as this is a simple arithmetic operation that can be performed mentally.
  #
  # Observation: I can easily add these two numbers together in my mind to get the result.
  #
  # Thought: To find the sum, I will simply add the two numbers together: 5 + 5 = ?
  #
  # Action: Perform the addition.
  #
  # Observation: The result of adding 5 and 5 is 10.
  #
  # FINISH[10]
  # [1] 10

## End(Not run)

tidyprompt documentation built on April 4, 2025, 12:24 a.m.