LuffyTheFox

Untitled

Mar 29th, 2026 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. {%- set image_count = namespace(value=0) %}
  2. {%- set video_count = namespace(value=0) %}
  3. {%- set enable_thinking = False %}
  4. {%- macro render_content(content, do_vision_count, is_system_content=false) %}
  5. {%- if content is string %}
  6. {{- content }}
  7. {%- elif content is iterable and content is not mapping %}
  8. {%- for item in content %}
  9. {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
  10. {%- if is_system_content %}
  11. {{- raise_exception('System message cannot contain images.') }}
  12. {%- endif %}
  13. {%- if do_vision_count %}
  14. {%- set image_count.value = image_count.value + 1 %}
  15. {%- endif %}
  16. {%- if add_vision_id %}
  17. {{- 'Picture ' ~ image_count.value ~ ': ' }}
  18. {%- endif %}
  19. {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
  20. {%- elif 'video' in item or item.type == 'video' %}
  21. {%- if is_system_content %}
  22. {{- raise_exception('System message cannot contain videos.') }}
  23. {%- endif %}
  24. {%- if do_vision_count %}
  25. {%- set video_count.value = video_count.value + 1 %}
  26. {%- endif %}
  27. {%- if add_vision_id %}
  28. {{- 'Video ' ~ video_count.value ~ ': ' }}
  29. {%- endif %}
  30. {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
  31. {%- elif 'text' in item %}
  32. {{- item.text }}
  33. {%- else %}
  34. {{- raise_exception('Unexpected item type in content.') }}
  35. {%- endif %}
  36. {%- endfor %}
  37. {%- elif content is none or content is undefined %}
  38. {{- '' }}
  39. {%- else %}
  40. {{- raise_exception('Unexpected content type.') }}
  41. {%- endif %}
  42. {%- endmacro %}
  43. {%- macro strip_system_tokens(text) %}
  44. {%- set result = text %}
  45. {%- set result = result | replace('<eos>', '') %}
  46. {%- set result = result | replace('<bos>', '') %}
  47. {%- set result = result | replace('<direct>', '') %}
  48. {%- set result = result | replace('<pad>', '') %}
  49. {%- set result = result | replace('<unk>', '') %}
  50. {%- set result = result | replace('<mask>', '') %}
  51. {%- set result = result | replace('<sep>', '') %}
  52. {%- set result = result | replace('<cls>', '') %}
  53. {%- set result = result | replace('<s>', '') %}
  54. {%- set result = result | replace('</s>', '') %}
  55. {{- result }}
  56. {%- endmacro %}
  57. {%- if not messages %}
  58. {{- raise_exception('No messages provided.') }}
  59. {%- endif %}
  60. {%- if tools and tools is iterable and tools is not mapping %}
  61. {{- '<|im_start|>system
  62. ' }}
  63. {{- "# Tools
  64.  
  65. You have access to the following functions:
  66.  
  67. <tools>" }}
  68. {%- for tool in tools %}
  69. {{- "
  70. " }}
  71. {{- tool | tojson }}
  72. {%- endfor %}
  73. {{- "
  74. </tools>" }}
  75. {{- '
  76.  
  77. If you choose to call a function ONLY reply in the following format with NO suffix:
  78.  
  79. <tool_call>
  80. <function=example_function_name>
  81. <parameter=example_parameter_1>
  82. value_1
  83. </parameter>
  84. <parameter=example_parameter_2>
  85. This is the value for the second parameter
  86. that can span
  87. multiple lines
  88. </parameter>
  89. </function>
  90. </tool_call>
  91.  
  92. <IMPORTANT>
  93. Reminder:
  94. - Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags
  95. - Required parameters MUST be specified
  96. - You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after
  97. - 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
  98. </IMPORTANT>' }}
  99. {%- if messages[0].role == 'system' %}
  100. {%- set content = strip_system_tokens(render_content(messages[0].content, false, true)|trim)|trim %}
  101. {%- if content %}
  102. {{- '
  103.  
  104. ' + content }}
  105. {%- endif %}
  106. {%- endif %}
  107. {{- '<|im_end|>
  108. ' }}
  109. {%- else %}
  110. {%- if messages[0].role == 'system' %}
  111. {%- set content = strip_system_tokens(render_content(messages[0].content, false, true)|trim)|trim %}
  112. {{- '<|im_start|>system
  113. ' + content + '<|im_end|>
  114. ' }}
  115. {%- endif %}
  116. {%- endif %}
  117. {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
  118. {%- for message in messages[::-1] %}
  119. {%- set index = (messages|length - 1) - loop.index0 %}
  120. {%- if ns.multi_step_tool and message.role == "user" %}
  121. {%- set content = strip_system_tokens(render_content(message.content, false)|trim)|trim %}
  122. {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
  123. {%- set ns.multi_step_tool = false %}
  124. {%- set ns.last_query_index = index %}
  125. {%- endif %}
  126. {%- endif %}
  127. {%- endfor %}
  128. {%- if ns.multi_step_tool %}
  129. {{- raise_exception('No user query found in messages.') }}
  130. {%- endif %}
  131. {%- for message in messages %}
  132. {%- set content = strip_system_tokens(render_content(message.content, true)|trim)|trim %}
  133. {%- if message.role == "system" %}
  134. {%- if not loop.first %}
  135. {{- raise_exception('System message must be at the beginning.') }}
  136. {%- endif %}
  137. {%- elif message.role == "user" %}
  138. {{- '<|im_start|>' + message.role + '
  139. ' + content + '<|im_end|>' + '
  140. ' }}
  141. {%- elif message.role == "assistant" %}
  142. {{- '<|im_start|>' + message.role + '
  143. ' }}
  144. {%- if message.reasoning_content and message.reasoning_content is string %}
  145. {%- set cleaned_reasoning = strip_system_tokens(message.reasoning_content)|trim %}
  146. {{- '<think>
  147. ' + cleaned_reasoning + '
  148. </think>
  149.  
  150. ' }}
  151. {%- endif %}
  152. {{- content }}
  153. {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
  154. {%- for tool_call in message.tool_calls %}
  155. {%- if tool_call.function is defined %}
  156. {%- set tool_call = tool_call.function %}
  157. {%- endif %}
  158. {%- if loop.first %}
  159. {%- if content|trim %}
  160. {{- '
  161.  
  162. <tool_call>
  163. <function=' + tool_call.name + '>
  164. ' }}
  165. {%- else %}
  166. {{- '<tool_call>
  167. <function=' + tool_call.name + '>
  168. ' }}
  169. {%- endif %}
  170. {%- else %}
  171. {{- '
  172. <tool_call>
  173. <function=' + tool_call.name + '>
  174. ' }}
  175. {%- endif %}
  176. {%- if tool_call.arguments is mapping %}
  177. {%- for args_name in tool_call.arguments %}
  178. {%- set args_value = tool_call.arguments[args_name] %}
  179. {{- '<parameter=' + args_name + '>
  180. ' }}
  181. {%- if args_value is mapping or (args_value is iterable and args_value is not string) %}
  182. {{- args_value | tojson }}
  183. {%- else %}
  184. {{- args_value | string }}
  185. {%- endif %}
  186. {{- '
  187. </parameter>
  188. ' }}
  189. {%- endfor %}
  190. {%- endif %}
  191. {{- '</function>
  192. </tool_call>' }}
  193. {%- endfor %}
  194. {%- endif %}
  195. {{- '<|im_end|>
  196. ' }}
  197. {%- elif message.role == "tool" %}
  198. {%- if loop.previtem and loop.previtem.role != "tool" %}
  199. {{- '<|im_start|>user' }}
  200. {%- endif %}
  201. {{- '
  202. <tool_response>
  203. ' }}
  204. {{- content }}
  205. {{- '
  206. </tool_response>' }}
  207. {%- if not loop.last and loop.nextitem.role != "tool" %}
  208. {{- '<|im_end|>
  209. ' }}
  210. {%- elif loop.last %}
  211. {{- '<|im_end|>
  212. ' }}
  213. {%- endif %}
  214. {%- else %}
  215. {{- raise_exception('Unexpected message role.') }}
  216. {%- endif %}
  217. {%- endfor %}
  218. {%- if add_generation_prompt %}
  219. {{- '<|im_start|>assistant\n' }}
  220. {%- if enable_thinking is defined and enable_thinking is false %}
  221. {{- '<think>\n\n</think>\n\n' }}
  222. {%- else %}
  223. {{- '<think>\n' }}
  224. {%- endif %}
  225. {%- endif %}
Advertisement
Add Comment
Please, Sign In to add comment