liveBook · Manning

ReAct

As explained in LLMs in Production

ReAct is a prompting framework that interleaves a language model's reasoning with external actions and observations to solve tasks.

ReAct is a few-shot prompting framework that interleaves a language model’s reasoning with external actions and observations. Instead of answering in one step, the model identifies an action, observes its result, reasons about what remains unknown, and chooses the next action. section 7.4.2

A typical ReAct loop contains:

  • A question or task that establishes the goal. section 7.4.2
  • A thought that identifies missing information or the next decision. section 7.4.2
  • An action such as Search, Lookup, or Calculator. section 7.4.2
  • An observation that feeds the next reasoning step, followed by a final answer when the task is complete. section 7.4.2

From lookup to calculation

A ReAct agent can search for Olivia Wilde’s boyfriend, search for Harry Styles’ age, and then use a calculator to raise 29 to the 0.23 power. Each result determines the next action rather than requiring every step in advance. section 7.4.2

Listing 7.5 Example ReAct with Langchain
 import os
from langchain.llms import OpenAI
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from dotenv import load_dotenv

load_dotenv()

os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")     #1
os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")

llm = OpenAI(model_name="text-davinci-003", temperature=0)
tools = load_tools(["google-serper", "llm-math"], llm=llm)
agent = initialize_agent(
    tools, llm, agent="zero-shot-react-description", verbose=True
)

agent.run(
    "Who is Olivia Wilde's boyfriend? \
    What is his current age raised to the 0.23 power?"
)

A compact LangChain setup connects an OpenAI model to Google Serper search and an LLM-powered math tool through a zero-shot ReAct agent. section 7.4.2

How LLMs in Production covers it

How deep it goes
Explains the reasoning-action-observation loop with a worked trace, then demonstrates agent configuration without detailing implementation internals.
Examples
Demonstrates swallow-speed research and an Olivia Wilde, Harry Styles, and exponent-calculation agent trace.
What you can do after
Apply it via LangChain by connecting an OpenAI model with search and math tools in a ReAct agent.
How it connects
Connects few-shot prompting, tool use, external search, calculation, and LangChain agents.

Also explained in

Compare how 11 books explain ReAct →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.