微软发布Prompt压缩技术LLMLingua,另一种方式突破上下文长度限制,提升推理性能

人工智能与算法增长营销数据中台

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

picture.image

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

picture.image

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

picture.image

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

picture.image

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

picture.image

picture.image

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

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

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

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

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

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

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

使用方法也比较简单 :


          
!pip install llmlingua
          

          
from llmlingua import PromptCompressor
          

          
llm_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 $10 each box. He reanged five of boxes into packages of sixlters each and sold them $3 per. He sold the rest theters separately at the of three pens $2. 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 $2 each, so made 110 * 2 = $220 from them.\nIn total, then, he earned $220 + $15 = $235.\nSince his original cost was $120, he earned $235 - $120 = $115 in profit.\nThe answer is 115',
          
#  'origin_tokens': 2365,
          
#  'compressed_tokens': 211,
          
#  'ratio': '11.2x',
          
#  'saving': ', Saving $0.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-gptq
          
llm_lingua = PromptCompressor("TheBloke/Llama-2-7b-Chat-GPTQ", model_config={"revision": "main"})
      

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

picture.image

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

‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍ ‍

官方介绍视频:

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
DevOps 在字节移动研发中的探索和实践
在日益复杂的APP工程架构下,如何保证APP能高效开发,保障团队效能和工程质量?本次将结合字节内部应用的事件案例,介绍DevOps团队对移动研发效能建设的探索和思考。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论