Retrieval-augmented generation (RAG) helps a language model answer by searching relevant documents first, then adding those documents to the user’s prompt. The model generates from that combined context rather than relying only on its training or fine-tuning data. section 5.5.1
A RAG system usually follows this path:
- Turns the user’s input into a search query. section 5.5.1
- Searches a general or subject-specific document index. section 5.5.1
- Combines the retrieved documents with the original question. section 5.5.1
- Asks the language model to produce an answer from that context, potentially with citations to the source documents. section 5.5.1
RAG can make answers more accurate, factually grounded, and useful because the model receives relevant source material. It does not guarantee correctness: the language model still writes the final response, so its errors remain possible. section 5.5.1 section 5.6
A small DSPy implementation
A DSPy RAG module can configure GPT-3.5 as the language model, ColBERTv2 over a Wikipedia database as the retriever, fetch three passages, and pass the question and passages to an answer generator. The components can later be replaced separately. section 5.5.2
Figure 5.11 On the left, we show the normal use of an LLM of a user asking about how to write JSON. LLMs naturally have the chance of producing errant outputs, which we want to minimize. On the right, we show the RAG approach. By using a search engine, we can find documents that are relevant to a query and combine them into a new prompt, giving the LLM more information and context to produce a better answer.
Compare ordinary language-model prompting with RAG: the latter inserts search results into a new prompt before generation. section 5.5.1
