KatInterview

Build a Document Q&A Pipeline with Retrieval and Grounded Prompts

Machine LearningModel answer
MediumModel answerMachine Learningragembeddingsvector-search

Last updated

Wire together a given embedder, vector store and LLM into a retrieval-augmented question-answering flow, then justify vector similarity search and when this approach beats alternatives.

You are given three ready-made components and a small data class for documents. Your job is to write the glue code that turns a pile of documents into a question-answering system, and then to explain the reasoning behind it. Pseudocode is fine.

from dataclasses import dataclass

@dataclass
class Document:
    doc_id: str
    text: str
    source: str

@dataclass
class Hit:
    chunk_id: str
    score: float      # similarity computed by the store
    metadata: dict    # whatever was passed to add() for this chunk

class Embedder:
    def embed(self, texts: list[str]) -> list[list[float]]: ...

class VectorStore:
    def add(self, ids: list[str], vectors: list[list[float]], metadata: list[dict]) -> None: ...
    def query(self, vector: list[float], k: int) -> list[Hit]: ...

class LLM:
    def generate(self, prompt: str) -> str: ...

Embedder, VectorStore and LLM already work. You write chunking, indexing and answering. Some of this tooling may be unfamiliar, so state your assumptions out loud and ask focused questions (for example: does the store use cosine, dot product or distance? Are vectors normalized? How big is the corpus and how often does it change? Should answers cite sources? What is the context limit?).

Part 1: Build it. Write the indexing path (documents in, searchable entries out) and the query path (question in, answer out). Cover:

  • Chunking with stable IDs and metadata that allow citations
  • Batched embedding and insertion
  • Embedding the question, retrieving the top-k chunks, and building a prompt that keeps the model grounded in them
  • What happens when nothing relevant is retrieved

Part 2: Explain retrieval. Why are embeddings needed? How does the search find relevant chunks? Why is cosine similarity a sensible way to compare the question vector with stored vectors? Discuss exact versus approximate nearest-neighbor search, the relationship between cosine, dot product and Euclidean distance, and the limits of cosine.

Part 3: Choose the right tool. When is this retrieval approach the right choice, and when would you pick fine-tuning, stuffing the whole corpus into the prompt, or a plain keyword search or database query instead? Address cost, latency and maintenance.

Hints

Hint 1

Store the chunk text and its source inside the metadata dictionary passed to add(); the Hit objects returned by query() are then all you need to build the prompt and the citations.

Hint 2

Give each chunk a deterministic ID such as doc_id plus chunk index, split with a small overlap so sentences are not cut off from their context, and embed in batches rather than one call per chunk.

Hint 3

Decide what 'nothing relevant' means before writing the prompt: compare the top score to a threshold, and also instruct the model to say it cannot answer when the context lacks the information.