Lý thuyết
30 phút
Bài 2/5

ReAct Pattern

Tìm hiểu ReAct - pattern kết hợp Reasoning và Acting cho AI Agents

🔄 ReAct Pattern: Reasoning + Acting

ReAct (Reasoning and Acting) là pattern quan trọng nhất trong AI Agents. Bài này sẽ giúp bạn hiểu và implement ReAct pattern.

ReAct là gì?

ReAct Definition

ReAct kết hợp:

  • Reasoning: LLM suy nghĩ và phân tích
  • Acting: Thực hiện actions dựa trên suy nghĩ
  • Observing: Quan sát kết quả từ actions

Vòng lặp ReAct:

Diagram
graph LR
    T[Thought] --> A[Action]
    A --> O[Observation]
    O --> T
    O -->|Done| F[Final Answer]

Cấu trúc ReAct Loop

Mỗi bước trong ReAct loop gồm 3 phần:

Text
1Thought: [LLM suy nghĩ về vấn đề]
2Action: [Tool/function cần gọi]
3Observation: [Kết quả từ action]

Ví dụ cụ thể

Question: "Thời tiết hôm nay ở Hà Nội thế nào?"

Text
1Thought: Tôi cần tìm thông tin thời tiết ở Hà Nội.
2 Tôi sẽ sử dụng tool get_weather.
3
4Action: get_weather(city="Hanoi")
5
6Observation: {"temp": 25, "condition": "sunny", "humidity": 65}
7
8Thought: Đã có thông tin thời tiết. Tôi sẽ tổng hợp
9 và trả lời người dùng.
10
11Action: final_answer("Hà Nội hôm nay trời nắng,
12 nhiệt độ 25°C, độ ẩm 65%.")

Implement ReAct với LangChain

Step 1: Define Tools

Python
1from langchain.tools import tool
2from langchain_community.tools import DuckDuckGoSearchRun
3
4@tool
5def get_weather(city: str) -> str:
6 """
7 Ly thông tin thi tiết hin ti ca mt thành ph.
8
9 Args:
10 city: Tên thành ph (tiếng Anh)
11
12 Returns:
13 Thông tin thi tiết dng JSON
14 """
15 # Giả lập API call
16 import json
17 weather_data = {
18 "city": city,
19 "temperature": 25,
20 "condition": "Sunny",
21 "humidity": 65
22 }
23 return json.dumps(weather_data)
24
25@tool
26def calculate(expression: str) -> str:
27 """
28 Tính toán biu thc toán hc.
29
30 Args:
31 expression: Biu thc cn tính (ví d: "2 + 2 * 3")
32
33 Returns:
34 Kết qu tính toán
35 """
36 try:
37 result = eval(expression)
38 return str(result)
39 except Exception as e:
40 return f"Error: {str(e)}"
41
42# Search tool
43search = DuckDuckGoSearchRun()
44
45tools = [get_weather, calculate, search]

Step 2: Create ReAct Prompt

Python
1from langchain.prompts import PromptTemplate
2
3react_prompt = PromptTemplate.from_template("""
4Bn là mt AI assistant hu ích. Tr li câu hi bng cách
5suy nghĩ tng bưc và s dng tools khi cn.
6
7Bn có access đến các tools sau:
8{tools}
9
10Format response ca bn:
11
12Question: câu hi cn tr li
13Thought: suy nghĩ v cách gii quyết
14Action: tên_tool[input]
15Observation: kết qu t tool
16... (lp li Thought/Action/Observation nếu cn)
17Thought: Tôi đã có đ thông tin
18Final Answer: câu tr li cui cùng
19
20Bt đu!
21
22Question: {input}
23Thought: {agent_scratchpad}
24""")

Step 3: Create Agent

Python
1from langchain.agents import create_react_agent, AgentExecutor
2from langchain_openai import ChatOpenAI
3
4# Initialize LLM
5llm = ChatOpenAI(model="gpt-4o", temperature=0)
6
7# Create ReAct agent
8agent = create_react_agent(
9 llm=llm,
10 tools=tools,
11 prompt=react_prompt
12)
13
14# Create executor
15agent_executor = AgentExecutor(
16 agent=agent,
17 tools=tools,
18 verbose=True, # Xem chi tiết reasoning
19 max_iterations=5, # Giới hạn số bước
20 handle_parsing_errors=True
21)

Step 4: Run Agent

Python
1# Test agent
2response = agent_executor.invoke({
3 "input": "Thời tiết Hà Nội hôm nay thế nào? Nếu nhiệt độ > 30 thì tính 30 * 1.5"
4})
5
6print(response["output"])

Output (verbose=True):

Text
1> Entering new AgentExecutor chain...
2
3Thought: Tôi cần kiểm tra thời tiết Hà Nội trước,
4sau đó quyết định có cần tính toán không.
5
6Action: get_weather[Hanoi]
7Observation: {"city": "Hanoi", "temperature": 25, "condition": "Sunny", "humidity": 65}
8
9Thought: Nhiệt độ là 25°C, không lớn hơn 30,
10nên không cần tính toán. Tôi có thể trả lời.
11
12Final Answer: Thời tiết Hà Nội hôm nay nắng đẹp,
13nhiệt độ 25°C, độ ẩm 65%. Vì nhiệt độ không vượt quá
1430°C nên không cần thực hiện phép tính.
15
16> Finished chain.

ReAct với LangGraph

LangGraph cung cấp cách build ReAct agents linh hoạt hơn:

Python
1from langgraph.prebuilt import create_react_agent
2from langchain_openai import ChatOpenAI
3
4# Cách đơn giản với LangGraph
5llm = ChatOpenAI(model="gpt-4o")
6
7graph = create_react_agent(
8 model=llm,
9 tools=tools,
10 state_modifier="Bạn là AI assistant thông minh."
11)
12
13# Invoke
14result = graph.invoke({
15 "messages": [("user", "Tìm thông tin về Python 3.12")]
16})

Advanced: Custom ReAct Logic

Khi cần control flow phức tạp hơn:

Python
1from langgraph.graph import StateGraph, END
2from typing import TypedDict, Annotated
3import operator
4
5class AgentState(TypedDict):
6 messages: Annotated[list, operator.add]
7 next_step: str
8
9def reasoning_node(state: AgentState):
10 """LLM reasoning step"""
11 messages = state["messages"]
12 response = llm.invoke(messages)
13
14 if "FINAL ANSWER" in response.content:
15 return {"messages": [response], "next_step": "end"}
16 else:
17 return {"messages": [response], "next_step": "action"}
18
19def action_node(state: AgentState):
20 """Execute tool based on LLM decision"""
21 last_message = state["messages"][-1]
22 tool_call = parse_tool_call(last_message)
23
24 result = execute_tool(tool_call)
25
26 return {
27 "messages": [f"Observation: {result}"],
28 "next_step": "reasoning"
29 }
30
31def route(state: AgentState):
32 return state["next_step"]
33
34# Build graph
35workflow = StateGraph(AgentState)
36workflow.add_node("reasoning", reasoning_node)
37workflow.add_node("action", action_node)
38
39workflow.add_conditional_edges(
40 "reasoning",
41 route,
42 {
43 "action": "action",
44 "end": END
45 }
46)
47workflow.add_edge("action", "reasoning")
48
49workflow.set_entry_point("reasoning")
50graph = workflow.compile()

Best Practices cho ReAct

1. Tool Descriptions rõ ràng

Python
1@tool
2def search_products(query: str, category: str = None, max_price: float = None) -> str:
3 """
4 Tìm kiếm sn phm trong database.
5
6 QUAN TRNG: Ch s dng tool này khi user hi v sn phm.
7
8 Args:
9 query: T khóa tìm kiếm (bt buc)
10 category: Danh mc sn phm (optional): electronics, clothing, books
11 max_price: Giá ti đa (optional)
12
13 Returns:
14 Danh sách sn phm phù hp dng JSON
15
16 Examples:
17 - search_products("laptop gaming")
18 - search_products("áo", category="clothing", max_price=500000)
19 """
20 # Implementation

2. Handle Errors gracefully

Python
1agent_executor = AgentExecutor(
2 agent=agent,
3 tools=tools,
4 handle_parsing_errors=True, # Tự động retry khi parse error
5 max_iterations=5, # Tránh infinite loops
6 early_stopping_method="generate" # Tạo response khi max iterations
7)

3. Add Fallbacks

Python
1from langchain_core.runnables import RunnableWithFallbacks
2
3# Fallback khi GPT-4 fail
4agent_with_fallback = agent_executor.with_fallbacks([
5 fallback_agent_executor # Dùng GPT-3.5 như backup
6])

Bài tập thực hành

Hands-on Exercise

Build một ReAct Agent đơn giản:

  1. Tạo 3 tools:

    • get_current_time(): Trả về thời gian hiện tại
    • search_wikipedia(query): Tìm trên Wikipedia
    • calculate(expression): Tính toán
  2. Create ReAct agent với LangChain

  3. Test với các câu hỏi:

    • "Mấy giờ rồi?"
    • "Albert Einstein sinh năm nào?"
    • "Tính 15% của 2000"
    • "Einstein sống được bao nhiêu tuổi?" (cần combine tools)
Python
1# Starter code
2from langchain.tools import tool
3from langchain_openai import ChatOpenAI
4from langchain.agents import create_react_agent, AgentExecutor
5
6# TODO: Define your tools
7@tool
8def get_current_time() -> str:
9 """Lấy thời gian hiện tại"""
10 from datetime import datetime
11 return datetime.now().strftime("%Y-%m-%d %H:%M:%S")
12
13# TODO: Add more tools
14
15# TODO: Create agent
16
17# TODO: Test agent

Tiếp theo

Trong bài tiếp theo, chúng ta sẽ học về LangGraph Basics - framework mạnh mẽ để build complex agent workflows.


Tài liệu tham khảo