This code demonstrates the implementation of contextual compression in a document retrieval system using LangChain and OpenAI’s language models. The technique aims to improve the relevance and conciseness of retrieved information by compressing and extracting the most pertinent parts of documents in the context of a given query.
Motivation
Traditional document retrieval systems often return entire chunks or documents, which may contain irrelevant information. Contextual compression addresses this by intelligently extracting and compressing only the most relevant parts of retrieved documents, leading to more focused and efficient information retrieval.
Key Components
- Vector store creation from a PDF document
- Base retriever setup
- LLM-based contextual compressor
- Contextual compression retriever
- Question-answering chain integrating the compressed retriever
Method Details
Document Preprocessing and Vector Store Creation
- The PDF is processed and encoded into a vector store using a custom
encode_pdf
function.
Retriever and Compressor Setup
- A base retriever is created from the vector store.
- An LLM-based contextual compressor (LLMChainExtractor) is initialized using OpenAI’s GPT-4 model.
Contextual Compression Retriever
- The base retriever and compressor are combined into a ContextualCompressionRetriever.
- This retriever first fetches documents using the base retriever, then applies the compressor to extract the most relevant information.
Question-Answering Chain
- A RetrievalQA chain is created, integrating the compression retriever.
- This chain uses the compressed and extracted information to generate answers to queries.
Benefits of this Approach
- Improved relevance: The system returns only the most pertinent information to the query.
- Increased efficiency: By compressing and extracting relevant parts, it reduces the amount of text the LLM needs to process.
- Enhanced context understanding: The LLM-based compressor can understand the context of the query and extract information accordingly.
- Flexibility: The system can be easily adapted to different types of documents and queries.
Contextual compression in document retrieval offers a powerful way to enhance the quality and efficiency of information retrieval systems. By intelligently extracting and compressing relevant information, it provides more focused and context-aware responses to queries. This approach has potential applications in various fields requiring efficient and accurate information retrieval from large document collections.
Import libraries
import os
import sys
from dotenv import load_dotenv
from langchain.retrievers.document_compressors import LLMChainExtractor
from langchain.retrievers import ContextualCompressionRetriever
from langchain.chains import RetrievalQA
sys.path.append(os.path.abspath(os.path.join(os.getcwd(), '..'))) # Add the parent directory to the path sicnce we work with notebooks
from helper_functions import *
from evaluation.evalute_rag import *
# Load environment variables from a .env file
load_dotenv()
# Set the OpenAI API key environment variable
os.environ["OPENAI_API_KEY"] = os.getenv('OPENAI_API_KEY')
Define document’s path
path = "../data/Understanding_Climate_Change.pdf"
Create a vector store
vector_store = encode_pdf(path)
Create a retriever + contexual compressor + combine them
# Create a retriever
retriever = vector_store.as_retriever()
#Create a contextual compressor
llm = ChatOpenAI(temperature=0, model_name="gpt-4o-mini", max_tokens=4000)
compressor = LLMChainExtractor.from_llm(llm)
#Combine the retriever with the compressor
compression_retriever = ContextualCompressionRetriever(
base_compressor=compressor,
base_retriever=retriever
)
# Create a QA chain with the compressed retriever
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=compression_retriever,
return_source_documents=True
)
Example usage
query = "What is the main topic of the document?"
result = qa_chain.invoke({"query": query})
print(result["result"])
print("Source documents:", result["source_documents"])
The main topic of the document is climate change, focusing on international collaboration, national strategies, policy development, and the ethical dimensions of climate justice. It discusses frameworks like the UNFCCC and the Paris Agreement, as well as the importance of sustainable practices for future generations. Source documents: [Document(metadata={'source': '../data/Understanding_Climate_Change.pdf', 'page': 9}, page_content='Chapter 6: Global and Local Climate Action \nInternational Collaboration \nUnited Nations Framework Convention on Climate Change (UNFCCC) \nThe UNFCCC is an international treaty aimed at addressing climate change. It provides a \nframework for negotiating specific protocols and agreements, such as the Kyoto Protocol and \nthe Paris Agreement. Global cooperation under the UNFCCC is crucial for coordinated \nclimate action. \nParis Agreement \nThe Paris Agreement, adopted in 2015, aims to limit global warming to well below 2 degrees \nCelsius above pre-industrial levels, with efforts to limit the increase to 1.5 degrees Celsius. \nCountries submit nationally determined contributions (NDCs) outlining their climate action \nplans and targets. \nNational Strategies \nCarbon Pricing \nCarbon pricing mechanisms, such as carbon taxes and cap-and-trade systems, incentivize \nemission reductions by assigning a cost to carbon emissions. These policies encourage'), Document(metadata={'source': '../data/Understanding_Climate_Change.pdf', 'page': 27}, page_content='Legacy for Future Generations \nOur actions today shape the world for future generations. Ensuring a sustainable and resilient \nplanet is our responsibility to future generations. By working together, we can create a legacy \nof environmental stewardship, social equity, and global solidarity. \nChapter 19: Climate Change and Policy \nPolicy Development and Implementation \nNational Climate Policies \nCountries around the world are developing and implementing national climate policies to \naddress climate change. These policies set emission reduction targets, promote renewable \nenergy, and support adaptation measures. Effective policy implementation requires'), Document(metadata={'source': '../data/Understanding_Climate_Change.pdf', 'page': 18}, page_content='This vision includes a healthy planet, thriving ecosystems, and equitable societies. Working together towards this vision creates a sense of purpose and motivation . By embracing these principles and taking concerted action, we can address the urgent challenge of climate change and build a sustainable, resilient, and equitable world for all. The path forward requires courage, commitment, and collaboration, but the rewa rds are immense—a thriving planet and a prosperous future for generations to come. \nChapter 13: Climate Change and Social Justice \nClimate Justice \nUnderstanding Climate Justice \nClimate justice emphasizes the ethical dimensions of climate change, recognizing that its impacts are not evenly distributed. Vulnerable populations, including low -income communities, indigenous peoples, and marginalized groups, often face the greatest ris ks while contributing the least to greenhouse gas emissions. Climate justice advocates for')]