Guest User

QWEN3.5.MOD

a guest
Feb 25th, 2026
2,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | Source Code | 0 0
  1. {%- set image_count = namespace(value=0) %}
  2. {%- set video_count = namespace(value=0) %}
  3.  
  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. {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
  17. {%- elif 'video' in item or item.type == 'video' %}
  18. {%- if is_system_content %}
  19. {{- raise_exception('System message cannot contain videos.') }}
  20. {%- endif %}
  21. {%- if do_vision_count %}
  22. {%- set video_count.value = video_count.value + 1 %}
  23. {%- endif %}
  24. {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
  25. {%- elif 'text' in item %}
  26. {{- item.text }}
  27. {%- else %}
  28. {{- raise_exception('Unexpected item type in content.') }}
  29. {%- endif %}
  30. {%- endfor %}
  31. {%- elif content is none or content is undefined %}
  32. {{- '' }}
  33. {%- else %}
  34. {{- raise_exception('Unexpected content type.') }}
  35. {%- endif %}
  36. {%- endmacro %}
  37.  
  38. {%- if not messages %}
  39. {{- raise_exception('No messages provided.') }}
  40. {%- endif %}
  41.  
  42. {# ========================================= #}
  43. {# Detect /think ONLY in FIRST system msg #}
  44. {# ========================================= #}
  45. {%- set ns_enable = namespace(enable_thinking=false) %}
  46. {%- if messages[0].role == 'system' %}
  47. {%- set sys_raw = render_content(messages[0].content, false, true)|trim %}
  48. {%- if '/think' in sys_raw %}
  49. {%- set ns_enable.enable_thinking = true %}
  50. {%- endif %}
  51. {%- endif %}
  52.  
  53. {%- if tools and tools is iterable and tools is not mapping %}
  54. {{- '<|im_start|>system\n' }}
  55. {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
  56. {%- for tool in tools %}
  57. {{- "\n" }}
  58. {{- tool | tojson }}
  59. {%- endfor %}
  60. {{- "\n</tools>" }}
  61.  
  62. {%- if messages[0].role == 'system' %}
  63. {%- set content = render_content(messages[0].content, false, true)|trim %}
  64. {%- set content = content.replace('/think','')|trim %}
  65. {%- if content %}
  66. {{- '\n\n' + content }}
  67. {%- endif %}
  68. {%- endif %}
  69.  
  70. {{- '<|im_end|>\n' }}
  71.  
  72. {%- else %}
  73.  
  74. {%- if messages[0].role == 'system' %}
  75. {%- set content = render_content(messages[0].content, false, true)|trim %}
  76. {%- set content = content.replace('/think','')|trim %}
  77. {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
  78. {%- endif %}
  79.  
  80. {%- endif %}
  81.  
  82. {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
  83. {%- for message in messages[::-1] %}
  84. {%- set index = (messages|length - 1) - loop.index0 %}
  85. {%- if ns.multi_step_tool and message.role == "user" %}
  86. {%- set content = render_content(message.content, false)|trim %}
  87. {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
  88. {%- set ns.multi_step_tool = false %}
  89. {%- set ns.last_query_index = index %}
  90. {%- endif %}
  91. {%- endif %}
  92. {%- endfor %}
  93.  
  94. {%- if ns.multi_step_tool %}
  95. {{- raise_exception('No user query found in messages.') }}
  96. {%- endif %}
  97.  
  98. {%- for message in messages %}
  99. {%- set content = render_content(message.content, true)|trim %}
  100.  
  101. {%- if message.role == "system" %}
  102. {%- if not loop.first %}
  103. {{- raise_exception('System message must be at the beginning.') }}
  104. {%- endif %}
  105.  
  106. {%- elif message.role == "user" %}
  107. {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
  108.  
  109. {%- elif message.role == "assistant" %}
  110.  
  111. {{- '<|im_start|>assistant\n' }}
  112.  
  113. {%- if ns_enable.enable_thinking and loop.index0 > ns.last_query_index %}
  114. {%- if message.reasoning_content %}
  115. {{- '<think>\n' + message.reasoning_content|trim + '\n</think>\n\n' }}
  116. {%- endif %}
  117. {%- endif %}
  118.  
  119. {{- content }}
  120.  
  121. {{- '<|im_end|>\n' }}
  122.  
  123. {%- elif message.role == "tool" %}
  124.  
  125. {{- '<|im_start|>user\n<tool_response>\n' }}
  126. {{- content }}
  127. {{- '\n</tool_response><|im_end|>\n' }}
  128.  
  129. {%- else %}
  130. {{- raise_exception('Unexpected message role.') }}
  131. {%- endif %}
  132. {%- endfor %}
  133.  
  134. {# ========================================= #}
  135. {# Final generation prompt control #}
  136. {# ========================================= #}
  137.  
  138. {%- if add_generation_prompt %}
  139. {{- '<|im_start|>assistant\n' }}
  140. {%- if ns_enable.enable_thinking %}
  141. {{- '<think>\n' }}
  142. {%- else %}
  143. {{- '<think>\n</think>\n\n' }}
  144. {%- endif %}
  145. {%- endif %}
Advertisement
Add Comment
Please, Sign In to add comment