liveBook · Manning

Retrieval-augmented generation

As explained in Building LLM Applications with DSPy

A language-model application pattern that retrieves external information and supplies it as context for response generation.

Retrieval-augmented generation (RAG) supplies a language model with relevant external information at answer time. It helps answer questions that require information the model did not see during training, such as questions about a company’s policies, products, or services. section 9.0

A RAG application combines retrieval and generation: it searches a knowledge base for material related to the user’s question, then passes the retrieved content and question to the language model. A DSPy implementation can package both steps in a custom module. section 9.2 section 9.2.2

A typical knowledge-base workflow:

  • Represent the question and knowledge-base articles as vectors, then find the articles most similar to the question. section 9.2
  • Pass the selected article contents and the original question to the language model to generate an answer. section 9.2
  • Article titles can be used for vector search; they may work better than full articles when extra content makes embeddings less relevant. section 9.2

Customer-support questions

A chatbot answering questions about a company’s return policy can retrieve relevant policy documents and provide them with the customer’s question as context. A Wix support example retrieves knowledge-base articles to answer a question about accepting payments during account verification. section 9.0 section 9.2

Figure 9.1 The two main stages of RAG: retrieval and generation. During the retrieval stage, we search for documents that are most similar to the query. Once we have these, we can prepare the prompt by injecting the top-k results along with the user query as input for the generation stage. The LM can then generate a grounded answer based on these documents. If requested, it may also quote the source documents.
figure

Diagram of the retrieval and generation stages, including selecting top-k documents and supplying them with the query as generation context. section 9.2

How Building LLM Applications with DSPy covers it

How deep it goes
Builds a DSPy module around retrieval and generation, including vector search, but does not detail the full search infrastructure here.
Examples
Uses company return-policy questions and Wix support queries, retrieving relevant articles to ground answers.
What it assumes
Builds on language-model generation and custom DSPy modules; the treatment introduces retrieval as the new step.
What you can do after
Build a DSPy application that retrieves relevant knowledge-base articles and supplies them with a user query to generate an answer.
How it connects
Connects language models, knowledge bases, vector search, embeddings, and DSPy modules.

Also explained in

Compare how 32 books explain retrieval-augmented generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.