Zarządzanie pamięcią agenta

Agenty AI z Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agenty AI z Hugging Face smolagents

Domyślnie bezstanowy

Każde wywołanie .run() zaczyna od nowa.

  • Przykład:
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?
Agenty AI z Hugging Face smolagents

Zachowywanie pamięci między interakcjami

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.
  • Przekaż 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
...
Agenty AI z Hugging Face smolagents

Pamięć pomaga też w debugowaniu

Użytkownik: Jakie jest oczekiwane wynagrodzenie?
Agent: Wynosi 80 000 USD
Użytkownik: Poczekaj, to chyba błąd...
Agent: Przepraszam, nie rozumiem, o co chodzi

Sprawdź, co działo się podczas działania agenta:

  • Przegląd całego wygenerowanego kodu
  • Śledzenie rozumowania, działań i użycia narzędzi
  • Debugowanie błędnych odpowiedzi lub logiki
Agenty AI z Hugging Face smolagents

Jaki kod wykonał agent?

Metoda .return_full_code() pokazuje cały wykonany kod.

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

salary = 80000  # <- hardcoded?

# script continues...
Agenty AI z Hugging Face smolagents

Co myślał 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},
  ...
}
Agenty AI z Hugging Face smolagents

Zapisywanie sesji agenta do analizy

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)

Logi przydają się do:

  • Analizy post-hoc
  • Testów regresji
  • Ulepszania zachowania agenta
Agenty AI z Hugging Face smolagents

Naprawianie błędów agenta: co dostosować

  • Problemy z pamięcią: Użyj reset=False lub zresetuj ręcznie
  • Błędy rozumowania: Wypróbuj mocniejszy model
  • Niespójne działanie: Popraw prompt systemowy
  • Mylenie narzędzi: Doprecyzuj docstringi narzędzi
Agenty AI z Hugging Face smolagents

Czas na ćwiczenia!

Agenty AI z Hugging Face smolagents

Preparing Video For Download...