Juan Pablo García
All writing
Explainer6 steps · scroll to play

How RAG works

Retrieval-augmented generation lets a language model answer from your own documents instead of from memory. Scroll to follow a question from the documents to a cited answer.

  1. 1

    Split the documents

    Long documents are cut into short chunks, a few paragraphs each, so the system can retrieve just the part that answers a question.

  2. 2

    Turn chunks into vectors

    An embedding model turns each chunk into a list of numbers. Chunks with similar meaning land close together in this space.

  3. 3

    Embed the question

    The question goes through the same embedding model, so it lands in the same space as the chunks.

  4. 4

    Find the nearest chunks

    An approximate nearest-neighbor search returns the chunks closest to the question. It's fast, but closeness in embedding space is only a rough guess at relevance.

  5. 5

    Rerank

    A reranker, usually a cross-encoder, reads the question and each candidate together and scores how well it actually answers. The order changes.

  6. 6

    Answer with sources

    The top chunks go into the prompt. The model answers from them and cites the chunk behind each claim.

Split the documents

Long documents are cut into short chunks, a few paragraphs each, so the system can retrieve just the part that answers a question.

Turn chunks into vectors

An embedding model turns each chunk into a list of numbers. Chunks with similar meaning land close together in this space.

Embed the question

The question goes through the same embedding model, so it lands in the same space as the chunks.

Find the nearest chunks

An approximate nearest-neighbor search returns the chunks closest to the question. It's fast, but closeness in embedding space is only a rough guess at relevance.

Rerank

A reranker, usually a cross-encoder, reads the question and each candidate together and scores how well it actually answers. The order changes.

Answer with sources

The top chunks go into the prompt. The model answers from them and cites the chunk behind each claim.

In short

  • RAG keeps answers grounded in documents you control, and you can update them without retraining the model (Lewis et al., 2020).
  • Retrieval usually decides answer quality: chunking, the embedding model and reranking tend to matter more than prompt wording.
  • Hybrid search, keyword matching (BM25) plus vectors, catches exact terms like product codes that embeddings miss.
  • Evaluate retrieval and generation separately: was the right chunk retrieved, and did the answer stay faithful to it?

References

  1. Lewis et al. (2020). Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks. NeurIPS.
  2. Robertson & Zaragoza (2009). The Probabilistic Relevance Framework: BM25 and Beyond. Foundations and Trends in IR.
  3. Nogueira & Cho (2019). Passage Re-ranking with BERT.
  4. Karpukhin et al. (2020). Dense Passage Retrieval for Open-Domain Question Answering. EMNLP.

Share

Keep reading

All writing

Want to work together?