avatarLaxfed Paulacy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2245

Abstract

’s performance.</p><h2 id="ff46">Problem</h2><p id="5d66">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.</p><h2 id="d975">Solution: Introducing Contextual Compression</h2><p id="f5de">To address this issue, LangChain has introduced the <code>DocumentCompressor</code> 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.</p><h2 id="09a2">Features</h2><p id="3396">LangChain has implemented several features in the Python package to support contextual compression, including:</p><ul><li>A set of ready-to-use <code>DocumentCompressors</code>.</li><li>A <code>ContextualCompressionRetriever</code> which wraps another retriever along with a <code>DocumentCompressor</code>, automatically compressing the retrieved documents.</li></ul><h2 id="c934">Example Document Compressors</h2><ol><li><b>LLMChainExtractor</b>: Uses an LLMChain to extract only the statements relevant to the query from each document.</li></ol><div id="a7ba"><pre><span class="hljs-keyword">from</span> langchain.llms import OpenAI <span class="hljs-keyword">from</span> langchain.retrievers import ContextualCompressionRetriever <span class="hljs-keyword">from</span> langchain.retrievers.document_compressors import LLMChainExtractor

<span class="hljs-comment"># Define base_retriever</span> base_retriever = <span class="hljs-built_in">..</span>.

<span class="hljs-comment"># Create LLMChainExtractor compressor</span> compressor = LLMChainExtractor.from_llm(OpenAI(<span class="hljs-attribute">temperature</span>=0))

<span class="hljs-comment"># Create contextual compression retriever</span> compression_retriever = ContextualCompressionRetriev

Options

er(<span class="hljs-attribute">base_compressor</span>=compressor, <span class="hljs-attribute">base_retriever</span>=base_retriever) compression_retriever.get_relevant_documents(<span class="hljs-string">"insert query here"</span>)</pre></div><ol><li><b>EmbeddingsFilter</b>: Embeds both the retrieved documents and the query and filters out any documents whose embeddings aren’t sufficiently similar to the embedded query.</li><li><b>DocumentCompressorPipeline</b>: Enables the creation of a pipeline of transformations and compressors, allowing for sequential execution.</li></ol><h2 id="d540">Getting Started</h2><p id="5f0c">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.</p><p id="8e9a">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.</p><div id="ad23" class="link-block"> <a href="https://readmedium.com/langchain-auto-evaluator-55f338b43417"> <div> <div> <h2>LANGCHAIN — Auto Evaluator</h2> <div><h3>In the software world, the moment you start using someone else’s software, you are living in their world, under their…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*nu7ZXSdSXeo6aCLEJYoZpg.jpeg)"></div> </div> </div> </a> </div><p id="b12c">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.</p></article></body>

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 ContextualCompressionRetriever which wraps another retriever along with a DocumentCompressor, automatically compressing the retrieved documents.

Example Document Compressors

  1. 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")
  1. EmbeddingsFilter: Embeds both the retrieved documents and the query and filters out any documents whose embeddings aren’t sufficiently similar to the embedded query.
  2. 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.

Enhanced
Document
Retrieval
Langchain
ChatGPT
Recommended from ReadMedium