Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {%- if not add_generation_prompt is defined -%}
- {%- set add_generation_prompt = false -%}
- {%- endif -%}
- {%- if not thinking is defined -%}
- {%- if enable_thinking is defined -%}
- {%- set thinking = enable_thinking -%}
- {%- else -%}
- {%- set thinking = false -%}
- {%- endif -%}
- {%- endif -%}
- {%- if not drop_thinking is defined -%}
- {%- set drop_thinking = true -%}
- {%- endif -%}
- {%- set dsml_token = '|DSML|' -%}
- {%- set thinking_start_token = '<think>' -%}
- {%- set thinking_end_token = '</think>' -%}
- {%- set system_start_token = '<|begin▁sys|>' -%}
- {%- set system_end_token = '<|end▁sys|>' -%}
- {%- 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' -%}
- {%- 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' -%}
- {%- 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' -%}
- {%- 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' -%}
- {%- set response_format_template = '## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -%}
- {%- set has_tools = false -%}
- {%- 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' -%}
- {%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%}
- {#
- Собираем только непрерывный начальный блок system-сообщений.
- System после первого несистемного сообщения здесь больше не собирается.
- #}
- {%- set ns = namespace(
- system_prompt='',
- initial_system_count=0,
- collecting_initial_system=true,
- has_tool_calls=false
- ) -%}
- {%- for message in messages -%}
- {%- if ns.collecting_initial_system and message['role'] == 'system' -%}
- {%- if ns.initial_system_count > 0 -%}
- {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%}
- {%- endif -%}
- {%- set ns.system_prompt =
- ns.system_prompt + (message['content'] or '')
- -%}
- {%- set ns.initial_system_count =
- ns.initial_system_count + 1
- -%}
- {%- else -%}
- {%- set ns.collecting_initial_system = false -%}
- {%- endif -%}
- {%- endfor -%}
- {#
- Tool schemas по-прежнему добавляются в начальный системный prompt.
- #}
- {%- if tools is defined and tools -%}
- {%- set has_tools = true -%}
- {%- set ts = namespace(schemas='') -%}
- {%- for tool in tools -%}
- {%- if tool['type'] == 'function' -%}
- {%- set ts.schemas =
- ts.schemas + (tool['function'] | tojson) + '\n'
- -%}
- {%- endif -%}
- {%- endfor -%}
- {%- if ns.system_prompt -%}
- {%- set ns.system_prompt =
- ns.system_prompt
- + '\n\n'
- + tools_header
- + ts.schemas
- + tools_footer
- -%}
- {%- else -%}
- {%- set ns.system_prompt =
- tools_header + ts.schemas + tools_footer
- -%}
- {%- endif -%}
- {%- endif -%}
- {%- if response_format is defined -%}
- {%- if ns.system_prompt -%}
- {%- set ns.system_prompt = ns.system_prompt + '\n\n' -%}
- {%- endif -%}
- {%- set ns.system_prompt =
- ns.system_prompt
- + response_format_template
- + (response_format | tojson)
- -%}
- {%- endif -%}
- {#
- reasoning_effort является текстовым переключателем уровня анализа.
- Неизвестное или отсутствующее значение не добавляет отдельную инструкцию.
- #}
- {%- if
- messages
- and thinking
- and reasoning_effort is defined
- -%}
- {%- if reasoning_effort == 'low' -%}
- {%- set ns.system_prompt =
- reasoning_effort_low + ns.system_prompt
- -%}
- {%- elif reasoning_effort == 'medium' -%}
- {%- set ns.system_prompt =
- reasoning_effort_medium + ns.system_prompt
- -%}
- {%- elif reasoning_effort == 'high' -%}
- {%- set ns.system_prompt =
- reasoning_effort_high + ns.system_prompt
- -%}
- {%- elif reasoning_effort == 'max' -%}
- {%- set ns.system_prompt =
- reasoning_effort_max + ns.system_prompt
- -%}
- {%- endif -%}
- {%- endif -%}
- {{- bos_token -}}
- {%- if ns.system_prompt -%}
- {{- system_start_token -}}
- {{- ns.system_prompt -}}
- {{- system_end_token -}}
- {%- endif -%}
- {#
- Ищем последнее входное сообщение.
- Поздние system и developer считаются входными сообщениями,
- но не пользовательскими репликами.
- #}
- {%- set last_input_idx = namespace(value=-1) -%}
- {%- for message in messages -%}
- {%- if
- message['role'] == 'user'
- or message['role'] == 'developer'
- or message['role'] == 'tool'
- or (
- message['role'] == 'system'
- and loop.index0 >= ns.initial_system_count
- )
- -%}
- {%- set last_input_idx.value = loop.index0 -%}
- {%- endif -%}
- {%- endfor -%}
- {%- set state = namespace(in_user=false) -%}
- {#
- Если в истории уже были tool-result сообщения, сохраняем reasoning
- вокруг соответствующей цепочки даже при отсутствии текущего tools массива.
- #}
- {%- for message in messages -%}
- {%- if message['role'] == 'tool' -%}
- {%- set ns.has_tool_calls = true -%}
- {%- endif -%}
- {%- endfor -%}
- {%- for message in messages -%}
- {#
- Начальные system уже выведены после BOS.
- Все последующие system остаются на исходной позиции.
- #}
- {%- if message['role'] == 'system' -%}
- {%- if loop.index0 >= ns.initial_system_count -%}
- {%- set state.in_user = false -%}
- {{- system_start_token -}}
- {{- message['content'] or '' -}}
- {{- system_end_token -}}
- {%- endif -%}
- {#
- Developer больше не кодируется как User.
- #}
- {%- elif message['role'] == 'developer' -%}
- {%- set state.in_user = false -%}
- {{- system_start_token -}}
- {{- message['content'] or '' -}}
- {{- system_end_token -}}
- {%- elif message['role'] == 'user' -%}
- {%- if state.in_user -%}
- {{- '\n\n' -}}
- {%- else -%}
- {{- '<|User|>' -}}
- {%- set state.in_user = true -%}
- {%- endif -%}
- {{- message['content'] or '' -}}
- {#
- Последняя user-реплика перед post-history system/developer
- должна быть явно завершена EOS.
- #}
- {%- if
- not loop.last
- and (
- messages[loop.index0 + 1]['role'] == 'system'
- or messages[loop.index0 + 1]['role'] == 'developer'
- )
- -%}
- {{- '<|end▁of▁sentence|>' -}}
- {%- endif -%}
- {%- elif message['role'] == 'tool' -%}
- {%- if state.in_user -%}
- {{- '\n\n' -}}
- {%- else -%}
- {{- '<|User|>' -}}
- {%- set state.in_user = true -%}
- {%- endif -%}
- {{- '<tool_result>'
- + (message['content'] or '')
- + '</tool_result>'
- -}}
- {%- elif message['role'] == 'assistant' -%}
- {%- set state.in_user = false -%}
- {{- '<|Assistant|>' -}}
- {%- set is_after_last_input =
- loop.index0 > last_input_idx.value
- -%}
- {%- set keep_reasoning =
- thinking and (
- (not drop_thinking)
- or has_tools
- or ns.has_tool_calls
- or (
- preserve_thinking is defined
- and preserve_thinking
- )
- or is_after_last_input
- )
- -%}
- {%- if keep_reasoning -%}
- {{- thinking_start_token -}}
- {%- if
- message['reasoning_content'] is defined
- and message['reasoning_content']
- -%}
- {{- message['reasoning_content'] -}}
- {%- endif -%}
- {{- thinking_end_token -}}
- {%- else -%}
- {{- thinking_end_token -}}
- {%- endif -%}
- {%- if
- message['content'] is defined
- and message['content']
- -%}
- {{- message['content'] -}}
- {%- endif -%}
- {%- if
- message['tool_calls'] is defined
- and message['tool_calls']
- -%}
- {{- '\n\n<' + dsml_token + 'tool_calls>\n' -}}
- {%- for tool in message['tool_calls'] -%}
- {%- set func = tool['function'] -%}
- {{- '<'
- + dsml_token
- + 'invoke name="'
- + func['name']
- + '">\n'
- -}}
- {%- set args = func['arguments'] -%}
- {%- if args is string -%}
- {%- set args = args | from_json -%}
- {%- endif -%}
- {%- for key, val in args.items() -%}
- {%- if val is string -%}
- {{- '<'
- + dsml_token
- + 'parameter name="'
- + key
- + '" string="true">'
- + val
- + '</'
- + dsml_token
- + 'parameter>\n'
- -}}
- {%- else -%}
- {{- '<'
- + dsml_token
- + 'parameter name="'
- + key
- + '" string="false">'
- + (val | tojson)
- + '</'
- + dsml_token
- + 'parameter>\n'
- -}}
- {%- endif -%}
- {%- endfor -%}
- {%- if not args -%}
- {{- '\n' -}}
- {%- endif -%}
- {{- '</' + dsml_token + 'invoke>\n' -}}
- {%- endfor -%}
- {{- '</' + dsml_token + 'tool_calls>' -}}
- {%- endif -%}
- {{- '<|end▁of▁sentence|>' -}}
- {%- endif -%}
- {%- endfor -%}
- {%- if add_generation_prompt -%}
- {{- '<|Assistant|>' -}}
- {%- if thinking -%}
- {{- 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' -}}
- {%- else -%}
- {{- thinking_end_token -}}
- {%- endif -%}
- {%- endif -%}
Advertisement
Add Comment
Please, Sign In to add comment