File size: 2,891 Bytes
18efded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{%- if tools %}
    {{- '<|BOT|>system
' }}
    {%- if messages[0]['role'] == 'system' %}
        {{- messages[0]['content'] + '<|EOT|>' }}
    {%- else %}
        {{- 'You are a helpful assistant. Please think step by step and provide your reasoning process within <think> </think> tags, followed by your final answer. Format: <think>your reasoning here</think>your final answer<|EOT|>' }}
    {%- endif %}
    {{- '<|BOT|>' }}
    {{- "tool_json_schemas
" }}
    {{- tools | tojson }}
    {{- '<|EOT|>' }}
{%- else %}
    {%- if messages[0]['role'] == 'system' %}
        {{- '<|BOT|>system
' + messages[0]['content'] + '<|EOT|>' }}
    {%- else %}
        {{- '<|BOT|>system
You are a helpful assistant. Please think step by step and provide your reasoning process within <think> </think> tags, followed by your final answer. Format: <think>your reasoning here</think>your final answer<|EOT|>' }}
    {%- endif %}
{%- endif %}
{%- for message in messages %}
    {%- if message["role"] == "user" %}
        {{- '<|BOT|>human
' + message["content"] + '<|EOT|>' }}
    {%- elif (message["role"] == "system" and not loop.first) or (message["role"] == "assistant" and not message["tool_calls"]) %}
        {{- '<|BOT|>' + message["role"] + '
' + message["content"] + '<|EOT|>' }}
    {%- elif message["role"] == "assistant" %}
        {{- '<|BOT|>' + message["role"] + '
' }}
        {%- if message["content"] %}
            {{- message["content"] }}
        {%- endif %}
        {%- for tool_call in message.tool_calls %}
            {%- if tool_call["function"] is defined %}
                {%- set tool_call = tool_call["function"] %}
            {%- endif %}
            {{- '<|CALL_START|>' + 'function
' + tool_call["name"] + '
' }}
            {{- tool_call["arguments"] | tojson }}
            {{- '<|CALL_END|>' }}
        {%- endfor %}
        {{- '<|EOT|>' }}
    {%- elif message["role"] == "tool" %}
        {{- '<|BOT|>' }}
        {%- set ns = namespace(function_name="tool") %}
        {%- if message["tool_call_id"] %}
            {%- for prev_msg in messages %}
                {%- if prev_msg["role"] == "assistant" and prev_msg["tool_calls"] %}
                    {%- for tool_call in prev_msg["tool_calls"] %}
                        {%- if tool_call["id"] == message["tool_call_id"] %}
                            {%- if tool_call["function"] is defined %}
                                {%- set ns.function_name = tool_call["function"]["name"] %}
                            {%- endif %}
                        {%- endif %}
                    {%- endfor %}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {{- 'function_output
' + ns.function_name + '
' }}
        {{- message["content"] }}
        {{- '<|EOT|>' }}
    {%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<|BOT|>assistant
<think>
' }}
{%- endif %}