File size: 3,630 Bytes
d4571da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{%- 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 -%}