LangChain and Groq: Build a Python PDF RAG agent

Learn to build a PDF question-answering agent with local Hugging Face embeddings, Groq inference, and retrieval of three matching text chunks.

Player not loading? Watch on YouTube

This Python tutorial builds a PDF question-answering AI agent with LangChain and Groq. The example uses a 34-page document about artificial intelligence. Embeddings run locally, while the language model uses Groq's hosted API, so the demonstrated setup depends on an external inference service.

The walkthrough starts with package installation, a PDF, a Python script, and an API key in a .env file. PyPDFLoader reads the document, and RecursiveCharacterTextSplitter divides its text into overlapping chunks. The speaker explains how overlap preserves context across chunk boundaries.

HuggingFaceEmbeddings uses sentence-transformers/all-MiniLM-L6-v2 to generate vectors for an InMemoryVectorStore. The speaker notes that the first run downloads the embedding model and subsequent runs reuse it. A retrieval function searches for the three closest chunks and returns their text with source metadata. LangChain's tool decorator makes that function available to the agent.

The final section streams agent steps so viewers can inspect the question, tool call, retrieved context, and answer. The example asks what AI means according to the document. The walkthrough also fixes a string-formatting issue before running the script.