AskReference
ConceptIntermediate

What is query transformation in RAG and how does HyDE implement it?

Query transformation, or query rewriting, is a RAG optimization where an LLM rephrases the user's original query so it more closely matches the wording used in documents for vector similarity search. HyDE, short for Hypothetical Document Embeddings, implements this by generating a hypothetical answer to the query and then running the vector DB search using that synthetic text instead of the raw question.

Query transformation is needed because a vector database looks for semantic similarity, and a user's phrasing may not align with how the documents are written. To solve this, another LLM call rewrites the query into a version more likely to match useful chunks. HyDE is a popular instance of this approach: the LLM is prompted to generate a paragraph of text answering the query, and that hypothetical answer is embedded and used as the search input to the vector DB. This synthetic text can bridge wording gaps between the user and the documents. The technique can also be adapted to handle language mismatches, such as when documents are in Spanish but the question is in English, by using a prompt and LLM to translate the query first.

Key points

  • Query transformation uses an LLM to rephrase a user query into a form better suited for vector similarity search.
  • It addresses the problem that user wording may not semantically align with how documents are phrased.
  • HyDE stands for Hypothetical Document Embeddings.
  • HyDE generates a hypothetical answer paragraph and searches the vector DB with that synthetic text.
  • The same prompt-and-LLM pattern can be used to translate queries when documents are in a different language.
Source:AI Agents and Applications· Augmenting LLMs with your own documents· p. 116–117

Related questions

Cover of AI Agents and Applications

AI Agents and Applications

Andrea De Mauro

MEAP Edition Version 7 · Manning Publications

View this ebook