การจัดการหน่วยความจำของ Agent

AI Agents ด้วย Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

AI Agents ด้วย Hugging Face smolagents

ไม่มีสถานะโดยค่าเริ่มต้น

การเรียก .run() แต่ละครั้งเริ่มต้นใหม่เสมอ

  • ตัวอย่าง:
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 ด้วย Hugging Face smolagents

การรักษาหน่วยความจำระหว่างการโต้ตอบ

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.
  • ส่ง 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 ด้วย Hugging Face smolagents

หน่วยความจำช่วยในการดีบักด้วย

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

ตรวจสอบสิ่งที่เกิดขึ้นในการรันของ agent:

  • ดูโค้ดทั้งหมดที่ agent สร้างขึ้น
  • ติดตามการใช้เหตุผล การดำเนินการ และการใช้เครื่องมือ
  • แก้ไขคำตอบที่ผิดหรือตรรกะที่ขัดข้อง
AI Agents ด้วย Hugging Face smolagents

Agent รันโค้ดอะไรบ้าง?

เมธอด .return_full_code() ให้ดูโค้ดทั้งหมดที่ถูกรัน

executed_code = career_advisor.memory.return_full_code()
print(executed_code)
# ...other steps omitted for brevity

salary = 80000  # <- hardcoded?

# script continues...
AI Agents ด้วย Hugging Face smolagents

Agent คิดอะไรอยู่?

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 found 15 relevant skills for transition",
  "token_usage": {"total_tokens": 334},
  ...
}
AI Agents ด้วย Hugging Face smolagents

บันทึก Session ของ Agent เพื่อวิเคราะห์

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)

# Save memory to a file
save_agent_memory(career_advisor)

ล็อกช่วยได้ในด้านต่างๆ:

  • การวิเคราะห์ย้อนหลัง
  • การทดสอบ regression
  • การปรับปรุงพฤติกรรมของ agent ในระยะยาว
AI Agents ด้วย Hugging Face smolagents

แก้ไขความล้มเหลวของ Agent: ควรปรับอะไร

  • ปัญหาหน่วยความจำ: ใช้ reset=False หรือรีเซ็ตอย่างตั้งใจ
  • ปัญหาการใช้เหตุผล: ลองใช้โมเดลที่แข็งแกร่งขึ้น
  • พฤติกรรมไม่สม่ำเสมอ: ปรับปรุง system prompt
  • ความสับสนของเครื่องมือ: ชี้แจง docstring ของเครื่องมือ
AI Agents ด้วย Hugging Face smolagents

มาฝึกกันเถอะ!

AI Agents ด้วย Hugging Face smolagents

Preparing Video For Download...