Вступ до Agentic RAG

AI-агенти з Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agentic RAG: ітеративний пошук + міркування

AI-агенти з Hugging Face smolagents

Безстанні vs. станні інструменти в smolagents

Функції-інструменти (декоратор @tool)

  • Без стану: немає пам'яті між викликами
  • Не пам'ятають ваш vector store між викликами

Інструменти на основі класів (підклас Tool)

  • Зі станом: зберігають посилання між викликами
  • Можуть зберігати складні об'єкти
AI-агенти з Hugging Face smolagents

Будова інструмента на основі класу

from smolagents import Tool

class ToolName(Tool):
    name = "tool_name"

description = "Clear description for the agent"
inputs = { "parameter_name": {"type": "parameter_type", "description": "Parameter purpose"} } output_type = "string"
def __init__(self, custom_parameters): super().__init__() self.custom_attribute = custom_parameters
def forward(self, parameter_name): # Agent calls this method return "processed output"
AI-агенти з Hugging Face smolagents

Створення інструмента пошуку рецептів

class RecipeSearchTool(Tool):
    name = "recipe_search" 
    description = "Search cooking documentation for recipes, techniques, and meal planning information"
    inputs = {
        "query": {"type": "string", "description": "Natural language cooking query"}
    }
    output_type = "string"

def __init__(self, vectorstore, k=6): super().__init__() self.vectorstore = vectorstore self.k = k
def forward(self, query): docs = self.vectorstore.similarity_search(query, k=self.k) return "\n\n".join(doc.page_content for doc in docs) or "Nothing found."
AI-агенти з Hugging Face smolagents

Агент-кухар: об'єднуємо все докупи

# Initialize the retrieval tool
recipe_search = RecipeSearchTool(vector_store)

agent = CodeAgent(
    tools=[recipe_search],
    model=model,
    instructions="Search thoroughly to provide complete recipe answers. 
      If initial results seem incomplete, try different search terms.",
    verbosity_level=1,
    max_steps=8
)
AI-агенти з Hugging Face smolagents

Приклад запуску агента

Запит: Як приготувати лосося з травами професійними техніками?

[Крок 1] Пошук «salmon herbs cooking techniques»...

[Крок 2] Пошук «professional salmon preparation»...

[Фінальна відповідь]

"Промокніть філе насухо, приправте травами, і:

  • Обсмажте шкірою донизу, поливаючи вершковим маслом, часником, чебрецем
  • Або запікайте за 200°C з лимоном і трав'яним маслом 10–12 хв"
AI-агенти з Hugging Face smolagents

Давайте потренуємось!

AI-агенти з Hugging Face smolagents

Preparing Video For Download...