Challenge - 5 Problems
Contextual Compression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this LangChain Contextual Compression Retriever code?
Given the following code snippet using LangChain's ContextualCompressionRetriever, what will be the output when retrieving documents for the query 'Explain AI'?
LangChain
from langchain.document_loaders import TextLoader from langchain.indexes import VectorstoreIndexCreator from langchain.retrievers import ContextualCompressionRetriever from langchain.text_splitter import RecursiveCharacterTextSplitter from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS # Load documents loader = TextLoader('ai_text.txt') docs = loader.load() # Split documents splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=10) split_docs = splitter.split_documents(docs) # Create vectorstore embeddings = OpenAIEmbeddings() vectorstore = FAISS.from_documents(split_docs, embeddings) # Create compression retriever compression_retriever = ContextualCompressionRetriever( base_compressor=None, # No compressor for simplicity base_retriever=vectorstore.as_retriever() ) # Retrieve compressed docs results = compression_retriever.get_relevant_documents('Explain AI') print(len(results))
Attempts:
2 left
💡 Hint
Think about how the retriever works when no compressor is provided.
✗ Incorrect
When base_compressor is None, ContextualCompressionRetriever behaves like the base retriever, returning all matching documents. So the output is the count of matching chunks.
📝 Syntax
intermediate1:30remaining
Which option correctly initializes a ContextualCompressionRetriever with a compressor?
Select the code snippet that correctly creates a ContextualCompressionRetriever with a base compressor and base retriever.
Attempts:
2 left
💡 Hint
Check the exact parameter names required by ContextualCompressionRetriever.
✗ Incorrect
ContextualCompressionRetriever requires both base_compressor and base_retriever parameters. Option C uses the correct parameter names and provides both.
🔧 Debug
advanced2:00remaining
Why does this ContextualCompressionRetriever code raise a ValueError?
Consider this code snippet:
from langchain.retrievers import ContextualCompressionRetriever
retriever = ContextualCompressionRetriever(base_compressor=compressor)
What is the cause of the ValueError?
Attempts:
2 left
💡 Hint
Check the required parameters for ContextualCompressionRetriever initialization.
✗ Incorrect
ContextualCompressionRetriever requires both base_compressor and base_retriever. Omitting base_retriever causes a ValueError.
❓ state_output
advanced1:30remaining
What is the effect of using a compressor in ContextualCompressionRetriever on retrieved documents?
If a compressor is used in ContextualCompressionRetriever, how does it affect the documents returned for a query?
Attempts:
2 left
💡 Hint
Think about what compression means in this context.
✗ Incorrect
The compressor summarizes or compresses documents before retrieval, so fewer but more focused documents are returned.
🧠 Conceptual
expert2:30remaining
Which statement best describes the role of ContextualCompressionRetriever in LangChain?
Choose the statement that best explains what ContextualCompressionRetriever does in LangChain.
Attempts:
2 left
💡 Hint
Consider how retrieval and compression work together.
✗ Incorrect
ContextualCompressionRetriever combines a base retriever and a compressor to return compressed relevant documents, improving efficiency.