Navigation

Introduction to AI

Machine Learning

Deep Learning

Generative AI

Tools & Frameworks

General

Retrieval-Augmented Generation (RAG): Combining Search with AI

An LLM alone can hallucinate. A search engine alone can’t reason. RAG combines them: search your knowledge base for relevant information, pass it to an LLM, and get a reasoned answer grounded in real data.

The RAG Pipeline

  1. Query Embedding: Convert the user’s question into a vector using an embedding model
  2. Retrieval: Search your vector database for the most similar documents
  3. Augmentation: Insert the retrieved documents into the prompt as context
  4. Generation: The LLM generates an answer based on the provided context
User: "What's our return policy?"

Embed: [0.23, -0.45, 0.89, ...]

Search: Find similar docs in vector DB
Result: [return_policy.md, customer_faq.md]

Augmented Prompt:
"Based on this policy: [content], answer: What's our return policy?"

LLM Response: "Based on our policy, returns are accepted within 30 days..."

Why RAG Matters

  • Reduced Hallucinations: Model has facts to work from
  • Up-to-Date Info: Retrieve from current documents, not training data
  • Knowledge Control: Only documents in your database can be referenced
  • Transparency: Users can see which sources informed the answer
  • Cost-Effective: Cheaper than fine-tuning while staying competitive
AspectKeyword SearchVector/RAG
Match TypeExact keywordsSemantic similarity
Query”return policy""Can I send this back?”
ResultPages with keywordsSemantically similar docs
RecallHigh if keywords matchHigh for meaning
PrecisionLower (many false positives)Higher (semantic matching)

Common RAG Architecture

┌─────────────────┐
│  Knowledge Base │ (PDFs, docs, markdown, etc.)
└────────┬────────┘

    [Chunk & Embed]

    ┌────▼─────────────┐
    │ Vector Database  │ (Pinecone, Weaviate, etc.)
    └────────┬─────────┘

    ┌────────▼──────────┐      ┌──────────────┐
    │  User Question    │      │   LLM Model  │
    └────────┬──────────┘      └──────┬───────┘
             │ Embed                  │ Generate
    ┌────────▼──────────┐             │
    │ Semantic Search   │────────────►│
    │ (Top-K retrieval) │ Insert docs │
    └───────────────────┘             │
                              ┌───────▼────────┐
                              │  Final Answer  │
                              └────────────────┘

Implementation Challenges

  1. Chunking Strategy: How to split documents? Overlapping chunks can improve retrieval
  2. Embedding Quality: Different embedding models produce different results
  3. Retrieval Relevance: Bad retrieval = bad answers even if LLM is good
  4. Latency: Vector search adds latency; optimize for speed
  5. Hallucinations Still Happen: RAG reduces but doesn’t eliminate them

Best Practices

  • Chunk Size: 256-512 tokens often works well (experiment)
  • Overlap: 10-20% overlap between chunks improves retrieval
  • Top-K: Retrieve 3-5 most relevant documents (too many confuse the model)
  • Hybrid Search: Combine keyword search with semantic search for better results
  • Reranking: Use a second model to rank retrieved documents by relevance
  • User Feedback: Log which sources led to good/bad answers and improve iteratively