ferrariedhgs commited on
Commit
aee17b3
·
verified ·
1 Parent(s): 1e0d208

Upload nemotron-template.jinja

Browse files
Files changed (1) hide show
  1. nemotron-template.jinja +221 -0
nemotron-template.jinja ADDED
@@ -0,0 +1,221 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {# Unsloth template fixes #}
2
+ {% macro render_extra_keys(json_dict, handled_keys) %}
3
+ {%- if json_dict is mapping %}
4
+ {%- for json_key in json_dict if json_key not in handled_keys %}
5
+ {%- if json_dict[json_key] is mapping or (json_dict[json_key] is iterable and json_dict[json_key] is not string) %}
6
+ {{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson) ~ '</' ~ json_key ~ '>' }}
7
+ {%- else %}
8
+ {# FIX: Added default('', true) to prevent null errors #}
9
+ {{-'\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | default('', true) | string) ~ '</' ~ json_key ~ '>' }}
10
+ {%- endif %}
11
+ {%- endfor %}
12
+ {%- endif %}
13
+ {% endmacro %}
14
+
15
+ {# --- Default is True (Standard behavior) --- #}
16
+ {%- set enable_thinking = enable_thinking if enable_thinking is defined else True %}
17
+ {%- set truncate_history_thinking = truncate_history_thinking if truncate_history_thinking is defined else True %}
18
+
19
+ {%- set ns = namespace(last_user_idx = -1) %}
20
+ {%- set loop_messages = messages %}
21
+ {%- for m in loop_messages %}
22
+ {%- if m["role"] == "user" %}
23
+ {%- set ns.last_user_idx = loop.index0 %}
24
+ {%- endif %}
25
+ {%- endfor %}
26
+
27
+ {%- if messages[0]["role"] == "system" %}
28
+ {# FIX: Handle null system content #}
29
+ {%- set system_message = messages[0]["content"] | default('', true) %}
30
+ {# --- Logic: Check for /nothink trigger --- #}
31
+ {%- if '/nothink' in system_message %}
32
+ {%- set enable_thinking = False %}
33
+ {%- endif %}
34
+ {# --------------------------------------------- #}
35
+ {%- set loop_messages = messages[1:] %}
36
+ {%- else %}
37
+ {%- set system_message = "" %}
38
+ {%- set loop_messages = messages %}
39
+ {%- endif %}
40
+ {%- if not tools is defined %}
41
+ {%- set tools = [] %}
42
+ {%- endif %}
43
+ {# Recompute last_user_idx relative to loop_messages after handling system #}
44
+ {%- set ns = namespace(last_user_idx = -1) %}
45
+ {%- for m in loop_messages %}
46
+ {%- if m["role"] == "user" %}
47
+ {%- set ns.last_user_idx = loop.index0 %}
48
+ {%- endif %}
49
+ {%- endfor %}
50
+ {%- if system_message is defined %}
51
+ {{- "<|im_start|>system\n" + system_message }}
52
+ {%- else %}
53
+ {%- if tools is iterable and tools | length > 0 %}
54
+ {{- "<|im_start|>system\n" }}
55
+ {%- endif %}
56
+ {%- endif %}
57
+ {%- if tools is iterable and tools | length > 0 %}
58
+ {%- if system_message is defined and system_message | length > 0 %}
59
+ {{- "\n\n" }}
60
+ {%- endif %}
61
+ {{- "# Tools\n\nYou have access to the following functions:\n\n" }}
62
+ {{- "<tools>" }}
63
+ {%- for tool in tools %}
64
+ {%- if tool.function is defined %}
65
+ {%- set tool = tool.function %}
66
+ {%- endif %}
67
+ {{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
68
+ {%- if tool.description is defined %}
69
+ {{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
70
+ {%- endif %}
71
+ {{- '\n<parameters>' }}
72
+ {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
73
+ {%- for param_name, param_fields in tool.parameters.properties|items %}
74
+ {{- '\n<parameter>' }}
75
+ {{- '\n<name>' ~ param_name ~ '</name>' }}
76
+ {%- if param_fields.type is defined %}
77
+ {{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
78
+ {%- endif %}
79
+ {%- if param_fields.description is defined %}
80
+ {{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
81
+ {%- endif %}
82
+ {%- if param_fields.enum is defined %}
83
+ {{- '\n<enum>' ~ (param_fields.enum | tojson) ~ '</enum>' }}
84
+ {%- endif %}
85
+ {%- set handled_keys = ['name', 'type', 'description', 'enum'] %}
86
+ {{- render_extra_keys(param_fields, handled_keys) }}
87
+ {{- '\n</parameter>' }}
88
+ {%- endfor %}
89
+ {%- endif %}
90
+ {% set handled_keys = ['type', 'properties', 'required'] %}
91
+ {{- render_extra_keys(tool.parameters, handled_keys) }}
92
+ {%- if tool.parameters is defined and tool.parameters.required is defined %}
93
+ {{- '\n<required>' ~ (tool.parameters.required | tojson) ~ '</required>' }}
94
+ {%- endif %}
95
+ {{- '\n</parameters>' }}
96
+ {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
97
+ {{- render_extra_keys(tool, handled_keys) }}
98
+ {{- '\n</function>' }}
99
+ {%- endfor %}
100
+ {{- "\n</tools>" }}
101
+
102
+ {{- '\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>' }}
103
+ {%- endif %}
104
+
105
+
106
+ {%- if system_message is defined %}
107
+ {{- '<|im_end|>\n' }}
108
+ {%- else %}
109
+ {%- if tools is iterable and tools | length > 0 %}
110
+ {{- '<|im_end|>\n' }}
111
+ {%- endif %}
112
+ {%- endif %}
113
+
114
+ {%- for message in loop_messages %}
115
+ {%- if message.role == "assistant" %}
116
+ {# Add reasoning content in to content field for unified processing below. #}
117
+ {%- if message.reasoning_content is defined and message.reasoning_content is string and message.reasoning_content | trim | length > 0 %}
118
+ {%- set content = "<think>\n" ~ message.reasoning_content ~ "\n</think>\n" ~ (message.content | default('', true)) %}
119
+ {%- else %}
120
+ {%- set content = message.content | default('', true) %}
121
+ {%- if content is string -%}
122
+ {# Allow downstream logic to to take care of broken thought, only handle coherent reasoning here. #}
123
+ {%- if '<think>' not in content and '</think>' not in content -%}
124
+ {%- set content = "<think></think>" ~ content -%}
125
+ {# Fix content with closing think but not opening #}
126
+ {%- elif '<think>' not in content and '</think>' in content -%}
127
+ {%- set content = "<think>
128
+ " ~ content -%}
129
+ {%- endif -%}
130
+ {%- else -%}
131
+ {%- set content = content -%}
132
+ {%- endif -%}
133
+ {%- endif %}
134
+ {%- if message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
135
+ {# Assistant message has tool calls. #}
136
+ {{- '<|im_start|>assistant\n' }}
137
+ {%- set include_content = not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
138
+ {%- if content is string and content | trim | length > 0 %}
139
+ {%- if include_content %}
140
+ {{- (content | trim) ~ '\n' -}}
141
+ {%- else %}
142
+ {%- set c = (content | string) %}
143
+ {%- if '</think>' in c %}
144
+ {# Keep only content after the last closing think. Also generation prompt causes this. #}
145
+ {%- set c = (c.split('</think>')|last) %}
146
+ {%- elif '<think>' in c %}
147
+ {# If <think> was opened but never closed, drop the trailing think segment #}
148
+ {%- set c = (c.split('<think>')|first) %}
149
+ {%- endif %}
150
+ {%- set c = "<think></think>" ~ c | trim %}
151
+ {%- if c | length > 0 %}
152
+ {{- c ~ '\n' -}}
153
+ {%- endif %}
154
+ {%- endif %}
155
+ {%- else %}
156
+ {{- "<think></think>" -}}
157
+ {%- endif %}
158
+ {%- for tool_call in message.tool_calls %}
159
+ {%- if tool_call.function is defined %}
160
+ {%- set tool_call = tool_call.function %}
161
+ {%- endif %}
162
+ {{- '<tool_call>\n<function=' ~ tool_call.name ~ '>\n' -}}
163
+ {%- if tool_call.arguments is defined %}{%- if tool_call.arguments is mapping %}
164
+ {%- for args_name, args_value in tool_call.arguments|items %}
165
+ {{- '<parameter=' ~ args_name ~ '>\n' -}}
166
+ {# FIX: Added default('', true) to tool args #}
167
+ {%- set args_value = args_value | tojson if args_value is mapping or (args_value is iterable and args_value is not string) else args_value | default('', true) | string %}
168
+ {{- args_value ~ '\n</parameter>\n' -}}
169
+ {%- endfor %}{%- endif %}
170
+ {%- endif %}
171
+ {{- '</function>\n</tool_call>\n' -}}
172
+ {%- endfor %}
173
+ {{- '<|im_end|>\n' }}
174
+ {%- else %}
175
+ {# Assistant message doesn't have tool calls. #}
176
+ {%- if not (truncate_history_thinking and loop.index0 < ns.last_user_idx) %}
177
+ {{- '<|im_start|>assistant\n' ~ (content | default('', true) | string | trim) ~ '<|im_end|>\n' }}
178
+ {%- else %}
179
+ {%- set c = (content | default('', true) | string) %}
180
+ {%- if '<think>' in c and '</think>' in c %}
181
+ {%- set c = "<think></think>" ~ (c.split('</think>')|last) %}
182
+ {%- endif %}
183
+ {%- set c = c | trim %}
184
+ {%- if c | length > 0 %}
185
+ {{- '<|im_start|>assistant\n' ~ c ~ '<|im_end|>\n' }}
186
+ {%- else %}
187
+ {{- '<|im_start|>assistant\n<|im_end|>\n' }}
188
+ {%- endif %}
189
+ {%- endif %}
190
+ {%- endif %}
191
+ {%- elif message.role == "user" or message.role == "system" %}
192
+ {{- '<|im_start|>' + message.role + '\n' }}
193
+ {# FIX: Safely handle null message content #}
194
+ {%- set content = message.content | default('', true) | string %}
195
+ {{- content }}
196
+ {{- '<|im_end|>\n' }}
197
+ {%- elif message.role == "tool" %}
198
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
199
+ {{- '<|im_start|>user\n' }}
200
+ {%- endif %}
201
+ {{- '<tool_response>\n' }}
202
+ {{- message.content | default('', true) }}
203
+ {{- '\n</tool_response>\n' }}
204
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
205
+ {{- '<|im_end|>\n' }}
206
+ {%- elif loop.last %}
207
+ {{- '<|im_end|>\n' }}
208
+ {%- endif %}
209
+ {%- else %}
210
+ {{- '<|im_start|>' + message.role + '\n' + (message.content | default('', true)) + '<|im_end|>\n' }}
211
+ {%- endif %}
212
+ {%- endfor %}
213
+
214
+ {%- if add_generation_prompt %}
215
+ {%- if enable_thinking %}
216
+ {{- '<|im_start|>assistant\n<think>\n' }}
217
+ {%- else %}
218
+ {{- '<|im_start|>assistant\n<think></think>' }}
219
+ {%- endif %}
220
+ {%- endif %}
221
+ {# Copyright 2025-present Unsloth. Apache 2.0 License. #}