OliviaRossi commited on
Commit
817840d
·
verified ·
1 Parent(s): 5b34b4d

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +223 -0
chat_template.jinja ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 item is not mapping %}
9
+ {{- item }}
10
+ {%- elif 'image' in item or 'image_url' in item or item.type == 'image' %}
11
+ {%- if is_system_content %}
12
+ {{- raise_exception('System message cannot contain images.') }}
13
+ {%- endif %}
14
+ {%- if do_vision_count %}
15
+ {%- set image_count.value = image_count.value + 1 %}
16
+ {%- endif %}
17
+ {%- if add_vision_id is defined and add_vision_id %}
18
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
19
+ {%- endif %}
20
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
21
+ {%- elif 'video' in item or item.type == 'video' %}
22
+ {%- if is_system_content %}
23
+ {{- raise_exception('System message cannot contain videos.') }}
24
+ {%- endif %}
25
+ {%- if do_vision_count %}
26
+ {%- set video_count.value = video_count.value + 1 %}
27
+ {%- endif %}
28
+ {%- if add_vision_id is defined and add_vision_id %}
29
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
30
+ {%- endif %}
31
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
32
+ {%- elif 'audio' in item or 'audio_url' in item or item.type in ['audio', 'input_audio'] %}
33
+ {%- if is_system_content %}
34
+ {{- raise_exception('System message cannot contain audio.') }}
35
+ {%- endif %}
36
+ {{- '<|audio_start|><|audio_pad|><|audio_end|>' }}
37
+ {%- elif 'text' in item %}
38
+ {{- item.text }}
39
+ {%- else %}
40
+ {{- raise_exception('Unexpected item type in content.') }}
41
+ {%- endif %}
42
+ {%- endfor %}
43
+ {%- elif content is none or content is undefined %}
44
+ {{- '' }}
45
+ {%- else %}
46
+ {{- raise_exception('Unexpected content type.') }}
47
+ {%- endif %}
48
+ {%- endmacro %}
49
+
50
+ {%- if not messages %}
51
+ {{- raise_exception('No messages provided.') }}
52
+ {%- endif %}
53
+
54
+ {# --- 1. REASONING EFFORT RESOLUTION & PRESERVE-THINKING DISCOVERY --- #}
55
+ {%- if preserve_reasoning is defined and preserve_reasoning is not none %}
56
+ {%- set _preserve_thinking = preserve_reasoning %}
57
+ {%- elif preserve_thinking is defined and preserve_thinking is not none %}
58
+ {%- set _preserve_thinking = preserve_thinking %}
59
+ {%- else %}
60
+ {%- set _preserve_thinking = true %}
61
+ {%- endif %}
62
+
63
+ {%- set _raw_effort = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else ((thinking_effort | string | lower) if thinking_effort is defined and thinking_effort is not none else 'medium') %}
64
+ {%- set _enable_thinking = true %}
65
+ {%- if enable_thinking is defined and enable_thinking is false %}
66
+ {%- set _enable_thinking = false %}
67
+ {%- elif _raw_effort in ['none', 'off', 'false', '0'] %}
68
+ {%- set _enable_thinking = false %}
69
+ {%- endif %}
70
+
71
+ {%- set _resolved_effort = 'medium' %}
72
+ {%- if _raw_effort in ['minimal', 'min', 'low', '1'] %}
73
+ {%- set _resolved_effort = 'low' %}
74
+ {%- elif _raw_effort in ['high', '3'] %}
75
+ {%- set _resolved_effort = 'high' %}
76
+ {%- elif _raw_effort in ['xhigh', 'max', 'extreme', 'ultracode', '4'] %}
77
+ {%- set _resolved_effort = 'xhigh' %}
78
+ {%- endif %}
79
+
80
+ {%- set _effort_prompt = '' %}
81
+ {%- if _enable_thinking %}
82
+ {%- if _resolved_effort == 'low' %}
83
+ {%- set _effort_prompt = 'Reasoning effort is set to low. Keep your thinking concise, efficient, and direct to the point.' %}
84
+ {%- elif _resolved_effort == 'high' %}
85
+ {%- set _effort_prompt = 'Reasoning effort is set to high. Think thoroughly through all steps, verify logic, and test potential edge cases.' %}
86
+ {%- elif _resolved_effort == 'xhigh' %}
87
+ {%- set _effort_prompt = 'Reasoning effort is set to xhigh. Please think exhaustively through the task, systematically exploring alternative hypotheses and proving edge cases before answering.' %}
88
+ {%- endif %}
89
+ {%- endif %}
90
+
91
+ {# --- 2. SYSTEM PROMPT & TOOL DECLARATIONS --- #}
92
+ {%- set _has_tools = (tools and tools is iterable and tools is not mapping) %}
93
+ {%- if _has_tools %}
94
+ {{- '<|im_start|>system\n' }}
95
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
96
+ {%- for tool in tools %}
97
+ {{- "\n" }}
98
+ {{- tool | tojson }}
99
+ {%- endfor %}
100
+ {{- "\n</tools>" }}
101
+ {{- '\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>' }}
102
+ {%- if messages[0].role == 'system' %}
103
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
104
+ {%- if content %}
105
+ {{- '\n\n' + content }}
106
+ {%- endif %}
107
+ {%- endif %}
108
+ {%- if _effort_prompt %}
109
+ {{- '\n\n' + _effort_prompt }}
110
+ {%- endif %}
111
+ {{- '<|im_end|>\n' }}
112
+ {%- else %}
113
+ {%- if messages[0].role == 'system' %}
114
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
115
+ {%- if _effort_prompt %}
116
+ {%- set content = content + ('\n\n' + _effort_prompt if content else _effort_prompt) %}
117
+ {%- endif %}
118
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
119
+ {%- elif _effort_prompt %}
120
+ {{- '<|im_start|>system\n' + _effort_prompt + '<|im_end|>\n' }}
121
+ {%- endif %}
122
+ {%- endif %}
123
+
124
+ {# --- 3. MULTI-TURN MESSAGE EXTRACTION & REASONING PRESERVATION --- #}
125
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
126
+ {%- for message in messages[::-1] %}
127
+ {%- set index = (messages|length - 1) - loop.index0 %}
128
+ {%- if ns.multi_step_tool and message.role == "user" %}
129
+ {%- set content = render_content(message.content, false)|trim %}
130
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
131
+ {%- set ns.multi_step_tool = false %}
132
+ {%- set ns.last_query_index = index %}
133
+ {%- endif %}
134
+ {%- endif %}
135
+ {%- endfor %}
136
+ {%- if ns.multi_step_tool %}
137
+ {{- raise_exception('No user query found in messages.') }}
138
+ {%- endif %}
139
+
140
+ {%- for message in messages %}
141
+ {%- set content = render_content(message.content, true)|trim %}
142
+ {%- if message.role == "system" %}
143
+ {%- if not loop.first %}
144
+ {{- raise_exception('System message must be at the beginning.') }}
145
+ {%- endif %}
146
+ {%- elif message.role == "user" %}
147
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>\n' }}
148
+ {%- elif message.role == "assistant" %}
149
+ {%- set reasoning_content = '' %}
150
+ {%- if message.reasoning_content is string %}
151
+ {%- set reasoning_content = message.reasoning_content %}
152
+ {%- else %}
153
+ {%- if '</think>' in content %}
154
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
155
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
156
+ {%- endif %}
157
+ {%- endif %}
158
+ {%- set reasoning_content = reasoning_content|trim %}
159
+
160
+ {# Honor preserve_thinking: keep prior thoughts if true, strip older thoughts if false #}
161
+ {%- if _preserve_thinking or loop.index0 > ns.last_query_index %}
162
+ {%- if reasoning_content %}
163
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
164
+ {%- else %}
165
+ {{- '<|im_start|>' + message.role + '\n' + content }}
166
+ {%- endif %}
167
+ {%- else %}
168
+ {{- '<|im_start|>' + message.role + '\n' + content }}
169
+ {%- endif %}
170
+
171
+ {# Function call serialization #}
172
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
173
+ {%- for tool_call in message.tool_calls %}
174
+ {%- if tool_call.function is defined %}
175
+ {%- set tool_call = tool_call.function %}
176
+ {%- endif %}
177
+ {%- if loop.first %}
178
+ {%- if content|trim %}
179
+ {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
180
+ {%- else %}
181
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
182
+ {%- endif %}
183
+ {%- else %}
184
+ {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
185
+ {%- endif %}
186
+ {%- if tool_call.arguments is defined %}
187
+ {%- for args_name, args_value in tool_call.arguments|items %}
188
+ {{- '<parameter=' + args_name + '>\n' }}
189
+ {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
190
+ {{- args_value }}
191
+ {{- '\n</parameter>\n' }}
192
+ {%- endfor %}
193
+ {%- endif %}
194
+ {{- '</function>\n</tool_call>' }}
195
+ {%- endfor %}
196
+ {%- endif %}
197
+ {{- '<|im_end|>\n' }}
198
+ {%- elif message.role == "tool" %}
199
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
200
+ {{- '<|im_start|>user' }}
201
+ {%- endif %}
202
+ {{- '\n<tool_response>\n' }}
203
+ {{- content }}
204
+ {{- '\n</tool_response>' }}
205
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
206
+ {{- '<|im_end|>\n' }}
207
+ {%- elif loop.last %}
208
+ {{- '<|im_end|>\n' }}
209
+ {%- endif %}
210
+ {%- else %}
211
+ {{- raise_exception('Unexpected message role: ' ~ message.role) }}
212
+ {%- endif %}
213
+ {%- endfor %}
214
+
215
+ {# --- 4. GENERATION PROMPT PREFILL --- #}
216
+ {%- if add_generation_prompt %}
217
+ {{- '<|im_start|>assistant\n' }}
218
+ {%- if not _enable_thinking %}
219
+ {{- '<think>\n\n</think>\n\n' }}
220
+ {%- else %}
221
+ {{- '<think>\n' }}
222
+ {%- endif %}
223
+ {%- endif %}