Multi-Query Retrieval
Asking 5 questions is better than asking 1. Expanding the search net.
1. The Single Query Failure
User Query: "Why is my screen dark?" Document: "Display brightness settings can be adjusted in the Control Panel."
Semantic Distance: Far. The user used "dark," the doc uses "brightness." The user used "screen," the doc uses "Display."
If you rely on a single query, you rely on a perfect semantic match.
2. Query Expansion (Paraphrasing)
We ask an LLM to generate 3 variations of the user's question.
- Variation 1: "How do I increase display brightness?"
- Variation 2: "Monitor is not lighting up fix."
- Variation 3: "Troubleshoot backlight issues."
We then run ALL 3 queries against the vector store. If any of them hits the document, we retrieve it.
from langchain.retrievers.multi_query import MultiQueryRetriever
retriever = MultiQueryRetriever.from_llm(
retriever=vectorstore.as_retriever(),
llm=chat_model
)
# It generates queries, searches all, and creates a unique union of docs.3. Benefits
It solves the "Vocabulary Mismatch" problem. It covers different "Embeddings Neighborhoods" (synonyms often live in different clusters).
4. Summary
Users are bad at prompting. Don't let their poor wording break your retrieval. Expand their intent.
Key Intuition: "Cast a wider net by saying the same thing in different ways."