Guest User

deepseek jinja

a guest
Jul 27th, 2026
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | None | 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. {%- set dsml_token = '|DSML|' -%}
  14. {%- set thinking_start_token = '<think>' -%}
  15. {%- set thinking_end_token = '</think>' -%}
  16. {%- set system_start_token = '<|begin▁sys|>' -%}
  17. {%- set system_end_token = '<|end▁sys|>' -%}
  18. {%- set reasoning_effort_max = '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' -%}
  19. {%- set response_format_template = '## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -%}
  20. {%- set has_tools = false -%}
  21.  
  22. {%- 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' -%}
  23.  
  24. {%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%}
  25.  
  26. {#
  27. Собираем только непрерывный начальный блок system-сообщений.
  28. System после первого несистемного сообщения здесь больше не собирается.
  29. #}
  30. {%- set ns = namespace(
  31. system_prompt='',
  32. initial_system_count=0,
  33. collecting_initial_system=true
  34. ) -%}
  35.  
  36. {%- for message in messages -%}
  37. {%- if ns.collecting_initial_system and message['role'] == 'system' -%}
  38. {%- if ns.initial_system_count > 0 -%}
  39. {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%}
  40. {%- endif -%}
  41.  
  42. {%- set ns.system_prompt =
  43. ns.system_prompt + (message['content'] or '')
  44. -%}
  45.  
  46. {%- set ns.initial_system_count =
  47. ns.initial_system_count + 1
  48. -%}
  49. {%- else -%}
  50. {%- set ns.collecting_initial_system = false -%}
  51. {%- endif -%}
  52. {%- endfor -%}
  53.  
  54. {#
  55. Tool schemas по-прежнему добавляются в начальный системный prompt.
  56. #}
  57. {%- if tools is defined and tools -%}
  58. {%- set has_tools = true -%}
  59. {%- set ts = namespace(schemas='') -%}
  60.  
  61. {%- for tool in tools -%}
  62. {%- if tool['type'] == 'function' -%}
  63. {%- set ts.schemas =
  64. ts.schemas + (tool['function'] | tojson) + '\n'
  65. -%}
  66. {%- endif -%}
  67. {%- endfor -%}
  68.  
  69. {%- if ns.system_prompt -%}
  70. {%- set ns.system_prompt =
  71. ns.system_prompt
  72. + '\n\n'
  73. + tools_header
  74. + ts.schemas
  75. + tools_footer
  76. -%}
  77. {%- else -%}
  78. {%- set ns.system_prompt =
  79. tools_header + ts.schemas + tools_footer
  80. -%}
  81. {%- endif -%}
  82. {%- endif -%}
  83.  
  84. {%- if response_format is defined -%}
  85. {%- if ns.system_prompt -%}
  86. {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%}
  87. {%- endif -%}
  88.  
  89. {%- set ns.system_prompt =
  90. ns.system_prompt
  91. + response_format_template
  92. + (response_format | tojson)
  93. -%}
  94. {%- endif -%}
  95.  
  96. {%- if
  97. messages
  98. and thinking
  99. and reasoning_effort is defined
  100. and reasoning_effort == 'max'
  101. -%}
  102. {%- set ns.system_prompt =
  103. reasoning_effort_max + ns.system_prompt
  104. -%}
  105. {%- endif -%}
  106.  
  107. {{- bos_token -}}
  108.  
  109. {%- if ns.system_prompt -%}
  110. {{- system_start_token -}}
  111. {{- ns.system_prompt -}}
  112. {{- system_end_token -}}
  113. {%- endif -%}
  114.  
  115. {#
  116. Ищем последнее входное сообщение.
  117. Поздние system и developer считаются входными сообщениями,
  118. но не пользовательскими репликами.
  119. #}
  120. {%- set last_input_idx = namespace(value=-1) -%}
  121.  
  122. {%- for message in messages -%}
  123. {%- if
  124. message['role'] == 'user'
  125. or message['role'] == 'developer'
  126. or message['role'] == 'tool'
  127. or (
  128. message['role'] == 'system'
  129. and loop.index0 >= ns.initial_system_count
  130. )
  131. -%}
  132. {%- set last_input_idx.value = loop.index0 -%}
  133. {%- endif -%}
  134. {%- endfor -%}
  135.  
  136. {%- set state = namespace(in_user=false) -%}
  137.  
  138. {%- for message in messages -%}
  139.  
  140. {#
  141. Начальные system уже выведены после BOS.
  142. Все последующие system остаются на исходной позиции.
  143. #}
  144. {%- if message['role'] == 'system' -%}
  145. {%- if loop.index0 >= ns.initial_system_count -%}
  146. {%- set state.in_user = false -%}
  147.  
  148. {{- system_start_token -}}
  149. {{- message['content'] or '' -}}
  150. {{- system_end_token -}}
  151. {%- endif -%}
  152.  
  153. {#
  154. Developer больше не кодируется как User.
  155. #}
  156. {%- elif message['role'] == 'developer' -%}
  157. {%- set state.in_user = false -%}
  158.  
  159. {{- system_start_token -}}
  160. {{- message['content'] or '' -}}
  161. {{- system_end_token -}}
  162.  
  163. {%- elif message['role'] == 'user' -%}
  164. {%- if state.in_user -%}
  165. {{- '\n\n' -}}
  166. {%- else -%}
  167. {{- '<|User|>' -}}
  168. {%- set state.in_user = true -%}
  169. {%- endif -%}
  170.  
  171. {{- message['content'] or '' -}}
  172.  
  173. {%- elif message['role'] == 'tool' -%}
  174. {%- if state.in_user -%}
  175. {{- '\n\n' -}}
  176. {%- else -%}
  177. {{- '<|User|>' -}}
  178. {%- set state.in_user = true -%}
  179. {%- endif -%}
  180.  
  181. {{- '<tool_result>'
  182. + (message['content'] or '')
  183. + '</tool_result>'
  184. -}}
  185.  
  186. {%- elif message['role'] == 'assistant' -%}
  187. {%- set state.in_user = false -%}
  188.  
  189. {{- '<|Assistant|>' -}}
  190.  
  191. {%- set is_after_last_input =
  192. loop.index0 > last_input_idx.value
  193. -%}
  194.  
  195. {%- set keep_reasoning =
  196. thinking and (
  197. has_tools
  198. or (
  199. preserve_thinking is defined
  200. and preserve_thinking
  201. )
  202. or is_after_last_input
  203. )
  204. -%}
  205.  
  206. {%- if keep_reasoning -%}
  207. {{- thinking_start_token -}}
  208.  
  209. {%- if
  210. message['reasoning_content'] is defined
  211. and message['reasoning_content']
  212. -%}
  213. {{- message['reasoning_content'] -}}
  214. {%- endif -%}
  215.  
  216. {{- thinking_end_token -}}
  217. {%- else -%}
  218. {{- thinking_end_token -}}
  219. {%- endif -%}
  220.  
  221. {%- if
  222. message['content'] is defined
  223. and message['content']
  224. -%}
  225. {{- message['content'] -}}
  226. {%- endif -%}
  227.  
  228. {%- if
  229. message['tool_calls'] is defined
  230. and message['tool_calls']
  231. -%}
  232. {{- '\n\n<' + dsml_token + 'tool_calls>\n' -}}
  233.  
  234. {%- for tool in message['tool_calls'] -%}
  235. {%- set func = tool['function'] -%}
  236.  
  237. {{- '<'
  238. + dsml_token
  239. + 'invoke name="'
  240. + func['name']
  241. + '">\n'
  242. -}}
  243.  
  244. {%- set args = func['arguments'] -%}
  245.  
  246. {%- if args is string -%}
  247. {%- set args = args | from_json -%}
  248. {%- endif -%}
  249.  
  250. {%- for key, val in args.items() -%}
  251. {%- if val is string -%}
  252. {{- '<'
  253. + dsml_token
  254. + 'parameter name="'
  255. + key
  256. + '" string="true">'
  257. + val
  258. + '</'
  259. + dsml_token
  260. + 'parameter>\n'
  261. -}}
  262. {%- else -%}
  263. {{- '<'
  264. + dsml_token
  265. + 'parameter name="'
  266. + key
  267. + '" string="false">'
  268. + (val | tojson)
  269. + '</'
  270. + dsml_token
  271. + 'parameter>\n'
  272. -}}
  273. {%- endif -%}
  274. {%- endfor -%}
  275.  
  276. {%- if not args -%}
  277. {{- '\n' -}}
  278. {%- endif -%}
  279.  
  280. {{- '</' + dsml_token + 'invoke>\n' -}}
  281. {%- endfor -%}
  282.  
  283. {{- '</' + dsml_token + 'tool_calls>' -}}
  284. {%- endif -%}
  285.  
  286. {{- '<|end▁of▁sentence|>' -}}
  287. {%- endif -%}
  288. {%- endfor -%}
  289.  
  290. {%- if add_generation_prompt -%}
  291. {{- '<|Assistant|>' -}}
  292.  
  293. {%- if thinking -%}
  294. {{- 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 and established history. If I find any inconsistencies - I need to rewrite the plan. I should iterate the replanning and checking phases until there are no inconsistencies. \nOut-of-character analysis and response planning:\n' -}}
  295. {%- else -%}
  296. {{- thinking_end_token -}}
  297. {%- endif -%}
  298. {%- endif -%}
Advertisement
Add Comment
Please, Sign In to add comment