File size: 4,518 Bytes
123d4d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{%- if not messages %}{{ raise_exception('A conversation must contain at least one message') }}{% endif -%}
{%- if tools -%}
{{- '<|im_start|>system\nAvailable tools are described by the following JSON schemas. To call a tool, emit <tool_call>{"id":"call_0","name":"tool_name","arguments":{}}</tool_call>. Use a unique id for every call. Wait for matching tool results before continuing. Call tools only when needed.\n' -}}
{{- tools | tojson(sort_keys=true) -}}
{{- '<|im_end|>\n' -}}
{%- endif -%}
{%- set state = namespace(pending={}, seen=[]) -%}
{%- for message in messages -%}
    {%- set mi = loop.index0 -%}
    {%- set role = message['role'] -%}
    {%- if role not in ['system', 'developer', 'user', 'assistant', 'tool'] -%}
        {{- raise_exception('Unknown message role') -}}
    {%- endif -%}
    {%- if state.pending and role != 'tool' -%}
        {{- raise_exception('Unresolved tool calls before next conversation turn') -}}
    {%- endif -%}
    {%- set content = message.get('content') if message.get('content') is not none else '' -%}
    {%- if content is not string -%}{{ raise_exception('Text content required') }}{%- endif -%}
    {%- if role != 'assistant' and (message.get('reasoning_content') or message.get('tool_calls')) -%}
        {{- raise_exception('Only assistant can contain reasoning/tool_calls') -}}
    {%- endif -%}
    {%- set header = role -%}
    {%- if role == 'tool' -%}
        {%- set call_id = message.get('tool_call_id') -%}
        {%- if call_id not in state.pending -%}{{ raise_exception('Orphan or duplicate tool result') }}{%- endif -%}
        {%- set name = state.pending[call_id] -%}
        {%- if message.get('name', name) != name -%}{{ raise_exception('Tool result name mismatch') }}{%- endif -%}
        {%- set state.pending = dict(state.pending.items() | rejectattr('0', 'equalto', call_id)) -%}
        {%- set header = 'tool name=' + name + ' tool_call_id=' + call_id -%}
    {%- endif -%}
    {{- '<|im_start|>' + header + '\n' -}}
    {%- if role == 'assistant' -%}
        {% generation %}
        {%- if message.get('reasoning_content') -%}
            {{- '<think>' + message['reasoning_content'] + '</think>' -}}
        {%- endif -%}
        {{- content -}}
        {%- for call in message.get('tool_calls') or [] -%}
            {%- set name = call['function']['name'] -%}
            {%- set arguments = call['function'].get('arguments', {}) -%}
            {%- set call_id = call.get('id', 'call_' + mi|string + '_' + loop.index0|string) -%}
            {%- if arguments is not mapping -%}{{ raise_exception('Normalize arguments to an object before templating') }}{%- endif -%}
            {%- if name is not string or call_id is not string or not name or not call_id -%}
                {{- raise_exception('Function name and call id must be nonempty strings') -}}
            {%- endif -%}
            {%- if name|length > 128 or call_id|length > 64 or name[0] not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_' -%}
                {{- raise_exception('Invalid function name or call id') -}}
            {%- endif -%}
            {%- for char in name -%}
                {%- if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.:-' -%}
                    {{- raise_exception('Invalid function name') -}}
                {%- endif -%}
            {%- endfor -%}
            {%- for char in call_id -%}
                {%- if char not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-' -%}
                    {{- raise_exception('Invalid tool call id') -}}
                {%- endif -%}
            {%- endfor -%}
            {%- if call_id in state.seen -%}{{ raise_exception('Duplicate tool call id') }}{%- endif -%}
            {%- set state.seen = state.seen + [call_id] -%}
            {%- set state.pending = dict(state.pending, **{call_id: name}) -%}
            {{- '<tool_call>' -}}
            {{- {'id': call_id, 'name': name, 'arguments': arguments} | tojson(sort_keys=true) -}}
            {{- '</tool_call>' -}}
        {%- endfor -%}
        {{- '<|im_end|>' -}}
        {% endgeneration %}
    {%- else -%}
        {{- content + '<|im_end|>' -}}
    {%- endif -%}
    {{- '\n' -}}
{%- endfor -%}
{%- if add_generation_prompt -%}
    {%- if state.pending -%}{{ raise_exception('Cannot generate before tool results arrive') }}{%- endif -%}
    {{- '<|im_start|>assistant\n' -}}
    {%- if enable_thinking | default(false) -%}{{ '<think>' }}{%- endif -%}
{%- endif -%}