Table of Contents
Build a sample vector database
Simple usage
Custom Tips
Overview of context compression
Implementation steps
Create a custom searcher
interface
Example
in conclusion
Frequently Asked Questions
Home Technology peripherals AI 3 Advanced Strategies for Retrievers in LangChain

3 Advanced Strategies for Retrievers in LangChain

Mar 19, 2025 am 11:16 AM

LangChain Retrieval: Efficient and Flexible Access to Documents

The searcher in the LangChain framework plays a crucial role, providing a flexible interface for returning documents based on unstructured queries. Unlike vector databases, the searcher does not need to store documents; its main function is to retrieve relevant information from a large amount of data. While vector databases can serve as the basis for retriers, there are various types of retriers, each customizable for a specific use case.

3 Advanced Strategies for Retrievers in LangChain

Learning Objectives

  • Understand the key role of searchers in LangChain and achieve efficient and flexible document retrieval to meet various application needs.
  • Learn how LangChain's searcher (from vector databases to multi-queries and context compression) simplifies access to relevant information.
  • This guide covers various searcher types in LangChain and explains how each searcher is customized to optimize query processing and data access.
  • Dig into LangChain's searcher capabilities and check out tools for enhancing document retrieval accuracy and relevance.
  • Learn how LangChain's custom retriever adapts to specific needs, enabling developers to create highly responsive applications.
  • Explore LangChain's search techniques that integrate language models and vector databases to obtain more accurate and efficient search results.

Table of contents

  • Learning Objectives
  • Searcher in LangChain
  • Use vector database as a searcher
  • Using MultiQueryRetriever
    • Build a sample vector database
    • Simple usage
    • Custom Tips
  • How to search using context compression
    • Overview of context compression
  • Create a custom searcher
    • interface
    • Example
  • in conclusion
  • Frequently Asked Questions

Searcher in LangChain

The searcher receives a string query as input and outputs a list of Document objects. This mechanism allows applications to efficiently acquire relevant information, enabling advanced interactions with large data sets or knowledge bases.

  1. Use vector database as a searcher

The vector database retriever efficiently retrieves documents by utilizing vector representations. It acts as a lightweight wrapper for vector storage classes, conforms to the searcher interface and uses methods such as similarity search and maximum marginal correlation (MMR).

To create a retriever from a vector database, use the .as_retriever method. For example, for a Pinecone vector database based on customer reviews, we can set it up as follows:

 from langchain_community.document_loaders import CSVLoader
from langchain_community.vectorstores import Pinecone
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

loader = CSVLoader("customer_reviews.csv")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=50)
texts = text_splitter.split_documents(documents)
embeddings = OpenAIEmbeddings()
vectorstore = Pinecone.from_documents(texts, embeddeds)
retriever = vectorstore.as_retriever()
Copy after login

We can now use this searcher to query related comments:

 docs = retriever.invoke("What do customers think about the battery life?")
Copy after login

By default, the searcher uses similarity search, but we can specify MMR as the search type:

 retriever = vectorstore.as_retriever(search_type="mmr")
Copy after login

Additionally, we can pass parameters such as similarity score thresholds, or use top-k to limit the number of results:

 retriever = vectorstore.as_retriever(search_kwargs={"k": 2, "score_threshold": 0.6})
Copy after login

Output:

3 Advanced Strategies for Retrievers in LangChain

Using a vector database as a searcher can enhance document retrieval by ensuring efficient access to relevant information.

  1. Using MultiQueryRetriever

MultiQueryRetriever enhances distance-based vector database retrieval by addressing common limitations such as changes in query wording and suboptimal embedding. Using large language model (LLM) automation prompt adjustments, multiple queries can be generated for a given user input from different angles. This process allows retrieval of relevant documents for each query and combine the results to produce a richer set of potential documents.

Build a sample vector database

To demonstrate MultiQueryRetriever, let's create a vector store using the product description from the CSV file:

 from langchain_community.document_loaders import CSVLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

# Load product description loader = CSVLoader("product_descriptions.csv")
data = loader.load()

# Split text into blocks text_splitter = CharacterTextSplitter(chunk_size=300, chunk_overlap=50)
documents = text_splitter.split_documents(data)

# Create vector storage embeddings = OpenAIEmbeddings()
vectordb = FAISS.from_documents(documents, embeddeds)
Copy after login

Simple usage

To use MultiQueryRetriever, specify the LLM used for query generation:

 from langchain.retrievers.multi_query import MultiQueryRetriever
from langchain_openai import ChatOpenAI

question = "What features do customers value in smartphones?"
llm = ChatOpenAI(temperature=0)
retriever_from_llm = MultiQueryRetriever.from_llm(
    retriever=vectordb.as_retriever(), llm=llm
)
unique_docs = retriever_from_llm.invoke(question)
len(unique_docs) # Number of unique documents retrieved
Copy after login

