更新机制解析:从 HuggingFace 获取、变体选择到本地覆盖实战)
llama.cpp 聊天模板models/templates更新机制解析从 HuggingFace 获取、变体选择到本地覆盖实战【免费下载链接】llama.cppLLM inference in C/C项目地址: https://gitcode.com/GitHub_Trending/ll/llama.cppmodels/templates目录是 llama.cpp 中存放各模型官方 Jinja 聊天模板chat template的中央仓库。本篇以该目录下 models/templates/README.md 为核心完整讲解如何用 scripts/get_chat_template.py 从模型仓库抓取并更新这些.jinja模板文件并结合源码剖析该脚本的变体variant解析逻辑、模板在推理与服务端的实际用途--chat-template-file覆盖以及模板文件如何参与 GGUF 转换与测试校验帮助读者掌握一套可复制的获取—落盘—覆盖—验证工作流。模板文件的角色为什么需要 models/templatesllama.cpp 在渲染对话时支持使用 common/jinja 目录中用 C 实现的 Jinja 模板引擎来格式化消息。模型自带的模板通常内嵌在 GGUF 的tokenizer.chat_template属性里但在以下场景中需要一份独立的模板文件工具调用function calling部分模型的官方tool_use模板没有被完整打进 GGUF需要用本目录中的模板做覆盖官方模板有 bug 或格式不适配llama.cpp 会自研修正版模板存放于此例如 llama-cpp-deepseek-r1.jinja转换脚本回填Python 转换代码在遇到 Mistral 系模型时会直接读取本目录中的模板文件写入 GGUF后文详述。目录内目前保存了约 60 个模板覆盖 DeepSeek、Qwen、GLM、Kimi、MiniMax、Mistral、Llama、Gemma、IBM Granite、NVIDIA Nemotron 等主流开源模型完整清单见 models/templates 目录。文件名约定为org-model-name[-variant].jinja例如CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja中的tool_use即对应该模型chat_template列表中的变体名。更新模板的标准命令get_chat_template.pymodesl 目录的 README即 models/templates/README.md给出的核心操作流程是把下面这批命令的输出重定向写入对应模板文件。以下命令块完整继承自原文档可直接复制执行./scripts/get_chat_template.py CohereForAI/c4ai-command-r-plus tool_use models/templates/CohereForAI-c4ai-command-r-plus-tool_use.jinja ./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 default models/templates/CohereForAI-c4ai-command-r7b-12-2024-default.jinja ./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 rag models/templates/CohereForAI-c4ai-command-r7b-12-2024-rag.jinja ./scripts/get_chat_template.py CohereForAI/c4ai-command-r7b-12-2024 tool_use models/templates/CohereForAI-c4ai-command-r7b-12-2024-tool_use.jinja ./scripts/get_chat_template.py deepseek-ai/DeepSeek-R1-Distill-Llama-8B models/templates/deepseek-ai-DeepSeek-R1-Distill-Llama-8B.jinja ./scripts/get_chat_template.py deepseek-ai/DeepSeek-R1-Distill-Qwen-32B models/templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja ./scripts/get_chat_template.py fireworks-ai/llama-3-firefunction-v2 models/templates/fireworks-ai-llama-3-firefunction-v2.jinja ./scripts/get_chat_template.py google/gemma-2-2b-it models/templates/google-gemma-2-2b-it.jinja ./scripts/get_chat_template.py meetkai/functionary-medium-v3.1 models/templates/meetkai-functionary-medium-v3.1.jinja ./scripts/get_chat_template.py meetkai/functionary-medium-v3.2 models/templates/meetkai-functionary-medium-v3.2.jinja ./scripts/get_chat_template.py meta-llama/Llama-3.1-8B-Instruct models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja ./scripts/get_chat_template.py meta-llama/Llama-3.2-3B-Instruct models/templates/meta-llama-Llama-3.2-3B-Instruct.jinja ./scripts/get_chat_template.py meta-llama/Llama-3.3-70B-Instruct models/templates/meta-llama-Llama-3.3-70B-Instruct.jinja ./scripts/get_chat_template.py microsoft/Phi-3.5-mini-instruct models/templates/microsoft-Phi-3.5-mini-instruct.jinja ./scripts/get_chat_template.py mistralai/Mistral-Nemo-Instruct-2407 models/templates/mistralai-Mistral-Nemo-Instruct-2407.jinja ./scripts/get_chat_template.py NousResearch/Hermes-2-Pro-Llama-3-8B tool_use models/templates/NousResearch-Hermes-2-Pro-Llama-3-8B-tool_use.jinja ./scripts/get_chat_template.py NousResearch/Hermes-3-Llama-3.1-8B tool_use models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja ./scripts/get_chat_template.py Qwen/Qwen2.5-7B-Instruct models/templates/Qwen-Qwen2.5-7B-Instruct.jinja ./scripts/get_chat_template.py Qwen/QwQ-32B models/templates/Qwen-QwQ-32B.jinja ./scripts/get_chat_template.py Qwen/Qwen3-0.6B models/templates/Qwen-Qwen3-0.6B.jinja ./scripts/get_chat_template.py zai-org/GLM-4.5 models/templates/zai-org-GLM-4.5.jinja ./scripts/get_chat_template.py deepseek-ai/DeepSeek-V3.1 models/templates/deepseek-ai-DeepSeek-V3.1.jinja命令语法与参数说明摘自脚本头部注释 scripts/get_chat_template.py参数必填说明model_id是形如组织名/模型名的模型仓库 ID脚本会校验其必须符合组织/名称格式variant否当模型的chat_template是带name/template字段的列表时指定要抓取的变体如tool_use、default、rag省略时若存在default变体则自动选用脚本内部实现抓取、容错与变体解析阅读 scripts/get_chat_template.py 源码可以确认上述命令背后的完整链路1. 双通道下载L19-L33优先使用huggingface_hub.hf_hub_download拉取模型的tokenizer_config.json。走该通道的意义在于如果目标模型是gated受权限控制模型用户在执行过huggingface-cli login后依然可以正常下载若未安装huggingface_hubImportError分支脚本退化为用requests直接请求https://huggingface.co/{model_id}/resolve/main/tokenizer_config.json。此时若返回 401脚本会给出明确报错提示该模型为 gated 模型需要申请权限、执行huggingface-cli login并安装huggingface_hub。2. JSON 解析容错L35-L40正常路径下用json.loads解析配置针对个别上游仓库tokenizer_config.json末尾多出一个}导致 JSON 非法的已知问题脚本内置了一个正则替换作为兜底修复源码注释中点名了 NousResearch 的 Meta-Llama-3-8B-Instruct 仓库即属于此类。3. 变体选择逻辑L42-L62若chat_template是普通字符串直接返回若是列表每个元素含name与template脚本构建name - template映射未指定 variant 且存在default时自动选default并向 stderr 输出提示未指定 variant 但列表中没有default时脚本报错并列出所有可用变体名便于人工指定指定了不存在的变体名时同样报错并附全部可用变体清单。4. 输出L65-L72模板文本通过sys.stdout原样写出因此 README 中的 models/templates/xxx.jinja重定向写法是刻意为之——脚本不写文件由 shell 决定落盘位置与文件名。模板文件长什么样以 Llama 3.1 官方模板为例抓取落盘后的模板是一个标准 Jinja2 文本文件。以 models/templates/meta-llama-Llama-3.1-8B-Instruct.jinja 为例可以看到官方模板的典型结构{%- if not tools_in_user_message is defined %} {%- set tools_in_user_message true %} {%- endif %} {%- if not date_string is defined %} {%- set date_string 26 Jul 2024 %} {%- endif %} {#- This block extracts the system message, so we can slot it into the right place. #} {%- if messages[0][role] system %} {%- set system_message messages[0][content]|trim %} {%- set messages messages[1:] %} {%- else %} {%- set system_message %} {%- endif %} ... {%- if add_generation_prompt %} {{- |start_header_id|assistant|end_header_id|\n\n }} {%- endif %}值得注意的模板要素可注入变量messages、tools、custom_tools、date_string、bos_token、add_generation_prompt等均由 llama.cpp 的 Jinja 运行时提供模板作者只负责编排渲染逻辑默认值防御tools_in_user_message、date_string在未定义时回退为内置默认值保证同一模板在不同调用方下行为稳定多分支渲染该模板区分内置工具ipython 风格与自定义 JSON 工具两种工具调用格式并约束一次只允许一个tool_calls否则raise_exception。而 llama.cpp 自研修正模板 models/templates/llama-cpp-deepseek-r1.jinja 则展示了另一种典型写法用{%- set ns namespace(...) %}维护跨循环状态用{%- macro flush_tool_outputs() %}宏批量冲刷连续的tool/ipython消息块专门适配 DeepSeek R1 蒸馏版的tool▁calls▁begin特殊标记协议。模板如何被消费推理、转换与测试三条链路链路一运行时覆盖llama-server / llama-clidocs/function-calling.md 中给出了完整的覆盖用法——用--chat-template-file指向本目录的模板文件llama-server --jinja -fa -hf bartowski/DeepSeek-R1-Distill-Qwen-32B-GGUF:Q4_K_M \ --chat-template-file models/templates/llama-cpp-deepseek-r1.jinja llama-server --jinja -fa -hf bartowski/Hermes-3-Llama-3.1-8B-GGUF:Q4_K_M \ --chat-template-file models/templates/NousResearch-Hermes-3-Llama-3.1-8B-tool_use.jinja该文档同时建议启动后通过http://localhost:8080/props检查chat_template/chat_template_tool_use属性来确认生效的模板若模型没有官方tool_use模板可退回--chat-template chatml这类通用格式或像 DeepSeek R1 一样使用本目录中的自研模板。链路二GGUF 转换时回填Python 侧从源码结构看转换脚本也会直接读取本目录。conversion/base.py 中定义了template_dir Path(__file__).parent.parent / models/templates/当 Mistral 系模型缺少可用模板时会调用 conversion/mistral.py 的get_community_chat_template该方法按词元化器的版本与类型v3/v7 的 spm/tekken 组合、v11、v13映射到本目录中的具体模板文件如Mistral-Small-3.2-24B-Instruct-2506.jinja、unsloth-mistral-Devstral-Small-2507.jinja读出文本写入 GGUF若 tokenizer 版本未知则报错并提示可用--disable-mistral-community-chat-template回退到官方预处理库。这意味着本目录的模板不仅是参考样本而是转换管线的真实数据源维护模板文件时需注意文件名的稳定性。链路三测试与自动解析校验tests/test-chat-analysis.cpp 维护了一份ALL_TEMPLATE_PATHS清单逐一加载models/templates/下的模板做渲染分析确保模板语法对 llama.cpp 的 Jinja 引擎common/jinja 中的 lexer/parser/runtime 三层架构完全兼容common/chat-auto-parser-generator.cpp 中则提示用户若模型没有被自动识别应检查 llama.cpp 安装目录的 models/templates 目录中是否有该模型的修正模板。实操建议如何为新模型补充一份模板结合以上机制新增或更新一份模板的完整流程如下抓取执行./scripts/get_chat_template.py org/model [variant] models/templates/org-model[-variant].jinja若报错列出可用变体按提示补上 variant 参数。注意 gated 模型需先pip install huggingface_hub并huggingface-cli login。验证兼容性模板必须只使用 llama.cpp Jinja 引擎支持的语法与内置函数。新增/修改内置函数时官方指引是修改 common/jinja/value.cpp 并在 tests/test-jinja.cpp 中补测试见 common/jinja/README.md 的维护者说明。注意输入标记限制llama.cpp 的jinja::string会对用户输入打is_input标记以防御特殊 token 注入common/jinja/README.md 的 Caveats 节指出由用户输入动态拼接出的特殊 token如| message[role] |不会被当作真正的特殊 token 处理模板中为 content 前置的空格也会被单独分词。撰写或修正模板时应对这两点保持预期。落位引用推理侧通过--chat-template-file指向该文件即可参照 docs/function-calling.md 的命令示例转换侧若需被get_community_chat_template引用文件名必须与其版本映射表一致。回归校验运行 chat 相关测试如 tests/test-chat.cpp 所覆盖的模板用例与 tests/test-chat-analysis.cpp确认渲染输出符合预期。小结modesl/templates/README.md 用一份命令清单定义了 llama.cpp 聊天模板的更新协议以org/model为主键、variant为可选维度经 scripts/get_chat_template.py 从模型上游的tokenizer_config.json提取 Jinja 模板并落盘到 models/templates。该目录既是运行时--chat-template-file覆盖的取值池也是 Python 转换管线回填 Mistral 系模板的数据源还被 chat 分析测试持续校验。掌握了变体解析规则 三条消费链路之后即可安全地为任意新模型抓取、修正并维护模板文件。【免费下载链接】llama.cppLLM inference in C/C项目地址: https://gitcode.com/GitHub_Trending/ll/llama.cpp创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考