File size: 12,165 Bytes
ceb9d94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{%- set tools = tools | default(none) -%}
{%- set add_generation_prompt = add_generation_prompt | default(false) -%}
{%- set reasoning_effort = reasoning_effort | default('medium') -%}
{%- set cutoff_date = cutoff_date | default('2026-01-04') -%}
{%- set current_date = current_date | default(strftime_now('%Y-%m-%d') if strftime_now is defined else none) -%}
{%- set reasoning_effort_map = {'minimal': 1, 'low': 16, 'medium': 64, 'high': 256, 'xhigh': 512} -%}

{#- Neutralise control sequences in caller-supplied text so it cannot be
    tokenized as structure. "<|" is broken because every special token has the
    form "<|...|>"; the bare media placeholders are broken because the
    multimodal processor rewrites them positionally and a literal one in text
    would desynchronise placeholders from the attached media. -#}
{%- macro esc(value) -%}
    {{- value | string
        | replace('<|', '< |')
        | replace('<image>', '< image>')
        | replace('<video>', '< video>')
        | replace('<audio>', '< audio>') -}}
{%- endmacro -%}

{%- macro esc_header(value) -%}
    {{- esc(value) | replace('\r', ' ') | replace('\n', ' ') -}}
{%- endmacro -%}

{%- macro esc_attr(value) -%}
    {{- esc_header(value)
        | replace('&', '&amp;')
        | replace('"', '&quot;')
        | replace("'", '&#39;')
        | replace('<', '&lt;')
        | replace('>', '&gt;') -}}
{%- endmacro -%}

{%- macro render_content(content) -%}
    {%- if content is string -%}
        {{- esc(content) -}}
    {%- elif content is not none -%}
        {%- for part in content -%}
            {%- set part_type = part.get('type', '') -%}
            {%- if part_type in ['image', 'input_image', 'image_url'] -%}
                {{- '<|patch|>' -}}
            {%- elif part_type in ['video', 'input_video', 'video_url'] -%}
                {{- '<|video|>' -}}
            {%- elif part_type in ['audio', 'input_audio', 'audio_url'] -%}
                {{- '<|audio|>' -}}
            {%- elif part_type in ['text', 'input_text', 'output_text'] -%}
                {{- esc(part.get('text', '')) -}}
            {%- endif -%}
        {%- endfor -%}
    {%- endif -%}
{%- endmacro -%}

{%- macro render_atem(tc) -%}
    {%- set args = tc.function.arguments -%}
    {%- if args is not mapping -%}
        {{- raise_exception('This chat template requires tool_call.function.arguments to be a dict (mapping). The jinja sandbox has no JSON parser, so deserialize the arguments string before calling apply_chat_template.') -}}
    {%- endif -%}
    {{- '<atem:function_calls>\n<atem:invoke name="' -}}{{- esc_attr(tc.function.name) -}}{{- '">\n' -}}
    {%- for k, v in args.items() -%}
        {{- '<atem:parameter name="' -}}{{- esc_attr(k) -}}{{- '">' -}}
        {%- if v is boolean -%}
            {%- if v -%}
                true
            {%- else -%}
                false
            {%- endif -%}
        {%- elif v is none -%}
            null
        {%- elif v is mapping or (v is iterable and v is not string) -%}
            {{- esc(v | tojson) -}}
        {%- else -%}
            {{- esc(v) -}}
        {%- endif -%}
        {{- '</atem:parameter>\n' -}}
    {%- endfor -%}
    {{- '</atem:invoke>\n</atem:function_calls>' -}}
{%- endmacro -%}

{%- macro render_tool_defs(tools) -%}
    {{- 'In this environment you have access to a set of tools you can use to answer the user\'s question.\n\n' -}}
    {{- 'You can invoke a function by writing a "<atem:function_calls>" block like the following as part of your reply to the user:\n' -}}
    {{- '<atem:function_calls>\n<atem:invoke name="$FUNCTION_NAME">\n<atem:parameter name="$PARAMETER_NAME">$PARAMETER_VALUE</atem:parameter>\n...\n</atem:invoke>\n</atem:function_calls>\n\n' -}}
    {{- 'String and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.\n' -}}
    {{- 'Here are the functions available in JSONSchema format:\n' -}}
    {{- '// Tool metadata\n{"name": "", "description": ""}\n' -}}
    {{- '// Function schemas' -}}
    {%- for tool in tools -%}
        {%- set fn = tool.function if tool.function is defined else tool -%}
        {{- '\n{"name": ' -}}{{- fn.name | tojson -}}
        {{- ', "description": ' -}}{{- (fn.description if fn.description is defined and fn.description else '') | tojson -}}
        {{- ', "parameters": ' -}}{{- (fn.parameters if fn.parameters is defined and fn.parameters else {}) | tojson -}}{{- '}' -}}
    {%- endfor -%}
    {{- '\n\n' -}}
    {{- 'Here are examples of how to call functions in the tool set. To make parallel tool calls, emit each call as its own message (one tool call per message) in consecutive messages, as shown below:\n' -}}
    {{- '(If the tool namespace is not specified, invoke the function directly as `example_function_name_1` rather than `example_tool_name_1.example_function_name_1`)\n\n' -}}
    {{- 'to=example_tool_name_1.example_function_name_1\n\n' -}}
    {{- '<atem:function_calls>\n<atem:invoke name="example_tool_name_1.example_function_name_1">\n' -}}
    {{- '<atem:parameter name="example_parameter_1">value_1</atem:parameter>\n' -}}
    {{- '</atem:invoke>\n</atem:function_calls>\n\n' -}}
    {{- 'to=example_tool_name_2.example_function_name_2\n\n' -}}
    {{- '<atem:function_calls>\n<atem:invoke name="example_tool_name_2.example_function_name_2">\n' -}}
    {{- '<atem:parameter name="example_parameter_2">This is the value for the second parameter\nthat can span\n"multiple" lines\n</atem:parameter>\n' -}}
    {{- '<atem:parameter name="example_parameter_3">value_3</atem:parameter>\n' -}}
    {{- '</atem:invoke>\n</atem:function_calls>' -}}
{%- endmacro -%}

{%- macro render_valid_recipients(tools) -%}
    {%- set rns = namespace(recipients=[]) -%}
    {{- 'Use the appropriate recipient for each message:\n\n' -}}
    {%- set rns.recipients = rns.recipients + ['"self"'] -%}
    {{- '- "self": private reasoning and tool planning.\n' -}}
    {%- set rns.recipients = rns.recipients + ['"commentary"'] -%}
    {{- '- "commentary": user-visible intermediate updates while the assistant will continue working, including messages sent to the user before or between tool calls.\n' -}}
    {%- if tools -%}
        {%- set first_tool = tools | first -%}
        {%- set first_fn = (first_tool.function if first_tool.function is defined else first_tool).name -%}
        {%- set rns.recipients = rns.recipients + ['"[func_name] (e.g., ' ~ esc_header(first_fn) ~ ')"'] -%}
        {{- '- "tool" or "[tool name]": the name of the tool to call.\n' -}}
    {%- endif -%}
    {%- set rns.recipients = rns.recipients + ['"user"'] -%}
    {{- '- "user": messages that end the assistant turn, such as a completed response or clarification question that waits for the user\'s reply; do not use for pre-tool updates or partial responses.\n' -}}
    {{- '\n# Valid recipients: ' ~ rns.recipients | join(', ') ~ '.' -}}
{%- endmacro -%}

{{- bos_token -}}
{%- set ns = namespace(instructions=none, rest=messages) -%}
{%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
    {%- set ns.instructions = render_content(messages[0]['content'])
          | replace('Reasoning effort', 'Reasoning strength')
          | replace('Reasoning Effort', 'Reasoning Strength')
          | replace('reasoning effort', 'reasoning strength')
          | replace('REASONING EFFORT', 'REASONING STRENGTH') -%}
    {%- set ns.rest = messages[1:] -%}
{%- endif -%}
{%- set sys = namespace(parts=['You are a helpful AI assistant.', 'Knowledge cutoff: ' ~ cutoff_date ~ '.']) -%}
{%- if current_date -%}
    {%- set sys.parts = sys.parts + ['Current date: ' ~ current_date ~ '.'] -%}
{%- endif -%}
{%- if ns.instructions -%}
    {%- set sys.parts = sys.parts + [ns.instructions] -%}
{%- endif -%}
{%- if tools -%}
    {%- set sys.parts = sys.parts + [render_tool_defs(tools) | trim] -%}
{%- endif -%}
{%- if reasoning_effort in reasoning_effort_map and 'reasoning strength:' not in ((ns.instructions if ns.instructions else '') | lower) -%}
    {%- set sys.parts = sys.parts + ['Reasoning strength: ' ~ reasoning_effort_map[reasoning_effort] ~ '.'] -%}
{%- endif -%}
{%- set sys.parts = sys.parts + [render_valid_recipients(tools) | trim] -%}
{{- '<|start|>system<|message|>' ~ sys.parts | join('\n\n') ~ '<|eot|>' -}}
{%- for message in ns.rest -%}
    {%- set role = message['role'] -%}
    {%- set end_token = '<|eom|>' if (not loop.last and ns.rest[loop.index0 + 1]['role'] == role) else '<|eot|>' -%}
    {%- if role in ['system', 'developer'] -%}
        {{- '<|start|>' + role + '<|message|>' -}}
        {{- render_content(message['content']) -}}
        {{- '<|eot|>' -}}
    {%- elif role == 'user' -%}
        {{- '<|start|>user<|message|>' -}}
        {%- if message.get('render_timestamp') and message.get('timestamp') -%}
            {%- set ts = message['timestamp'] -%}
            {%- if ts is string -%}
                {{- '[' ~ esc_header(ts) ~ '] ' -}}
            {%- elif ts.strftime is defined -%}
                {{- '[' ~ (ts.strftime('%a %Y-%m-%d %H:%M %Z') | trim) ~ '] ' -}}
            {%- endif -%}
        {%- endif -%}
        {{- render_content(message['content']) -}}
        {{- '<|eot|>' -}}
    {%- elif role == 'tool' -%}
        {%- set tname = message.get('name') -%}
        {%- if not tname -%}
            {%- set tcid = message.get('tool_call_id') -%}
            {%- set tns = namespace(name=tcid if tcid else '') -%}
            {%- for m in messages -%}
                {%- if m.get('tool_calls') -%}
                    {%- for tc in m['tool_calls'] -%}
                        {%- if tcid is not none and tc.id is defined and tc.id == tcid -%}
                            {%- set tns.name = tc.function.name -%}
                        {%- endif -%}
                    {%- endfor -%}
                {%- endif -%}
            {%- endfor -%}
            {%- set tname = tns.name -%}
        {%- endif -%}
        {%- if not tname -%}
            {{- raise_exception('tool message has no name and no matching tool_call_id') -}}
        {%- endif -%}
        {{- '<|start|>tool ' -}}{{- esc_header(tname) -}}{{- '<|message|><tool_output name="' -}}{{- esc_attr(tname) -}}{{- '">\n' -}}
        {{- render_content(message['content']) -}}
        {{- '\n</tool_output><|eot|>' -}}
    {%- elif role == 'assistant' -%}
        {%- generation %}
        {%- set reasoning = message.get('reasoning_content') or message.get('reasoning') -%}
        {%- if reasoning -%}
            {{- '<|start|>assistant to=self<|message|>' -}}{{- esc(reasoning) -}}{{- '<|eom|>' -}}
        {%- endif -%}
        {%- set body = render_content(message.get('content')) -%}
        {%- if message.get('tool_calls') -%}
            {%- if body -%}
                {{- '<|start|>assistant to=commentary<|message|>' + body + '<|eom|>' -}}
            {%- endif -%}
            {%- for tc in message['tool_calls'] -%}
                {{- '<|start|>assistant to=' -}}{{- esc_header(tc.function.name) -}}{{- '<|message|>' -}}
                {{- render_atem(tc) -}}
                {{- (end_token if loop.last else '<|eom|>') -}}
            {%- endfor -%}
        {%- elif body or not reasoning -%}
            {%- set recipient = message.get('recipient') or 'user' -%}
            {%- if recipient == 'user' -%}
                {%- set end_turn = true -%}
            {%- elif recipient in ['self', 'commentary'] -%}
                {%- set end_turn = false -%}
            {%- elif message.get('end_turn') is not none -%}
                {%- set end_turn = message.get('end_turn') -%}
            {%- else -%}
                {%- set end_turn = false -%}
            {%- endif -%}
            {{- '<|start|>assistant to=' -}}{{- esc_header(recipient) -}}{{- '<|message|>' -}}
            {{- body -}}
            {{- ('<|eot|>' if end_turn else '<|eom|>') -}}
        {%- endif -%}
        {%- endgeneration %}
    {%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
    {{- '<|start|>assistant' -}}
{%- endif -%}