File size: 4,596 Bytes
5b565fd
99da7d1
 
 
 
 
 
 
23dd25f
 
b8de5d7
 
23dd25f
 
 
b8de5d7
23dd25f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8de5d7
 
23dd25f
b8de5d7
23dd25f
99da7d1
 
5b565fd
99da7d1
 
5b565fd
 
23dd25f
 
5b565fd
 
 
 
 
b8de5d7
 
 
 
 
 
 
 
 
 
5b565fd
b8de5d7
5b565fd
b8de5d7
 
 
 
 
 
5b565fd
 
b8de5d7
 
99da7d1
b8de5d7
 
 
 
5b565fd
99da7d1
5b565fd
 
 
 
 
b8de5d7
 
 
 
5b565fd
99da7d1
b8de5d7
99da7d1
 
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
{%- set default_system_message = 'You are AI.' -%}{{ bos_token }}
{%- if messages[0]['role'] == 'system' %}
    {%- set system_message = messages[0]['content'] %}
    {%- set loop_messages = messages[1:] %}
{%- else %}
    {%- set system_message = default_system_message %}
    {%- set loop_messages = messages %}
{%- endif %}
{%- set user_messages = loop_messages | selectattr("role", "equalto", "user") | list %}

{% if tools is not none and tools|length > 0 %}
    {%- set tool_instructions =
          'Use the available tools appropriately to fulfill your instructions and achieve your goals by returning a JSON array pre-pended with `[TOOL_CALLS]` like this: '
          ~ '[TOOL_CALLS][{"name": "tool1", "arguments": {"p1": "val1"}}, {"name": "tool2", "arguments": {"p2": "val2", "p3": 23}}] '
          ~ 'Your available tools are: '
    -%}

    {# Build out the tool list however you want #}
    {%- set tool_instructions = tool_instructions ~ '[AVAILABLE_TOOLS]' %}
    {%- for tool in tools %}
        {%- if not loop.first %}, {% endif %}
        {%- set tool_definition = tool.function %}
        {%- set tool_instructions = tool_instructions ~ '{"type":"function","function":{' %}
        {%- for key, val in tool_definition.items() if key != "return" %}
            {%- if not loop.first %}, {% endif %}
            {%- if val is string %}
                {%- set tool_instructions = tool_instructions ~ '"' ~ key ~ '":"' ~ val ~ '"' %}
            {%- else %}
                {%- set tool_instructions = tool_instructions ~ '"' ~ key ~ '":' ~ (val|tojson) %}
            {%- endif %}
        {%- endfor %}
        {%- set tool_instructions = tool_instructions ~ '}}' %}
    {%- endfor %}
    {%- set tool_instructions = tool_instructions ~ '[/AVAILABLE_TOOLS]\n' %}

    {%- set system_message = system_message ~ tool_instructions -%}
{% endif %}
{{ '[SYSTEM_PROMPT]' ~ system_message ~ '[/SYSTEM_PROMPT]' }}

{%- for message in loop_messages %}
    {%- if message['role'] == 'user' %}
        {{- '[INST]' + message['content'] + '[/INST]' }}

    {%- elif message['role'] == 'system' %}
        {{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }}

    {%- elif message['role'] == 'assistant' %}
        {%- if message.tool_calls is defined and message.tool_calls is not none and message.tool_calls|length > 0 -%}
            [TOOL_CALLS][{{ message.tool_calls|tojson }}]
        {%- elif message['content'] is defined and message['content'] is not none -%}
            {{- message['content'] + eos_token }}
        {%- endif %}

    {%- elif message['role'] == 'tool_calls' or message.tool_calls is defined %}
        {%- if message.tool_calls is defined %}
            {%- set tool_calls = message.tool_calls %}
        {%- else %}
            {%- set tool_calls = message.content %}
        {%- endif %}
        {{- "[TOOL_CALLS] [" }}
        {%- for tool_call in tool_calls %}
            {%- set out = tool_call.function|tojson %}
            {{- out[:-1] }}
            {%- if not tool_call.id is defined or tool_call.id|length < 9 %}
                {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!" + tool_call.id) }}
            {%- endif %}
            {{- ', "id": "' + tool_call.id + '"}' }}
            {%- if not loop.last %}
                {{- ", " }}
            {%- else %}
                {{- "]" + eos_token }}
            {%- endif %}
        {%- endfor %}

    {%- elif message["role"] == "tool" %}
        {%- if message.content is defined and message.content.content is defined %}
            {%- set content = message.content.content %}
        {%- else %}
            {%- set content = message.content %}
        {%- endif %}
        {{- '[TOOL_RESULTS] {"content": ' + content|string + ", " }}
        {%- if not message.tool_call_id is defined or message.tool_call_id|length < 9 %}
            {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!" + message.tool_call_id) }}
        {%- endif %}
        {{- '"call_id": "' + message.tool_call_id + '"}[/TOOL_RESULTS]' }}

    {%- elif message["role"] == "assistant" and message.content is defined and message.content is not none %}
        {{- message["content"] + eos_token }}

    {%- elif message['role'] == 'tool' -%}
        [TOOL_CONTENT]
            {{ {"tool_call_id": message['tool_call_id'], "tool_call": message['content']}|tojson }}
        [/TOOL_CONTENT]

    {%- else %}
        {{- raise_exception('Only user, system, assistant, tool_call & tool roles are supported!') }}
    {%- endif %}
{%- endfor %}