Output:

3 Advanced Strategies for Retrievers in LangChain

MultiQueryRetriever generates multiple queries, enhancing the diversity and relevance of retrieved documents.

Custom Tips

To adjust the generated query, you can create a custom PromptTemplate and an output parser:

 from langchain_core.output_parsers import BaseOutputParser
from langchain_core.prompts import PromptTemplate
from typing import List

# Custom output parser class LineListOutputParser(BaseOutputParser[List[str]]):
    def parse(self, text: str) -> List[str]:
        return list(filter(None, text.strip().split("\n")))

output_parser = LineListOutputParser()

# Custom prompts for query generation QUERY_PROMPT = PromptTemplate(
    input_variables=["question"],
    template="""Generate five different versions of the question: {question}"""
)

llm_chain = QUERY_PROMPT | llm | output_parser

# Initialize the retriever retriever = MultiQueryRetriever(
    retriever=vectordb.as_retriever(), llm_chain=llm_chain, parser_key="lines"
)

unique_docs = retriever.invoke("What features do customers value in smartphones?")
len(unique_docs) # Number of unique documents retrieved
Copy after login

Output

3 Advanced Strategies for Retrievers in LangChain

Using MultiQueryRetriever can achieve a more efficient search process, ensuring diverse and comprehensive results based on user queries.

  1. How to search using context compression

Retrieving relevant information from a large collection of documents can be challenging, especially when data ingestion is not known about the specific query the user will make. Often, valuable insights are hidden in lengthy documents, resulting in inefficient and costly calls to the language model (LLM) while providing less responsiveness than ideal. Context compression solves this problem by improving the search process, ensuring that relevant information is returned only based on the user's query. This compression includes reducing the content of a single document and filtering out irrelevant documents.

Overview of context compression

The context compression searcher runs by integrating the basic searcher with the document compressor. This method does not return the document in its entirety, but compresses the document based on the context provided by the query. This compression includes reducing the content of a single document and filtering out irrelevant documents.

Implementation steps

  1. Initialize the basic searcher: First set up a normal vector storage searcher. For example, consider a news article about climate change policy:
 from langchain_community.document_loaders import TextLoader
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter

# Load and split the article documents = TextLoader("climate_change_policy.txt").load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

# Initialize vector storage retriever retriever = FAISS.from_documents(texts, OpenAIEmbeddings()).as_retriever()
Copy after login
  1. Execute an initial query: Execute a query to view the results returned by the basic retriever, which may include relevant and unrelated information.
 docs = retriever.invoke("What actions are being proposed to combat climate change?")
Copy after login
  1. Enhanced search using context compression: wrap the basic searcher with ContextualCompressionRetriever, and extract relevant content using LLMChainExtractor:
 from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor
from langchain_openai import OpenAI

llm = OpenAI(temperature=0)
compressor = LLMChainExtractor.from_llm(llm)
compression_retriever = ContextualCompressionRetriever(
    base_compressor=compressor, base_retriever=retriever
)

# Perform compressed_docs = compression_retriever.invoke("What actions are being proposed to combat climate change?")
Copy after login

View the compressed results: ContextualCompressionRetriever processes the initial document and extracts only relevant information related to the query, thereby optimizing the response.

Create a custom searcher

Retrieval is essential in many LLM applications. Its task is to obtain relevant documents based on user queries. These documents are formatted as LLM prompts, enabling them to generate appropriate responses.

interface

To create a custom retriever, extend the BaseRetriever class and implement the following:

method describe Required/Optional
_get_relevant_documents Search documents related to the query. Required
_aget_relevant_documents Asynchronous implementation for native support. Optional

Inherited from BaseRetriever will provide standard Runnable functionality to your retriever.

Example

Here is an example of a simple retriever:

 from typing import List
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever

class ToyRetriever(BaseRetriever):
    """A simple retriever that returns the first k documents containing the user's query."""
    documents: List[Document]
    k: int

    def _get_relevant_documents(self, query: str) -> List[Document]:
        matching_documents = [doc for doc in self.documents if query.lower() in doc.page_content.lower()]
        return matching_documents[:self.k]

# Example usage documents = [
    Document("Dogs are great companies.", {"type": "dog"}),
    Document("Cats are independent pets.", {"type": "cat"}),
]

retriever = ToyRetriever(documents=documents, k=1)
result = retriever.invoke("dog")
print(result[0].page_content)
Copy after login

Output

3 Advanced Strategies for Retrievers in LangChain

This implementation provides a simple way to retrieve documents based on user input, illustrating the core functionality of a custom searcher in LangChain.

in conclusion

