bigonotes
AIRAG

Pipeline

The two phases of a RAG pipeline: offline indexing (load, chunk, embed, store) and retrieval at question time (embed, search, prompt).

2 min readLast updated

A RAG system has two phases. Indexing prepares your documents ahead of time, offline. Retrieval runs on every question. Keeping the two apart makes the rest of RAG easier to reason about, because most problems belong clearly to one phase or the other.

The indexing phase loads documents, chunks them, embeds the chunks and stores them in a vector database; the retrieval phase embeds the question, searches that database for the top k chunks, builds a prompt and generates a cited answer

Indexing phase#

Indexing turns raw documents into searchable vectors:

  1. Load your documents: files, pages, tickets, database rows.
  2. Chunk each document into smaller pieces. See chunking.
  3. Embed each chunk into a vector with an embedding model.
  4. Store each vector in a vector database, together with the original chunk text and metadata such as source, title and access rules.

This runs once up front, and again whenever documents change. Store the chunk text next to the vector, because the vector is only for searching. The text is what goes into the prompt.

Retrieval phase#

Retrieval runs when a user asks a question:

  1. Embed the question with the same embedding model used for indexing.
  2. Search the vector database for the top k most similar chunks.
  3. Build the prompt: the retrieved chunks plus the question.
  4. Generate: the model reads the chunks and answers, citing its sources.

Step 1 must use the same model as indexing, or the vectors are not comparable, as covered in embeddings. Step 4 only works well with clear answer rules in the prompt.

Short line#

Indexing runs offline: load, chunk, embed and store chunks with their text and metadata. At question time: embed the question with the same model, retrieve the top k chunks, and prompt the model to answer from them with citations.

Key takeaways#

  • Indexing is offline: load, chunk, embed, store. It reruns when documents change.
  • Store the chunk text and metadata with each vector. The text goes into the prompt.
  • Retrieval runs per question: embed, search top k, build the prompt, generate.
  • Embed the question with the same model used to index the documents.
Revision notes · 1 min

Core idea#

A RAG pipeline has two phases: indexing, offline, and retrieval, on every question.

Indexing (offline)#

  1. Load the documents.
  2. Chunk each document.
  3. Embed each chunk with an embedding model.
  4. Store the vector, the chunk text and metadata in a vector database.

Retrieval (per question)#

  1. Embed the question with the same embedding model.
  2. Search for the top k most similar chunks.
  3. Put the chunks and the question in the prompt.
  4. The model answers and cites its sources.

Key points#

  • Indexing runs once up front and again whenever documents change.
  • The vector is for searching. The stored chunk text goes into the prompt.
  • Metadata holds source, title and access rules.

Pitfalls#

  • Do not embed the question with a different model than the documents.
  • Do not store vectors without their chunk text.

Short line#

Index offline: load, chunk, embed, store. At question time: embed the question, retrieve top k, prompt with citations.

Found a mistake? Email ask@bigonotes.com