Get AI summaries of any video or article — Sign up free
Complete RAG Crash Course With Langchain In 2 Hours thumbnail

Complete RAG Crash Course With Langchain In 2 Hours

Krish Naik·
5 min read

Based on Krish Naik's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

RAG improves LLM responses by retrieving relevant passages from an external knowledge base and injecting them into the prompt, avoiding expensive retraining.

Briefing

Retrieval-Augmented Generation (RAG) is presented as a practical way to make large language models answer with up-to-date, domain-specific accuracy—without retraining. The core fix targets two common failures of “prompt-only” LLM use: hallucinations when the model lacks knowledge of recent or internal events, and the high cost of fine-tuning when a company’s private documents (policies, HR/finance rules, manuals) keep changing. RAG solves both by routing each user query through an external knowledge base built from the company’s documents, then feeding the retrieved passages back into the LLM as context.

The crash course breaks RAG into two pipelines. First is the data injection pipeline: documents are loaded from multiple formats (PDF, HTML, Excel/CSV, SQL/unstructured), parsed, chunked into smaller pieces, converted into embeddings, and stored in a vector database (vector store). Chunking is treated as a make-or-break step because embedding models and LLMs have fixed context windows; large documents must be split so each chunk fits within those limits. Embeddings turn text into numerical vectors so similarity search can retrieve the most relevant chunks using distance metrics (e.g., cosine similarity).

Second is the retrieval pipeline, which performs “retrieval augmented generation.” For each new query, the system embeds the query, runs similarity search against the vector store, and collects the top-matching chunks as context. A prompt then instructs the LLM to answer using that context, producing a grounded response. Hallucination is not eliminated entirely, but the model is constrained to evidence from the retrieved passages; if the answer isn’t present in the vector store, the LLM can still generate something unsupported.

After laying out the conceptual flow, the session moves into implementation with LangChain-style document handling and modular Python code. It demonstrates LangChain’s Document structure—page_content plus metadata—and shows how loaders (e.g., text loader, directory loader, PyMuPDF/PyPDF-based PDF loaders) convert raw files into that structure. It then implements chunking using a recursive character text splitter with configurable chunk_size and chunk_overlap, producing hundreds of chunks from a small set of PDFs.

Embeddings are implemented with an open-source Hugging Face sentence-transformers model (all-MiniLM-L6-v2, yielding 384-dimensional vectors). For storage, the course uses a persistent vector database approach (ChromaDB in the earlier notebook-style build, then a more self-contained pipeline using a Firestore-like local persistence with pickled index/metadata). A retriever class is built to embed queries, query the vector store, compute similarity scores from returned distances, apply a score threshold, and return retrieved documents plus metadata.

Finally, the course integrates an LLM for generation using Grok via LangChain (ChatGrok with a Grok API key). It builds three RAG variants: a simple pipeline that retrieves context and asks the LLM to answer; an enhanced pipeline that returns sources, page numbers, confidence scores, and optional context; and a more advanced version adding streaming, citation, history, and summarization. The concluding section refactors the notebook into a modular project structure (data loader, embedding pipeline, vector store, search) so the full RAG workflow can be reused and deployed more cleanly in real applications.

Cornell Notes

RAG is framed as a cost-effective alternative to fine-tuning: it improves LLM answers by retrieving relevant passages from an external knowledge base and supplying them as context. The course splits RAG into two pipelines—data injection (load → parse → chunk → embed → store in a vector DB) and retrieval augmented generation (embed the query → similarity search → prompt the LLM with retrieved context). Chunking is emphasized because both embedding models and LLMs have fixed context limits, so large documents must be divided with overlap. Implementation uses LangChain Document objects (page_content + metadata), an open-source embedding model (all-MiniLM-L6-v2), and a persistent vector store. It then integrates Grok (ChatGrok) to generate answers, with progressively richer outputs like sources, confidence, streaming, citations, history, and summarization.

Why does RAG reduce hallucinations compared with using an LLM alone?

