Search Types in LangChain Retrievers
Posted: Feb 2, 2025.
The SearchType enum in LangChain allows you to specify what kind of vector search to perform when using retrievers. Let's explore how this works and how to use it effectively.
What is SearchType?
SearchType is an enumeration class that defines the different types of vector searches that can be performed by LangChain retrievers. It provides two options:
similarity
: A standard similarity search that finds the most similar vectors to the querymmr
(Maximal Marginal Relevance): A search method that provides diversity in results by considering both similarity and diversity when selecting documents
Reference
Option | Description |
---|---|
similarity | Performs a standard similarity search using vector embeddings |
mmr | Uses Maximal Marginal Relevance to rerank similarity search results for better diversity |
How to Use SearchType
Here are some examples of using SearchType in your retrievers:
Basic Similarity Search
Using MMR for Diverse Results
With MultiVectorRetriever
The key difference between similarity
and mmr
is that MMR will try to ensure diversity in the results by balancing similarity scores with how different documents are from each other. This can be particularly useful when you want to:
- Avoid redundant information in your results
- Get a broader coverage of potentially relevant information
- Reduce the impact of near-duplicate documents in your results
When using MMR, you can adjust the mmr_lambda
parameter (typically between 0 and 1) to control the tradeoff between relevance and diversity. A higher value gives more weight to similarity, while a lower value prioritizes diversity.
This functionality is particularly useful in applications like:
- Question answering systems where you want diverse context
- Content recommendation systems
- Document summarization where you need varied perspectives
- Research tasks requiring broad coverage of a topic
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.
LangChain DocsJoin 10,000+ subscribers
Every 2 weeks, latest model releases and industry news.
An alternative to LangSmith
Open-source LangChain monitoring, prompt management, and magic. Get started in 2 minutes.