
LANGCHAIN — How Can Document Retrieval be Enhanced with Contextual Compression?
The most profound technologies are those that disappear. They weave themselves into the fabric of everyday life until they are indistinguishable from it. — Mark Weiser.
Document retrieval is a critical aspect of many language model-powered applications. However, the traditional approach of retrieving documents based on pre-defined queries often results in irrelevant or excessive information being presented to the model, which can negatively impact its performance. To address this issue, LangChain has introduced a new abstraction called contextual compression. This abstraction allows for the extraction of only relevant information from retrieved documents, based on the context of a given query. In this tutorial, we'll explore how to enhance document retrieval with contextual compression using the LangChain Python package.
Introduction
Consider a scenario where a chatbot needs to retrieve and present relevant information from a set of documents in response to user queries. The traditional approach involves performing a similarity search over a vector store, retrieving documents based on the query, and then presenting them to the language model. However, the retrieved documents may contain both relevant and irrelevant information, which can impact the model’s performance.
Problem
When documents are ingested into the storage system, it’s often not known what specific queries will be used to retrieve those documents. This can lead to the retrieval of documents containing both relevant and irrelevant information. This is problematic as it can distract the language model and occupy valuable space with unnecessary information.
Solution: Introducing Contextual Compression
To address this issue, LangChain has introduced the DocumentCompressor abstraction. This abstraction allows for the compression of retrieved documents using the context of a given query, ensuring that only the relevant information is returned. The goal of this approach is to provide only the necessary information to the language model, enabling a focus on recall during the initial retrieval step, while letting the compressors handle precision.
Features
LangChain has implemented several features in the Python package to support contextual compression, including:
- A set of ready-to-use
DocumentCompressors. - A
ContextualCompressionRetrieverwhich wraps another retriever along with aDocumentCompressor, automatically compressing the retrieved documents.
Example Document Compressors
- LLMChainExtractor: Uses an LLMChain to extract only the statements relevant to the query from each document.
from langchain.llms import OpenAI
from langchain.retrievers import ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor
# Define base_retriever
base_retriever = ...
# Create LLMChainExtractor compressor
compressor = LLMChainExtractor.from_llm(OpenAI(temperature=0))
# Create contextual compression retriever
compression_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=base_retriever)
compression_retriever.get_relevant_documents("insert query here")- EmbeddingsFilter: Embeds both the retrieved documents and the query and filters out any documents whose embeddings aren’t sufficiently similar to the embedded query.
- DocumentCompressorPipeline: Enables the creation of a pipeline of transformations and compressors, allowing for sequential execution.
Getting Started
To get started with contextual compression in LangChain, refer to the detailed walkthrough in the LangChain Python package documentation. This tutorial provides a comprehensive guide to utilizing the contextual compression features to enhance document retrieval for language model-powered applications.
In conclusion, contextual compression offers a powerful approach to improving document retrieval by ensuring that only relevant information is presented to the language model. By leveraging the DocumentCompressor abstraction and the associated features in the LangChain Python package, developers can enhance the performance and precision of their language model-powered applications.
By integrating contextual compression into document retrieval workflows, developers can optimize the flow of information to language models, ultimately improving the overall user experience and accuracy of the applications.






