Similarity search helps find items close to your query. MMR retrieval balances closeness and variety to avoid repeats.
Similarity search vs MMR retrieval in LangChain
from langchain.vectorstores import FAISS # Similarity search results = vectorstore.similarity_search(query, k=5) # MMR retrieval results = vectorstore.max_marginal_relevance_search(query, k=5, fetch_k=10)
similarity_search returns the top k closest matches to the query.
max_marginal_relevance_search returns k results balancing similarity and diversity, using fetch_k candidates internally.
results = vectorstore.similarity_search('What is AI?', k=3)
results = vectorstore.max_marginal_relevance_search('What is AI?', k=3, fetch_k=6)
results = vectorstore.similarity_search('Nonexistent topic', k=3)
results = vectorstore.max_marginal_relevance_search('Nonexistent topic', k=3, fetch_k=5)
This program creates a small vector store from example sentences about AI. It runs both similarity search and MMR retrieval for the query 'Tell me about AI'. It prints the top 3 results from each method so you can see the difference.
from langchain.vectorstores import FAISS from langchain.embeddings import OpenAIEmbeddings # Sample documents documents = [ 'AI is the simulation of human intelligence.', 'Machine learning is a subset of AI.', 'Deep learning uses neural networks.', 'AI can be used in healthcare.', 'Neural networks mimic the brain.' ] # Create embeddings embeddings = OpenAIEmbeddings() # Build vector store vectorstore = FAISS.from_texts(documents, embeddings) query = 'Tell me about AI' # Similarity search similar_results = vectorstore.similarity_search(query, k=3) # MMR retrieval mmr_results = vectorstore.max_marginal_relevance_search(query, k=3, fetch_k=5) print('Similarity Search Results:') for doc in similar_results: print('-', doc.page_content) print('\nMMR Retrieval Results:') for doc in mmr_results: print('-', doc.page_content)
Similarity search is fast and finds the closest matches but can return very similar or repeated results.
MMR retrieval adds diversity by balancing similarity and novelty, which is useful when you want varied answers.
MMR is slightly slower because it fetches more candidates internally before picking diverse results.
Similarity search finds the closest matches to your query.
MMR retrieval balances closeness and diversity to avoid repeated or very similar results.
Use similarity search for quick, relevant results; use MMR when you want variety in answers.
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
