Bird
Raised Fist0
LangChainframework~20 mins

Similarity search vs MMR retrieval in LangChain - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Langchain Similarity & MMR Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding Similarity Search in Langchain

What is the main purpose of similarity search in Langchain?

ATo randomly select documents from the database.
BTo find documents most similar to a query based on vector embeddings.
CTo sort documents alphabetically by title.
DTo remove duplicate documents from the dataset.
Attempts:
2 left
💡 Hint

Think about how Langchain uses vectors to find related content.

❓ component_behavior
intermediate
1:30remaining
Behavior of MMR Retrieval in Langchain

What unique behavior does Maximal Marginal Relevance (MMR) retrieval add compared to simple similarity search?

AIt only returns the single most relevant document.
BIt sorts documents by their creation date.
CIt balances relevance with diversity to avoid redundant results.
DIt filters out documents with low word counts.
Attempts:
2 left
💡 Hint

MMR tries to mix relevance with variety in results.

📝 Syntax
advanced
2:00remaining
Correct Usage of MMR Retrieval in Langchain Code

Which code snippet correctly initializes an MMR retriever with a vector store in Langchain?

LangChain
from langchain.vectorstores import FAISS
from langchain.retrievers import MMRRetriever

vectorstore = FAISS.load_local('my_faiss_index')

# Choose the correct MMR retriever initialization:
Aretriever = MMRRetriever(vectorstore=vectorstore, k=5, fetch_k=10, lambda_mult=0.5)
Bretriever = MMRRetriever(vectorstore=vectorstore, top_k=5, fetch_k=10, lambda_mult=0.5)
Cretriever = MMRRetriever(store=vectorstore, k=5, fetch_k=10, lambda_mult=0.5)
Dretriever = MMRRetriever(vectorstore=vectorstore, k=5, fetch=10, lambda_mult=0.5)
Attempts:
2 left
💡 Hint

Check the parameter names carefully in the MMRRetriever constructor.

❓ state_output
advanced
2:00remaining
Output Difference Between Similarity Search and MMR Retrieval

Given a query, what difference would you expect in the list of documents returned by similarity search vs MMR retrieval?

ASimilarity search returns documents sorted by length; MMR returns documents sorted by date.
BSimilarity search returns only one document; MMR returns all documents.
CSimilarity search returns random documents; MMR returns documents with the highest word count.
DSimilarity search returns the top most similar documents, possibly with redundancy; MMR returns relevant but more diverse documents.
Attempts:
2 left
💡 Hint

Think about how MMR tries to reduce repeated information.

🔧 Debug
expert
2:30remaining
Debugging Unexpected Results in MMR Retrieval

You use MMR retrieval but notice the results are very similar and not diverse. Which mistake in your code is most likely causing this?

LangChain
retriever = MMRRetriever(vectorstore=vectorstore, k=5, fetch_k=5, lambda_mult=0.0)
results = retriever.get_relevant_documents(query)
ASetting lambda_mult to 0 disables diversity, causing results to be similar.
BSetting fetch_k equal to k causes an error and no results.
CNot setting a random seed causes repeated results.
DUsing get_relevant_documents instead of get_documents causes wrong output.
Attempts:
2 left
💡 Hint

Check the effect of the lambda_mult parameter on diversity.

Practice

(1/5)
1. What is the main difference between Similarity search and MMR retrieval in Langchain?
easy
A. Similarity search uses machine learning; MMR retrieval uses rule-based filtering.
B. Similarity search finds closest matches; MMR retrieval balances closeness and diversity.
C. Similarity search always returns random results; MMR retrieval returns exact matches.
D. Similarity search is slower than MMR retrieval in all cases.

Solution

  1. Step 1: Understand Similarity Search

    Similarity search finds the closest matches to the query based on similarity scores.
  2. Step 2: Understand MMR Retrieval

    MMR retrieval balances between closeness and diversity to avoid repeated or very similar results.
  3. Final Answer:

    Similarity search finds closest matches; MMR retrieval balances closeness and diversity. -> Option B
  4. Quick Check:

    Difference = Closest vs Balanced [OK]
Hint: Similarity = closeness; MMR = closeness plus diversity [OK]
Common Mistakes:
  • Thinking MMR returns only closest matches
  • Confusing MMR with random retrieval
  • Assuming similarity search ensures diverse results
