Guest User

Untitled

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