02. Contextual CompressionRetriever
One of the difficulties faced by the search system is that when collecting data into the system, it is not known in advance which specific queries to be processed.
This means that the information most relevant to the inquiry may be buried in documents containing large amounts of unrelated text.
Passing these entire documents to your application can lead to more expensive LLM calls and lower quality responses.
ContextualCompressionRetriever Silver is designed to solve this problem.
The idea is simple. Instead of immediately returning the retrieved document as it is, you can use the context of a given query to compress the document so that only relevant information is returned.
"Compression" here means both compressing the contents of an individual document and filtering the document as a whole.
ContextualCompressionRetriever Pass the query to the base retriever, take the initial document and pass the Document Compressor.
Document Compressor takes a list of documents to reduce the content of the document or completely delete the document to shrink the list.
Source: https://drive.google.com/uc?id=1CtNgWODXZudxAWSRiWgSGEoTNrUFT98v
Copy
# API A configuration file for managing keys as environment variables.
from dotenv import load_dotenv
# load API key information
load_dotenv()Copy
TrueCopy
Copy
Copy
pretty_print_docs A function is a helper function that outputs a document list beautifully.
Copy
Default Retriever Settings
Let's start by initializing a simple vector store retriever and storing text documents in chunks.
When you ask an example question, you can confirm that retriever returns a few unrelated documents from 1~2 related documents.
Copy
Copy
ContextualCompression
LLMChainExtractor Created using DocumentCompressor That's what I applied to retriever ContextualCompressionRetriever is.
Copy
Copy
Filter documents using LLM
LLMChainFilter
LLMChainFilter Is a simpler but more powerful compressor that uses the LLM chain to filter out which of the initially searched documents and decide which one to return.
This filter does not change (compress) the document content Optionally returned To.
Copy
Copy
EmbeddingsFilter
Performing additional LLM calls for each searched document is expensive and slow.
EmbeddingsFilter It provides a cheaper and faster option by embedding documents and queries and returning only documents with embedding sufficiently similar to queries.
This saves you money and time while maintaining the relevance of your search results.
EmbeddingsFilter Wow ContextualCompressionRetriever The process of compressing and retrieving related documents using.
EmbeddingsFilterSpecified using Similarity threshold (0.86) Filter the document above.
Copy
Copy
Pipeline generation (compressor + document converter)
DocumentCompressorPipeline Using allows you to combine multiple compressors sequentially.
With Compressor BaseDocumentTransformer You can add to the pipeline, which does not perform contextual compression, but simply converts to a set of documents.
For example, TextSplitter can be used as a document transformer to split documents into smaller pieces, EmbeddingsRedundantFilter Can be used to filter duplicate documents based on embedding similarity between documents (default: 0.95 similarity or higher is considered duplicate documents).
Below, the document is first divided into smaller chunks, then duplicate documents are removed, and filtered based on relevance to the query to create a compressor pipeline.
Copy
ContextualCompressionRetriever Initialize, base_compressor in pipeline_compressor , base_retriever in retriever Use.
Copy
Copy
Last updated