2. Which of the following is the correct way to initialize an MMR retriever in Langchain?
easy
A. mmr_retriever = MMRRetriever(documents, k=5, lambda_mult=0.7)
B. mmr_retriever = MMRRetriever(documents, top_k=5, lambda=0.7)
C. mmr_retriever = MMRRetriever(documents, k=5, lambda=0.7)
D. mmr_retriever = MMRRetriever(documents, top_k=5, lambda_mult=0.7)

Solution

  1. Step 1: Check parameter names for MMRRetriever

    The correct parameters include k for number of results and lambda_mult for diversity weight.
  2. Step 2: Identify correct parameter usage

    Options B, C, and D use top_k or lambda which are incorrect parameter names.
  3. Final Answer:

    mmr_retriever = MMRRetriever(documents, k=5, lambda_mult=0.7) -> Option A
  4. Quick Check:

    Correct params = k and lambda_mult [OK]
Hint: MMR uses k and lambda_mult parameters [OK]
Common Mistakes:
  • Using 'top_k' instead of 'k'
  • Using 'lambda' instead of 'lambda_mult'
  • Mixing parameter names from similarity search
3. Given the following code snippet using Langchain's similarity search and MMR retrieval, what will be the main difference in their outputs?
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)
medium
A. sim_results will return the two most similar apple recipes; mmr_results will return one apple and one different dessert recipe.
B. sim_results will return random recipes; mmr_results will return the two closest apple recipes.
C. Both sim_results and mmr_results will return the same two apple recipes.
D. sim_results will return only one recipe; mmr_results will return three recipes.

Solution

  1. Step 1: Analyze similarity_search output

    Similarity search returns the top 2 closest matches, likely both apple recipes due to query "apple dessert".
  2. 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.
  3. Final Answer:

    sim_results will return the two most similar apple recipes; mmr_results will return one apple and one different dessert recipe. -> Option A
  4. Quick Check:

    Similarity = closest; MMR = closest + diverse [OK]
Hint: Similarity = closest matches; MMR adds variety [OK]
Common Mistakes:
  • Assuming both return identical results
  • Thinking MMR returns random results
  • Confusing number of results returned
4. You wrote this code to perform MMR retrieval but get an error:
mmr = MMRRetriever(documents, k=3, lambda_mult=0.6)
results = mmr.get_relevant_documents(query)
What is the likely cause of the error?
medium
A. Calling 'get_relevant_documents' instead of 'retrieve_documents'.
B. Using 'k' instead of 'top_k' when initializing MMRRetriever.
C. Missing import statement for MMRRetriever.
D. Using 'lambda_mult' instead of 'lambda' parameter.

Solution

  1. Step 1: Check parameter names in MMRRetriever initialization

    The parameters k and lambda_mult are correct.
  2. Step 2: Confirm method name correctness

    get_relevant_documents is a valid method for retrievers.
  3. Final Answer:

    Missing import statement for MMRRetriever. -> Option C
  4. Quick Check:

    Missing import = NameError [OK]
Hint: Import MMRRetriever before use [OK]
Common Mistakes:
  • Forgetting import statement for MMRRetriever
  • Confusing method names
  • Wrong parameter names like top_k or lambda
5. You want to retrieve 3 documents related to "climate change" using Langchain. You want results that are relevant but also cover different aspects like causes, effects, and solutions. Which retrieval method and parameters should you choose?
hard
A. Use MMR retrieval with k=3 and lambda_mult=0 to get only closest matches.
B. Use similarity search with k=3 for closest matches only.
C. Use similarity search with k=1 repeated 3 times to get variety.
D. Use MMR retrieval with k=3 and lambda_mult=0.8 to balance relevance and diversity.

Solution

  1. Step 1: Identify need for relevance and diversity

    You want documents that are relevant but cover different aspects, so diversity is important.
  2. Step 2: Choose retrieval method and parameters

    MMR retrieval balances relevance and diversity using lambda_mult. A value like 0.8 prioritizes relevance while introducing some diversity.
  3. Final Answer:

    Use MMR retrieval with k=3 and lambda_mult=0.8 to balance relevance and diversity. -> Option D
  4. Quick Check:

    Diversity + relevance = MMR with 0 < lambda_mult < 1 [OK]
Hint: Lower lambda_mult increases diversity in MMR [OK]
Common Mistakes:
  • Using similarity search only for diverse results
  • Setting lambda_mult to 0 disables relevance
  • Repeating similarity search calls for variety
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard