Guest User

DeepseekV4_3107_ChatTemplate

a guest
Aug 26th, 2026
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.40 KB | Software | 0 0
  1. {%- if not add_generation_prompt is defined -%}
  2. {%- set add_generation_prompt = false -%}
  3. {%- endif -%}
  4.  
  5. {%- if not thinking is defined -%}
  6. {%- if enable_thinking is defined -%}
  7. {%- set thinking = enable_thinking -%}
  8. {%- else -%}
  9. {%- set thinking = false -%}
  10. {%- endif -%}
  11. {%- endif -%}
  12.  
  13. {%- if not drop_thinking is defined -%}
  14. {%- set drop_thinking = true -%}
  15. {%- endif -%}
  16.  
  17. {%- set dsml_token = '|DSML|' -%}
  18. {%- set thinking_start_token = '<think>' -%}
  19. {%- set thinking_end_token = '</think>' -%}
  20. {%- set system_start_token = '<|begin▁sys|>' -%}
  21. {%- set system_end_token = '<|end▁sys|>' -%}
  22. {%- set reasoning_effort_low = 'Reasoning Effort: Minimal and efficient.\nThink only as much as necessary to understand the request, avoid obvious mistakes, and produce a direct coherent answer. Do not explore remote alternatives, repeat checks, or expand the analysis unless the task genuinely requires it.\n\n' -%}
  23. {%- set reasoning_effort_medium = 'Reasoning Effort: Careful and deliberate.\nAnalyze the request before answering, identify the important constraints, check continuity, assumptions, and likely failure points, and construct a coherent response plan. Be thorough where it materially improves the answer, but avoid unnecessary exhaustive branching, repeated verification, or repetition.\n\n' -%}
  24. {%- set reasoning_effort_high = 'Reasoning Effort: Absolute maximum with no shortcuts permitted.\nYou MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\nExplicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\n\n' -%}
  25. {%- set reasoning_effort_max = 'Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\nYou MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\nDo not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n' -%}
  26. {%- set response_format_template = '## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -%}
  27. {%- set has_tools = false -%}
  28.  
  29. {%- set tools_header = '## Tools\n\nYou have access to a set of tools to help answer the user\'s question. You can invoke tools by writing a "<' + dsml_token + 'tool_calls>" block like the following:\n\n<' + dsml_token + 'tool_calls>\n<' + dsml_token + 'invoke name="$TOOL_NAME">\n<' + dsml_token + 'parameter name="$PARAMETER_NAME" string="true|false">$PARAMETER_VALUE</' + dsml_token + 'parameter>\n...\n</' + dsml_token + 'invoke>\n<' + dsml_token + 'invoke name="$TOOL_NAME2">\n...\n</' + dsml_token + 'invoke>\n</' + dsml_token + 'tool_calls>\n\nString parameters should be specified as is and set `string="true"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string="false"`.\n\nIf thinking_mode is enabled (triggered by ' + thinking_start_token + '), you MUST output your complete reasoning inside ' + thinking_start_token + '...' + thinking_end_token + ' BEFORE any tool calls or final response.\n\nOtherwise, output directly after ' + thinking_end_token + ' with tool calls or final response.\n\n### Available Tool Schemas\n\n' -%}
  30.  
  31. {%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%}
  32.  
  33. {#
  34. Собираем только непрерывный начальный блок system-сообщений.
  35. System после первого несистемного сообщения здесь больше не собирается.
  36. #}
  37. {%- set ns = namespace(
  38. system_prompt='',
  39. initial_system_count=0,
  40. collecting_initial_system=true,
  41. has_tool_calls=false
  42. ) -%}
  43.  
  44. {%- for message in messages -%}
  45. {%- if ns.collecting_initial_system and message['role'] == 'system' -%}
  46. {%- if ns.initial_system_count > 0 -%}
  47. {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%}
  48. {%- endif -%}
  49.  
  50. {%- set ns.system_prompt =
  51. ns.system_prompt + (message['content'] or '')
  52. -%}
  53.  
  54. {%- set ns.initial_system_count =
  55. ns.initial_system_count + 1
  56. -%}
  57. {%- else -%}
  58. {%- set ns.collecting_initial_system = false -%}
  59. {%- endif -%}
  60. {%- endfor -%}
  61.  
  62. {#
  63. Tool schemas по-прежнему добавляются в начальный системный prompt.
  64. #}
  65. {%- if tools is defined and tools -%}
  66. {%- set has_tools = true -%}
  67. {%- set ts = namespace(schemas='') -%}
  68.  
  69. {%- for tool in tools -%}
  70. {%- if tool['type'] == 'function' -%}
  71. {%- set ts.schemas =
  72. ts.schemas + (tool['function'] | tojson) + '\n'
  73. -%}
  74. {%- endif -%}
  75. {%- endfor -%}
  76.  
  77. {%- if ns.system_prompt -%}
  78. {%- set ns.system_prompt =
  79. ns.system_prompt
  80. + '\n\n'
  81. + tools_header
  82. + ts.schemas
  83. + tools_footer
  84. -%}
  85. {%- else -%}
  86. {%- set ns.system_prompt =
  87. tools_header + ts.schemas + tools_footer
  88. -%}
  89. {%- endif -%}
  90. {%- endif -%}
  91.  
  92. {%- if response_format is defined -%}
  93. {%- if ns.system_prompt -%}
  94. {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%}
  95. {%- endif -%}
  96.  
  97. {%- set ns.system_prompt =
  98. ns.system_prompt
  99. + response_format_template
  100. + (response_format | tojson)
  101. -%}
  102. {%- endif -%}
  103.  
  104. {#
  105. reasoning_effort является текстовым переключателем уровня анализа.
  106. Неизвестное или отсутствующее значение не добавляет отдельную инструкцию.
  107. #}
  108. {%- if
  109. messages
  110. and thinking
  111. and reasoning_effort is defined
  112. -%}
  113. {%- if reasoning_effort == 'low' -%}
  114. {%- set ns.system_prompt =
  115. reasoning_effort_low + ns.system_prompt
  116. -%}
  117. {%- elif reasoning_effort == 'medium' -%}
  118. {%- set ns.system_prompt =
  119. reasoning_effort_medium + ns.system_prompt
  120. -%}
  121. {%- elif reasoning_effort == 'high' -%}
  122. {%- set ns.system_prompt =
  123. reasoning_effort_high + ns.system_prompt
  124. -%}
  125. {%- elif reasoning_effort == 'max' -%}
  126. {%- set ns.system_prompt =
  127. reasoning_effort_max + ns.system_prompt
  128. -%}
  129. {%- endif -%}
  130. {%- endif -%}
  131.  
  132. {{- bos_token -}}
  133.  
  134. {%- if ns.system_prompt -%}
  135. {{- system_start_token -}}
  136. {{- ns.system_prompt -}}
  137. {{- system_end_token -}}
  138. {%- endif -%}
  139.  
  140. {#
  141. Ищем последнее входное сообщение.
  142. Поздние system и developer считаются входными сообщениями,
  143. но не пользовательскими репликами.
  144. #}
  145. {%- set last_input_idx = namespace(value=-1) -%}
  146.  
  147. {%- for message in messages -%}
  148. {%- if
  149. message['role'] == 'user'
  150. or message['role'] == 'developer'
  151. or message['role'] == 'tool'
  152. or (
  153. message['role'] == 'system'
  154. and loop.index0 >= ns.initial_system_count
  155. )
  156. -%}
  157. {%- set last_input_idx.value = loop.index0 -%}
  158. {%- endif -%}
  159. {%- endfor -%}
  160.  
  161. {%- set state = namespace(in_user=false) -%}
  162.  
  163. {#
  164. Если в истории уже были tool-result сообщения, сохраняем reasoning
  165. вокруг соответствующей цепочки даже при отсутствии текущего tools массива.
  166. #}
  167. {%- for message in messages -%}
  168. {%- if message['role'] == 'tool' -%}
  169. {%- set ns.has_tool_calls = true -%}
  170. {%- endif -%}
  171. {%- endfor -%}
  172.  
  173. {%- for message in messages -%}
  174.  
  175. {#
  176. Начальные system уже выведены после BOS.
  177. Все последующие system остаются на исходной позиции.
  178. #}
  179. {%- if message['role'] == 'system' -%}
  180. {%- if loop.index0 >= ns.initial_system_count -%}
  181. {%- set state.in_user = false -%}
  182.  
  183. {{- system_start_token -}}
  184. {{- message['content'] or '' -}}
  185. {{- system_end_token -}}
  186. {%- endif -%}
  187.  
  188. {#
  189. Developer больше не кодируется как User.
  190. #}
  191. {%- elif message['role'] == 'developer' -%}
  192. {%- set state.in_user = false -%}
  193.  
  194. {{- system_start_token -}}
  195. {{- message['content'] or '' -}}
  196. {{- system_end_token -}}
  197.  
  198. {%- elif message['role'] == 'user' -%}
  199. {%- if state.in_user -%}
  200. {{- '\n\n' -}}
  201. {%- else -%}
  202. {{- '<|User|>' -}}
  203. {%- set state.in_user = true -%}
  204. {%- endif -%}
  205.  
  206. {{- message['content'] or '' -}}
  207.  
  208. {#
  209. Последняя user-реплика перед post-history system/developer
  210. должна быть явно завершена EOS.
  211. #}
  212. {%- if
  213. not loop.last
  214. and (
  215. messages[loop.index0 + 1]['role'] == 'system'
  216. or messages[loop.index0 + 1]['role'] == 'developer'
  217. )
  218. -%}
  219. {{- '<|end▁of▁sentence|>' -}}
  220. {%- endif -%}
  221.  
  222. {%- elif message['role'] == 'tool' -%}
  223. {%- if state.in_user -%}
  224. {{- '\n\n' -}}
  225. {%- else -%}
  226. {{- '<|User|>' -}}
  227. {%- set state.in_user = true -%}
  228. {%- endif -%}
  229.  
  230. {{- '<tool_result>'
  231. + (message['content'] or '')
  232. + '</tool_result>'
  233. -}}
  234.  
  235. {%- elif message['role'] == 'assistant' -%}
  236. {%- set state.in_user = false -%}
  237.  
  238. {{- '<|Assistant|>' -}}
  239.  
  240. {%- set is_after_last_input =
  241. loop.index0 > last_input_idx.value
  242. -%}
  243.  
  244. {%- set keep_reasoning =
  245. thinking and (
  246. (not drop_thinking)
  247. or has_tools
  248. or ns.has_tool_calls
  249. or (
  250. preserve_thinking is defined
  251. and preserve_thinking
  252. )
  253. or is_after_last_input
  254. )
  255. -%}
  256.  
  257. {%- if keep_reasoning -%}
  258. {{- thinking_start_token -}}
  259.  
  260. {%- if
  261. message['reasoning_content'] is defined
  262. and message['reasoning_content']
  263. -%}
  264. {{- message['reasoning_content'] -}}
  265. {%- endif -%}
  266.  
  267. {{- thinking_end_token -}}
  268. {%- else -%}
  269. {{- thinking_end_token -}}
  270. {%- endif -%}
  271.  
  272. {%- if
  273. message['content'] is defined
  274. and message['content']
  275. -%}
  276. {{- message['content'] -}}
  277. {%- endif -%}
  278.  
  279. {%- if
  280. message['tool_calls'] is defined
  281. and message['tool_calls']
  282. -%}
  283. {{- '\n\n<' + dsml_token + 'tool_calls>\n' -}}
  284.  
  285. {%- for tool in message['tool_calls'] -%}
  286. {%- set func = tool['function'] -%}
  287.  
  288. {{- '<'
  289. + dsml_token
  290. + 'invoke name="'
  291. + func['name']
  292. + '">\n'
  293. -}}
  294.  
  295. {%- set args = func['arguments'] -%}
  296.  
  297. {%- if args is string -%}
  298. {%- set args = args | from_json -%}
  299. {%- endif -%}
  300.  
  301. {%- for key, val in args.items() -%}
  302. {%- if val is string -%}
  303. {{- '<'
  304. + dsml_token
  305. + 'parameter name="'
  306. + key
  307. + '" string="true">'
  308. + val
  309. + '</'
  310. + dsml_token
  311. + 'parameter>\n'
  312. -}}
  313. {%- else -%}
  314. {{- '<'
  315. + dsml_token
  316. + 'parameter name="'
  317. + key
  318. + '" string="false">'
  319. + (val | tojson)
  320. + '</'
  321. + dsml_token
  322. + 'parameter>\n'
  323. -}}
  324. {%- endif -%}
  325. {%- endfor -%}
  326.  
  327. {%- if not args -%}
  328. {{- '\n' -}}
  329. {%- endif -%}
  330.  
  331. {{- '</' + dsml_token + 'invoke>\n' -}}
  332. {%- endfor -%}
  333.  
  334. {{- '</' + dsml_token + 'tool_calls>' -}}
  335. {%- endif -%}
  336.  
  337. {{- '<|end▁of▁sentence|>' -}}
  338. {%- endif -%}
  339. {%- endfor -%}
  340.  
  341. {%- if add_generation_prompt -%}
  342. {{- '<|Assistant|>' -}}
  343.  
  344. {%- if thinking -%}
  345. {{- thinking_start_token + '\nI need to analyze the user request and the current scene objectively, check continuity and characters knowledge, then construct a concrete ordered response plan. After the plan is finished I need to check it against the Guidelines, established history and directives.\nOut-of-character analysis and response planning:\n' -}}
  346. {%- else -%}
  347. {{- thinking_end_token -}}
  348. {%- endif -%}
  349. {%- endif -%}
Advertisement
Add Comment
Please, Sign In to add comment