hzhwcmhf commited on
Commit
65e34c6
·
verified ·
1 Parent(s): fe5a6d3

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +55 -21
chat_template.jinja CHANGED
@@ -1,29 +1,48 @@
1
  {%- set image_count = namespace(value=0) %}
2
  {%- set video_count = namespace(value=0) %}
3
- {%- macro render_content(content, do_vision_count) %}
4
  {%- if content is string %}
5
  {{- content }}
6
- {%- else %}
7
  {%- for item in content %}
8
  {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
 
 
 
9
  {%- if do_vision_count %}
10
  {%- set image_count.value = image_count.value + 1 %}
11
  {%- endif %}
12
- {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif -%}
13
- <|vision_start|><|image_pad|><|vision_end|>
 
 
14
  {%- elif 'video' in item or item.type == 'video' %}
 
 
 
15
  {%- if do_vision_count %}
16
  {%- set video_count.value = video_count.value + 1 %}
17
  {%- endif %}
18
- {%- if add_vision_id %}Video {{ video_count.value }}: {% endif -%}
19
- <|vision_start|><|video_pad|><|vision_end|>
 
 
20
  {%- elif 'text' in item %}
21
  {{- item.text }}
 
 
22
  {%- endif %}
23
  {%- endfor %}
 
 
 
 
24
  {%- endif %}
25
  {%- endmacro %}
26
- {%- if tools is defined and tools is iterable and tools | length > 0 %}
 
 
 
27
  {{- '<|im_start|>system\n' }}
28
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
29
  {%- for tool in tools %}
@@ -32,34 +51,44 @@
32
  {%- endfor %}
33
  {{- "\n</tools>" }}
34
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
35
- {%- if messages[0].role == 'system' and messages[0].content | trim %}
36
- {%- set content = render_content(messages[0].content, false) %}
37
- {{- '\n\n' + content.lstrip('\n') }}
 
 
38
  {%- endif %}
39
  {{- '<|im_end|>\n' }}
40
  {%- else %}
41
  {%- if messages[0].role == 'system' %}
42
- {{- '<|im_start|>system\n' + render_content(messages[0].content, false) + '<|im_end|>\n' }}
 
43
  {%- endif %}
44
  {%- endif %}
45
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
46
  {%- for message in messages[::-1] %}
47
  {%- set index = (messages|length - 1) - loop.index0 %}
48
  {%- if ns.multi_step_tool and message.role == "user" %}
49
- {%- set content = render_content(message.content, False) %}
50
  {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
51
  {%- set ns.multi_step_tool = false %}
52
  {%- set ns.last_query_index = index %}
53
  {%- endif %}
54
  {%- endif %}
55
  {%- endfor %}
 
 
 
56
  {%- for message in messages %}
57
- {%- set content = render_content(message.content, true) %}
58
- {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
 
 
 
 
59
  {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
60
  {%- elif message.role == "assistant" %}
61
  {%- set reasoning_content = '' %}
62
- {%- if message.reasoning_content is defined and message.reasoning_content is not none %}
63
  {%- set reasoning_content = message.reasoning_content %}
64
  {%- else %}
65
  {%- if '</think>' in content %}
@@ -67,15 +96,13 @@
67
  {%- set content = content.split('</think>')[-1].lstrip('\n') %}
68
  {%- endif %}
69
  {%- endif %}
70
- {%- if message.tool_calls is defined and message.tool_calls is not none and message.tool_calls is iterable and message.tool_calls | length > 0 %}
71
- {%- set content = content.rstrip('\n') %}
72
- {%- endif %}
73
  {%- if loop.index0 > ns.last_query_index %}
74
- {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
75
  {%- else %}
76
  {{- '<|im_start|>' + message.role + '\n' + content }}
77
  {%- endif %}
78
- {%- if message.tool_calls is defined and message.tool_calls is not none and message.tool_calls is iterable and message.tool_calls | length > 0 %}
79
  {%- for tool_call in message.tool_calls %}
80
  {%- if tool_call.function is defined %}
81
  {%- set tool_call = tool_call.function %}
@@ -113,8 +140,15 @@
113
  {%- elif loop.last %}
114
  {{- '<|im_end|>\n' }}
115
  {%- endif %}
 
 
116
  {%- endif %}
117
  {%- endfor %}
118
  {%- if add_generation_prompt %}
119
- {{- '<|im_start|>assistant\n<think>\n' }}
 
 
 
 
 
120
  {%- endif %}
 
1
  {%- set image_count = namespace(value=0) %}
2
  {%- set video_count = namespace(value=0) %}
3
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
4
  {%- if content is string %}
5
  {{- content }}
6
+ {%- elif content is iterable and content is not mapping %}
7
  {%- for item in content %}
8
  {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
+ {%- if is_system_content %}
10
+ {{- raise_exception('System message cannot contain images.') }}
11
+ {%- endif %}
12
  {%- if do_vision_count %}
13
  {%- set image_count.value = image_count.value + 1 %}
14
  {%- endif %}
15
+ {%- if add_vision_id %}
16
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
17
+ {%- endif %}
18
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19
  {%- elif 'video' in item or item.type == 'video' %}
20
+ {%- if is_system_content %}
21
+ {{- raise_exception('System message cannot contain videos.') }}
22
+ {%- endif %}
23
  {%- if do_vision_count %}
24
  {%- set video_count.value = video_count.value + 1 %}
25
  {%- endif %}
26
+ {%- if add_vision_id %}
27
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
28
+ {%- endif %}
29
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30
  {%- elif 'text' in item %}
31
  {{- item.text }}
32
+ {%- else %}
33
+ {{- raise_exception('Unexpected item type in content.') }}
34
  {%- endif %}
35
  {%- endfor %}
36
+ {%- elif content is none or content is undefined %}
37
+ {{- '' }}
38
+ {%- else %}
39
+ {{- raise_exception('Unexpected content type.') }}
40
  {%- endif %}
41
  {%- endmacro %}
42
+ {%- if not messages %}
43
+ {{- raise_exception('No messages provided.') }}
44
+ {%- endif %}
45
+ {%- if tools and tools is iterable and tools is not mapping %}
46
  {{- '<|im_start|>system\n' }}
47
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
48
  {%- for tool in tools %}
 
51
  {%- endfor %}
52
  {{- "\n</tools>" }}
53
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
54
+ {%- if messages[0].role == 'system' %}
55
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
56
+ {%- if content %}
57
+ {{- '\n\n' + content }}
58
+ {%- endif %}
59
  {%- endif %}
60
  {{- '<|im_end|>\n' }}
61
  {%- else %}
62
  {%- if messages[0].role == 'system' %}
63
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
64
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
65
  {%- endif %}
66
  {%- endif %}
67
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
68
  {%- for message in messages[::-1] %}
69
  {%- set index = (messages|length - 1) - loop.index0 %}
70
  {%- if ns.multi_step_tool and message.role == "user" %}
71
+ {%- set content = render_content(message.content, false)|trim %}
72
  {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
73
  {%- set ns.multi_step_tool = false %}
74
  {%- set ns.last_query_index = index %}
75
  {%- endif %}
76
  {%- endif %}
77
  {%- endfor %}
78
+ {%- if ns.multi_step_tool %}
79
+ {{- raise_exception('No user query found in messages.') }}
80
+ {%- endif %}
81
  {%- for message in messages %}
82
+ {%- set content = render_content(message.content, true)|trim %}
83
+ {%- if message.role == "system" %}
84
+ {%- if not loop.first %}
85
+ {{- raise_exception('System message must be at the beginning.') }}
86
+ {%- endif %}
87
+ {%- elif message.role == "user" %}
88
  {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
89
  {%- elif message.role == "assistant" %}
90
  {%- set reasoning_content = '' %}
91
+ {%- if message.reasoning_content is string %}
92
  {%- set reasoning_content = message.reasoning_content %}
93
  {%- else %}
94
  {%- if '</think>' in content %}
 
96
  {%- set content = content.split('</think>')[-1].lstrip('\n') %}
97
  {%- endif %}
98
  {%- endif %}
99
+ {%- set reasoning_content = reasoning_content|trim %}
 
 
100
  {%- if loop.index0 > ns.last_query_index %}
101
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
102
  {%- else %}
103
  {{- '<|im_start|>' + message.role + '\n' + content }}
104
  {%- endif %}
105
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
106
  {%- for tool_call in message.tool_calls %}
107
  {%- if tool_call.function is defined %}
108
  {%- set tool_call = tool_call.function %}
 
140
  {%- elif loop.last %}
141
  {{- '<|im_end|>\n' }}
142
  {%- endif %}
143
+ {%- else %}
144
+ {{- raise_exception('Unexpected message role.') }}
145
  {%- endif %}
146
  {%- endfor %}
147
  {%- if add_generation_prompt %}
148
+ {{- '<|im_start|>assistant\n' }}
149
+ {%- if enable_thinking is defined and enable_thinking is false %}
150
+ {{- '<think>\n\n</think>\n\n' }}
151
+ {%- else %}
152
+ {{- '<think>\n' }}
153
+ {%- endif %}
154
  {%- endif %}