In the LangChain framework, the searcher is a powerful tool that can effectively access relevant information in various document types and use cases. By understanding and implementing different searcher types (such as vector storage retrievers, MultiQueryRetriever, and context compression retrievers), developers can customize document retrievals based on the specific needs of their applications.

Each retriever type has unique advantages, from using MultiQueryRetriever to using context compression to optimizing responses. Additionally, creating a custom searcher provides greater flexibility to accommodate special requirements that built-in options may not meet. Mastering these search techniques allows developers to build more efficient and responsive applications that leverage the potential of language models and large data sets.

Frequently Asked Questions

Q1. What is the main function of the searcher in LangChain? A1. The main function of the searcher is to obtain relevant documents based on the query. This helps the application effectively access necessary information in large datasets without storing documents themselves.

Q2. How is the difference between a searcher and a vector database? A2. A vector database is used to store documents in a way that allows similarity-based searches, and the searcher is an interface for searching documents based on queries. Although a vector database can be part of a searcher, the searcher's task focuses on obtaining relevant information.

Q3. What is MultiQueryRetriever and how does it work? A3. MultiQueryRetriever improves search results by creating multiple variants of queries using language models. This method captures a wider range of documents that may be related to different wording issues, thereby enhancing the richness of the search information.

Q4. Why is context compression important? A4. Context compression optimizes search results by reducing the content of the document to only relevant parts and filtering out unrelated information. This is especially useful in large collections, as the complete documentation may contain unrelated details, saving resources and providing a more centralized response.

Q5. What are the requirements for setting MultiQueryRetriever? A5. To set up MultiQueryRetriever, you need a vector database for document storage, a language model (LLM) for generating multiple query perspectives, and optional custom prompt templates to further optimize query generation.

The above is the detailed content of 3 Advanced Strategies for Retrievers in LangChain. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

I Tried Vibe Coding with Cursor AI and It's Amazing! I Tried Vibe Coding with Cursor AI and It's Amazing! Mar 20, 2025 pm 03:34 PM

Vibe coding is reshaping the world of software development by letting us create applications using natural language instead of endless lines of code. Inspired by visionaries like Andrej Karpathy, this innovative approach lets dev

Top 5 GenAI Launches of February 2025: GPT-4.5, Grok-3 & More! Top 5 GenAI Launches of February 2025: GPT-4.5, Grok-3 & More! Mar 22, 2025 am 10:58 AM

February 2025 has been yet another game-changing month for generative AI, bringing us some of the most anticipated model upgrades and groundbreaking new features. From xAI’s Grok 3 and Anthropic’s Claude 3.7 Sonnet, to OpenAI’s G

How to Use YOLO v12 for Object Detection? How to Use YOLO v12 for Object Detection? Mar 22, 2025 am 11:07 AM

YOLO (You Only Look Once) has been a leading real-time object detection framework, with each iteration improving upon the previous versions. The latest version YOLO v12 introduces advancements that significantly enhance accuracy

Is ChatGPT 4 O available? Is ChatGPT 4 O available? Mar 28, 2025 pm 05:29 PM

ChatGPT 4 is currently available and widely used, demonstrating significant improvements in understanding context and generating coherent responses compared to its predecessors like ChatGPT 3.5. Future developments may include more personalized interactions and real-time data processing capabilities, further enhancing its potential for various applications.

Best AI Art Generators (Free & Paid) for Creative Projects Best AI Art Generators (Free & Paid) for Creative Projects Apr 02, 2025 pm 06:10 PM

The article reviews top AI art generators, discussing their features, suitability for creative projects, and value. It highlights Midjourney as the best value for professionals and recommends DALL-E 2 for high-quality, customizable art.

Google's GenCast: Weather Forecasting With GenCast Mini Demo Google's GenCast: Weather Forecasting With GenCast Mini Demo Mar 16, 2025 pm 01:46 PM

Google DeepMind's GenCast: A Revolutionary AI for Weather Forecasting Weather forecasting has undergone a dramatic transformation, moving from rudimentary observations to sophisticated AI-powered predictions. Google DeepMind's GenCast, a groundbreak

Which AI is better than ChatGPT? Which AI is better than ChatGPT? Mar 18, 2025 pm 06:05 PM

The article discusses AI models surpassing ChatGPT, like LaMDA, LLaMA, and Grok, highlighting their advantages in accuracy, understanding, and industry impact.(159 characters)

o1 vs GPT-4o: Is OpenAI's New Model Better Than GPT-4o? o1 vs GPT-4o: Is OpenAI's New Model Better Than GPT-4o? Mar 16, 2025 am 11:47 AM

OpenAI's o1: A 12-Day Gift Spree Begins with Their Most Powerful Model Yet December's arrival brings a global slowdown, snowflakes in some parts of the world, but OpenAI is just getting started. Sam Altman and his team are launching a 12-day gift ex

See all articles