エージェントメモリの管理

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 エージェント

メモリはデバッグにも役立つ

ユーザー: 想定給与はいくらですか?
エージェント: 80,000ドルです
ユーザー: それは違うような気がします...
エージェント: ご質問の意図が分かりかねます

エージェントの実行内容を調査する:

  • 生成されたコードの全体確認
  • 推論・アクション・ツール使用の追跡
  • 誤った回答やロジックエラーのデバッグ
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 を使用するか、意図的にリセットする
  • 推論の問題: より高性能なモデルを試す
  • 動作の不安定: システムプロンプトを改善する
  • ツールの混乱: ツールのdocstringを明確にする
Hugging Face smolagents で学ぶ AI エージェント

練習しましょう!

Hugging Face smolagents で学ぶ AI エージェント

Preparing Video For Download...