Introduction to Agentic RAG

AI Agents with Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Agentic RAG: Iterative Retrieval + Reasoning

AI Agents with Hugging Face smolagents

Stateless vs. Stateful Tools in smolagents

Function Tools (@tool decorator)

  • Stateless: no memory between calls
  • Can't remember your vector store between calls

Class-Based Tools (Tool subclass)

  • Stateful: keep references across calls
  • Store complex objects
AI Agents with Hugging Face smolagents

The Anatomy of a Class-Based Tool

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

Build a Recipe Search Tool

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

Cooking Assistant Agent: Putting It All Together

# 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

Agent Run Example

Query: How do I cook salmon with herbs using professional techniques?

[Step 1] Search "salmon herbs cooking techniques"...

[Step 2] Search "professional salmon preparation"...

[Final Answer]

"Pat fillets dry, season with herbs, and:

  • Pan-sear skin-side down, basting with butter, garlic, thyme
  • Or bake at 200°C with lemon & herb butter for 10 to 12 min"
AI Agents with Hugging Face smolagents

Let's practice!

AI Agents with Hugging Face smolagents

Preparing Video For Download...