Chapter 9: Question transformations
The introduction explains why a poorly phrased user question can undermine RAG retrieval even when the data has been carefully prepared.
After this chapter you will be able to
- Question quality and RAG retrieval. Explain why low-quality RAG results can stem from the user’s question rather than missing content, and why refining the question can improve retrieval and the eventual answer.
- Rewrite Retrieve Read workflow. Describe how Rewrite-Retrieve-Read differs from standard Retrieve-and-Read: a rewriter, often an LLM, clarifies the question before the retriever searches.
- Separate retrieval and answer questions. Explain why the rewritten query is used to search the vector store while the original user question is kept in the answer-synthesis prompt.
- Original-question search limitation. Explain that searching with the original user question returned some information about fun activities, but the results seemed limited, possibly because of how the question was worded.
- LLM query rewriter chain. Describe how the query rewriter chain uses a prompt to have an LLM transform a user question into a ChromaDB search query, and state why the transformation is useful.
- Rewritten Query Retrieval. Describe how the rewritten query is used for vector-store search and what changes in the retrieved results compared with searching the original question.
- Rewritten query and original question. Explain how the combined RAG workflow uses a rewritten version of the user’s question for vector retrieval while retaining the original question in the prompt that generates the final answer.
- Multiple queries versus rewriting. Explain why generating multiple queries can work better than rewriting a well-formed question as one query when it contains several implicit questions.
- Multi-query answer workflow. Trace how the application turns an original question into multiple explicit questions, retrieves context for each from a vector store, and synthesizes an answer using the original question and retrieved context.
- Multiple Query Chain Output. Explain that the prompt asks the LLM for five variations of one user question, and that a custom result parser returns them as a list of strings so each question can be processed independently.
- Custom retriever setup sequence. Describe how to set up a custom multi-query retriever by creating a standard retriever and embedding it within the multi-query retriever, then testing it with a query and printing the retrieved documents.
- Standard multi-query retriever option. Describe when to use a standard MultiQueryRetriever instance and what to expect when testing it with the same question.
- Overly narrow retrieval context. Explain why retrieving with a highly detailed question can leave an LLM without enough context for a comprehensive answer, particularly when documents are split into small, specific chunks.
- Detailed and abstract retrieval. Trace how the application retrieves context for both the original detailed question and an LLM-generated broader question, then uses both contexts with the original question to produce an answer.
- Step-back question generation. Explain that a prompt generates a broader question from the user's question, and that the generated question can be used in a standard RAG workflow to retrieve broader context for the LLM's answer.
- Step-back retrieval rationale. Explain why broadening a question can improve retrieval in a RAG workflow and how this differs from embedding-focused approaches.
- HyDE retrieval approach. Explain how HyDE uses an LLM-generated hypothetical document to retrieve content, and why that document can match stored chunks better than the original question.
- HyDE document generation and retrieval. Explain that a HyDE chain generates a hypothetical document that could answer the user’s question, then uses that document as the input for retrieval to better align the search with document chunks in the vector store.
- HyDE retrieval enhancement. Explain that integrating a HyDE chain into a RAG workflow enhances retrieval by using a hypothetical document.
- Independent versus dependent subquestions. Distinguish when single-step decomposition is suitable from when a complex question requires a sequence of dependent queries.
- Multi-step query sequence. Trace how an LLM-generated sequence of parameterized questions is executed to answer an interdependent question.
What it builds on
- Question-answering stage (chapter 8) · Assumes the question-answering stage can affect RAG accuracy.
- The RAG design pattern (chapter 6) · Assumes the basic RAG pattern of retrieval followed by answer generation.
- Completing the RAG chain setup (chapter 7) · Assumes a RAG chain that retrieves content and uses a prompt to generate an answer.
- Splitting strategy (chapter 8) · Document splitting into chunks.
- Retrieving content from the vector database (chapter 6) · Retrieving content from a vector database for a question.
- Embedding hypothetical questions (chapter 8) · Contrasts HyDE with indexing chunks using embeddings of hypothetical questions.
Section by section
Introduction
The introduction explains why a poorly phrased user question can undermine RAG retrieval even when the data has been carefully prepared.
- Question quality and RAG retrieval: Explain why low-quality RAG results can stem from the user’s question rather than missing content, and why refining the question can improve retrieval and the eventual answer.
Why it matters: It gives you another place to look when careful data preparation still produces weak RAG answers.
Key terms: Retrieval-Augmented Generation (RAG), vector store, user’s question, retrieval performance
9.1 Rewrite-Retrieve-Read
This section introduces Rewrite-Retrieve-Read and explains why a rewritten search query can improve retrieval while the original question remains available for answer synthesis.
- Rewrite Retrieve Read workflow: Describe how Rewrite-Retrieve-Read differs from standard Retrieve-and-Read: a rewriter, often an LLM, clarifies the question before the retriever searches.
Why it matters: The extra rewrite step addresses questions whose wording may lead to poor retrieval results. - Separate retrieval and answer questions: Explain why the rewritten query is used to search the vector store while the original user question is kept in the answer-synthesis prompt.
Why it matters: Keeping the two forms of the question serves both effective search and an answer to what the user actually asked.
Key terms: Retrieve-and-Read, Rewrite-Retrieve-Read, rewriter, tailored search query, vector store, answer-synthesis prompt, original question
9.1.1 Retrieving content using the original user question
The section uses a search with the original user question to show that its results may be limited by the question’s wording.
- Original-question search limitation: Explain that searching with the original user question returned some information about fun activities, but the results seemed limited, possibly because of how the question was worded.
Why it matters: This observation gives a reason to try a different query rather than assume the initial results are complete.
Key terms: original user question, search
9.1.2 Setting up the query rewriter chain
This section sets up an LLM chain that rewrites a user question into a query better suited to ChromaDB semantic search.
- LLM query rewriter chain: Describe how the query rewriter chain uses a prompt to have an LLM transform a user question into a ChromaDB search query, and state why the transformation is useful.
Why it matters: Rewriting the question can help ChromaDB retrieve more relevant content.
Key terms: query rewriter chain, LLM, ChromaDB, semantic search
9.1.3 Retrieving content with the rewritten query
This section uses a rewritten query to retrieve vector-store content and compares its relevance with results from the original question.
- Rewritten Query Retrieval: Describe how the rewritten query is used for vector-store search and what changes in the retrieved results compared with searching the original question.
Why it matters: Rewriting the search query can help retrieval find content that better addresses the original question.
Key terms: rewriter chain, search_query, vector store retriever, text chunks
9.1.4 Combining everything into a single RAG chain
This section combines query rewriting, vector retrieval, and answer generation into one RAG workflow while preserving the original question for the final prompt.
- Rewritten query and original question: Explain how the combined RAG workflow uses a rewritten version of the user’s question for vector retrieval while retaining the original question in the prompt that generates the final answer.
Why it matters: The distinction clarifies which version of the question serves retrieval and which guides the final answer.
Key terms: RAG chain, query rewriting, vector retrieval, original question
9.2 Generating multiple queries
This section explains when to turn one user question into multiple vector-store queries and how their retrieved context supports a combined answer.
- Multiple queries versus rewriting: Explain why generating multiple queries can work better than rewriting a well-formed question as one query when it contains several implicit questions.
Why it matters: It helps you choose a query transformation that fits the question rather than assuming every question needs a better wording. - Multi-query answer workflow: Trace how the application turns an original question into multiple explicit questions, retrieves context for each from a vector store, and synthesizes an answer using the original question and retrieved context.
Why it matters: The separate retrievals gather context for different parts of the question before the application forms one answer. - MultiQueryRetriever automation: Describe what LangChain’s MultiQueryRetriever takes as inputs and what workflow it automates for a user query.
Why it matters: It identifies the LangChain component offered for automating the workflow just described.
Key terms: Rewrite-Retrieve-Read, multiple explicit questions, vector store, retrieved context, answer synthesis, MultiQueryRetriever, prompt, LLM reference, retriever
9.2.1 Setting up the chain for generating multiple queries
This subsection sets up a chain that generates five alternative versions of a user question and returns them as a list.
- Multiple Query Chain Output: Explain that the prompt asks the LLM for five variations of one user question, and that a custom result parser returns them as a list of strings so each question can be processed independently.
Why it matters: Separate list items make the generated questions available for independent processing.
Key terms: multiple queries, custom result parser, StrOutputParser, list of strings
9.2.2 Setting up a custom multi-query retriever
This subsection gives the basic setup and test sequence for a custom multi-query retriever.
- Custom retriever setup sequence: Describe how to set up a custom multi-query retriever by creating a standard retriever and embedding it within the multi-query retriever, then testing it with a query and printing the retrieved documents.
Why it matters: This sequence gives you a way to assemble the retriever and check that it returns documents.
Key terms: standard retriever, custom multi-query retriever, retrieved_docs
9.2.3 Using a standard MultiQueryRetriever instance
This subsection presents the standard MultiQueryRetriever as a straightforward alternative for multi-query generation and notes that question transformations do not always produce the desired results.
- Standard multi-query retriever option: Describe when to use a standard MultiQueryRetriever instance and what to expect when testing it with the same question.
Why it matters: It gives you a standard option when a straightforward multi-query setup is enough. - Limits of question transformations: State the limitation the subsection identifies for rewriting or splitting a question into multiple questions.
Why it matters: It cautions against assuming that generating alternative questions will always improve retrieval.
Key terms: MultiQueryRetriever, multi-query generation, retrieved_docs, rewriting the original question, multiple explicit questions
9.3 Step-back question
This section explains how a step-back question retrieves broader context alongside detailed context so a RAG application can produce a more comprehensive answer.
- Overly narrow retrieval context: Explain why retrieving with a highly detailed question can leave an LLM without enough context for a comprehensive answer, particularly when documents are split into small, specific chunks.
Why it matters: It explains why a relevant-looking retrieval result may still be insufficient for a full answer. - Detailed and abstract retrieval: Trace how the application retrieves context for both the original detailed question and an LLM-generated broader question, then uses both contexts with the original question to produce an answer.
Why it matters: Combining specific and higher-level evidence gives the LLM material for a fuller response.
Key terms: step-back question, vector store, broader context, detailed question (Q_D), detailed context (C_D), abstract question (Q_A), abstract context (C_A)
9.3.1 Setting up the chain to generate a step-back question
This section explains how to prompt a chain to generate a broader step-back question and why that question can help a RAG system retrieve useful context.
- Step-back question generation: Explain that a prompt generates a broader question from the user's question, and that the generated question can be used in a standard RAG workflow to retrieve broader context for the LLM's answer.
Why it matters: The broader question changes what context retrieval can find for the final answer. - Model choice for abstraction: Describe why the example uses GPT-5 rather than GPT-5-nano or GPT-5-mini, while noting that different models' outputs can be compared.
Why it matters: The model used to generate the question can affect how useful and relevant that question is.
Key terms: step-back question, broader context, RAG workflow, vector store, GPT-5, GPT-5-nano, GPT-5-mini
9.3.2 Incorporating step-back question generation into the RAG chain
The section explains that a step-back question can improve RAG retrieval by broadening the question to provide better context.
- Step-back retrieval rationale: Explain why broadening a question can improve retrieval in a RAG workflow and how this differs from embedding-focused approaches.
Why it matters: It clarifies what the question transformation is intended to improve without requiring the reader to recall implementation details.
Key terms: step-back question, RAG, retrieval, embedding-focused methods
9.4 Hypothetical Document Embeddings (HyDE)
Introduces HyDE as a way to improve RAG retrieval by searching with an LLM-generated, answer-like document instead of the user’s question.
- HyDE retrieval approach: Explain how HyDE uses an LLM-generated hypothetical document to retrieve content, and why that document can match stored chunks better than the original question.
Why it matters: It explains why changing the retrieval query can help without changing the stored chunks.
Key terms: Hypothetical Document Embeddings (HyDE), hypothetical documents, document chunk embeddings
9.4.1 Generating a hypothetical document for the user question
This subsection introduces generating a hypothetical answer document from a user question for use in RAG retrieval.
- HyDE document generation and retrieval: Explain that a HyDE chain generates a hypothetical document that could answer the user’s question, then uses that document as the input for retrieval to better align the search with document chunks in the vector store.
Why it matters: The generated document gives retrieval a different input that may match relevant stored chunks more closely.
Key terms: HyDE, hypothetical document, RAG chain, vector store
9.4.2 Integrating the HyDE chain into the RAG chain
This subsection integrates a HyDE chain into a RAG workflow to enhance retrieval with a hypothetical document.
- HyDE retrieval enhancement: Explain that integrating a HyDE chain into a RAG workflow enhances retrieval by using a hypothetical document.
Why it matters: The hypothetical document is the reason this integration changes retrieval.
Key terms: HyDE chain, RAG chain, hypothetical document, retrieval process
9.5 Single-step and multi-step decomposition
This section distinguishes independent subquestions from dependent ones and explains how to answer a complex question through a sequence of queries whose answers feed later queries.
- Independent versus dependent subquestions: Distinguish when single-step decomposition is suitable from when a complex question requires a sequence of dependent queries.
Why it matters: The relationship between subquestions determines whether they can be retrieved independently or must be handled in order. - Multi-step query sequence: Trace how an LLM-generated sequence of parameterized questions is executed to answer an interdependent question.
Why it matters: Passing earlier answers into later queries makes it possible to retrieve information that the original question cannot obtain in one independent search.
Key terms: single-step decomposition, independent questions, interdependent questions, multi-step decomposition, parameterized questions, vector store, SQL database
9.6 Summary
The section recaps ways to transform questions and retrieval inputs so a RAG system can find more relevant documents.
Where this is used later
- (chapter intro) (chapter 10) · Transforming a question before retrieval
- RAG fusion (Reciprocal Rank Fusion) (chapter 10) · Generating multiple queries from a user question
Concepts explained in this chapter
- query rewritingQuery rewriting is a technique that transforms an original search request into one or more clearer or more targeted queries to improve retrieval results.
- step-back promptingStep-back question generation is a technique that reformulates a specific question into a broader question to elicit useful contextual information for retrieval or answering.
- pipelineA multi-step workflow is a process composed of several ordered or coordinated operations that collectively accomplish a task.
- reciprocal rank fusionReciprocal Rank Fusion is a result-merging method that scores documents by summing reciprocal rank contributions across multiple ranked lists.
- promptA system prompt is an instruction message supplied to a language model to establish high-level behavior or constraints.
- similarity searchRetrieval is the process of finding relevant stored documents from an indexed collection in response to a query.
- SQLAn SQL query is a statement written in SQL that specifies data to retrieve or filter from a relational database.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
