📖 Scenario: You are building a simple document search tool using LangChain. You want to compare two ways to find relevant documents: similarity search and Maximal Marginal Relevance (MMR) retrieval. Similarity search finds documents closest to your query. MMR retrieval finds documents that are both relevant and diverse.
🎯 Goal: Build a LangChain script that loads a small set of documents, sets up a vector store, and performs both similarity search and MMR retrieval. See how the results differ.
📋 What You'll Learn
Create a list of 3 documents with exact texts
Create a LangChain vector store from the documents
Perform a similarity search for the query 'apple fruit'
Perform an MMR retrieval for the query 'apple fruit' with k=2 and fetch_k=3
💡 Why This Matters
🌍 Real World
This project shows how to build a simple document search tool that can find relevant information quickly and also provide diverse results to avoid repetition.
💼 Career
Understanding similarity search and MMR retrieval is important for building smart search engines, recommendation systems, and chatbots that use document retrieval.
Progress0 / 4 steps
1
Create the documents list
Create a list called documents containing these exact strings: 'Apple is a fruit.', 'Banana is yellow.', and 'Carrots are vegetables.'
LangChain
Hint
Use a Python list with the exact strings given.
2
Set up the vector store
Import FAISS and OpenAIEmbeddings from LangChain. Create a variable called embeddings as OpenAIEmbeddings(). Then create a vector_store by calling FAISS.from_texts(documents, embeddings).
LangChain
Hint
Use the exact class names and method calls as shown.
3
Perform similarity search
Use vector_store.similarity_search with the query 'apple fruit' and k=2. Store the result in a variable called similar_docs.
LangChain
Hint
Call similarity_search on vector_store with the exact query and k value.
4
Perform MMR retrieval
Use vector_store.max_marginal_relevance_search with the query 'apple fruit', k=2, and fetch_k=3. Store the result in a variable called mmr_docs.
LangChain
Hint
Call max_marginal_relevance_search on vector_store with the exact parameters.
Practice
(1/5)
1. What is the main difference between Similarity search and MMR retrieval in Langchain?
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
Step 1: Check parameter names in MMRRetriever initialization
The parameters k and lambda_mult are correct.
Step 2: Confirm method name correctness
get_relevant_documents is a valid method for retrievers.
Final Answer:
Missing import statement for MMRRetriever. -> Option C
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
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 using lambda_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 D