What if your search results were not just relevant but also refreshingly different every time?
Similarity search vs MMR retrieval in LangChain - When to Use Which
Imagine you have a huge library of documents and you want to find the most relevant ones for your question by looking for similar words or phrases.
You try to pick the top matches just by similarity scores.
Picking only the most similar documents often gives you many that say almost the same thing.
This means you get repeated information and miss out on other useful perspectives.
MMR retrieval smartly balances similarity with diversity.
It picks documents that are relevant but also different from each other, giving you a richer set of answers.
results = similarity_search(query, top_k=5)results = mmr_retrieval(query, top_k=5, lambda_=0.5)
You get a set of results that cover different angles of your question, not just repeats of the same idea.
When searching for travel tips, similarity search might give you five tips about packing, while MMR retrieval gives you packing, safety, food, and sightseeing tips.
Similarity search finds the closest matches but can be repetitive.
MMR retrieval balances relevance and diversity for better coverage.
Using MMR helps you get richer, more useful results from your searches.
Practice
Similarity search and MMR retrieval in Langchain?Solution
Step 1: Understand Similarity Search
Similarity search finds the closest matches to the query based on similarity scores.Step 2: Understand MMR Retrieval
MMR retrieval balances between closeness and diversity to avoid repeated or very similar results.Final Answer:
Similarity search finds closest matches; MMR retrieval balances closeness and diversity. -> Option BQuick Check:
Difference = Closest vs Balanced [OK]
- Thinking MMR returns only closest matches
- Confusing MMR with random retrieval
- Assuming similarity search ensures diverse results
Solution
Step 1: Check parameter names for MMRRetriever
The correct parameters includekfor number of results andlambda_multfor diversity weight.Step 2: Identify correct parameter usage
Options B, C, and D usetop_korlambdawhich are incorrect parameter names.Final Answer:
mmr_retriever = MMRRetriever(documents, k=5, lambda_mult=0.7) -> Option AQuick Check:
Correct params = k and lambda_mult [OK]
- Using 'top_k' instead of 'k'
- Using 'lambda' instead of 'lambda_mult'
- Mixing parameter names from similarity search
docs = ["apple pie recipe", "apple tart recipe", "banana bread recipe", "cherry pie recipe"] query = "apple dessert" sim_results = similarity_search(docs, query, k=2) mmr_results = mmr_retrieval(docs, query, k=2, lambda_mult=0.5)
Solution
Step 1: Analyze similarity_search output
Similarity search returns the top 2 closest matches, likely both apple recipes due to query "apple dessert".Step 2: Analyze mmr_retrieval output
MMR balances similarity and diversity, so it will likely return one apple recipe and one different dessert (like cherry pie) to increase variety.Final Answer:
sim_results will return the two most similar apple recipes; mmr_results will return one apple and one different dessert recipe. -> Option AQuick Check:
Similarity = closest; MMR = closest + diverse [OK]
- Assuming both return identical results
- Thinking MMR returns random results
- Confusing number of results returned
mmr = MMRRetriever(documents, k=3, lambda_mult=0.6) results = mmr.get_relevant_documents(query)What is the likely cause of the error?
Solution
Step 1: Check parameter names in MMRRetriever initialization
The parameterskandlambda_multare correct.Step 2: Confirm method name correctness
get_relevant_documentsis a valid method for retrievers.Final Answer:
Missing import statement for MMRRetriever. -> Option CQuick Check:
Missing import = NameError [OK]
- Forgetting import statement for MMRRetriever
- Confusing method names
- Wrong parameter names like top_k or lambda
Solution
Step 1: Identify need for relevance and diversity
You want documents that are relevant but cover different aspects, so diversity is important.Step 2: Choose retrieval method and parameters
MMR retrieval balances relevance and diversity usinglambda_mult. A value like 0.8 prioritizes relevance while introducing some diversity.Final Answer:
Use MMR retrieval with k=3 and lambda_mult=0.8 to balance relevance and diversity. -> Option DQuick Check:
Diversity + relevance = MMR with 0 < lambda_mult < 1 [OK]
- Using similarity search only for diverse results
- Setting lambda_mult to 0 disables relevance
- Repeating similarity search calls for variety
Ready to prove it?
Pass the test and this pattern moves unknown
