0
0
LangChainframework~30 mins

Similarity search vs MMR retrieval in LangChain - Hands-On Comparison

Choose your learning style9 modes available
Similarity Search vs MMR Retrieval with LangChain
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Call max_marginal_relevance_search on vector_store with the exact parameters.