Course/Chapter 8/4. RAG & Vector Store Integration
    advanced
    Chapter 8: AI & LLM Integration

    4. RAG & Vector Store Integration

    Build knowledge base chatbots with RAG.

    20m Lesson 4 of 5

    Retrieval Augmented Generation (RAG) combines a knowledge base with AI to answer questions accurately based on your own data.

    RAG Architecture

    1. Embed: Convert documents into vector embeddings
    2. Store: Save embeddings in a vector database (Pinecone, Qdrant, Supabase)
    3. Retrieve: Find relevant documents for a user's question
    4. Generate: Use retrieved context + AI to generate accurate answers

    Implementation in n8n

    n8n has built-in support for vector stores through the AI Agent and Vector Store nodes. You can build a complete RAG pipeline without writing code.

    Ingestion Pipeline 1. Load documents (PDF, HTML, Markdown) 2. Split into chunks using the Text Splitter node 3. Generate embeddings via OpenAI Embeddings node 4. Store in Pinecone, Qdrant, or Supabase pgvector

    Query Pipeline 1. Receive user question via webhook or chat 2. Embed the question 3. Search vector store for top-K similar chunks 4. Pass chunks as context to the LLM 5. Return the AI-generated answer

    Code Examples

    RAG Query via Code Node
    javascript
    // After retrieving relevant chunks from vector store:
    const question = $json.user_question;
    const chunks = $json.retrieved_chunks;
    
    // Build context from retrieved documents
    const context = chunks
      .map((c, i) => `[Source ${i + 1}: ${c.metadata.filename}]\n${c.text}`)
      .join("\n\n---\n\n");
    
    const prompt = `Answer the question based ONLY on the provided context.
    If the answer isn't in the context, say "I don't have enough information."
    
    Context:
    ${context}
    
    Question: ${question}
    
    Provide a clear answer and cite which source(s) you used.`;
    
    return [{ json: { prompt, sources: chunks.map(c => c.metadata.filename) } }];

    Pro Tips

    • 💡Chunk your documents into 500–1000 token segments for optimal retrieval accuracy
    • 💡Always include the source document reference in AI responses for transparency
    • 💡Use overlap between chunks (50-100 tokens) to avoid losing context at chunk boundaries

    Comprehension Quiz

    Answer 2 of 2 correctly to unlock lesson completion.

    1. What does RAG stand for?

    2. What is the recommended chunk size for RAG documents?

    Pass the quiz above to unlock lesson completion