记录在应用 LLM 时需要注意的 chat-template 问题

碰到的问题

  • 场景一:在使用 VS Code 代码补全插件 Continue(https://www.continue.dev) 的时候,配置了 CPT 后的 LLM,结果无法正常补全,chatbot 中输出的 markdown 格式也不稳定,有时候连基本的代码也会解析错误。

解决方案: 在 Continue 插件的配置中添加自动补全的 FIM template

{
  // ...
  "tabAutocompleteOptions": {
    // ...
    "template": "<|fim_prefix|>{{{prefix}}}<|fim_suffix|>{{{suffix}}}<|fim_middle|>"
  }
}
  • 场景二:在使用 vllm 推理 CPT 后的 LLM(huggingface 格式)使用正常,在转换成 gguf 导入 ollama 进行推理时(使用了这里的方法: HF 格式模型导入 Ollama 进行推理),api 调用总是答非所问。

解决方案:在编写 Ollama 的 Modelfile 时,增加 template 配置

FROM ./XXXX.gguf
# ...

# 具体模板要根据模型的 chat template 来确定,我 CPT 的模型是基于 Qwen2.5-code 做的,所以这里使用 Qwen 的 template
TEMPLATE """<|im_start|>system
{{ .System }}<|im_end|>
<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant
"""

类似的用法

在调用大模型的接口时,经常会写类似于这样的 template:

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Hello!"},
    {"role": "assistant", "content": "Hi! How can I help you today?"},
    {"role": "user", "content": "What's the weather?"},
]

或者是使用 langchain 的语法做不同角色 SystemMessageHumanMessageAIMessage 对应到不同的 role

messages = [
    SystemMessage("You are a helpful assistant."),
    HumanMessage("Hello!"),
    AIMessage("Hi! How can I help you today?"),
    HumanMessage("What's the weather?")
]

这里 langchain 帮我们根据调用模型的类型,自动做了 role 和 template 的转换。

出现原因

pre-train 结束后的 LLM 并不直接具备处理对话式消息的能力,需要进行 Instruction-tuning 之后才可以。那么不同厂商的模型的 Instruction-tuning 训练数据格式不一定相同。
可参考 huggingface 的 NLP Course 中的例子:

  • Qwen 2 and SmolLM2 ChatML template:
<|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
Hello!<|im_end|>
<|im_start|>assistant
Hi! How can I help you today?<|im_end|>
<|im_start|>user
What's the weather?<|im_start|>assistant
  • Mistral template:
<s>[INST] You are a helpful assistant. [/INST]
Hi! How can I help you today?</s>
[INST] Hello! [/INST]

用好 chat template

除了基础的对话结构,chat template 还能帮我们实现 tool use/function calling 和多模态的输入,因此在不同的工具、不同的 LLM 之间切换的时候,一定要注意 template 是否有差异、有没有做自动的适配。

posted @ 2025-03-25 15:14  zion03  阅读(294)  评论(0)    收藏  举报