File size: 2,409 Bytes
778c4c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{{ bos_token }}

{# Define the hardcoded thinking instructions #}

{%- set system_instruction = "Think deeply and carefully about the user's request. Compose your thoughts about the user's prompt between <think> and </think> tags, then output the final answer based on your thoughts." -%}



{%- if messages[0]['role'] == 'system' -%}

    {# If the user provided a system prompt, prepend our instruction to it #}

    {%- if messages[0]['content'] is string -%}

        {%- set first_user_prefix = system_instruction + '\n\n' + messages[0]['content'] + '\n\n' -%}

    {%- else -%}

        {%- set first_user_prefix = system_instruction + '\n\n' + messages[0]['content'][0]['text'] + '\n\n' -%}

    {%- endif -%}

    {%- set loop_messages = messages[1:] -%}

{%- else -%}

    {# If no system prompt exists, just use our instruction #}

    {%- set first_user_prefix = system_instruction + '\n\n' -%}

    {%- set loop_messages = messages -%}

{%- endif -%}



{%- for message in loop_messages -%}

    {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}

        {{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}

    {%- endif -%}

    

    {%- if (message['role'] == 'assistant') -%}

        {%- set role = "model" -%}

    {%- else -%}

        {%- set role = message['role'] -%}

    {%- endif -%}

    

    {{ '<start_of_turn>' + role + '\n' }}

    

    {# Inject the system prefix only into the very first turn #}

    {%- if loop.first -%}

        {{ first_user_prefix }}

    {%- endif -%}



    {# Check for a 'thought' key to wrap in tags if it exists in history #}

    {%- if message['thought'] is defined and message['thought'] -%}

        {{ '<think>\n' + (message['thought'] | trim) + '\n</think>\n' }}

    {%- endif -%}



    {# Render message content #}

    {%- if message['content'] is string -%}

        {{ message['content'] | trim }}

    {%- elif message['content'] is iterable -%}

        {%- for item in message['content'] -%}

            {%- if item['type'] == 'image' -%}

                {{ '<image>' }}

            {%- elif item['type'] == 'text' -%}

                {{ item['text'] | trim }}

            {%- endif -%}

        {%- endfor -%}

    {%- endif -%}

    {{ '<end_of_turn>\n' }}

{%- endfor -%}



{%- if add_generation_prompt -%}

    {{ '<start_of_turn>model\n<think>\n' }}

{%- endif -%}