Введение в агентный RAG

ИИ-агенты с Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Агентный RAG: итеративное извлечение + рассуждение

ИИ-агенты с Hugging Face smolagents

Инструменты без состояния и с состоянием в smolagents

Функциональные инструменты (декоратор @tool)

  • Без состояния: нет памяти между вызовами
  • Не помнят векторное хранилище между вызовами

Инструменты на основе классов (подкласс Tool)

  • С состоянием: сохраняют ссылки между вызовами
  • Хранят сложные объекты
ИИ-агенты с 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"
ИИ-агенты с 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."
ИИ-агенты с 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
)
ИИ-агенты с Hugging Face smolagents

Пример работы агента

Запрос: Как приготовить лосося с травами, используя профессиональные техники?

[Шаг 1] Поиск по запросу «salmon herbs cooking techniques»...

[Шаг 2] Поиск по запросу «professional salmon preparation»...

[Итоговый ответ]

«Обсушите филе, приправьте травами и:

  • обжарьте кожей вниз, поливая сливочным маслом с чесноком и тимьяном
  • или запекайте при 200°C с лимонным маслом и травами 10–12 мин»
ИИ-агенты с Hugging Face smolagents

Давайте потренируемся!

ИИ-агенты с Hugging Face smolagents

Preparing Video For Download...