提示词压缩
经过几轮对话后,聊天记录可能会变得相当冗长,特别是当其中包含代码和执行结果时。这可能导致超出大语言模型的上下文窗口限制。为解决这个问题,一种方法是对较早的聊天记录进行摘要总结,仅保留最近几轮的对话内容。
另一种方法是使用向量数据库存储聊天历史记录条目,仅根据当前用户请求检索最近几轮的相关部分。然而,在TaskWeaver中,代码也是聊天历史的一部分。为了正确生成当前用户请求的代码,跳过某些中间代码和执行结果并非可行方案。因此,我们选择第一种方法来解决这个问题。
下图展示了聊天历史摘要的概念,其中聊天历史被分为两部分:
- 压缩轮次:这部分内容会被总结,只有总结会保留在聊天历史中。如果context_summary已存在,则会基于之前的总结加上需要总结的轮次生成新的总结。
- 保留轮次:这部分内容将保留在聊天历史中,不进行摘要处理。
假设最初ConversationSummary是空的。
当聊天记录达到rounds_to_compress(默认2轮)加上rounds_to_retain(默认3轮)时,
系统会基于rounds_to_compress轮生成ConversationSummary,同时保留rounds_to_retain轮在聊天记录中。
此后,聊天记录中将仅保留rounds_to_retain轮。
当下次聊天记录再次达到rounds_to_compress轮加上rounds_to_retain轮时,
系统会基于rounds_to_compress轮和之前的ConversationSummary生成新的摘要。
我们通过这两个参数来控制聊天记录摘要生成的频率。
以下是代码生成器中聊天历史摘要的一个示例:
{
"ConversationSummary": "The user requested the generation of 100 random numbers, which was successfully executed. Then, the user asked to show the top 5 largest numbers from the generated random numbers. The assistant provided a code snippet to sort the generated random numbers in descending order and select the top 5 largest numbers, which was also successfully executed. After that, the user requested to plot the distribution of the 100 numbers, which was successfully executed. The user then asked to count the frequency of numbers in each bin of the histogram and identify the bin with the most numbers for the 0.1 bin width, which was also successfully executed.",
"Variables": [
{
"name": "random_numbers_100",
"type": "numpy array",
"description": "An array containing 100 random numbers generated using np.random.rand()"
},
{
"name": "top_5_largest",
"type": "numpy array",
"description": "An array containing the top 5 largest numbers from the generated random numbers"
}
]
}
JSON对象包含两个字段:
- ConversationSummary: 聊天历史记录的摘要。
- 变量:聊天历史中可用于当前用户请求的变量。
Planner的聊天历史摘要仅包含ConversationSummary字段。
在汇总轮次中生成的实际代码会被忽略,仅保留变量在摘要中,以便LLM在未来的代码生成中仍能引用这些变量。
需要注意的是,聊天历史摘要功能需要调用大语言模型(LLM),这会带来额外的延迟和成本。 聊天历史摘要的提示词可以在planner和code generator中找到。
配置
如上所述,控制聊天历史摘要有两个参数:
round_compressor.rounds_to_compress (默认值 2) 和 round_compressor.rounds_to_retain (默认值 3)。
要启用聊天历史摘要功能,您需要将 planner.prompt_compression
和 code_generator.prompt_compression 设置为 true。