Úvod do Agentic RAG

AI Agents with Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agentic RAG: Iterativní získávání + uvažování

AI Agents with Hugging Face smolagents

Bezstavové vs. stavové nástroje v smolagents

Funkční nástroje (dekorátor @tool)

  • Bezstavové: bez paměti mezi voláními
  • Vektorové úložiště si mezi voláními nepamatují

Třídové nástroje (podtřída Tool)

  • Stavové: uchovávají reference mezi voláními
  • Ukládají složité objekty
AI Agents with Hugging Face smolagents

Anatomie třídového nástroje

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

Sestavení nástroje pro vyhledávání receptů

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

Kuchařský asistent: Vše dohromady

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

Příklad běhu agenta

Dotaz: Jak připravit lososa s bylinkami profesionálními technikami?

[Krok 1] Hledání „salmon herbs cooking techniques"...

[Krok 2] Hledání „professional salmon preparation"...

[Výsledná odpověď]

„Filety osušte, okořeňte bylinkami a:

  • Opečte kůží dolů, přelévejte máslem, česnekem a tymiánem
  • Nebo pečte při 200 °C s citronem a bylinkový máslem 10–12 min"
AI Agents with Hugging Face smolagents

Pojďme si procvičit!

AI Agents with Hugging Face smolagents

Preparing Video For Download...