{%- set roles = {"assistant": "assistant:", "user": "user:", "system": "system:", "meta": "meta:"} -%} {%- set TOOL_CALL_START = "[TOOL_CALL_START]" -%} {%- set COT_START = "[COT_START]" -%} {%- set COT_END = "[COT_END]" -%} {%- set tools_prefix = "Тебе доступны следующие функции:" -%} {%- set json_seps = (",", ":") -%} {%- macro js(x) -%} {{- x | tojson(separators=json_seps) | replace("&", "\\u0026") | replace("<", "\\u003c") | replace(">", "\\u003e") | replace("'", "\\u0027") | replace("/", "\\/") -}} {%- endmacro -%} {%- macro tool_definition(tool) -%} {%- if tool.function is defined -%} {%- set tool = tool.function -%} {%- endif -%} {{- 'function {"name":"' ~ tool.name ~ '",' -}} {%- if tool.description is defined and tool.description -%} {{- '"description":"' ~ tool.description ~ '",' -}} {%- endif -%} {{- '"parameters":' ~ js(tool.parameters) -}} {%- if tool.return_parameters is defined and tool.return_parameters -%} {{- ',"return_parameters":' ~ js(tool.return_parameters) -}} {%- endif -%} {%- if tool.few_shot_examples is defined and tool.few_shot_examples -%} {{- ',"few_shot_examples":' ~ js(tool.few_shot_examples) -}} {%- endif -%} {{- "}" -}} {%- endmacro -%} {%- macro render_tools(tools) -%} {{- tools_prefix -}} {%- for tool in tools -%} {{- "\n" ~ tool_definition(tool) -}} {%- endfor -%} {%- endmacro -%} {%- macro render_tool_calls(calls) -%} {%- for call in calls -%} {%- set fn = call.function if call.function is defined else call -%} {{- "\n" ~ TOOL_CALL_START ~ fn.name ~ "\n" -}} {%- if fn.arguments is mapping -%} {{- js(fn.arguments) -}} {%- else -%} {{- fn.arguments -}} {%- endif -%} {%- endfor -%} {%- endmacro -%} {%- macro render_assistant(message) -%} {%- if message.tool_calls is defined and message.tool_calls -%} {%- set content = render_tool_calls(message.tool_calls) -%} {%- elif message.content is defined and message.content is not none -%} {%- set content = message.content -%} {%- else -%} {%- set content = "" -%} {%- endif -%} {{- roles.assistant -}} {%- if message.reasoning_content is defined and message.reasoning_content -%} {{- COT_START ~ message.reasoning_content ~ COT_END -}} {%- endif -%} {{- " " ~ content -}} {%- endmacro -%} {%- macro render_message(message) -%} {%- if message.role == "system" -%} {{- roles.system ~ " " ~ message.content -}} {%- elif message.role == "user" -%} {{- roles.user ~ " " ~ message.content -}} {%- elif message.role == "assistant" -%} {{- render_assistant(message) -}} {%- elif message.role == "tool" -%} {{- "Tool " ~ message.name ~ ": " ~ (message.content | default("", true)) -}} {%- elif message.role == "meta" -%} {{- roles.meta ~ message.content -}} {%- endif -%} {%- endmacro -%} {{- bos_token | default("", true) -}} {%- if tools is defined and tools -%} {{- render_tools(tools) -}} {%- endif -%} {%- for message in messages -%} {%- set sep = "\n\n " if (tools is defined and tools) or not loop.first else "" -%} {%- set block = render_message(message) -%} {%- if loop.last and not (add_generation_prompt | default(false)) -%} {%- set block = block | trim -%} {%- endif -%} {%- if block -%} {{- sep ~ block -}} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt | default(false) -%} {{- "\n\n " ~ roles.assistant -}} {%- endif -%}