编者按: 如何最大限度地发挥 LLMs 的强大能力,同时还能控制其推理成本?这是当前业界研究的一个热点课题。
针对这一问题,本期精心选取了一篇关于"提示词压缩"(Prompt Compression)技术的综述文章。正如作者所说,提示词压缩技术的核心目标是压缩向 LLMs 输入的上下文信息,删减非关键内容,保留语义核心,从而在不影响模型表现的前提下,降低推理成本。
文中全面介绍了多种提示词压缩算法的原理和实现细节,包括基于信息熵的Selective Context、基于软提示调优的AutoCompressor、引入数据蒸馏方法的LLMLingua-2、综合利用问题语义的LongLLMLingua等。作者还贴心地附上了代码示例,以便各位读者可以动手实践,加深对算法的理解。
你是否曾因难以处理冗长的提示词而寝食难安,被昂贵的推理成本所困扰?现在,就让我们跟随本文的脚步,开启一场 Prompt Compression 技术的学习之旅吧!也许在了解某个算法时灵感闪现,你就能找到突破瓶颈的金钥匙。
作者 | Florian June
编译 | 岳扬
RAG 方法可能会面临两大挑战:
- 大语言模型(LLMs)往往有上下文长度(context length)的限制。 这意味着,随着输入文本的长度增长,处理过程不仅变得更加耗时,成本也随之增加。
- 检索出的上下文未必都能派上用场。 有时,仅有一小部分信息对解答问题有帮助。在某些情形下,为了回答某些特定问题,可能需要整合来自多个文本片段的信息。即便实施了重排序(re-ranking)技术,这一难题依然未能得到解决。
为了解决上述问题,LLM 的提示词压缩技术(Prompt compression)应运而生。从本质上讲,其目的是精炼提示词中的关键信息,使得每个输入的词元(input tokens)都承载更多价值,从而提升模型效率并还能控制成本。这一理念在图 1 的右下角进行了直观展示。
图 1:RAG 架构中的提示词压缩技术(见图右下角)。如紫色虚线标记的部分所示,某些压缩方法能够直接作用于已检索的上下文信息。此图由作者绘制。
如图 1 中紫色虚线标记的部分所示,部分压缩方法可以直接应用于从大语言模型中检索出的上下文信息。
总的来说,提示词压缩方法可以分为四大类:
- 基于信息熵(information entropy)的方法:例如 Selective Context[1]、LLMLingua[2] 和 LongLLMLingua[3]。这些方法利用小型语言模型来计算原始提示词中每个 token 的自信息(self-information )(译者注:自信息,又称为惊喜度(surprisal)或信息含量(information content),是信息理论中的核心概念之一。它用来量化某个事件所传达的信息量的大小。)或困惑度(perplexity)。接着删除那些困惑度较低的 token ,实现压缩目的。
- 基于 soft prompt tuning (译者注:soft prompt tuning 不直接修改模型的权重,而是引入一组可学习的连续向量(通常称为"soft prompts"),这种方法允许模型在不改变其核心结构的情况下适应不同的下游任务,同时保留了模型在预训练阶段学到的一般知识。)的方法:如 AutoCompressor[4] 和 GIST[5]。此类方法需要对大语言模型的参数进行微调,使其适用于特定领域,但不能直接应用于黑盒大语言模型(black-box LLM)。
- 先进行数据蒸馏,再训练模型生成更易解释的文本摘要:这类方法可以跨不同语言模型迁移,并能应用于无需梯度更新的黑盒大语言模型。代表性的方法包括 LLMLingua-2[6] 和 RECOMP[7]。
- 基于词元合并(token merging)或词元剪枝(token pruning)的方法:如 ToMe[8] 和 AdapLeR[9]。这些方法通常需要在推理过程中对模型进行微调或生成中间结果。
鉴于第四类方法最初是为了像 ViT 或 BERT 这样的较小模型而提出的,本文将重点介绍前三类方法中代表性算法的原理。
01 Selective Context
1.1 作者的领悟见解
图 2 表明,大语言模型(LLM)即使在缺乏完整上下文或对话历史的情况下,也能对用户的询问做出回应。即便某些相关细节被省略,大语言模型(LLM)依旧能给出用户期望的回答。这或许是因为大语言模型(LLM)能够从上下文信息和预训练阶段积累的知识中推断出缺失的信息。
图 2:即便去除了部分非关键信息,大语言模型(LLM)依然能准确作答。来源:《Selective Context》[1]
由此看来,我们可以通过筛选掉非关键信息来优化上下文长度(context length),而不会影响其整体性能。这就是 Selective Context 方法的关键所在。
Selective Context 策略采用小型语言模型(SLM),来计算给定上下文中各个词汇单元(比如句子、短语或词语)的自信息值。然后,基于这些自信息值(self-information)进一步评估各单元的信息含量。通过仅保留自信息值较高的内容,Selective Context 为大语言模型(LLM)提供了更为简洁、高效的 context representation (译者注:经过数学化或模型化文本或对话后的机器可处理的上下文信息)。这一做法不会对其在各种任务中的表现造成负面影响。
1.2 Self-Information 自信息
Selective Context 运用自信息(self-information)来衡量内容的价值。
自信息,又称为惊喜度(surprisal)或信息含量(information content),是信息理论中的核心概念之一。它用来量化某个事件所传达的信息量的大小。具体来说,它是 token 出现概率的负对数形式:
这里,𝐼(𝑥) 代表 token 𝑥 的自信息量,而 𝑃(𝑥) 则指代该 token 的出现概率。
在信息论框架(information theory)下,自信息反映了事件发生时带来的惊喜程度或不确定性程度。那些不常见的事件,由于包含了更多新颖的信息,因而具有较高的自信息值。 相比之下,频繁发生的事件,因其提供的新信息较少,自信息值也就相应较低。
1.3 Algorithm 算法
为了便于阐述其背后的原理,我们不妨一同探究一下其源代码。
首要步骤是配置开发环境,安装必需的 Python 库以及下载 Spacy 模型。
安装完成后,版本信息如下:
测试代码如下所示:
初次执行时,系统会自动下载 GPT-2 模型,该模型的文件大小接近 500MB。图 3 呈现了测试代码的具体运行结果。
图 3:Selective Context 算法测试代码运行结果。截图由作者提供。
随后,我们将深入研究 sc(text) 函数。该函数的内部实现代码[10]如下:
这段代码的核心操作分为三个阶段:
- 首先,计算出上下文中每一个 token 的自信息值。
- 接着,依据词汇单位(比如短语或句子)整合 token 与其对应的自信息。
- 最后,采取有选择的方式保留必要的信息上下文,从而达到优化的目的。
第一步:自信息的计算
给定上下文 𝐶=𝑥0,𝑥1,…,𝑥𝑛 ,其中每个 𝑥𝑖 均代表一个 token ,我们可以借助因果语言模型(例如 GPT-2、OPT 或 LLaMA)来求解每个 token 𝑥𝑖 的自信息值:
若你选用的是 GPT-2 模型,以下便是实现此计算过程的相应代码片段[11]:
第二步:整合为词汇单元(Lexical Units)
如果仅仅在 tokens 层面上执行 selective context filtering(译者注:识别和保留那些对当前任务或用户查询最为关键的信息,同时过滤掉不太相关或冗余的部分。),可能会导致最终的上下文失去连贯性。举个例子,原本的数字"2009"在压缩后可能会变成"209",这样的结果显然不够合理。
鉴于此,除了在 tokens 层面进行筛选外,同时在短语和句子层面上实行过滤策略也是极其重要的。在这里,我们所说的过滤(filtering)基本单位------词汇单元,可以是个别的 token ,也可以是完整的短语或是句子。
那么,怎样才能计算出每个词汇单元 𝑢=(𝑥𝑡,…,𝑥𝑡+𝛼) 的自信息呢?我们可以根据自信息的可加性原则,将组成 u 的每个 token 的自信息相加:
下面是具体的代码实现[12],为了便于调试,我对部分变量添加了详细的注释:
第三步:精选保留信息含量高的上下文
在计算了每个词汇单元的自信息之后,我们面临的问题是如何判断其信息含量。论文介绍了一种创新方法,利用基于百分位数的筛选策略,动态挑选出信息最丰富的内容。这种方法相较于设定固定阈值或仅仅保留前 k 个最高信息量的词汇单元更为灵活有效。
我们的操作流程是先按自信息值(self-information values)从高到低排序所有词汇单元 ,接着计算所有词汇单元自信息值的 p-th percentile (译者注:“p-th percentile” 在统计学中指的是数据分布的一个特定点,在这一点之下包含了总数据中 p% 的数值。举个例子,假设你有一个班级的数学成绩分布,如果某个学生的成绩位于第90百分位(90th percentile),这意味着班上90%的学生的成绩低于或等于他的成绩,而他仅比剩下的10%的学生成绩低。)。最后,我们精挑细选出那些自信息值不低于该百分位数的词汇单元,确保所保留的都是信息含量最高的部分。
相关代码[13]如下:
02 LLMLingua
2.1 Overview 概览
LLMLingua[2] 这种方法认为,Selective Context[1] 方法常常忽略了压缩内容间的内在联系及 LLM 与用于提示词压缩的小型语言模型间的协同作用。LLMLingua 正好解决了这些问题。
具体而言,参照图 4,LLMLingua 利用 budget controller 为原始提示词的各个组成部分(如指导性提示词、演示样例和问题)动态分配不同的压缩率。同时,它采取粗粒度的 demonstration-level (译者注:在完整的演示案例上进行压缩或处理,而不是单独处理每个小的组成部分(比如单词或短语)。)压缩策略,确保即使在高度压缩的情况下,语义依然完整无损。此外,LLMLingua[2] 还引入了一种基于 tokens 的迭代算法,进一步优化细粒度的提示词压缩过程。
图 4:LLMLingua 方法的架构概览。来源:LLMLingua[2]
与 Selective Context 相比,LLMLingua 能更有效地保留提示词中的关键信息,同时还能够考虑到 tokens 之间的条件依赖关系,其压缩倍数可达 20 倍。
2.2 Budget controller
Budget controller 是 LLMLingua 的关键组件,用于为原始提示词的不同部分动态分配不同的压缩率。
考虑到提示词各部分对压缩行为的敏感程度各不相同 ------ 例如,问题需要保持较高的信息密度,而演示样例部分则可适度压缩。budget controller 的职责就在于此:对指导性提示词和问题采用较低的压缩比率,确保核心信息的完整留存;而对于演示样例部分,则可实施更高比率的压缩,剔除不必要的冗余信息。
budget controller 的具体算法,详述于图 5 中。
图 5:budget controller 的具体算法。Source: LLMLingua[2]
其核心变量定义如下:
- Mₛ: 小型语言模型,比如 GPT-2 或 LLaMA。
- x = (x^ins , x^dems , x^que) : 原始提示词,整合了指导性提示词、演示样例与问题三大部分。
- L, L_ins, L_dems, 和 L_que 分别代表 x, x^ins , x^dems, 和 x^que 中的 token 总数。
- 𝜏_dems: 在总体压缩率 τ 的约束下,依据指导性提示词和问题预设的压缩率 τ_ins 和 τ_que 来决定的演示样例压缩率。
- D: 集合 D 将收纳所有经过压缩处理后的演示样例。
主要操作步骤如下:
- 确定演示样例的压缩比例。
- 利用小型语言模型(如 GPT-2 或 LLaMA)计算原始演示样例集合中每个演示样例的困惑度(perplexity)。
- 按照困惑度从高到低排序全部演示样例。
- 迭代挑选演示样例并将其添加到集合 D。
- 完成演示样例的压缩后,将未使用的 budget (译者注:在总体压缩率的限制下,算法会优先确保演示样例被充分压缩,然后利用剩下的压缩能力去进一步压缩指导性提示词和问题,以实现最佳的信息保留和资源利用平衡。)转用于指导性提示词和问题的处理。
- 输出经过粗粒度压缩后的集合 D。
借助 demonstration-level (译者注:在完整的演示案例上进行压缩或处理,而不是单独处理每个小的组成部分(比如单词或短语)。)的压缩流程,budget controller 可以确保在削减数据量的同时,核心信息得以保全,能够有效实现原始提示词的瘦身。这一策略特别适合处理包含多重演示样例的复杂提示词。
涉及的程序代码,可在 control_context_budget 函数[14]中找到实现细节。
2.3 Iterative Token-level Prompt Compression (ITPC)
利用困惑度(perplexity)作为压缩标准,有其内在的局限性:the independence assumption(译者注:假设文本序列中的每个词汇(token)或字符的出现是彼此独立的,其出现的概率只依赖于它前面的一个或几个词汇,而与序列中更远的其他词汇无关。)。该假设认为每个 token 在提示词中孤立存在,其出现的概率仅取决于紧邻的前一个token,而不受其他任何 tokens 的影响。
然而,这一假设忽略了自然语言中词元(token)间错综复杂的相互依存关系,而这种关系对于理解上下文和保持语义的完整性至关重要。
忽略这些相互依存的关系,极有可能在压缩过程中造成重要信息的流失。 例如,在进行高比例压缩时,倘若某个 token 承载着上下文中的核心推理环节或逻辑联系纽带,那么仅仅依据其困惑度判定其去留,可能会导致推理链的断裂。
为克服这一挑战,LLMLingua 引入了 Iterative Token-level Prompt Compression(ITPC) 算法。不同于仅凭独立概率评判 token 价值的传统方法,ITPC 算法在压缩提示词时,会更精细地评估每个 token 的实际贡献。通过反复审视提示词的每一部分,同时考量当前上下文中每个 token 的条件概率,这一算法能更有效地维系 token 间的内在联系,确保压缩后提示词的语义完整性和逻辑连贯性。
图 6 展示了 ITPC 算法的详细步骤:
图 6:ITPC 算法的详细步骤。图片由原文作者提供
借助这一流程,ITPC 算法可以有效缩短提示词信息的长度,同时还能确保其语义内容的完整性,进而有效地降低了 LLM 的推理成本。
相关的实现代码可以在函数 iterative_compress_prompt
[15] 中找到。
2.4 Instruction Tuning 指令调优
如图 4 所示,在 LLMLingua 框架内,指令调优(instruction tuning)扮演着至关重要的角色。该步骤的核心目的是缩小用于提示词压缩的小型语言模型与大语言模型(LLMs)之间在分布特性上的差异。
图 7 展示了 Instruction Tuning 算法的详细步骤:
图 7:Instruction Tuning 的详细步骤。图片由原文作者提供
2.5 Code Demonstration 代码演示
我们现在开始展示代码。首要步骤是配置好环境。
以下是已安装的版本信息:
下面是用于测试的代码段:
首次运行时会自动下载默认模型。当然,我们也有另一个选项,即使用量化模型(quantized model)。相关的运行结果展示在图 8 中:
图 8 :LLMLingua 测试代码的运行结果。此截图由原文作者提供
03 LongLLMLingua
LLMLingua 的问题在于,在压缩处理过程中忽略了用户提出的问题,这可能导致一些无关紧要的信息被无谓地保留下来。
而 LongLLMLingua[3] 的设计初衷正是为了解决这一缺陷,它创新性地在压缩流程中融入了对用户问题的考量和处理。
图 9:LongLLMLingua 框架,灰色斜体内容与 LLMLingua 相同。图片来源:LongLLMLingua[3]
如图 9 所示,LongLLMLingua 框架引入了四项新功能,以提升大语言模型识别关键信息的能力:
- 针对用户问题的粗粒度和细粒度两级压缩技术(Question-aware coarse-grained and fine-grained compression);
- 动态调整的文档排序机制(Document reordering mechanism);
- 可变的压缩比例设定(Dynamic compression ratio);
- 子序列恢复算法(Subsequence recovery algorithm)。
3.1 针对用户问题的粗粒度压缩技术
LongLLMLingua 推荐采用这样一种方法,利用在不同文档上下文 x^doc_k
背景下问题 x^que
的困惑度,来衡量两者间的关联强度。我们可以在问题 x^que
后面附加一句限定语 x^restrict = "我们可以在提供的文档里找到这个问题的答案"
。这样做的目的是强化 x^que
与 x^doc_k
之间的联系,同时,这句话作为一个正则化项(regularization item),能有效降低模型产生不切实际的预测结果的可能性。这可以表示为:
为什么不直接计算在问题 x^que 约束下的整体文档困惑度呢?原因在于,文档内往往充斥着许多与问题不相关的冗余信息。即便是在 x^que 的引导下,对整篇文档计算出的困惑度值也可能不够明显,从而导致它无法成为评估文档层面压缩效果的理想指标。
可以在函数 get_distance_longllmlingua[16] 中找到实现这一技术的相关代码。
3.2 针对用户问题的细粒度压缩技术
LongLLMLingua 引入了对比困惑度(contrastive perplexity)的概念。
首先,我们计算一个 token 的困惑度,不考虑问题的情况下,表示为 perplexity(x_i | x<i)
。然后,我们再次测量困惑度,这次包括了问题,表示为 perplexity(x_i | x^que, x<i)
。这衡量的是在给定问题 x^que
的情况下,词元 x_i
对之前所有词元(token)的惊讶程度。
我们的目标是确定每个 token 的惊讶程度随问题变化的程度。如果当问题被包括进来后,某个词变得不那么令人惊讶,那么这个词很可能与问题高度相关。
3.3 动态调整的文档排序机制
如图 10 所示,在推理阶段,大语言模型(LLMs)倾向于利用提示词信息的起始和尾部内容,而往往忽视了其中间部分的信息,这便是所谓的"Lost in the Middle"
问题。
图 10:大语言模型(LLM)对相关资讯的把握能力受到其在提示词信息中位置的影响。为了解决中间信息丢失的问题,我们引入了一项文档重排序机制。图片来源:LongLLMLingua[3]
图 10 进一步表明,当关键信息被置于开头时,LLMs 的表现最为出色。基于此, LongLLMLingua 会根据粗粒度压缩的结果来组织段落,从前往后依据评分高低进行排序。
3.4 可变的压缩比例设定
鉴于不同文档中关键信息的密集程度存在差异,我们应当对那些与问题更加相关的文档分配更多资源(即采取更低的压缩比率)。
LongLLMLingua 运用在粗粒度压缩过程中得出的重要性得分,来指引细粒度压缩阶段的资源分配策略。
具体操作如下:首先,通过 LLMLingua 的 budget controller 为保留的文档设定初始资源量。随后,在细粒度压缩阶段,为每个文档动态分配资源。这一分配策略基于文档在粗粒度压缩阶段确定的重要性得分排名,以排名顺序作为资源分配依据。
LongLLMLingua 实施了一种线性调度方法(linear scheduler),实现资源的自适应分配(adaptive allocation)。对于每个词元(token) xi
,其资源量计算公式如下:
其中,Nd
表示所有文档的数量,𝛿𝜏
是一个控制动态分配总资源量的超参数。
对应的源代码可以在 get_dynamic_compression_ratio[17] 函数中找到。
3.5 子序列恢复算法
如图 11 所示,在细粒度的逐 token 压缩环节中,一些关键实体的 token 有被丢弃的风险。例如,"2009"
在原始提示词中可能被压缩至"209"
,"Wilhelm Conrad Rontgen"
也可能被简化压缩为"Wilhelmgen"
。
图 11:展示了一个子序列恢复算法案例,其中红色文本代表原始内容,而蓝色文字则是经过压缩后的结果。来源:LongLLMLingua[3]
LongLLMLingua 设计了一套子序列恢复算法,能够从大语言模型(LLMs)的回应中复原原始信息,如图 12 所示。
图 12:子序列恢复算法流程图。图片来源:LongLLMLingua
其核心流程包括以下几个步骤:
- 遍历大语言模型(LLM)响应内容中的每一个词元(token)
yl
,从中选取在压缩提示词x˜
中出现的最长子序列y˜key,l
; - 在原始提示词
x
内,寻找与y˜key,l
匹配的最大公共最短子序列(maximum common shortest subsequence)xi,j
; - 将大语言模型(LLMs)响应内容中的相应词元
y˜key,l
替换为原始的xi,j
。
这一算法的具体代码可以在 recover 函数[18]中找到。
3.6 代码演示
环境配置的方法与 LLMLingua 相同。下面是测试代码:
运行结果如图 13 所示:
图 13:LongLLMLingua 测试代码的运行结果。截图由作者提供。
04 AutoCompressor
不同于先前提及的方法,AutoCompressor[4] 采取了一种基于软提示词的创新途径。
它巧妙地通过增加词汇量和利用"summary tokens"
和"summary vectors"
来提炼大量上下文信息,进而精调现有的模型结构。
图 14:AutoCompressor 通过递归生成 summary vectors 来处理长文档,这些 summary vectors 作为软提示词(soft prompts)被传递给后续的所有文档片段。图片来源:AutoCompressor[4]
图 14 描绘了 AutoCompressor 的工作原理,其运行步骤如下:
- 词汇扩展(Expand Vocabulary) :在这一步骤中,我们将 “summary tokens” 加入到模型现有的词汇库中。这些 tokens 的作用是帮助模型将庞大的信息量压缩成更紧凑的向量表征。
- 文档分割(Split Document) :待处理的文档被切割成若干小段,每一小段后都会附加有 summary tokens 。这些 tokens 不仅携带了本段的信息,还包含了前面所有段落的摘要信息,实现了摘要信息的连续积累(summary accumulation)。
- 微调训练(Fine-tuning Training) :采用无监督训练的方式,借助 “next word prediction” 任务对模型进行微调。该任务的核心在于,根据当前片段前的 tokens 序列以及之前片段的摘要向量(summary vectors),预测下一个单词。
- 反向传播(Backpropagation) :AutoCompressor 在每个文档片段上运用 backpropagation through time(BPTT)(译者注:对于每一个时间步,BPTT 都会计算损失函数关于当前时间步和所有之前时间步参数的梯度,然后将这些梯度反向传播回网络,以更新参数。) 和 gradient checkpointing(译者注:在标准的反向传播过程中,为了计算梯度,需要保存前向传播过程中的所有中间结果。但随着网络深度的增加,这会消耗大量的内存。Gradient checkpointing 通过牺牲一些计算效率来减少内存需求。) 技术,能够有效缩减计算图(computational graph)的规模。反向传播针对整个文档进行,使得模型能够全面理解并学习到整个上下文之间存在的关联。
4.1 代码演示
AutoCompressor[19] 开放了其源代码,感兴趣的读者可以试着读一读。
05 LLMLingua-2
LLMLingua-2[6] 发现,通过基于因果语言模型(如LLaMa-7B)的信息熵删除 tokens 或词汇单位(lexical units)来进行提示词压缩存在两大挑战:
(1) 用来计算信息熵的小型语言模型与提示词压缩的实际目标不一致。
(2) 这一方法仅依赖于单向的上下文信息,而这或许无法覆盖提示词压缩所需的所有必要信息。
这些问题的核心在于,基于信息熵(information entropy)进行提示词压缩可能并非是最优的选择。
LLMLingua-2 的整体架构如图 15 所示:
图 15:LLMLingua-2的架构总览。来源:LLMLingua-2[6]
针对第一个问题,LLMLingua-2 引入了数据蒸馏流程。该流程从大语言模型中提取知识,在不丢失关键信息的情况下压缩提示词。同时,它还构建了一个 extractive text compression dataset (译者注:从原始文本中挑选出最重要的句子、短语或词汇,直接组成一个较短的版本,以保留原文的主要信息和意义。一般来说不涉及生成新的句子来概括原文)。在这样的数据集上进行训练,有助于小型语言模型更精准地对齐提示词压缩的需求。
面对第二个问题,LLMLingua-2 采取了一种创新策略 ------ 将提示词压缩转化为词元(token)分类任务。这一策略确保了压缩后的提示词能忠实地反映原始提示词的意图。它选用 transformer 的编码器作为底层架构,能够充分利用完整的双向上下文信息(bidirectional context),捕捉到进行提示词压缩所需的全部必要细节。
5.1 如何构建有效的提示词压缩数据集?
数据蒸馏
数据蒸馏从大语言模型(比如 GPT-4)中抽取知识,以便在不丢失基本信息的情况下实现有效压缩提示词。
在 LLMLingua-2 这一项目中,指导性提示词的设计经过了精心设计,如图 16 所示。这些指导性提示词(instructions)指导 GPT-4 在不向生成文本中引入新词汇的前提下,剔除原始文本中的冗余词汇,从而实现文本的压缩。
与此同时,这些指导性提示词(instructions)并未强行规定压缩的比例。相反,GPT-4 被鼓励尽可能地缩减原始文本的体积,但前提是必须确保原始信息的完整性。
图 16:LLMLingua-2 中用于数据蒸馏的指导性提示词。
如图 17 所示,在处理非常长的文本时,GPT-4 倾向于采取高比例的压缩策略。可能是因为其处理长文本的能力有限。这种激进的压缩策略往往伴随着大量信息的流失,可能严重影响接下来的任务执行效果。
图 17:在 MeetingBank 数据集上,根据原始文本长度,GPT-4 的压缩比情况。在本研究中,我们使用了 GPT-4–32k ,并将输出 tokens 的数量上限设为 4096。来源:LLMLingua-2[6]。
为了解决这个问题,LLMLingua-2 引入了一种分块压缩(chunk compression) 技术,即先将长文本拆解为若干个不超过 512 tokens 的小文本块,再分别对每一小文本块进行压缩处理,由 GPT-4 来完成这一过程。
数据标注
现在,我们已经利用数据蒸馏手段,收集到了原始文本与其压缩内容的组合。数据标注的目的是为原始文本里的每个 token 标上一个二元标签,以此判断压缩后该字符是否应该被保留。
考虑到 GPT-4 不一定能够完美遵循指导性提示词,LLMLingua-2 采取了滑动窗口策略(sliding window) ,以此来限定搜索范围。同时,还引入了模糊匹配技术(fuzzy matching) ,有效处理了 GPT-4 在提示词压缩过程中对原始词汇可能做出的细微改动。
质量控制
在 LLMLingua-2 项目中,质量控制环节采用了两个关键指标来评估通过 GPT-4 蒸馏生成的压缩文本,以及自动标注标签的优劣:Variation Rate(VR) 和Alignment Gap(AG) 。
Variation Rate(VR)衡量的是,压缩后的文本与原始文本相比,有多少比例的词汇发生了改变。而Alignment Gap(AG),则是用来衡量自动标注的标签的精准程度。
通过这些评估指标,LLMLingua-2 便能有效地筛除不合格的样本,从而保障整个数据集质量。
5.2 Compressor 压缩器
将其视为二元分类问题
从本质上讲,可将提示词压缩问题重塑为二元分类问题。其基本概念是将每一个词汇单元视为一个独立的实体,并为其分配一个标签:"保留"
或 "丢弃"
。这一策略不仅确保了压缩后提示词内容的完整性,同时还简化了模型结构。
模型架构设计
采用了基于 Transformer 编码器的特征编码器(feature encoder),并在其上巧妙地叠加了一个线性分类层(linear classification layer)。
这样的架构设计使得模型能够深刻理解每个词汇单元的双向上下文信息,为高效完成压缩任务奠定了坚实的基础。
提示词压缩策略
压缩原始提示词 x
的策略分为三个步骤。目标压缩比率设定为 1/τ
,这里 τ
即为压缩后提示词的词汇量与原始提示词 x
的词汇量之商。
- 首先,我们计算出压缩后提示词
x˜
需要保留的 token 数量:N˜ = τN
。 - 随后,运用 token 分类模型来预估每个词汇
xi
被标定为"保留"的概率pi
。 - 最后,我们从原始提示词
x
中筛选出前N˜
个pi
值最高的词汇,严格保持其原有排列顺序,进而组成压缩后的提示词x˜
。
5.3 代码演示
从上文可以看出,LLMLingua-2 的主要工作是构建压缩器(compressor)。那么,当我们成功获取了这个压缩器之后,下一步该如何操作呢?
请参照下方的代码示例(环境配置方式与 LLMLingua 一致)。compress_prompt_llmlingua2[20] 函数内集中体现了主要的处理逻辑。
运行结果如图 18 所示:
图 18:LLMLingua-2 测试代码的运行结果。截图由原文作者提供
06 RECOMP
RECOMP[7] 创新性地引入了两类经过训练的压缩器:抽取型(extractive)和概括型(abstractive)。抽取型压缩器擅长从已检索的文档中精挑细选出有价值的部分 ;而概括型压缩器则通过融合多篇文档的精华,自动生成摘要。
图 19 生动描绘了压缩器在 RECOMP 架构中的位置。
图 19:RECOMP 架构。图片来源:RECOMP
6.1 抽取型压缩器
给定输入文档集中的 n 个句子 [s1, s2, ..., sn]
,使用一个双编码器模型(dual encoder model)进行训练。该模型能将每个句子 si
和输入序列 x
转换为固定长度的向量表征。这些嵌入向量的内积反映了将句子 si
添加到输入序列 x
中,对于大语言模型(LLM)生成目标输出序列(target output sequence)的帮助程度。
压缩器最终生成的摘要 s
由排名前 N
的句子组成,按照它们与输入序列的内积进行排序。
6.2 概括型压缩器
概括型压缩器采用的是编码器-解码器架构(encoder-decoder)。它处理输入序列 x
与检索出的文档集合并将其连接起来,进而产生摘要 s
。
该方法具体步骤如下:首先利用大语言模型(如GPT-3)来生成训练数据集;然后对数据集进行筛选;最后,使用经过筛选后的数据集来训练编码器-解码器模型(encoder-decoder model)。
6.3 代码演示
鉴于 RECOMP 当前尚处在开发初期,我们在此暂不进行演示。对此感兴趣的读者不妨亲自动手体验一番。
07 结论 Conclusion
本文探讨了提示词压缩技术,覆盖了该技术的方法分类、算法原理以及代码实践演示。
在本文所讨论的各种方法中,LongLLMLingua 或许是一个更为出色的选择 。我们已在项目实践中应用了这一方法。原文作者承诺一旦他们发现了 LongLLMLingua 存在的不足,或是发现了更为优秀的替代方案,他们将对原文( https://ai.gopubby.com/advanced-rag-09-prompt-compression-95a589f7b554 )进行更新(译者注:如果有小伙伴关注到了内容更新,请在下方留言,我们会尽量及时进行内容补充,感谢❤️。)。此外,LLMLingua-2 也值得一试,它在运行速度和内存消耗方面都表现优异。
Thanks for reading!
Florian June
An artificial intelligence researcher, mainly write articles about Large Language Models, data structures and algorithms, and NLP.
END
参考资料
[1] https://arxiv.org/pdf/2304.12102.pdf
[2] https://arxiv.org/pdf/2310.05736.pdf
[3] https://arxiv.org/pdf/2310.06839.pdf
[4] https://arxiv.org/pdf/2305.14788.pdf
[5] https://arxiv.org/pdf/2304.08467.pdf
[6] https://arxiv.org/pdf/2403.12968.pdf
[7] https://arxiv.org/pdf/2310.04408.pdf
[8] https://arxiv.org/pdf/2210.09461.pdf
[9] https://aclanthology.org/2022.acl-long.1.pdf
[10] https://github.com/liyucheng09/Selective_Context/blob/v0.1.0rc1/src/selective_context/init.py#L273
[11] https://github.com/liyucheng09/Selective_Context/blob/v0.1.0rc1/src/selective_context/init.py#L100
[12] https://github.com/liyucheng09/Selective_Context/blob/v0.1.0rc1/src/selective_context/init.py#L146
[13] https://github.com/liyucheng09/Selective_Context/blob/v0.1.0rc1/src/selective_context/init.py#L236
[14] https://github.com/microsoft/LLMLingua/blob/v0.2.1/llmlingua/prompt_compressor.py#L1108
[15] https://github.com/microsoft/LLMLingua/blob/v0.2.1/llmlingua/prompt_compressor.py#L1458
[16] https://github.com/microsoft/LLMLingua/blob/v0.2.1/llmlingua/prompt_compressor.py#L1967
[17] https://github.com/microsoft/LLMLingua/blob/v0.2.1/llmlingua/prompt_compressor.py#L958
[18] https://github.com/microsoft/LLMLingua/blob/v0.2.1/llmlingua/prompt_compressor.py#L1686
[19] https://github.com/princeton-nlp/AutoCompressors
[20] https://github.com/microsoft/LLMLingua/blob/v0.2.1/llmlingua/prompt_compressor.py#L661
本文经原作者授权,由 Baihai IDP 编译。如需转载译文,请联系获取授权。
原文链接:
https://ai.gopubby.com/advanced-rag-09-prompt-compression-95a589f7b554