에이전트 메모리 관리

Hugging Face smolagents로 AI 에이전트 만들기

Adel Nehme

VP of AI Curriculum, DataCamp

Hugging Face smolagents로 AI 에이전트 만들기

기본값은 상태 없음

.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?
Hugging Face smolagents로 AI 에이전트 만들기

상호작용 간 메모리 유지

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
...
Hugging Face smolagents로 AI 에이전트 만들기

메모리는 디버깅에도 도움됨

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

에이전트 실행에서 무슨 일이 있었는지 확인하십시오:

  • 에이전트가 생성한 모든 코드 검토
  • 추론, 동작, 도구 사용 추적
  • 오답이나 깨진 로직 디버깅
Hugging Face smolagents로 AI 에이전트 만들기

에이전트가 실행한 코드는?

.return_full_code() 메서드로 실행된 전체 코드를 볼 수 있습니다.

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

salary = 80000  # <- hardcoded?

# script continues...
Hugging Face smolagents로 AI 에이전트 만들기

에이전트의 생각은?

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},
  ...
}
Hugging Face smolagents로 AI 에이전트 만들기

분석을 위한 세션 저장

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)

로그는 다음에 유용합니다:

  • 사후 분석
  • 회귀 테스트
  • 시간 경과에 따른 에이전트 개선
Hugging Face smolagents로 AI 에이전트 만들기

에이전트 실패 수정: 무엇을 조정할까

  • 메모리 문제: reset=False 사용 또는 의도적으로 리셋
  • 추론 문제: 더 강한 모델 시도
  • 행동 일관성 부족: 시스템 프롬프트 개선
  • 도구 혼선: 도구 도크스트링 명확화
Hugging Face smolagents로 AI 에이전트 만들기

연습해 봅시다!

Hugging Face smolagents로 AI 에이전트 만들기

Preparing Video For Download...