NVIDIA-Nemotron-3-Nano-30B-A3B-BF16.jinja 10 KB

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