An LLM trained on a fixed dataset can lack knowledge about events after its training cutoff, so it may fabricate plausible-sounding answers. RAG embeds the user query, retrieves the most similar chunks from an external vector database built from internal documents, and passes those retrieved passages into the LLM prompt as context. The model is therefore grounded in retrieved evidence; hallucinations can still occur when relevant content is missing from the vector store, but the system strongly biases responses toward the company’s documents.

What exactly happens in the data injection pipeline?

Documents are loaded from various formats (the course demonstrates text and PDFs). They are converted into LangChain Document objects with page_content and metadata, then chunked using a recursive character text splitter with parameters like chunk_size and chunk_overlap. Each chunk is embedded into vectors using an embedding model (all-MiniLM-L6-v2), and the vectors plus metadata are stored in a persistent vector store (e.g., ChromaDB in the earlier build, and a local persistent index/metadata approach later).

How does chunking relate to embedding and LLM context limits?

Embedding models and LLMs accept only up to a fixed context window. If a 100-page PDF is embedded or processed as one block, it will exceed those limits. Chunking divides content into smaller pieces that fit within the model’s maximum input size. Overlap (chunk_overlap) helps preserve continuity across boundaries so retrieval can still find relevant information even when it spans two chunks.

How does the retriever compute relevance and filter results?

The retriever embeds the incoming query, queries the vector store for top results, and receives documents plus distance/similarity-related fields. It converts distance into a similarity score (the course uses a 1 - distance style computation) and applies a score threshold. Only chunks whose similarity score exceeds the threshold are returned as context, along with metadata such as source file and page number.

What changes between the simple, enhanced, and advanced RAG pipelines?

The simple pipeline retrieves context and asks the LLM to answer using that context. The enhanced pipeline adds structured outputs: sources, page numbers, confidence scores, and optionally previewed context. The advanced pipeline further layers capabilities like streaming responses, citations, conversation history, and summarization, while still relying on the same retrieval step to ground answers.

Why refactor the notebook into modular code (data loader, embedding, vector store, search)?

The course moves from a single notebook to a reusable pipeline structure. A data loader module handles reading and converting files into Document objects; an embedding pipeline handles chunking and embedding; a vector store module persists and reloads embeddings; and a search module ties retrieval to LLM generation. This separation makes it easier to swap components (different loaders, embedding models, or vector stores) and deploy the workflow in company use cases.

Review Questions

  1. What two pipelines make up the RAG workflow, and what is the purpose of each?
  2. How do chunk_size and chunk_overlap affect retrieval quality and why?
  3. In the retriever, how is similarity score derived from vector store results, and how does the score threshold change outputs?

Key Points

  1. 1

    RAG improves LLM responses by retrieving relevant passages from an external knowledge base and injecting them into the prompt, avoiding expensive retraining.

  2. 2

    The data injection pipeline is load → parse/convert to Document objects → chunk → embed → store vectors in a persistent vector database.

  3. 3

    Chunking is essential because embedding models and LLMs have fixed context windows; overlap helps preserve meaning across chunk boundaries.

  4. 4

    Similarity search works by embedding both documents and queries into the same vector space, then ranking by distance/similarity metrics.

  5. 5

    A retriever should return not just text context but also metadata (e.g., source file and page number) to support citations and debugging.

  6. 6

    Integrating an LLM (e.g., ChatGrok) turns retrieved context into grounded answers via prompt instructions to use the provided context.

  7. 7

    Modularizing the pipeline (data loader, embedding pipeline, vector store, search) makes RAG easier to maintain, test, and deploy.

Highlights

RAG is positioned as a practical alternative to fine-tuning for private, frequently updated company knowledge—by rebuilding embeddings and vector indexes instead of retraining the LLM.
LangChain Document objects (page_content + metadata) are treated as the central data structure that enables consistent chunking, embedding, and filtered retrieval.
The course demonstrates a full end-to-end loop: embed documents into vectors, persist them, embed each new query, retrieve top chunks, then prompt Grok to generate grounded answers.
Enhanced and advanced RAG variants add operational features—sources, confidence scoring, streaming, citations, history, and summarization—while keeping retrieval as the grounding mechanism.

Topics

Mentioned