微软推出LLMLingua Prompt压缩技术,打破上下文长度限制

发表时间: 2024-01-19 11:58

大模型应用开发迎来一波一波热潮,Prompt engineering被越来越多人关注,随之而来的是越来越长的Prompt需求。而Prompt过长会带来一系列的问题,如超出模型上下文长度限制、响应延迟变慢、上下文信息丢失、一些模型性能问题,如“处于Prompt中间位置的信息难以召回”以及高昂的Token成本。

一般的解决思路是通过训练更优秀的大模型来解决,但众所周知,由于大模型的二次复杂度,Context window(上下文长度)提升相对困难。再加之直接使用OpenAI等云服务接口,无法更换模型的开发者来讲,如何扩展上下文长度呢?

有一个比较直接的思路就是对Prompt进行压缩,它的基本假设就是自然语言本身是存在冗余的,在一些实际的高阶RAG架构中,Prompt压缩也可以作为一个模块内置在系统中。

比如,在langchain中,对于多轮的对话上下文进行压缩总结从而达到减少Prompt的目的。

基于这样的思路,微软在十月份公布了一项他们的研究进展,称受论文Ilya的LLMs is Compressors的观点启发,他们开发了一款工具LLMLingua及LongLLMLingua,实现了 20 倍的压缩比和最小的性能损失 (LLMLingua),以及 17.1% 的 4 倍压缩 (LongLLMLingua) 的性能提升,进而达到加速模型推理、降低成本并提高下游性能的目的。

其实现的基本思路是通过训练一个小的模型来对原始的Prompt进行压缩,从而获得更小的Prompt,再提交给大模型生成,并对生成结果进行解压。

在此基础上论文还提出了如下的Insights:

1.自然语言存在冗余性,信息量不尽相同。

2.LLM 可以理解被压缩后的Prompt。

3.LLMLingua是在语言完整性和压缩率之间进行权衡。

4.GPT-4 可以从被压缩的具有涌现能力的Prompt中恢复所有关键信息。(LLMLingua)

5.Prompt中关键信息的密度和位置会影响下游任务的性能。(LongLLMLingua)

使用方法也比较简单:

!pip install llmlinguafrom llmlingua import PromptCompressorllm_lingua = PromptCompressor()compressed_prompt = llm_lingua.compress_prompt(prompt, instruction="", question="", target_token=200)# > {'compressed_prompt': 'Question: Sam bought a dozen boxes, each with 30 highlighter pens inside, for  each box. He reanged five of boxes into packages of sixlters each and sold them  per. He sold the rest theters separately at the of three pens . How much did make in total, dollars?\nLets think step step\nSam bought 1 boxes x00 oflters.\nHe bought 12 * 300ters in total\nSam then took 5 boxes 6ters0ters.\nHe sold these boxes for 5 *5\nAfterelling these  boxes there were 3030 highlighters remaining.\nThese form 330 / 3 = 110 groups of three pens.\nHe sold each of these groups for  each, so made 110 * 2 = 0 from them.\nIn total, then, he earned 0 +  = 5.\nSince his original cost was 0, he earned 5 - 0 = 5 in profit.\nThe answer is 115',#  'origin_tokens': 2365,#  'compressed_tokens': 211,#  'ratio': '11.2x',#  'saving': ', Saving #  'saving': ', Saving $0.1 in GPT-4.'}.1 in GPT-4.'}## Or use the quantation model, like TheBloke/Llama-2-7b-Chat-GPTQ, only need <8GB GPU memory.## Before that, you need to pip install optimum auto-gptqllm_lingua = PromptCompressor("TheBloke/Llama-2-7b-Chat-GPTQ", model_config={"revision": "main"})

除此之外,还提供了该工具的使用例子和压缩效果直观展示。

https://huggingface.co/spaces/microsoft/LLMLingua