9 Question transformations

 

This chapter covers

  • Rewrite user questions with "Rewrite-Retrieve-Read" for better embedding alignment.
  • Use "step-back" queries to retrieve higher-level context.
  • Generate hypothetical documents to align questions with embeddings.
  • Decompose complex queries into single or multi-step sequences.

In some cases, you might spend a lot of time preparing RAG data—collecting documents, splitting them into chunks, and generating embeddings for synthesis and retrieval (as covered earlier). Yet, you may still see low-quality results from the vector store. This problem might not come from missing relevant content in the vector store, but from issues in the user's question itself.

For instance, the question might be poorly phrased, unclear, or overly complex. Questions that aren’t clearly and simply stated can confuse both the vector store and the LLM, leading to weaker retrieval results. In this section, I'll show you techniques to refine the user's question, making it easier for the query engine and the LLM to understand. By improving the question, you’ll likely see better retrieval performance, providing more relevant context for the LLM to deliver a solid answer.

Let's begin with a straightforward method: using the LLM to help rephrase the question.

9.1 Rewrite-Retrieve-Read

9.1.1 Retrieving Content Using the Original User Question

9.1.2 Setting Up the Query Rewriter Chain

9.1.3 Retrieving Content with the Rewritten Query

9.1.4 Combining Everything into a Single RAG Chain

9.2 Generating Multiple Queries

9.2.1 Setting Up the Chain for Generating Multiple Queries

9.2.2 Setting Up a Custom Multi-Query Retriever

9.2.3 Using a Standard MultiQueryRetriever Instance

9.3 Step-Back Question

9.3.1 Setting Up the Chain to Generate a Step-Back Question

9.3.2 Incorporating Step-Back Question Generation into the RAG Chain

9.4 Hypothetical Document Embeddings (HyDE)

9.4.1 Generating an Hypothetical Document for the User Question

9.4.2 Integrating the HyDE Chain into the RAG Chain

9.5 Single-Step and Multi-Step Decomposition

9.6 Summary