File size: 6,647 Bytes
5e72b87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{% set image_count = namespace(value=0) %}
{% set video_count = namespace(value=0) %}

{%- set role_indicators = {
    'user': '<|user|>\n',
    'assistant': '<|assistant|>\n',
    'system': '<|system|>\n',
    'tool': '<|tool|>\n',
    'tool_declare': '<|tool_declare|>\n'
} %}
{%- set end_of_turn = '<|endofturn|>\n' %}


{%- macro declare_available_tools(tools) %}
    {{- "# Tools\n" }}
    {{- "The available tools are defined below in JSON format.\n" }}
    {{- "When calling a tool, use XML with <function=...> and one <parameter=...> block per argument.\n" }}
    {{- "\n" }}
    {%- for tool in tools %}
        {{- "<tool>" }}
        {{- tool | tojson(ensure_ascii=False) | safe }}
        {{- "</tool>\n" }}
    {%- endfor %}
    {{- "\n# Tool Call Format\n" }}
    {{- "<tool_call>\n" }}
    {{- "<function=example_tool_name>\n" }}
    {{- "<parameter=arg1>\n" }}
    {{- "value1\n" }}
    {{- "</parameter>\n" }}
    {{- "<parameter=arg2>\n" }}
    {{- "2\n" }}
    {{- "</parameter>\n" }}
    {{- "</function>\n" }}
    {{- "</tool_call>" }}
{%- endmacro %}


{%- macro render_tool_call(tool_call, arguments) %}
    {{- "<tool_call>\n" }}
    {{- "<function=" }}{{- tool_call.name }}{{- ">\n" }}
    {%- for args_name, args_value in arguments.items() %}
        {{- "<parameter=" }}{{- args_name }}{{- ">\n" }}
        {%- if args_value is string %}
            {{- args_value }}
        {%- else %}
            {{- args_value | tojson(ensure_ascii=False) | safe }}
        {%- endif %}
        {{- "\n</parameter>\n" }}
    {%- endfor %}
    {{- "</function>\n" }}
    {{- "</tool_call>" }}
{%- endmacro %}


{%- macro render_tool_response(msg) %}
    {{- "<tool_result>" }}
    {%- if msg.content is defined %}
        {%- if msg.content is string %}
            {{- msg.content }}
        {%- else %}
            {{- msg.content | tojson(ensure_ascii=False) | safe }}
        {%- endif %}
    {%- endif %}
    {{- "</tool_result>" }}
{%- endmacro %}


{%- set ns = namespace(last_query_index = messages|length - 1, last_query_index_not_yet_determined = true) %}
{%- for message in messages[::-1] %}
    {%- set index = (messages|length - 1) - loop.index0 %}
    {%- if ns.last_query_index_not_yet_determined and message.role == "user" and message.content is string %}
        {%- set ns.last_query_index = index -%}
        {%- set ns.last_query_index_not_yet_determined = false -%}
    {%- endif %}
{%- endfor %}

{%- if tools is defined and tools %}
    {{- role_indicators['tool_declare'] }}
    {{- declare_available_tools(tools) }}
    {{- end_of_turn -}}
{%- endif %}

{%- for i in range(messages | length) %}
    {%- set msg = messages[i] %}
    {%- set role = msg.role %}
    {%- if role not in role_indicators %}
        {{- raise_exception('Unknown role: ' ~ role) }}
    {%- endif %}

    {%- if i == 0 %}
        {%- if role == 'system' %}
            {{- role_indicators['system'] }}
            {{- msg.content }}
            {{- end_of_turn -}}
            {%- continue %}
        {%- endif %}
    {%- endif %}

    {%- if role == 'assistant' %}
        {{- role_indicators['assistant'] }}

        {%- set content = (msg.content if (msg.content is defined and msg.content) else "") -%}
        {%- set reasoning = none -%}

        {%- if msg.reasoning_content is defined and msg.reasoning_content %}
            {%- set reasoning = msg.reasoning_content.strip() -%}
        {%- elif content and "</think>" in content %}
            {%- set _parts = content.split('</think>') -%}
            {%- set reasoning = _parts[0].lstrip('<think>').strip() -%}
            {%- set content = _parts[-1].strip() -%}
        {%- endif %}

        {%- if not (reasoning and ((preserve_thinking is defined and preserve_thinking) or i > ns.last_query_index))%}
            {%- set reasoning = none %}
        {%- endif %}

        {%- if content is string %}
            {%- set content = content.strip() -%}
        {%- endif %}

        {{- "<think>\n" }}
        {{- (reasoning if reasoning is not none else "") }}
        {{- "\n</think>\n\n" }}

        {{- content }}

        {%- if msg.tool_calls %}
            {%- if content is defined and content %}
                {{- "\n" }}
            {%- endif %}
            {%- for tool_call in msg.tool_calls %}
                {%- if tool_call.function is defined %}
                    {%- set tool_call = tool_call.function %}
                {%- endif %}

                {%- if tool_call.arguments is defined %}
                    {%- set arguments = tool_call.arguments %}
                {%- elif tool_call.parameters is defined %}
                    {%- set arguments = tool_call.parameters %}
                {%- else %}
                    {{- raise_exception('arguments or parameters are mandatory: ' ~ tool_call) }}
                {%- endif %}

                {{- render_tool_call(tool_call, arguments) }}
                {%- if not loop.last %}
                    {{- "\n" }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {{- end_of_turn -}}

    {%- elif role == "tool" %}
        {%- if i == 0 or messages[i - 1].role != "tool" %}
            {{- role_indicators['tool'] }}
        {%- endif %}
        {{- render_tool_response(msg) }}
        {%- if loop.last or messages[i + 1].role != "tool" %}
            {{- end_of_turn -}}
        {%- else %}
            {{- "\n" }}
        {%- endif %}

    {%- else %}
        {{- role_indicators[role] }}
        {%- if msg.content is string %}
            {{- msg.content }}
        {%- else %}
            {%- for content in msg.content %}
                {%- if content.type == 'image' %}
                    {%- set image_count.value = image_count.value + 1 %}
                    {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<vision><image_pad></vision>
                {%- elif content.type == 'video' %}
                    {%- set video_count.value = video_count.value + 1 %}
                    {%- if add_vision_id %}Video {{ video_count.value }}: {% endif %}<vision><video_pad></vision>
                {%- elif content.type == 'text' %}
                    {{- content.text }}
                {%- else %}
                    {{- content.text }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {{- end_of_turn -}}
    {%- endif %}
{% endfor %}


{%- if add_generation_prompt %}
    {{- role_indicators['assistant'] }}
    {%- if enable_thinking is not defined or enable_thinking is true %}
        {{- "<think>\n" }}
    {%- else %}
        {{- "<think>\n\n</think>\n\n" }}
    {%- endif %}
{%- endif %}