Quản lý bộ nhớ của Agent

AI Agents với Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

AI Agents với Hugging Face smolagents

Mặc định không lưu trạng thái

Mỗi lần gọi .run() là một khởi đầu mới.

  • Ví dụ:
career_advisor.run("What career skills should I highlight?")
You should highlight Python, SQL, data visualization, 
machine learning fundamentals, and communication skills tailored to business outcomes.
career_advisor.run("Can you format those skills as bullet points?")
Sorry, I'm not sure which skills you're referring to. Could you clarify?
AI Agents với Hugging Face smolagents

Giữ bộ nhớ giữa các lượt tương tác

career_advisor.run("What career skills should I highlight?")
You should highlight Python, SQL, data visualization, 
machine learning fundamentals, and communication skills tailored to business outcomes.
  • Truyền reset=False:
career_advisor.run("Can you format those skills as bullet points?", reset=False)
Sure! Here are the skills as bullet points:
- Python
- SQL
- Data visualization
...
AI Agents với Hugging Face smolagents

Bộ nhớ cũng giúp bạn gỡ lỗi

User: What's the expected salary?
Agent: It's $80,000
User: Wait, that seems wrong...
Agent: Sorry, I'm not sure what you mean

Kiểm tra điều gì đã xảy ra trong lượt chạy của agent:

  • Xem lại toàn bộ mã agent tạo ra
  • Truy vết suy luận, hành động và công cụ đã dùng
  • Gỡ lỗi câu trả lời sai hoặc logic hỏng
AI Agents với Hugging Face smolagents

Agent đã chạy mã gì?

Phương thức .return_full_code() cho phép bạn xem toàn bộ mã đã chạy.

executed_code = career_advisor.memory.return_full_code()
print(executed_code)
# ...các bước khác lược bớt

salary = 80000  # <- gán cứng?

# script tiếp tục...
AI Agents với Hugging Face smolagents

Agent đã suy nghĩ gì?

conversation_steps = career_advisor.memory.get_succinct_steps()
print(conversation_steps[5])
{
  "step_number": 5,
  "tool_calls": [
    {"function": {"name": "python_interpreter"}},
    {"function": {"name": "web_search"}}
  ],
  "code_action": "import requests\nskills = requests.get('api.jobsearch.com').json()",
  "observations": "resume_agent tìm thấy 15 kỹ năng phù hợp để chuyển đổi",
  "token_usage": {"total_tokens": 334},
  ...
}
AI Agents với Hugging Face smolagents

Lưu phiên agent để phân tích

import json

def save_agent_memory(agent):
    with open("agent_memory.json", "w") as f:
        json.dump(agent.memory.get_succinct_steps(), f, indent=2, default=str)

# Lưu bộ nhớ ra tệp
save_agent_memory(career_advisor)

Log hữu ích cho:

  • Phân tích hậu kiểm
  • Kiểm thử hồi quy
  • Cải thiện hành vi agent theo thời gian
AI Agents với Hugging Face smolagents

Sửa lỗi agent: Điều chỉnh gì

  • Vấn đề bộ nhớ: Dùng reset=False hoặc đặt lại có chủ đích
  • Vấn đề suy luận: Thử mô hình mạnh hơn
  • Hành vi không ổn định: Cải thiện system prompt
  • Nhầm lẫn công cụ: Làm rõ docstring của công cụ
AI Agents với Hugging Face smolagents

Ayo berlatih!

AI Agents với Hugging Face smolagents

Preparing Video For Download...