Správa paměti agenta

AI Agents with Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

AI Agents with Hugging Face smolagents

Bezstavový režim jako výchozí

Každé volání .run() začíná od nuly.

  • Příklad:
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 with Hugging Face smolagents

Uchování paměti mezi interakcemi

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.
  • Předejte 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 with Hugging Face smolagents

Paměť pomáhá i při ladění

Uživatel: Jaký je očekávaný plat?
Agent: Je to 80 000 $
Uživatel: Počkejte, to se zdá špatně...
Agent: Omlouvám se, nejsem si jistý, co tím myslíte

Prozkoumejte, co se stalo v průběhu běhu agenta:

  • Kontrola veškerého kódu, který agent vygeneroval
  • Sledování uvažování, akcí a využití nástrojů
  • Ladění nesprávných odpovědí nebo chybné logiky
AI Agents with Hugging Face smolagents

Jaký kód agent spustil?

Metoda .return_full_code() zobrazí veškerý spuštěný kód.

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

salary = 80000  # <- hardcoded?

# script continues...
AI Agents with Hugging Face smolagents

Co agent přemýšlel?

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 with Hugging Face smolagents

Ukládání relací agenta pro analýzu

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)

Logy jsou užitečné pro:

  • Zpětnou analýzu
  • Regresní testování
  • Postupné zlepšování chování agenta
AI Agents with Hugging Face smolagents

Oprava selhání agenta: Co upravit

  • Problémy s pamětí: Použijte reset=False nebo resetujte záměrně
  • Chyby v uvažování: Zkuste silnější model
  • Nekonzistentní chování: Vylepšete systémový prompt
  • Záměna nástrojů: Upřesněte docstringy nástrojů
AI Agents with Hugging Face smolagents

Lass uns üben!

AI Agents with Hugging Face smolagents

Preparing Video For Download...