Context Compression

What is Context Compression?

Context Compression is an optimization technique that sits between the retrieval step and the generation step in a RAG pipeline. It reduces the size of retrieved documents before they are fed into the LLM, keeping only the most relevant information while discarding redundant or irrelevant content.

CN: Context Compression(上下文压缩)是RAG流程中位于检索步骤和生成步骤之间的优化技术。它在将检索到的文档送入LLM之前,压缩这些文档的大小,只保留最相关的信息,同时丢弃冗余或不相关的内容。

Imagine you’re a busy executive preparing for a meeting. Your assistant brings you 50 pages of reports (retrieved documents). Instead of reading all 50 pages, your assistant first skims through them, highlights the 5 pages that are actually relevant to today’s agenda, and gives you only those. That’s Context Compression — a “smart assistant” that filters and condenses information before it reaches the LLM.

CN: 想象你是一位忙碌的高管,正在为会议做准备。你的助理拿来50页报告(检索到的文档)。与其让你读完50页,助理先快速浏览一遍,划出与今天议程真正相关的5页,只给你这些。这就是Context Compression——一个在信息到达LLM之前进行过滤和浓缩的“智能助理”。

What are the main contents?

Context Compression techniques can be classified into several categories:

By Compression Approach:

  • Extractive Compression(抽取式压缩)
    Selects and keeps only the most important parts of the text, discarding the rest. Think of it as “highlighting” key sentences.
    选择并只保留文本中最重要的部分,丢弃其余部分。可以理解为“高亮”关键句子。
  • Abstractive Compression(抽象式/生成式压缩)
    Generates a new, shorter summary that captures the essence of the original text. Think of it as “summarization.
    生成一个新的、更短的摘要,捕捉原文的精华。可以理解为“摘要生成
  • Soft/Hard Compression(软/硬压缩)

Key Takeaways

要点ENCN
Context Compression定义Optimization technique that reduces retrieved context size before LLM generation在LLM生成之前减少检索上下文大小的优化技术
核心动机Reduce token cost, latency, and context window issues减少token成本、延迟和上下文窗口问题
LLMLingua方法Uses a small model to calculate perplexity and remove non-essential tokens使用小模型计算困惑度,删除非必要token
LongLLMLingua改进Query-aware compression with contrastive perplexity使用对比困惑度的查询感知压缩
ACC-RAG特点Dynamically adjusts compression rate based on input complexity根据输入复杂度动态调整压缩率
SARA方法Combines text snippets with semantic compression vectors结合文本片段和语义压缩向量
LangChain实现ContextualCompressionRetriever + DocumentCompressorContextualCompressionRetriever + DocumentCompressor
压缩效果60-80% reduction with 92%+ accuracy maintained减少60-80%,保持92%+准确率
两种压缩类型Extractive (keep important parts) vs Abstractive (generate summary)抽取式(保留重要部分)vs 抽象式(生成摘要)
软硬压缩Hard (remove tokens) vs Soft (compress to vectors)硬(删除token)vs 软(压缩为向量)