Repository navigation
perf: 逐轮变化的上下文移出 system_prompt,恢复前缀缓存命中 - #106
Merged
Merged
Conversation
问题:`LLMUtils.build_request` 把「最近的聊天记录」(默认 20 条、每条带 `%Y-%m-%d %H:%M:%S` 时间戳)与「图片说明」拼进 `system_prompt`。system_prompt 是每轮请求的最前端,而这些内容每轮都变,于是从第 ~360 个字符起前缀分叉, 服务端前缀缓存(DeepSeek context caching 只按「完整前缀单元」命中)永远失效。 实测(AstrBot v4 + DeepSeek flash,同一群聊): - 改前:system_prompt 3029~3059 字,每轮 md5 都不同,`token_input_cached` 恒为 0, 整段 7~8k token 全部按未命中计费(未命中单价约为命中的 50 倍) - 改后:system_prompt 1656 字且 md5 恒定,补丁后第 2 条群聊请求即 `token_input_cached = 3328`(约 45%),稳态约 50% 改动: - 新增 `tail_block`,把聊天记录、空历史提示、图片说明这些逐轮变化的内容收进去 - `request_llm` 时拼到 `prompt` 尾部;`system_prompt` 只保留人格 + 机器人 id/群名等静态信息 - 指令类文本("你的用户名以 AstrBot 被代替"、"不要使用 [At:id] 格式"、 read_air 的 `<NO_RESPONSE>` 约定)仍保留在 system_prompt,不降级为用户数据 - 新增配置 `cache_friendly_prompt`(默认 true);关闭后退回旧行为,便于对比与回滚
v2.2.0 的 `smart_quote` 会在 `quote_mode` 开启时往 `env_description`(即
system_prompt)追加一句引用引导:
这条新消息的编号是 {current_no},
而 `current_no = len(quote_targets) + 1`,`quote_targets` 由
`format_history_for_llm` 按「可引用的历史消息条数」填充 —— 每轮历史长度不同,
编号就不同,这句话的文本也跟着变,system_prompt 又变回每轮不同,前缀缓存依旧失效。
实测(同一群,steady state 时 cached 稳定在 3328):
出现该分支的请求 `cached` 掉到 128,`miss` 涨到 6741。
改法与主改动一致:这句引导同样进 `tail_block`,和它引用的编号历史放在一起,
语义上也更连贯。
- 聊天记录、引用指令、图片说明作为额外内容块跟在用户原话后面,并用前后两句说明 标出哪条是新消息;system_prompt 只留静态信息,仍然每轮字节稳定 - req.prompt 不再拼接历史,知识库检索、prompt_prefix、on_llm_request 钩子 拿到的都是用户原话 - 去掉 cache_friendly_prompt 开关;不支持额外内容块的旧版 AstrBot 按原布局 写入 system_prompt - group_msg_history=0 时不再发送全部历史;图片只从展示的聊天记录中收集, 说明里的数量不再算上新消息自带的图片 - group_msg_history 回退默认值与 schema 一致(20) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TWrx9vAKHAB58p6dav5fPZ
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
utils/llm_utils.py的call_llm会把三样每轮都变的内容写进system_prompt:group_msg_history = 20条,每条带%Y-%m-%d %H:%M:%S时间戳system_prompt位于每轮请求的最前面,所以从人格之后(约第 360 个字符)起每轮都不一样。服务端前缀缓存(DeepSeek context caching、OpenAI / Anthropic prompt caching)只按前缀命中,因此每轮都完全失效。PR 作者的实测(AstrBot v4 + DeepSeek flash,同一个 QQ 群,抓真实请求体对比):
引用指令留在 system_prompt 里的请求,
cached会掉到 128。改前每条群聊请求 7~8k token 全部按未命中计费;按 DeepSeek flash 的价格,未命中单价约是命中的 50 倍。group_msg_history默认就是 20,所以用默认配置的用户都受影响。改动
[At:id]格式」),以及「现在你收到了一条新消息…」/ read_air 的<NO_RESPONSE>约定。req.extra_user_content_parts发送:聊天记录、引用指令和图片说明作为额外文本块,附在本轮用户消息后面。这段内容前后各有一句说明,标明最前面的是新消息、下面是供参考的背景信息;引用指令紧跟在新消息之后。req.prompt保持用户原话,AstrBot 的知识库检索、prompt_prefix和其他插件的on_llm_request钩子拿到的都是用户原话,不会混进聊天记录。extra_user_content_parts时,沿用 v2.2.0 的布局,全部写入 system_prompt,结果和 v2.2.0 逐字节一致。这种情况下没有缓存收益。不新增配置项。group_msg_history = 0时不再发送全部历史(原来[-0:]取的是整个列表)group_msg_history时的缺省值从 10 改为 20,和 schema 一致验证
req.prompt只有用户原话;旧版回退结果和 v2.2.0 逐字节一致(group_msg_history = 0是有意修改的除外)。cached列(或 DeepSeek 返回的usage.prompt_cache_hit_tokens),从第 2 次请求起应该在 3.3k 左右。行为影响 / 需要关注
🤖 Generated with Claude Code
https://claude.ai/code/session_01TWrx9vAKHAB58p6dav5fPZ