ReAct
Reasoning and Acting
An agent framework that interleaves reasoning and acting. Effective for solving complex tasks.
Reasoning and Acting
An agent framework that interleaves reasoning and acting. Effective for solving complex tasks.
ReAct (Reasoning and Acting) is a prompting framework in which an LLM solves complex problems by interleaving reasoning and acting. Rather than simply generating an answer, it repeats a cycle of thinking, using tools, and observing the results to produce more accurate outcomes.
ReAct was published in 2022 by Google Research and Princeton. The existing Chain-of-Thought (CoT) performed only reasoning and could not make use of external information, while the Action-only approach used tools without context. ReAct combined the two to create synergy between reasoning and acting.
The core of ReAct is the "Thought-Action-Observation" loop. It first analyzes the current situation (Thought), calls the necessary tools or APIs (Action), and decides the next step after checking the result (Observation). This process repeats until the goal is achieved.
In practice, ReAct has become a core pattern for AI agents. Major frameworks such as LangChain and LlamaIndex support the ReAct pattern by default, and it automates complex tasks by combining with various tools such as web search, database queries, and API calls.
This is an example of implementing a ReAct agent using LangChain.
# pip install langchain langchain-openai duckduckgo-search
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
from langchain import hub
from langchain_community.tools import DuckDuckGoSearchRun
# 1. λꡬ μ μ
search_tool = DuckDuckGoSearchRun()
tools = [search_tool]
# 2. LLM μ΄κΈ°ν
llm = ChatOpenAI(model="gpt-4o", temperature=0)
# 3. ReAct ν둬ννΈ λ‘λ
prompt = hub.pull("hwchase17/react")
# 4. ReAct μμ΄μ νΈ μμ±
agent = create_react_agent(llm, tools, prompt)
# 5. μμ΄μ νΈ μ€νκΈ°
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True, # Thought-Action-Observation μΆλ ₯
max_iterations=5,
handle_parsing_errors=True
)
# 6. μ€ν
result = agent_executor.invoke({
"input": "2024λ
λ
Έλ²¨ 물리νμ μμμμ κ·Έλ€μ μ°κ΅¬ μ£Όμ λ?"
})
print(f"μ΅μ’
λ΅λ³: {result['output']}")
# μΆλ ₯ μμ:
# Thought: 2024λ
λ
Έλ²¨ 물리νμ μ 보λ₯Ό κ²μν΄μΌκ² λ€
# Action: duckduckgo_search
# Action Input: 2024 Nobel Prize Physics winner
# Observation: κ²μ κ²°κ³Ό...
# Thought: μμμ μ 보λ₯Ό μ°Ύμλ€. μ 리ν΄μ λ΅λ³νκ² λ€
# Final Answer: ...
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free