Gestionarea memoriei agentului

Agenți AI cu Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agenți AI cu Hugging Face smolagents

Fără stare în mod implicit

Fiecare apel .run() pornește de la zero.

  • Exemplu:
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?
Agenți AI cu Hugging Face smolagents

Păstrarea memoriei între interacțiuni

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.
  • Transmiteți 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
...
Agenți AI cu Hugging Face smolagents

Memoria ajută și la depanare

Utilizator: Care este salariul estimat?
Agent: Este 80.000 $
Utilizator: Stai, asta pare greșit...
Agent: Îmi pare rău, nu înțeleg la ce vă referiți

Inspectați ce s-a întâmplat în rularea agentului:

  • Revizuirea întregului cod generat de agent
  • Urmărirea raționamentului, acțiunilor și instrumentelor utilizate
  • Depanarea răspunsurilor incorecte sau a logicii defectuoase
Agenți AI cu Hugging Face smolagents

Ce cod a rulat agentul?

Metoda .return_full_code() afișează tot codul executat.

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

salary = 80000  # <- hardcoded?

# script continues...
Agenți AI cu Hugging Face smolagents

Ce gândea agentul?

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},
  ...
}
Agenți AI cu Hugging Face smolagents

Salvarea sesiunilor agentului pentru analiză

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)

Jurnalele sunt utile pentru:

  • Analiza post-execuție
  • Testare de regresie
  • Îmbunătățirea comportamentului agentului
Agenți AI cu Hugging Face smolagents

Corectarea erorilor agentului: ce să ajustați

  • Probleme de memorie: Folosiți reset=False sau resetați intenționat
  • Probleme de raționament: Încercați un model mai performant
  • Comportament inconsistent: Îmbunătățiți promptul de sistem
  • Confuzie cu instrumentele: Clarificați docstring-urile instrumentelor
Agenți AI cu Hugging Face smolagents

Să exersăm!

Agenți AI cu Hugging Face smolagents

Preparing Video For Download...