NVIDIA-Nemotron-Nano-v2.jinja 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. {%- set ns = namespace(enable_thinking=true) -%}
  2. {%- for message in messages -%}
  3. {%- set content = message['content'] -%}
  4. {%- if message['role'] == 'user' or message['role'] == 'system' -%}
  5. {%- if '/think' in content -%}
  6. {%- set ns.enable_thinking = true -%}
  7. {%- elif '/no_think' in content -%}
  8. {%- set ns.enable_thinking = false -%}
  9. {%- endif -%}
  10. {%- endif -%}
  11. {%- endfor -%}
  12. {%- if messages[0]['role'] != 'system' -%}
  13. {%- set ns.non_tool_system_content = '' -%}
  14. {{- '<SPECIAL_10>System
  15. ' -}}
  16. {%- else -%}
  17. {%- set ns.non_tool_system_content = (messages[0]['content'] | default('', true)).replace('/think', '').replace('/no_think', '').strip() -%}
  18. {{- '<SPECIAL_10>System
  19. ' + ns.non_tool_system_content }}
  20. {%- endif -%}
  21. {%- if tools -%}
  22. {%- if ns.non_tool_system_content is defined and ns.non_tool_system_content != '' -%}
  23. {{- '
  24. ' -}}
  25. {%- endif -%}
  26. {{- 'You can use the following tools to assist the user if required:' -}}
  27. {{- '
  28. <AVAILABLE_TOOLS>[' -}}
  29. {%- for tool in tools -%}
  30. {{- (tool.function if tool.function is defined else tool) | tojson -}}
  31. {{- ', ' if not loop.last else '' -}}
  32. {%- endfor -%}
  33. {{- ']</AVAILABLE_TOOLS>
  34. ' -}}
  35. {{- 'If you decide to call any tool(s), use the following format:
  36. ' -}}
  37. {{- '<TOOLCALL>[{{"name": "tool_name1", "arguments": "tool_args1"}}, ' -}}
  38. {{- '{{"name": "tool_name2", "arguments": "tool_args2"}}]</TOOLCALL>
  39. ' -}}
  40. {{- 'The user will execute tool-calls and return responses from tool(s) in this format:
  41. ' -}}
  42. {{- '<TOOL_RESPONSE>[{{"tool_response1"}}, {{"tool_response2"}}]</TOOL_RESPONSE>
  43. ' -}}
  44. {{- 'Based on the tool responses, you can call additional tools if needed, correct tool calls if any errors are found, or just respond to the user.' -}}
  45. {%- endif -%}
  46. {{- '
  47. ' -}}
  48. {%- set messages = messages[1:] if messages[0]['role'] == 'system' else messages -%}
  49. {%- if messages[-1]['role'] == 'assistant' -%}
  50. {%- set ns.last_turn_assistant_content = (messages[-1]['content'] | default('', true)).strip() -%}
  51. {%- set ns.last_turn_assistant_tool_calls = messages[-1]['tool_calls'] if 'tool_calls' in messages[-1] else [] -%}
  52. {%- set messages = messages[:-1] -%}
  53. {%- endif -%}
  54. {%- for message in messages %}
  55. {%- set content = message['content'] %}
  56. {%- if message['role'] == 'user' -%}
  57. {{- '<SPECIAL_11>User
  58. ' + (content | default('', true)).replace('/think', '').replace('/no_think', '').strip() + '
  59. ' }}
  60. {%- elif message['role'] == 'tool' -%}
  61. {%- if loop.first or (messages[loop.index0 - 1].role != 'tool') -%}
  62. {{- '<SPECIAL_11>User
  63. ' + '<TOOL_RESPONSE>[' }}
  64. {%- endif -%}
  65. {{- message['content'] -}}
  66. {{- ', ' if not loop.last and (messages[loop.index0 + 1].role == 'tool') else '' -}}
  67. {%- if loop.last or (messages[loop.index0 + 1].role != 'tool') -%}
  68. {{- ']</TOOL_RESPONSE>' -}}
  69. {%- endif -%}
  70. {%- elif message['role'] == 'assistant' -%}
  71. {%- if content and '</think>' in content -%}
  72. {%- set content = (content.split('</think>')[1] | default('', true)).strip() %}
  73. {%- endif -%}
  74. {{- '<SPECIAL_11>Assistant
  75. ' + ((content | default('', true)).strip() if content is not none else '') }}
  76. {%- if message.tool_calls -%}
  77. {%- if (content | default('', true)).strip() != '' -%}
  78. {{- '
  79. ' -}}
  80. {%- endif -%}
  81. {{- '<TOOLCALL>[' -}}
  82. {%- for call in message.tool_calls -%}
  83. {%- set fn = call.function if call.function is defined else call -%}
  84. {{- '{"name": "' + fn.name + '", "arguments": ' -}}
  85. {%- if fn.arguments is string -%}
  86. {{- fn.arguments -}}
  87. {%- else -%}
  88. {{- fn.arguments | tojson -}}
  89. {%- endif -%}
  90. {{- '}' + (', ' if not loop.last else '') -}}
  91. {%- endfor -%}
  92. {{- ']</TOOLCALL>' -}}
  93. {%- endif -%}
  94. {{- '
  95. <SPECIAL_12>
  96. ' -}}
  97. {%- endif -%}
  98. {%- endfor -%}
  99. {%- if add_generation_prompt -%}
  100. {{- '<SPECIAL_11>Assistant
  101. ' -}}
  102. {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%}
  103. {{- '<think></think>' -}}
  104. {%- else -%}
  105. {{- '<think>
  106. ' -}}
  107. {%- endif -%}
  108. {%- if ns.last_turn_assistant_content is defined and ns.last_turn_assistant_content != '' -%}
  109. {{- ns.last_turn_assistant_content -}}
  110. {%- endif -%}
  111. {%- else -%}
  112. {%- if ns.last_turn_assistant_content is defined and ns.last_turn_assistant_content != '' -%}
  113. {{- '<SPECIAL_11>Assistant
  114. ' -}}
  115. {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%}
  116. {{- '<think></think>' -}}
  117. {%- else -%}
  118. {{- '<think>
  119. ' -}}
  120. {%- endif -%}
  121. {{- ns.last_turn_assistant_content -}}
  122. {%- if continue_final_message is defined -%}
  123. {%- if continue_final_message is false -%}
  124. {{- '
  125. <SPECIAL_12>
  126. ' -}}
  127. {%- endif -%}
  128. {%- else -%}
  129. {{- '
  130. <SPECIAL_12>
  131. ' -}}
  132. {%- endif -%}
  133. {%- endif -%}
  134. {%- if ns.last_turn_assistant_tool_calls is defined and ns.last_turn_assistant_tool_calls | length > 0 -%}
  135. {{- '<SPECIAL_11>Assistant
  136. ' -}}
  137. {{- '<TOOLCALL>[' -}}
  138. {%- for call in ns.last_turn_assistant_tool_calls -%}
  139. {%- set fn = call.function if call.function is defined else call -%}
  140. {{- '{"name": "' + fn.name + '", "arguments": ' -}}
  141. {%- if fn.arguments is string -%}
  142. {{- fn.arguments -}}
  143. {%- else -%}
  144. {{- fn.arguments | tojson -}}
  145. {%- endif -%}
  146. {{- '}' + (', ' if not loop.last else '') -}}
  147. {%- endfor -%}
  148. {{- ']</TOOLCALL>' -}}
  149. {{- '<SPECIAL_12>
  150. ' -}}
  151. {%- endif -%}
  152. {%- endif -%}