File size: 2,869 Bytes
5f923cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{{ bos_token }}
{%- if tools %}
    {{- '<start_of_turn>system\n' }}
    {%- for tool in tools %}
        {{- tool | trim }}
        {{- "\n\n" }}
    {%- endfor %}
    {{- '<end_of_turn>\n'}}
{%- endif %}
{%- if messages[0]['role'] == 'system' -%}
    {%- if messages[0]['content'] is string -%}
        {%- set first_user_prefix = messages[0]['content'] + '\n\n' -%}
    {%- else -%}
        {%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}
    {%- endif -%}
    {%- set loop_messages = messages[1:] -%}
{%- else -%}
    {%- set first_user_prefix = "" -%}
    {%- set loop_messages = messages -%}
{%- endif -%}
{%- for message in loop_messages -%}
    {%- if (message['role'] == 'assistant') -%}
        {%- set role = "model" -%}
    {%- elif (message['role'] == 'tool') -%}
        {%- set is_tool = True -%}
        {%- set role = "user" -%}
    {%- else -%}
        {%- set role = message['role'] -%}
    {%- endif -%}
    {{ '<start_of_turn>' + role + '\n' + (first_user_prefix if loop.first else "") }}
    {%- if is_tool -%}
        {{ '```tool_outputs\n' }}
    {%- endif -%}
    {%- if 'content' in message -%}
        {%- if message['content'] is string -%}
            {{ message['content'] | trim }}
        {%- elif message['content'] is iterable -%}
            {%- for item in message['content'] -%}
                {%- if item['type'] == 'audio' -%}
                    {{ '<audio_soft_token>' }}
                {%- elif item['type'] == 'image' -%}
                    {{ '<image_soft_token>' }}
                {%- elif item['type'] == 'text' -%}
                    {{ item['text'] | trim }}
                {%- endif -%}
                {%- if is_tool -%}
                    {{ '\n' }}
                {%- endif -%}
            {%- endfor -%}
        {%- else -%}
            {{ raise_exception("Invalid content type") }}
        {%- endif -%}
    {%- endif -%}
    {%- if is_tool -%}
        {{ '```' }}
        {%- set is_tool = False -%}
    {%- endif -%}
    {%- if 'tool_calls' in message -%}
        {{- '```tool_code\n' -}}
        {%- for tool_call in message['tool_calls'] -%}
            {%- if 'function' in tool_call -%}
                {%- set tool_call = tool_call['function'] -%}
            {%- endif -%}
            {{-  tool_call['name'] + '(' -}}
            {%- if 'arguments' in tool_call -%}
                {%- for key in tool_call['arguments'] -%}
                    {{- key + '=' + tool_call['arguments'][key] -}}
                    {% if not loop.last %}
                        {{- ', ' -}}
                    {% endif %}
                {%- endfor %}
            {{- ')\n' -}}
            {%- endif -%}
        {%- endfor -%}
        {{- '```' -}}
    {%- endif -%}
    {{ '<end_of_turn>\n' }}
{%- endfor -%}
{%- if add_generation_prompt -%}
    {{'<start_of_turn>model\n'}}
{%- endif -%}