AWS ReAct prompting
ReAct prompting
With ReAct prompting, an LLM can combine reasoning and action.
According to Shunyu Yao's article "ReAct: Synergizing Reasoning and Acting in Language Models," models are often used for reasoning or for acting, but they are not always effectively used for both at the same ti
CoT prompting shows promise for an LLM to reason and generate actions for straightforward tasks, like mathematics.
However, the inability to update information or access external contexts with CoT prompting can lead to errors in the output, such as fact hallucination.
With a ReAct framework, LLMs can generate both reasoning traces and task-specific actions that are based on external tools, such as Wikipedia pages or SQL databases.
This external context leads to more accurate and reliable output.
Consider the following prompts.
In the first example, a calculator tool is provided to the LLM.
In the second example, the prompt adds a SQL database tool in order to query stock prices.
Example prompt and output providing the LLM with a calculator tool
Output (Anthropic Claude v1 without calculator) – wrong answer:
So the answer is: 2.8337112781979765
Output (Anthropic Claude v1 with calculator) – right answer:
I will need to use the calculator for this.
Action: Calculator
Action Input: 3.14^0.12345
Observation: Answer: 1.1517174978619817
Thought: I have the final answer.
Final Answer: 1.1517174978619817
> Finished chain.
Example using a SQL database to query stock prices
Now you can extend this to use a SQL database tool for querying stock prices (with daily prices for the stock "ABC").
Prompt (LLM, Math, and SQL tool):
>> Corresponding prompt template:
Answer the following questions as best you can.
You have access to the following tools:
Calculator: Useful for when you need to answer questions about math problems.
SQL Database: Use when you need to answer questions about stocks and their prices. Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take should be one of [Calculator, Stock DB] Action
Input: the input to the action
Observation: the result of the action ... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Output (Anthropic Claude v1 with Math and SQL tool):
I will need historical stock price data for the two dates
Action: Stock DB Action
Input: Price of ABC stock on 2023-01-03 and 2023-01-04
> Entering new SQLDatabaseChain...
SQLInput: Price of ABC stock on 2023-01-03 and 2023-01-04.
SQLQuery: SELECT price FROM stocks WHERE ticker = "ABC" AND date BETWEEN '2023-01-03' AND '2023-01-04'
SQLResult: [232.0, 225.0]
Answer: The price of ABC stock on January 03, 2023 was 232.0 and on January 04, 2023 was 225.0.
> Finished chain.
Observation: The price of ABC stock on January 03, 2023 was 232.0 and on January 04, 2023 was 225.0.
Thought: Now I can compute the price ratio