File size: 4,866 Bytes
52408d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6135326
 
 
 
 
52408d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6135326
52408d8
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
96
97
98
99
100
{% set reasoning_effort = reasoning_effort | default('high', true) %}
{%- if messages[0].role == 'system' and messages[0].content != '' or tools or (reasoning_effort is defined and reasoning_effort != '') %}
  {{- '<role>SYSTEM</role>\n' }}
{%- endif %}
{%- if reasoning_effort is defined and reasoning_effort != '' %}
    {{- 'Reasoning: ' + reasoning_effort + '\n\n'}}
{%- endif %}
{%- if messages[0].role == 'system' %}
    {%- if messages[0].content != '' %}
        {{- messages[0].content }}
    {{- '\n\n' if tools else '<|role_end|>\n\n' }}
    {%- endif %}
{%- endif %}
{%- if tools %}
    {{- "# Tools\nYou may call one or more functions to assist with the user query.\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
    {%- for tool in tools %}
        {{- "\n" }}
        {{- tool | tojson }}
    {%- endfor %}
    {{- "\n</tools>\nIf none of the functions can be used, point it out. If the given question lacks the parameters required by the function, also point it out.\nIf you need to use a function, for each function call, output the function name and arguments within the following XML format:\n<tool_call>{function-name}\n<arg_key>{arg-key-1}</arg_key>\n<arg_value>{arg-value-1}</arg_value>\n<arg_key>{arg-key-2}</arg_key>\n<arg_value>{arg-value-2}</arg_value>\n...\n</tool_call><|role_end|>\n\n" }}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
    {%- set index = (messages|length - 1) - loop.index0 %}
    {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
        {%- set ns.multi_step_tool = false %}
        {%- set ns.last_query_index = index %}
    {%- endif %}
{%- endfor %}
{%- for message in messages %}
    {%- if message.content is string %}
        {%- set content = message.content %}
    {%- else %}
        {%- set content = '' %}
    {%- endif %}
    {%- if message.role == "user" %}
        {{- '<role>HUMAN</role>\n' + message.content + '<|role_end|>\n\n' }}
    {%- elif message.role == "system" and not loop.first %}
        {{- '<role>SYSTEM</role>\n' + message.content + '<|role_end|>\n\n' }}
    {%- elif message.role == "assistant" %}
        {%- set reasoning_content = '' %}
        {%- if message.reasoning_content is string %}
            {%- if message.reasoning_content !='' %}
                {%- set reasoning_content = message.reasoning_content %}
            {%- endif %}
        {%- else %}
            {%- if '</think>' in content %}
                {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
                {%- set content = content.split('</think>')[-1].lstrip('\n') %}
            {%- endif %}
        {%- endif %}
        {%- if loop.index0 > ns.last_query_index %}
            {%- if reasoning_content != '' %}
                {{- '<role>ASSISTANT</role>\n' + '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
            {%- else %}
                {{- '<role>ASSISTANT</role>\n' + content }}
            {%- endif %}
        {%- else %}
            {{- '<role>ASSISTANT</role>\n' + content }}
        {%- endif %}
        {%- if message.tool_calls %}
            {%- for tool_call in message.tool_calls %}
                {%- if (loop.first and content) or (not loop.first) %}
                    {{- '\n' }}
                {%- endif %}
                {%- if tool_call.function %}
                    {%- set tc = tool_call.function %}
                {%- endif %}
                {{- '<tool_call>' + tc.name }}
                {% set _args = tc.arguments %}
                {%- for k, v in _args.items() %}
                    {{- '<arg_key>' + k + '</arg_key>' }}
                    {{- '\n<arg_value>' }}
                    {%- if v is string %}
                        {{- v }}
                    {%- else %}
                        {{- v | tojson(ensure_ascii=False) }}
                    {%- endif %}
                    {{- '</arg_value>' }}
                {%- endfor %}
                {{- '\n</tool_call>' }}
            {%- endfor %}
        {%- endif %}
        {{- '<|role_end|>\n\n' }}
    {%- elif message.role == "tool" %}
        {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
            {{- '<role>OBSERVATION</role>' }}
        {%- endif %}
        {{- '\n<tool_response>\n' }}
        {{- content }}
        {{- '\n</tool_response>' }}
        {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
            {{- '<|role_end|>\n\n' }}
        {%- endif %}
    {%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<role>ASSISTANT</role>' }}
{%- endif %}