大模型提示词压缩

结构化语法用更少的 token 传达同样的指令,歧义也更少。

什么是提示词压缩?

提示词压缩,就是用更少的 token 向 AI 模型传达同样的指令。用的 token 少,处理就更快,上下文窗口里留给实际工作的空间更多,指令也常常更清楚,重试更少。

iLang 怎样压缩提示词

iLang 靠结构化语法压缩,去掉自然语言的冗余:冠词、填充词、含糊其辞和有歧义的说法。

改写前:简练的自然语言,49 个单词、58 个 token

Please read the sales data from the CSV file. Then filter it to only include records where the revenue is greater than 1000. After that, calculate statistics grouped by region. Sort the results by revenue in descending order. Finally, format the output as a markdown table and display it.

改写后:iLang(55 个 token)

[READ:@SRC|path=sales.csv]
=>[FILT|whr=revenue>1000]
=>[STAT|grp=region]
=>[SORT|srt=revenue,desc]
=>[FMT|fmt=md]
=>[OUT]

差别

指标自然语言iLang
token 数(cl100k_base)5855
歧义可能有多种理解只有一种理解

这组对比的差别很小,值得弄明白为什么这么小。上面那句话写得很简练,写的人已经知道这条链要表达什么。方括号、竖线和冒号不是白来的:分词器同样要把它们算成 token。一条没人注水的短指令,结构换来的是精确,不是 token。

压缩最有用的地方

人们实际写出来的请求

真实的请求不会这么简练。开头先打招呼,每条指令都含糊其辞,还要解释原因,最后再道谢。同一个六步请求,平时收到的通常是这样:

Hi! Hope you're doing well. I've got a quick favour to ask if you don't mind. So I have this sales data sitting in a CSV file and I was wondering if you could take a look at it for me? What I'm trying to do is basically narrow it down to just the bigger deals, so anything where the revenue is above 1000 I think. Once you've got that, could you please work out the summary statistics for me, broken down by region? I'd also really appreciate it if you could sort everything from highest revenue down to lowest, since that's how my manager likes to see it. And then if it's not too much trouble, please present the final result as a nice markdown table so I can paste it straight into our report. Thank you so much, really appreciate the help!

这段是 169 个 token,链还是那条 55 个 token 的链。链本身一点没变,消失的是问候、含糊其辞和道谢,而真实的提示词就是由这些组成的。

系统提示词和行为规则

系统提示词每一轮都要带上。一个 500 token 的系统提示词,每条消息都要占用 500 个 token。行为规则本来就写得紧凑,所以把它们改写成结构化的 ::GENE{} 定义,变化比上面的对话请求小。

长上下文会话

上下文越满,AI 的输出质量越差。压缩过的指令给真正的任务内容留出更多空间。

按 token 计费

如果你按 token 付费(API 调用、云端推理),系统提示词里的每个 token,每次请求都要付一次钱。

技术上怎么实现

iLang 用一套 88 个动词的词典,每个动词是 3 到 5 个字符的代码(READ、FILT、STAT、FMT、OUT)。动词之间用 => 串起来,修饰符用 key=value 键值对,目标用 @ENTITY 引用。没有冠词,没有介词,没有填充词。

压缩来自三个地方:

来源自然语言的冗余iLang 对应写法
动词短语「请从……读取数据」[READ:@SRC|path=...]
连接词「然后」「之后」「最后」=>
行为规则「永远先给结论,不要含糊其辞」T:conclusions_first A:hedging⇒remove

改写前后

同一条指令,一种写成句子,一种写成链。

你想要的iLang
从一个 URL 提取文字,排成 Markdown 格式[GET:@SRC|path=url]=>[FMT|fmt=md]=>[OUT]
读取全部 .md 文件,合并成一个,输出结果[LIST:@LOCAL|mch=*.md]=>[Π:READ]=>[Σ]=>[Ω]
把上一步的输出缩成 3 条要点,口吻专业[SHRT:@PREV|sty=bullets,len=3,ton=pro]=>[Ω]
翻译成日文,语气正式,再排成表格[θ:@PREV|lng=ja,ton=formal]=>[FMT|fmt=csv]=>[Ω]

试一试

在浏览器里直接试:Playground 练习场能把 iLang 链转成 IML 再转回来,按语法校验文件,还能数 token,全都在页面里完成,输入不会离开浏览器。

把提示词改写成 iLang 的工具会把你的文字发给模型,所以它放在英文站上:ilang.ai/prompt-compression ↗。

阅读完整的 iLang 规范 →