LuffyTheFox

Untitled

May 7th, 2026
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 KB | None | 0 0
  1. {%- set image_count = namespace(value=0) %}
  2. {%- set video_count = namespace(value=0) %}
  3. {%- set preserve_thinking = True %}
  4. {%- set enable_thinking = True %}
  5. {%- macro render_content(content, do_vision_count, is_system_content=false) %}
  6. {%- if content is string %}
  7. {{- content }}
  8. {%- elif content is iterable and content is not mapping %}
  9. {%- for item in content %}
  10. {%- if '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 'text' in item %}
  33. {{- item.text }}
  34. {%- else %}
  35. {{- raise_exception('Unexpected item type in content.') }}
  36. {%- endif %}
  37. {%- endfor %}
  38. {%- elif content is none or content is undefined %}
  39. {{- '' }}
  40. {%- else %}
  41. {{- raise_exception('Unexpected content type.') }}
  42. {%- endif %}
  43. {%- endmacro %}
  44. {%- set ns_flags = namespace(enable_thinking=true) %}
  45. {%- if enable_thinking is defined %}
  46. {%- set ns_flags.enable_thinking = enable_thinking %}
  47. {%- endif %}
  48. {%- if not messages %}
  49. {{- raise_exception('No messages provided.') }}
  50. {%- endif %}
  51. {%- if tools and tools is iterable and tools is not mapping %}
  52. {{- '<|im_start|>system\n' }}
  53. {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
  54. {%- for tool in tools %}
  55. {{- "\n" }}
  56. {{- tool | tojson }}
  57. {%- endfor %}
  58. {{- "\n</tools>" }}
  59. {{- '\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>' }}
  60. {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
  61. {%- set content = render_content(messages[0].content, false, true)|trim %}
  62. {%- if '<|think_off|>' in content %}
  63. {%- set ns_flags.enable_thinking = false %}
  64. {%- set content = content.replace('<|think_off|>', '') %}
  65. {%- endif %}
  66. {%- if '<|think_on|>' in content %}
  67. {%- set ns_flags.enable_thinking = true %}
  68. {%- set content = content.replace('<|think_on|>', '') %}
  69. {%- endif %}
  70. {%- set content = content.strip() %}
  71. {%- if content %}
  72. {{- '\n\n' + content }}
  73. {%- endif %}
  74. {%- endif %}
  75. {{- '<|im_end|>\n' }}
  76. {%- else %}
  77. {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
  78. {%- set content = render_content(messages[0].content, false, true)|trim %}
  79. {%- if '<|think_off|>' in content %}
  80. {%- set ns_flags.enable_thinking = false %}
  81. {%- set content = content.replace('<|think_off|>', '') %}
  82. {%- endif %}
  83. {%- if '<|think_on|>' in content %}
  84. {%- set ns_flags.enable_thinking = true %}
  85. {%- set content = content.replace('<|think_on|>', '') %}
  86. {%- endif %}
  87. {%- set content = content.strip() %}
  88. {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
  89. {%- endif %}
  90. {%- endif %}
  91. {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
  92. {%- for message in messages[::-1] %}
  93. {%- set index = (messages|length - 1) - loop.index0 %}
  94. {%- if ns.multi_step_tool and message.role == "user" %}
  95. {%- set content = render_content(message.content, false)|trim %}
  96. {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
  97. {%- set ns.multi_step_tool = false %}
  98. {%- set ns.last_query_index = index %}
  99. {%- endif %}
  100. {%- endif %}
  101. {%- endfor %}
  102. {%- if ns.multi_step_tool %}
  103. {%- set ns.last_query_index = messages|length - 1 %}
  104. {%- endif %}
  105. {%- for message in messages %}
  106. {%- set content = render_content(message.content, true)|trim %}
  107. {%- if '<|think_off|>' in content %}
  108. {%- set ns_flags.enable_thinking = false %}
  109. {%- set content = content.replace('<|think_off|>', '') %}
  110. {%- endif %}
  111. {%- if '<|think_on|>' in content %}
  112. {%- set ns_flags.enable_thinking = true %}
  113. {%- set content = content.replace('<|think_on|>', '') %}
  114. {%- endif %}
  115. {%- set content = content.strip() %}
  116. {%- if message.role == "system" or message.role == "developer" %}
  117. {%- if not loop.first %}
  118. {{- raise_exception('System message must be at the beginning.') }}
  119. {%- endif %}
  120. {%- elif message.role == "user" %}
  121. {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
  122. {%- elif message.role == "assistant" %}
  123. {%- set reasoning_content = '' %}
  124. {%- if message.reasoning_content is string %}
  125. {%- set reasoning_content = message.reasoning_content %}
  126. {%- else %}
  127. {%- set has_think_tag = false %}
  128. {%- set think_end_token = '</think>' %}
  129. {%- if '</think>' in content %}
  130. {%- set has_think_tag = true %}
  131. {%- elif '</thinking>' in content %}
  132. {%- set has_think_tag = true %}
  133. {%- set think_end_token = '</thinking>' %}
  134. {%- elif '<think>' in content %}
  135. {%- set reasoning_content = content.split('<think>')[-1].lstrip('\n') %}
  136. {%- set content = '' %}
  137. {%- endif %}
  138. {%- if has_think_tag %}
  139. {%- set reasoning_content = content.split(think_end_token)[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
  140. {%- set content = content.split(think_end_token)[-1].lstrip('\n') %}
  141. {%- endif %}
  142. {%- endif %}
  143. {%- set reasoning_content = reasoning_content|trim %}
  144. {%- set show_think = false %}
  145. {%- if loop.index0 > ns.last_query_index %}
  146. {%- set show_think = true %}
  147. {%- elif ns_flags.enable_thinking and (preserve_thinking is undefined or preserve_thinking is true) and reasoning_content|length > 0 %}
  148. {%- set show_think = true %}
  149. {%- endif %}
  150. {%- if show_think %}
  151. {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
  152. {%- else %}
  153. {{- '<|im_start|>' + message.role + '\n' + content }}
  154. {%- endif %}
  155. {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
  156. {%- for tool_call in message.tool_calls %}
  157. {%- if tool_call.function is defined %}
  158. {%- set tool_call = tool_call.function %}
  159. {%- endif %}
  160. {%- if loop.first %}
  161. {%- if content|trim %}
  162. {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
  163. {%- else %}
  164. {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
  165. {%- endif %}
  166. {%- else %}
  167. {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
  168. {%- endif %}
  169. {%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
  170. {%- if tool_call.arguments|length > 0 %}
  171. {%- for args_name in tool_call.arguments %}
  172. {%- set args_value = tool_call.arguments[args_name] %}
  173. {{- '<parameter=' + args_name + '>\n' }}
  174. {%- set args_value = args_value | string if args_value is string else args_value | tojson %}
  175. {{- args_value }}
  176. {{- '\n</parameter>\n' }}
  177. {%- endfor %}
  178. {%- endif %}
  179. {%- elif tool_call.arguments is defined and tool_call.arguments is string %}
  180. {%- if tool_call.arguments|trim|length > 0 %}
  181. {{- tool_call.arguments }}
  182. {{- '\n' }}
  183. {%- endif %}
  184. {%- endif %}
  185. {{- '</function>\n</tool_call>' }}
  186. {%- endfor %}
  187. {%- endif %}
  188. {{- '<|im_end|>\n' }}
  189. {%- elif message.role == "tool" %}
  190. {%- if loop.previtem and loop.previtem.role != "tool" %}
  191. {{- '<|im_start|>user' }}
  192. {%- endif %}
  193. {{- '\n<tool_response>\n' }}
  194. {{- content }}
  195. {{- '\n</tool_response>' }}
  196. {%- if not loop.last and loop.nextitem.role != "tool" %}
  197. {{- '<|im_end|>\n' }}
  198. {%- elif loop.last %}
  199. {{- '<|im_end|>\n' }}
  200. {%- endif %}
  201. {%- else %}
  202. {{- raise_exception('Unexpected message role.') }}
  203. {%- endif %}
  204. {%- endfor %}
  205. {%- if add_generation_prompt %}
  206. {{- '<|im_start|>assistant\n' }}
  207. {%- if ns_flags.enable_thinking is false %}
  208. {{- '<think>\n\n</think>\n\n' }}
  209. {%- else %}
  210. {{- '<think>\n' }}
  211. {%- endif %}
  212. {%- endif %}
Advertisement
Add Comment
Please, Sign In to add comment