2026-09-26-bucoid-heretic-27b严格system模板修复.md

bucoid-heretic-27b 严格 system 模板修复(GPU 机 llama.cpp 服务)

日期:2026-09-26 机器:GPU 机 192.168.31.31 服务:llama-bucoid-heretic.service(llama-server @ 8080,模型 bucoid-heretic-27b) 触发来源:DSH 机酒馆(SillyTavern)联调时报 500


1. 症状

任何客户端(酒馆 / Open WebUI / 脚本)只要把 system 消息放在非首位,就收到 HTTP 500:

Chat Completion API
While executing CallExpression at line 106, column 32 in source: ...first %}
raise_exception('System message must be at the beginning.'
Error: Jinja Exception: System message must be at the beginning.

2. 根因

模型内嵌 Jinja 模板里有一段硬检查(/props 取回的 chat_template 第 104-107 行):

{%- for message in messages %}
    {%- if message.role == "system" %}
        {%- if not loop.first %}
            {{- raise_exception('System message must be at the beginning.') }}
        {%- endif %}

只允许第 0 条是 system,出现第二条就 500。这是 Qwen3.x 系模板的已知通病 (llama.cpp #27367、 unsloth/Qwen3.8-27B-GGUF 讨论)。

为什么酒馆必踩:酒馆默认 squash_system_messages = false,会把「主提示 / 角色描述 / 性格 / 场景 / 世界书前后 / 对话示例」 作为多条连续 system 逐条发送 → 第 2 条就炸。世界书 at-depth 条目(角色写死默认 system)与 Author's Note 同理。

3. 修复(改 1 行模板 + 加 1 个 flag,不动模型文件)

/home/zyw/models/bucoid-heretic-27b/chat_template-fixed.jinja   ← 补丁模板(同目录另有 chat_template-orig.jinja 原件)

补丁内容(仅第 106 行;首条 system 仍由模板前面的分支渲染,故保留 not loop.first 守卫不会重复输出):

     {%- if message.role == "system" %}
         {%- if not loop.first %}
-            {{- raise_exception('System message must be at the beginning.') }}
+            {{- '<|im_start|>system\n' + content + '<|im_end|>' + '\n' }}
         {%- endif %}

单元 ExecStart 末尾追加:

--chat-template-file /home/zyw/models/bucoid-heretic-27b/chat_template-fixed.jinja
sudo systemctl daemon-reload          # ⚠️ 改 ExecStart 后必须 daemon-reload
sudo systemctl restart llama-bucoid-heretic

4. 验证(改前全部 500 → 改后全部通过)

消息形态改前改后
2 条连续 system + user❌ 500✅ 正常出正文
system + user + 尾部 system❌ 500✅ 正常出正文
system + user + assistant + 中间 system(世界书 at-depth)❌ 500✅ 且正确用上注入的世界书内容
常规 1 条 system(回归)✅✅
/health + /v1/models—✅ n_ctx=65536、IQ4_XS,重启后 5s 内就绪
ps -eo cmd 确认进程带该 flag—✅

该单元注释自己写的教训:只验 /health 不够,必须实发一次生成请求。本次已按此执行,输出内容与质量均正常。

5. 回滚(30 秒)

sudo cp /etc/systemd/system/llama-bucoid-heretic.service.bak-20260926-pre-template \
        /etc/systemd/system/llama-bucoid-heretic.service
sudo systemctl daemon-reload && sudo systemctl restart llama-bucoid-heretic

6. 注意与影响面

7. 关联产物

下载此文件