File size: 3,794 Bytes
91261b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{%- set tools = tools if tools is defined else None -%}
{%- set date_string = date_string if date_string is defined else "1 Sep 2024" -%}

{%- set system_message = messages[0].content if messages[0].role == "system" else "" -%}
{%- if messages[0].role == "system" -%}
    {%- set messages = messages[1:] -%}
{%- endif -%}

{%- if not tool_prompt -%}
    {%- set tool_prompt = "For each function call return a json object with function name and arguments within <tool_call> </tool_call> tags with the following schema:
<tool_call>
{\"name\": <function-name>, \"arguments\": <args-dict>}
</tool_call>" -%}
{%- endif -%}

{%- if system_message or tools -%}
  {{- '<|im_start|>system
'}}
{%- endif -%}

{%- if system_message %}
  {{- system_message + "
"}}
{%- endif -%}

{%- if tools  -%}
  {{- "You have function-calling capabilities. You are provided with function signatures within <tools> </tools> XML tags. You may call one or more functions to assist with the user query. Don't make assumptions about what values to plug into functions.
" }}
  {{- "<tools>
" }}
  {{- tools }}
  {{- "
</tools>
" }}
  {{- tool_prompt -}}
{%- endif -%}

{%- if system_message or tools -%}
  {{- '<|im_end|>
'}}
{%- endif -%}

{# Main message loop #}
{%- for message in messages -%}
    {%- if message.role == "user" or message.role == "assistant" or message.role == "tool" -%}
        {%- if loop.first and message.role != "user" -%}
            {{ raise_exception("Invalid sequence: The first message role must be 'user' after 'system' if provided .") }}
        {%- endif -%}

        {%- if not loop.first and message.role in ["user", "assistant"] and message.role == loop.previtem.role -%}
            {{ raise_exception("Invalid sequence: Consecutive messages cannot have the same role ('user' or 'assistant').") }}
        {%- endif -%}

        {%- if message.role == "user" and not loop.first and loop.previtem.role != "assistant" -%}
            {{ raise_exception("Invalid sequence: A 'user' message must be preceded by an 'assistant' message.") }}
        {%- endif -%}

        {%- if message.role == "tool" and not loop.first and loop.previtem.role not in ["assistant", "tool"] -%}
            {{ raise_exception("Invalid sequence: A 'tool' message must be preceded by 'assistant' or 'tool'.") }}
        {%- endif -%}
    {%- else -%}
        {{- raise_exception("Invalid role detected: only 'user', 'assistant', or 'tool' roles are accepted.") }}
    {%- endif -%}
    {%- if message.role == "user" or (message.role == "assistant" and message.tool_calls is not defined) -%}
        {{- '<|im_start|>' + message.role + '
' + message.content | trim + '<|im_end|>
'}}
    {%- elif message.role == "assistant" -%}
        {{- '<|im_start|>' + message.role }}
        {%- for tool_call in message.tool_calls -%}
            {{ '
<tool_call>
' }}
              {%- if tool_call.function -%}
                {"name": "{{ tool_call.function.name }}", "arguments": {{ tool_call.function.arguments | tojson }} }
              {%- else -%}
                {"name": "{{ tool_call.name }}", "arguments": {{ tool_call.arguments | tojson }} }
              {%- endif -%}
            {{ '
</tool_call>' }}
        {%- endfor -%}
        {{- '<|im_end|>
' }}
    {%- elif message.role == "tool" -%}
        {%- if loop.previtem and loop.previtem.role != "tool" -%}
            {{- '<|im_start|>tool
' }}
        {%- endif -%}
        {{- '<tool_response>
' }} 
            {{- message.content }}
        {{- '
</tool_response>
' }}
        {%- if loop.last or loop.nextitem.role != "tool" -%}
            {{- '<|im_end|>
'}}
        {%- endif -%}
    {%- endif -%}
{%- endfor -%}

{# Prompt for assistant generation if needed #}
{%- if add_generation_prompt -%}
    {{- '<|im_start|>assistant
' }}
{%- endif -%}