Retrieval-augmented generation (RAG) enhances a language model’s prompt with relevant context retrieved from external documents or other stored information. It supports document chat, question answering, reference lookup, and agent knowledge. section 8.1 section 8.2
A typical RAG workflow has two phases:
- Load documents, split them into context chunks, convert the chunks into vector embeddings, and store them in a vector database. section 8.2
- Embed a user query, search for similar stored chunks, and insert the retrieved content into the prompt as additional context. section 8.2
- Send the augmented prompt to the language model, which uses the retrieved context to help formulate an answer. section 8.2
Querying a document collection
A document-chat application can index a PDF or text file, then answer a question by retrieving semantically similar passages. In a LangChain exercise, token-based chunks are stored in Chroma and similarity search retrieves a nursery-rhyme passage even when the query uses different wording, such as asking why the girls are crying. section 8.4.2
Figure 8.2 The two phases of RAG: first, documents must be loaded, transformed, embedded, and stored, and, second, they can be queried using augmented generation.
Use this diagram to distinguish document indexing from query-time augmented generation. section 8.2































