RAG Refresher

AI Agents with Hugging Face smolagents

Adel Nehme

VP of AI Curriculum, DataCamp

Example: A Smart Cooking Assistant

  • Needs access to culinary knowledge
  • Info is scattered across recipes, guides, and planning resources
  • Must search and extract only the most relevant details
AI Agents with Hugging Face smolagents

What is Retrieval Augmented Generation (RAG)?

RAG = Combine information retrieval with LLM generation

AI Agents with Hugging Face smolagents

The RAG Workflow

AI Agents with Hugging Face smolagents

Loading and Splitting Documents

from langchain_community.document_loaders import PyPDFDirectoryLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter

# Load documentation from directory
loader = PyPDFDirectoryLoader("cooking_docs", mode="single")
documents = loader.load()

# Split into chunks
splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,
    chunk_overlap=200
)
chunks = splitter.split_documents(documents)
AI Agents with Hugging Face smolagents

Creating a Vector Store

from langchain_huggingface import HuggingFaceEndpointEmbeddings
from langchain_community.vectorstores import FAISS

# Create embeddings and vector store
embedder = HuggingFaceEndpointEmbeddings(
    model="BAAI/bge-base-en-v1.5",
    task="feature-extraction",
)
vector_store = FAISS.from_documents(chunks, embedder)
AI Agents with Hugging Face smolagents

Querying the Vector Store

query = "How do I cook salmon with herbs?"

# Similarity search
relevant_docs = vector_store.similarity_search(query, k=3)

# Create a context string
context = "\n\n".join(doc.page_content for doc in relevant_docs)
AI Agents with Hugging Face smolagents

Query: How do I cook salmon with herbs?

Top 2 retrieved chunks (semantic matches):

[1] Salmon preparation basics (p. 2) Pat the salmon dry to ensure browning. Season generously with salt, pepper, and fresh dill or parsley. Let the fillets rest 10 minutes so the salt penetrates. For even cooking, bring to room temperature...

[2] Baking salmon in the oven (p. 5) Preheat oven to 200°C (392°F). Place fillets on a parchment-lined tray, top with lemon slices and herb butter (dill/parsley). Bake 10 to 12 minutes until just opaque and flaky; rest 2 minutes before serving...

AI Agents with Hugging Face smolagents

Traditional RAG Pipeline Limitations

How do I plan a week of meals under $50 while meeting all nutritional requirements?

Answer is spread across docs (budget, nutrition, techniques, recipes).

AI Agents with Hugging Face smolagents

Let's practice!

AI Agents with Hugging Face smolagents

Preparing Video For Download...