当前位置: 首页 > news >正文

从大模型中的chat_template了解jinja模板语法

Python Jinja 教程:https://geek-docs.com/python/python-tutorial/python-jinja.html

1、大模型中的chat_template.json

在各个大模型权重仓库中都有chat_template.json文件。如
qwen3vl-8b模型
在这里插入图片描述
基于以下代码可以查看格式化后的模板

import json
with open("chat_template.json",'r') as f:d=json.load(f)print(d['chat_template'])

Qwen-VL-8B-Thinking的模板如下所示,与Qwen-VL-8B-Instruct相比只是多了一个<think>\n标签

{%- set image_count = namespace(value=0) %}
{%- set video_count = namespace(value=0) %}
{%- macro render_content(content, do_vision_count) %}{%- if content is string %}{{- content }}{%- else %}{%- for item in content %}{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}{%- if do_vision_count %}{%- set image_count.value = image_count.value + 1 %}{%- endif %}{%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}<|vision_start|><|image_pad|><|vision_end|>{%- elif 'video' in item or item.type == 'video' %}{%- if do_vision_count %}{%- set video_count.value = video_count.value + 1 %}{%- endif %}{%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}<|vision_start|><|video_pad|><|vision_end|>{%- elif 'text' in item %}{{- item.text }}{%- endif %}{%- endfor %}{%- endif %}
{%- endmacro %}
{%- if tools %}{{- '<|im_start|>system\n' }}{%- if messages[0].role == 'system' %}{{- render_content(messages[0].content, false) + '\n\n' }}{%- endif %}{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}{%- for tool in tools %}{{- "\n" }}{{- tool | tojson }}{%- endfor %}{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
{%- else %}{%- if messages[0].role == 'system' %}{{- '<|im_start|>system\n' + render_content(messages[0].content, false) + '<|im_end|>\n' }}{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}{%- set index = (messages|length - 1) - loop.index0 %}{%- if ns.multi_step_tool and message.role == "user" %}{%- set content = render_content(message.content, false) %}{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}{%- set ns.multi_step_tool = false %}{%- set ns.last_query_index = index %}{%- endif %}{%- endif %}
{%- endfor %}
{%- for message in messages %}{%- set content = render_content(message.content, True) %}{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}{%- elif message.role == "assistant" %}{%- set reasoning_content = '' %}{%- if message.reasoning_content is string %}{%- set reasoning_content = message.reasoning_content %}{%- else %}{%- if '</think>' in content %}{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}{%- set content = content.split('</think>')[-1].lstrip('\n') %}{%- endif %}{%- endif %}{%- if loop.index0 > ns.last_query_index %}{%- if loop.last or (not loop.last and reasoning_content) %}{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}{%- else %}{{- '<|im_start|>' + message.role + '\n' + content }}{%- endif %}{%- else %}{{- '<|im_start|>' + message.role + '\n' + content }}{%- endif %}{%- if message.tool_calls %}{%- for tool_call in message.tool_calls %}{%- if (loop.first and content) or (not loop.first) %}{{- '\n' }}{%- endif %}{%- if tool_call.function %}{%- set tool_call = tool_call.function %}{%- endif %}{{- '<tool_call>\n{"name": "' }}{{- tool_call.name }}{{- '", "arguments": ' }}{%- if tool_call.arguments is string %}{{- tool_call.arguments }}{%- else %}{{- tool_call.arguments | tojson }}{%- endif %}{{- '}\n</tool_call>' }}{%- endfor %}{%- endif %}{{- '<|im_end|>\n' }}{%- elif message.role == "tool" %}{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}{{- '<|im_start|>user' }}{%- endif %}{{- '\n<tool_response>\n' }}{{- content }}{{- '\n</tool_response>' }}{%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}{{- '<|im_end|>\n' }}{%- endif %}{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}{{- '<|im_start|>assistant\n<think>\n' }}
{%- endif %}

2、模板中涉及的语法

变量定义与使用

{%- set video_count = namespace(value=0) %}   #定义{%- set video_count.value = video_count.value + 1 %}  #赋值

条件语句与字符串处理语句

#---------变量类型判断-------{%- if tool_call.arguments is string %} #if条件判断{{- tool_call.arguments }} #字符串输出 {{- }}{%- else %}{{- tool_call.arguments | tojson }}  # 系统tojson函数 调用{%- endif %}#---------字符串比较-------{%- if messages[loop.index0 - 1].role != "tool"  %}{{- '<|im_start|>user' }} #字符串输出 {{- }}{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}   #拼接{%- endif %}#---------字符串存在与字符串分割-------{%- if '</think>' in content %}{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}{%- set content = content.split('</think>')[-1].lstrip('\n') %}{%- endif %}

循环语句 其中 loop.first为训练中的计数变量

            {%- for tool_call in message.tool_calls %}{%- if (loop.first and content) or (not loop.first) %}{{- '\n' }}{%- endif %}{%- if tool_call.function %}{%- set tool_call = tool_call.function %}{%- endif %}{%- endfor %}

函数定义与调用 基于macro 与 endmacro 闭合函数定义域;render_content为函数名

{%- macro render_content(content, do_vision_count) %}{%- if content is string %}{{- content }}{%- else %}{%- for item in content %}{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}{%- if do_vision_count %}{%- set image_count.value = image_count.value + 1 %}{%- endif %}{%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}<|vision_start|><|image_pad|><|vision_end|>{%- elif 'video' in item or item.type == 'video' %}{%- if do_vision_count %}{%- set video_count.value = video_count.value + 1 %}{%- endif %}{%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}<|vision_start|><|video_pad|><|vision_end|>{%- elif 'text' in item %}{{- item.text }}{%- endif %}{%- endfor %}{%- endif %}
{%- endmacro %}#调用函数,并输出结果
{{- '<|im_start|>system\n' + render_content(messages[0].content, false) + '<|im_end|>\n' }}

3、模板渲染

基于以下代码可以进行模板渲染

from jinja2 import Template
import jsonwith open("chat_template.json",'r') as f:d=json.load(f)tm = Template(d['chat_template'])
messages = [{"role": "user","content": [{"type": "image","image": "https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg",},{"type": "text", "text": "Describe this image."},],}
]
msg = tm.render(messages=messages,add_generation_prompt =True)
print(msg)

输出结果如下所示

<|im_start|>user
<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>
<|im_start|>assistant
<think>
http://www.dtcms.com/a/557359.html

相关文章:

  • Pandas--数据选择与索引
  • Linux下编译WebSocket++
  • 淄博哪家公司做网站最好莱钢吧贴吧
  • 调试的艺术:从崩溃到洞察的全面指南
  • 深入洞察:大模型服务之MaaS平台
  • 1024.5不是数位和--------题解
  • 加强门户网站建设的通知博客和网站的区别
  • 阿里云网站建站网站建设的公司这个
  • 基于AWS多区域部署的高可用性与灾难恢复架构设计
  • 海外注册域名的网站好石家庄专业网站营销
  • HTML CSS八股
  • 免费网页源代码网站个人微信营销
  • 【项目实战1 -瑞吉外卖|day23 】
  • QuickLook:文件预览
  • 使用QKeyEvent keyPress(QEvent::KeyPress, key模拟键盘发送事件,会导致主程序卡死
  • 512-Spring AI Alibaba 字段分类分级 Graph 示例
  • 网站开发培训中心wordpress怎么建页面
  • 朝阳做网站哪家公司好推广广告投放
  • 我是程序员吗?
  • 在线excel数据导入导出框架
  • 手撕vector:从零实现一个C++动态数组
  • 2025年--Lc228-523. 连续的子数组和(带测试用例)-Java版
  • 统计学---2.描述性统计-参数估计
  • 辽宁城乡和住房建设部网站怎么做个网站
  • 太平洋建设集团网站网站登录失败怎么回事
  • 住宅小区物业管理系统网站建设开票 网站建设
  • 模块互相依赖问题解决的一个记录
  • 使用mujoco加载模型和控制、以及训练(一)
  • (125页PPT)麦肯锡业务流程规划方法论及流程规划案例(附下载方式)
  • AI学习研究——KIMI对佛教四圣谛深度研究