AI Agents with Hugging Face smolagents
Adel Nehme
VP of AI Curriculum, DataCamp
RAG = Combine information retrieval with LLM generation
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)
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)
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)
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...
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