Bird
Raised Fist0
LangChainframework~20 mins

Contextual compression in LangChain - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Contextual Compression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2: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))
ARaises a TypeError because base_compressor cannot be None
BPrints the number of documents retrieved, equal to the number of chunks matching 'Explain AI' query
CPrints zero because no compressor filters out all documents
DRaises a NameError because ContextualCompressionRetriever is not imported
Attempts:
2 left
💡 Hint
Think about how the retriever works when no compressor is provided.
📝 Syntax
intermediate
1: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.
Acompression_retriever = ContextualCompressionRetriever(compressor=compressor, retriever=retriever)
Bcompression_retriever = ContextualCompressionRetriever(base_compressor=compressor)
Ccompression_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=retriever)
Dcompression_retriever = ContextualCompressionRetriever(base_retriever=retriever)
Attempts:
2 left
💡 Hint
Check the exact parameter names required by ContextualCompressionRetriever.
🔧 Debug
advanced
2: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?
AMissing base_retriever argument causes ValueError because it is required
Bcompressor variable is undefined causing ValueError
CContextualCompressionRetriever cannot be instantiated directly causing ValueError
DValueError occurs because base_compressor must be None
Attempts:
2 left
💡 Hint
Check the required parameters for ContextualCompressionRetriever initialization.
❓ state_output
advanced
1: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?
AThe compressor duplicates documents, increasing the number of results
BThe compressor has no effect; all documents are returned unchanged
CThe compressor filters out all documents, returning an empty list
DThe compressor reduces document size by summarizing, so fewer but more relevant documents are returned
Attempts:
2 left
💡 Hint
Think about what compression means in this context.
🧠 Conceptual
expert
2:30remaining
Which statement best describes the role of ContextualCompressionRetriever in LangChain?
Choose the statement that best explains what ContextualCompressionRetriever does in LangChain.
AIt wraps a retriever and a compressor to return compressed, relevant documents for a query
BIt replaces the retriever and compressor with a single unified model
CIt retrieves documents but does not support compression
DIt only compresses documents without retrieving them
Attempts:
2 left
💡 Hint
Consider how retrieval and compression work together.

Practice

(1/5)
1. What is the main purpose of Contextual compression in langchain?
easy
A. To check grammar and spelling errors
B. To translate text into multiple languages
C. To generate random text from a prompt
D. To reduce text size while keeping its important meaning

Solution

  1. Step 1: Understand the goal of contextual compression

    Contextual compression aims to shrink text but keep its main meaning intact.
  2. Step 2: Compare options with this goal

    Only To reduce text size while keeping its important meaning matches this goal; others describe different tasks.
  3. Final Answer:

    To reduce text size while keeping its important meaning -> Option D
  4. Quick Check:

    Contextual compression = reduce text size + keep meaning [OK]
Hint: Focus on meaning retention while reducing text size [OK]
Common Mistakes:
  • Confusing compression with translation
  • Thinking it generates new text
  • Assuming it checks grammar
2. Which of the following is the correct way to create a ContextualCompressionRetriever in langchain?
easy
A. ContextualCompressionRetriever(base_retriever, llm)
B. ContextualCompressionRetriever(llm, base_retriever)
C. ContextualCompressionRetriever()
D. ContextualCompressionRetriever(llm)

Solution

  1. Step 1: Recall the constructor parameters order

    The correct order is base_retriever first, then llm.
  2. Step 2: Match options with correct order

    Only ContextualCompressionRetriever(base_retriever, llm) matches this order; others have wrong order or missing arguments.
  3. Final Answer:

    ContextualCompressionRetriever(base_retriever, llm) -> Option A
  4. Quick Check:

    Constructor order = base_retriever, llm [OK]
Hint: Remember: retriever first, then language model [OK]
Common Mistakes:
  • Swapping the order of parameters
  • Omitting required arguments
  • Using empty constructor
3. Given this code snippet, what will be the output type of compressed_docs?
from langchain.retrievers import ContextualCompressionRetriever
compressed_retriever = ContextualCompressionRetriever(base_retriever, llm)
compressed_docs = compressed_retriever.get_relevant_documents(query)
medium
A. A list of compressed Document objects
B. A single compressed string
C. A dictionary with original and compressed texts
D. An error because get_relevant_documents is undefined

Solution

  1. Step 1: Understand what get_relevant_documents returns

    It returns a list of Document objects relevant to the query.
  2. Step 2: Consider compression effect

    ContextualCompressionRetriever compresses content inside Document objects but still returns a list of Documents.
  3. Final Answer:

    A list of compressed Document objects -> Option A
  4. Quick Check:

    get_relevant_documents returns list of Documents [OK]
Hint: Retriever methods return lists of Documents, not strings [OK]
Common Mistakes:
  • Expecting a single string instead of list
  • Thinking it returns a dictionary
  • Assuming method does not exist
4. You wrote this code but get an error: TypeError: ContextualCompressionRetriever() missing 1 required positional argument. What is the likely cause?
compressed_retriever = ContextualCompressionRetriever(llm)
medium
A. You passed arguments in the wrong order
B. You forgot to pass the base retriever argument
C. You need to import ContextualCompressionRetriever differently
D. The llm argument is invalid

Solution

  1. Step 1: Check constructor requirements

    ContextualCompressionRetriever requires both base_retriever and llm arguments.
  2. Step 2: Analyze the error message and code

    The code passes only llm, missing base_retriever, causing the error.
  3. Final Answer:

    You forgot to pass the base retriever argument -> Option B
  4. Quick Check:

    Missing base retriever causes TypeError [OK]
Hint: Always pass base retriever first, then llm [OK]
Common Mistakes:
  • Passing only one argument
  • Swapping argument order
  • Ignoring error message details
5. You want to speed up document retrieval by compressing context but keep key info. Which approach best applies ContextualCompressionRetriever correctly in a langchain app?
hard
A. Replace your retriever with only an LLM that compresses text manually
B. Use ContextualCompressionRetriever without a base retriever to compress all documents at once
C. Wrap your existing retriever with ContextualCompressionRetriever passing your LLM, then query it
D. Compress documents before adding them to the retriever, then use a normal retriever

Solution

  1. Step 1: Understand how ContextualCompressionRetriever works

    It wraps an existing retriever and uses an LLM to compress retrieved documents on the fly.
  2. Step 2: Evaluate options for correct usage

    Wrap your existing retriever with ContextualCompressionRetriever passing your LLM, then query it correctly wraps the retriever with compression; others either omit retriever or do manual steps outside the pattern.
  3. Final Answer:

    Wrap your existing retriever with ContextualCompressionRetriever passing your LLM, then query it -> Option C
  4. Quick Check:

    Wrap retriever + LLM = ContextualCompressionRetriever usage [OK]
Hint: Wrap retriever with compression, don't replace or skip it [OK]
Common Mistakes:
  • Skipping base retriever wrapping
  • Trying to compress all docs at once without retriever
  • Replacing retriever with only LLM
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard