Retrieval-Augmented Generation
Also RAG
Retrieval-augmented generation (RAG) answers a query over a text larger than an LLM's context size by splitting the text into chunks, embedding them, retrieving the k chunks most similar to the query embedding, and passing only their text to the LLM with a suitable prompt.
Properties#
- Motivation: context sizes are large (400k tokens for GPT-5, 1M for Gemini Pro), but many applications query texts larger than that. ES12: the transcripts of all lecture recordings exceed the context size of the provided LLM. Since only chunks enter the prompt, the text can be arbitrarily large.
- Embedding method: a sum of word embeddings (ES11) is simplistic, because not every word is equally relevant. ES12 uses an embedding API (
mixedbread-ai/mxbai-embed-large-v1) that computes one embedding per text (a dedicated embedding model, such as a sentence transformer). - Retrieval: a linear scan over all chunk embeddings suffices for ES12; real applications precompute a special data structure. With normalized embeddings, all similarities are one matrix-vector product (Cosine Similarity > Formula).
- Tuning knobs: chunk size, overlap, the embedding model, and the system prompt (ES12: experiment with , chunk sizes and system prompts).
- ES12 details: one chunk per subtitle paragraph (one paragraph per timestamp); remove the timestamps before embedding, as they carry no useful information.
Example#
Sources#
- Slides 12, p. 14 (motivation), Slides 12, p. 15 (preprocessing), Slides 12, p. 16 (embedding API), Slides 12, p. 17 (query processing)
- DBIS_VL12 > Retrieval-Augmented Generation, DBIS_VL12 > Preprocessing, DBIS_VL12 > Computing the embeddings, DBIS_VL12 > Processing a query