Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT SWIFT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST

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

U
What is 3.14 raised to the power of 0.12345?

Output (Anthropic Claude v1 without calculator) – wrong answer:

AI
3.140.12345 = 2.8337112781979765
So the answer is: 2.8337112781979765

Output (Anthropic Claude v1 with calculator) – right answer:

AI
Entering new AgentExecutor chain...
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):

U
Calculate the price ratio for stock 'ABC' between 2023-01-03 and 2023-01-04.

>> 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):

AI
> Entering new AgentExecutor chain...
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

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.