AI Model Wiki

    Skip to main content
    10 min read
    Lesson 3 of 5

    Building a RAG Pipeline Step-by-Step

    From the course RAG & Knowledge Bases: Ground AI in Your Data

    Let's build a complete RAG pipeline from scratch. We'll cover each step with practical guidance and code concepts.

    Step 1: Document Ingestion

    First, collect and prepare your source documents:

    • PDFs — Extract text with libraries like PyPDF2, pdfplumber, or Unstructured
    • Web pages — Scrape with BeautifulSoup or use Firecrawl for AI-optimized extraction
    • Markdown/Text — Read directly
    • Databases — Export relevant records as text
    • APIs — Pull data from Confluence, Notion, Google Docs, etc.

    Tip: Clean your documents. Remove headers/footers, navigation elements, and boilerplate. Garbage in = garbage out.

    Step 2: Chunking

    Split documents into smaller pieces that fit in the LLM's context and contain focused information:

    Document: "Company Handbook" (50 pages)
        ↓ Split into chunks
    Chunk 1: "Vacation Policy" (300 tokens)
    Chunk 2: "Remote Work Guidelines" (400 tokens)
    Chunk 3: "Expense Reimbursement" (350 tokens)
    ... (150 more chunks)
    

    Chunking parameters to tune:

    • Chunk size — 200-1000 tokens (start with 500)
    • Chunk overlap — 50-200 tokens (start with 100)
    • Splitting method — By paragraph, by heading, by sentence, or recursive

    Step 3: Generate Embeddings

    Convert each chunk into a vector embedding:

    Chunk: "Employees receive 20 days PTO per year..."
        ↓ Embedding model
    Vector: [0.12, -0.45, 0.78, ..., 0.33] (1536 dimensions)
    

    Store the vector alongside metadata:

    • Source document name
    • Page number or section
    • Date of last update
    • Any relevant tags or categories

    Step 4: Store in Vector Database

    Insert all chunk vectors + metadata into your vector database. Most databases handle indexing automatically.

    Step 5: Query Pipeline

    When a user asks a question:

    1. Embed the query — Convert the user's question to a vector
    2. Search — Find the top K most similar chunks (K = 3-10)
    3. Filter — Optionally filter by metadata (date, source, category)
    4. Re-rank (optional) — Use a cross-encoder model to re-score results
    5. Construct prompt — Insert retrieved chunks into the system prompt
    6. Generate — Send to the LLM for answer generation

    Step 6: Prompt Construction

    A good RAG prompt template:

    System: You are a helpful assistant. Answer questions based ONLY
    on the provided context. If the context doesn't contain enough
    information, say "I don't have enough information to answer that."
    Always cite your sources.
    
    Context:
    [Retrieved Chunk 1 - Source: handbook.pdf, p.12]
    [Retrieved Chunk 2 - Source: policy-update.md]
    [Retrieved Chunk 3 - Source: faq.pdf, p.3]
    
    User: {user's question}
    

    Common Tools and Frameworks

    • LangChain — Full-featured Python/JS framework for RAG pipelines
    • LlamaIndex — Specialized for document indexing and retrieval
    • Haystack — Open-source NLP framework with RAG components
    • Vercel AI SDK — TypeScript-first AI framework with RAG support
    • Superbase pgvector — Combine your app database with vector search

    Quick Start: Minimum Viable RAG

    For a fast prototype, use:

    1. Google NotebookLM — Upload docs, ask questions. Zero code.
    2. ChatGPT file uploads — Drag PDFs into a conversation.
    3. Claude Projects — Upload to Project Knowledge for persistent RAG.

    These let you validate whether RAG is useful for your use case before building a custom pipeline.

    Key Takeaways

    • •A RAG pipeline has 6 steps: ingest, chunk, embed, store, query, generate
    • •Chunking is critical — start with 500-token chunks and 100-token overlap
    • •Always include metadata (source, page, date) for better retrieval and citation
    • •Use frameworks like LangChain or LlamaIndex to accelerate development
    • •Prototype with no-code tools (NotebookLM, Claude Projects) before building custom
    0 of 5 lessons marked complete

    Progress is kept in this browser only.