Lý thuyết
25 phút
Bài 1/5

AI Agents là gì?

Tìm hiểu về AI Agents - hệ thống AI tự động có khả năng reasoning và thực thi tasks

🤖 AI Agents là gì?

AI Agents đang định hình tương lai của AI applications. Trong bài này, chúng ta sẽ tìm hiểu AI Agents là gì, tại sao chúng quan trọng, và cách chúng hoạt động.

Định nghĩa AI Agent

AI Agent Definition

AI Agent là một hệ thống AI có khả năng:

  • Nhận thức (Perceive) môi trường
  • Suy luận (Reason) để đưa ra quyết định
  • Hành động (Act) để đạt được mục tiêu
  • Học hỏi (Learn) từ kết quả
Diagram
graph LR
    E[Environment] -->|Observe| A[Agent]
    A -->|Reason| A
    A -->|Act| E
    A -->|Learn| A

So sánh: Chatbot vs AI Agent

Đặc điểmChatbot truyền thốngAI Agent
Input/OutputText → TextGoal → Actions → Result
AutonomyReactiveProactive
PlanningKhông
Tool UseGiới hạnĐa dạng
MemorySession-basedLong-term
Self-correctionKhông

Các thành phần của AI Agent

1. LLM Brain (Bộ não)

LLM đóng vai trò là "bộ não" của agent, chịu trách nhiệm:

  • Hiểu instructions và context
  • Reasoning để đưa ra plan
  • Quyết định action tiếp theo
  • Tổng hợp kết quả
Python
1from langchain_openai import ChatOpenAI
2
3# LLM làm brain cho agent
4llm = ChatOpenAI(
5 model="gpt-4o",
6 temperature=0 # Deterministic cho reasoning
7)

2. Tools (Công cụ)

Tools cho phép agent tương tác với external systems:

Python
1from langchain.tools import tool
2
3@tool
4def search_web(query: str) -> str:
5 """Tìm kiếm thông tin trên internet"""
6 # Implementation
7 return search_results
8
9@tool
10def send_email(to: str, subject: str, body: str) -> str:
11 """Gửi email đến người nhận"""
12 # Implementation
13 return "Email sent successfully"
14
15@tool
16def query_database(sql: str) -> str:
17 """Truy vấn database"""
18 # Implementation
19 return query_results

3. Memory (Bộ nhớ)

Agent cần memory để:

  • Nhớ conversation history
  • Lưu trữ learned knowledge
  • Track progress on tasks
Python
1from langchain.memory import ConversationBufferMemory
2
3memory = ConversationBufferMemory(
4 memory_key="chat_history",
5 return_messages=True
6)

4. Planning (Lập kế hoạch)

Agent có thể plan multi-step tasks:

Diagram
graph TD
    G[Goal: Book a trip to Tokyo] --> P1[Plan Steps]
    P1 --> S1[1. Search flights]
    P1 --> S2[2. Compare prices]
    P1 --> S3[3. Check hotels]
    P1 --> S4[4. Book best option]
    S1 --> E1[Execute]
    S2 --> E2[Execute]
    S3 --> E3[Execute]
    S4 --> E4[Execute]
    E4 --> R[Result: Trip booked!]

Agent Patterns phổ biến

ReAct Pattern (Reasoning + Acting)

Pattern phổ biến nhất cho AI Agents:

Text
1Thought: Tôi cần tìm giá vé máy bay đến Tokyo
2Action: search_flights("HAN", "TYO", "2024-03-01")
3Observation: Found 5 flights, cheapest $450
4
5Thought: $450 có vẻ hợp lý, kiểm tra khách sạn
6Action: search_hotels("Tokyo", "2024-03-01", "2024-03-05")
7Observation: Found 20 hotels, avg $100/night
8
9Thought: Đã có đủ thông tin, tổng hợp kết quả
10Action: final_answer("Tìm thấy vé máy bay $450 và khách sạn $100/đêm...")

Plan-and-Execute Pattern

Phù hợp cho complex, long-running tasks:

Python
1# Step 1: Create plan
2plan = planner.plan(
3 "Research and write a report about AI trends 2024"
4)
5# Output: ["Search AI news", "Analyze trends", "Write outline", "Draft report", "Review"]
6
7# Step 2: Execute each step
8for step in plan:
9 result = executor.execute(step)
10 plan = replanner.update_plan(plan, result)

Ví dụ thực tế: Customer Support Agent

Python
1from langchain.agents import create_react_agent
2from langchain_openai import ChatOpenAI
3
4# Define tools
5tools = [
6 search_knowledge_base, # Tìm trong FAQ
7 get_order_status, # Kiểm tra đơn hàng
8 create_support_ticket, # Tạo ticket hỗ trợ
9 send_email, # Gửi email
10]
11
12# Create agent
13agent = create_react_agent(
14 llm=ChatOpenAI(model="gpt-4o"),
15 tools=tools,
16 prompt=customer_support_prompt
17)
18
19# Example interaction
20response = agent.invoke({
21 "input": "Đơn hàng #12345 của tôi đang ở đâu?"
22})

Agent flow:

  1. 🤔 Agent reasoning: "Cần kiểm tra trạng thái đơn hàng"
  2. 🔧 Action: get_order_status("12345")
  3. 👁️ Observation: "Đơn hàng đang vận chuyển, dự kiến 2 ngày nữa"
  4. 💬 Response: "Đơn hàng #12345 đang trên đường vận chuyển..."

Limitations của AI Agents

Lưu ý quan trọng

AI Agents không phải là silver bullet. Cần hiểu limitations để sử dụng hiệu quả.

Challenges

  1. Hallucination - LLM có thể generate sai thông tin
  2. Error propagation - Sai lầm ở bước trước ảnh hưởng các bước sau
  3. Cost - Nhiều LLM calls = chi phí cao
  4. Latency - Multi-step reasoning cần thời gian
  5. Security - Agent có quyền execute actions → rủi ro

Best Practices

  • ✅ Human-in-the-loop cho critical actions
  • ✅ Validate outputs trước khi execute
  • ✅ Implement rate limits và safeguards
  • ✅ Log mọi actions để audit
  • ✅ Start simple, gradually increase autonomy

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

Hands-on Exercise

Bài tập: Thiết kế một AI Agent cho use case của bạn

  1. Xác định goal: Agent cần giải quyết vấn đề gì?
  2. List tools: Những tools nào agent cần?
  3. Design flow: Vẽ diagram cho agent workflow
  4. Identify risks: Những gì có thể sai?
  5. Plan safeguards: Làm sao để giảm thiểu risks?

Tiếp theo

Trong bài tiếp theo, chúng ta sẽ tìm hiểu về ReAct Pattern - framework phổ biến nhất để build AI Agents có khả năng reasoning và acting.


Tài liệu tham khảo