Learn/LangChain Mastery/Part 6 · 5. Retrieval & RAG Patterns
Lesson 40 of 68
SyllabusLangChain Mastery · Part 6

Multi-Query Retrieval

Asking 5 questions is better than asking 1. Expanding the search net.

Course outline & contents
LangChain Mastery
The Retriever InterfaceSimilarity vs MMRMulti-Query RetrievalRAG-FusionQuery DecompositionParent Document RetrievalContextual CompressionPost-Retrieval Re-RankingConversational RAG & History

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.

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.

multi_query.py
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.

It solves the "Vocabulary Mismatch" problem. It covers different "Embeddings Neighborhoods" (synonyms often live in different clusters).

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."

Track your progressSign in and your progress follows you across devices.
Sign in to track progress Next lesson
← Previous lessonSimilarity vs MMRNext lesson →RAG-Fusion