Skip to main content
This guide demonstrates an end-to-end Retrieval-Augmented Generation (RAG) pipeline using a single notebook. You will load a PDF, split it into chunks, embed those chunks, index them in a vector database, create a retriever, stitch retrieved passages into context, and run a LangChain Expression Language (LCEL) chain that answers user questions strictly from the document content. This lesson connects document loaders, chunking strategy, embedding models, and vector stores to build a document-grounded Q&A assistant.

Key libraries and imports

Use the following imports in your notebook:

High-level workflow

  1. Load the PDF and split it into pages.
  2. Chunk pages into smaller passages to control context length.
  3. Embed chunks with an embeddings model.
  4. Index embeddings into a vector store (Chroma).
  5. Create a retriever from the vector store to fetch relevant passages at query time.
  6. Format retrieved passages into a single context string.
  7. Compose an LCEL chain: retriever -> formatter -> prompt -> LLM -> output parser.
  8. Ask questions; the chain returns answers grounded in the document.

Step-by-step implementation

1) Load the PDF and split into page documents

This produces a list of page-level Document objects that preserve page content and metadata.

2) Create chunks from the pages

Use chunking to ensure passages are short enough for the embeddings model and downstream LLM context. Adjust chunk_size and chunk_overlap based on your LLM’s context window and the granularity you need.

3) Initialize embeddings and index chunks into Chroma

Chroma stores vector representations for each chunk so you can retrieve the most relevant passages at query time.

4) Create a retriever from the vector store

The retriever provides a simple API to fetch top-k relevant documents for a user query.

5) Helper to format retrieved docs into a single context string

This helper concatenates retrieved passages into a single context block that will be passed to the prompt. You can extend this to include source citations or metadata.

6) Set up the LLM and prompt template

This prompt explicitly instructs the model to answer only from the provided context, reducing hallucinations.
Tip: You can expand the prompt to include explicit formatting requirements, a maximum answer length, or citation formatting (e.g., “Answer with the source page number in brackets after each sentence”).

7) Build the LCEL chain that connects retriever -> formatter -> prompt -> LLM -> output parser

The LCEL pipeline retrieves relevant chunks at runtime, formats them, fills the prompt template, calls the LLM, and parses the output into a string.
This runnable pipeline takes a question as input, runs retrieval, and returns a parsed string answer.
Warning: Retrieval quality depends on chunking strategy, embedding model, and vector store configuration. Also monitor API usage and costs when calling embedding and LLM endpoints.

8) Invoke the chain with user questions

Example 1 — ask about sick leaves:
Example 2 — ask about unpaid personal leave:
Example 3 — ask for the sick leave policy:
The image displays a section of a document outlining policies for paid vacation leave, paid sick leave, and unpaid personal leave, each with eligibility details and requirements for request and approval.
When run against the employee handbook, the chain retrieves relevant passages and returns factual answers extracted from the document text:

Quick reference: Steps & commands

Summary

  • Load PDFs with a document loader (PyPDFLoader) and split them into pages.
  • Chunk pages with a TextSplitter (RecursiveCharacterTextSplitter) for reliable retrieval.
  • Embed chunks using OpenAIEmbeddings and index them in Chroma.
  • Use the vector store’s retriever to fetch relevant passages at query time.
  • Format retrieved passages into a context string and pass it to the LLM via an LCEL chain so the LLM answers strictly from the document.
  • This RAG pattern reduces hallucinations and enables document-grounded chatbots. Extend it by adding a UI, supporting arbitrary PDF uploads, or integrating advanced retrieval (reranking, hybrid search) and QA techniques.

Watch Video