MLCrack

    LLMS & TRANSFORMERS · INTERVIEW QUESTION

    Explain RAG (Retrieval-Augmented Generation). What are the key components and failure modes?

    🟡 intermediate · RAG & Agents

    Hint: Think about retrieval, context stuffing, and when the retriever fails.

    Answer

    RAG augments LLM generation with retrieved external knowledge, reducing hallucinations and enabling up-to-date answers.

    Pipeline

    Documents → Chunk → Embed → Vector DB ↓ User Query → Embed → Search → Top-K Chunks → Prompt → LLM → Answer

    Key Components

    ComponentOptions
    Embedding modelOpenAI, Cohere, sentence-transformers
    Vector storeFAISS, Pinecone, Weaviate, Chroma
    ChunkingRecursive, semantic, fixed-size
    RerankerCross-encoder, Cohere rerank

    Failure Modes

    • Poor retrieval: wrong chunks returned for the query
    • Lost in the middle: model ignores middle context in long prompts
    • Hallucination despite context: model overrides retrieved facts
    • Chunk boundary issues: important info split across chunks
    python
    # Illustrative interface sketch: supply tested retrieval and generation functions. def answer_question(question, retrieve, generate): passages = retrieve(question, k=5) # enforce user permissions here context = "\n\n".join(p["text"] for p in passages) return generate(question=question, context=context)

    Improvements: hybrid search (dense + sparse), reranking, query expansion, iterative retrieval.