File size: 4,755 Bytes
42aad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{{- bos_token }}\n

{%- if custom_tools is defined %}
    {%- set tools = custom_tools %}
{%- endif %}

{%- if not tools_in_user_message is defined %}
    {%- set tools_in_user_message = true %}
{%- endif %}

{%- if not date_string is defined %}
    {%- if strftime_now is defined %}
        {%- set date_string = strftime_now("%d %b %Y") %}
    {%- else %}
        {%- set date_string = "26 Jul 2024" %}
    {%- endif %}
{%- endif %}

{%- if not tools is defined %}
    {%- set tools = none %}
{%- endif %}

{# Extract the system message, if present #}
{%- if messages[0]['role'] == 'system' %}
    {%- set system_message = messages[0]['content'] | trim %}
    {%- set messages = messages[1:] %}
{%- else %}
    {%- set system_message = "" %}
{%- endif %}

{# Identify if any messages contain images #}
{% set image_ns = namespace(has_images=false) %}
{%- for message in messages %}
    {%- for content in message['content'] %}
        {%- if content['type'] == 'image' %}
            {%- set image_ns.has_images = true %}
        {%- endif %}
    {%- endfor %}
{%- endfor %}

{# Raise exception if images and system message coexist #}
{%- if image_ns.has_images and system_message != "" %}
    {{- raise_exception("Prompting with images is incompatible with system messages.") }}
{%- endif %}

{# Insert system message if no images are found #}
{%- if not image_ns.has_images %}
    {{- "<|start_header_id|>system<|end_header_id|>\\n\\n" }}
    {%- if tools is not none %}
        {{- "Environment: ipython\\n" }}
    {%- endif %}
    {{- "Cutting Knowledge Date: December 2023\\n" }}
    {{- "Today Date: " + date_string + "\\n\\n" }}
    
    {%- if tools is not none and not tools_in_user_message %}
        {{- "You have access to the following functions. " }}
        {{- "To call a function, respond with JSON for a function call." }}
        {{- 'Use the format {"name": function name, "parameters": dictionary of arguments}.' }}
        {{- "Do not use variables.\\n\\n" }}
        {%- for t in tools %}
            {{- t | tojson(indent=4) }}
            {{- "\\n\\n" }}
        {%- endfor %}
    {%- endif %}
    {{- system_message }}
    {{- "<|eot_id|>" }}
{%- endif %}

{# Custom tools in user messages #}
{%- if tools_in_user_message and tools is not none %}
    {%- if messages | length != 0 %}
        {%- set first_user_message = messages[0]['content'] | trim %}
        {%- set messages = messages[1:] %}
    {%- else %}
        {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
    {%- endif %}
    
    {{- '<|start_header_id|>user<|end_header_id|>\\n\\n' }}
    {{- "Given these functions, respond with a JSON for a function call " }}
    {{- "that best addresses the prompt.\\n\\n" }}
    {{- 'Format: {"name": function name, "parameters": dictionary of arguments}.' }}
    {{- "Do not use variables.\\n\\n" }}
    {%- for t in tools %}
        {{- t | tojson(indent=4) }}
        {{- "\\n\\n" }}
    {%- endfor %}
    {{- first_user_message + "<|eot_id|>" }}
{%- endif %}

{# Iterate over remaining messages #}
{%- for message in messages %}
    {%- if not (message.role in ['ipython', 'tool'] or 'tool_calls' in message) %}
        {{- '<|start_header_id|>' + message['role'] + '<|end_header_id|>\\n\\n' }}
        {%- if message['content'] is string %}
            {{- message['content'] }}
        {%- else %}
            {%- for content in message['content'] %}
                {%- if content['type'] == 'image' %}
                    {{- '<|image|>' }}
                {%- elif content['type'] == 'text' %}
                    {{- content['text'] }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {{- '<|eot_id|>' }}
    {%- elif 'tool_calls' in message %}
        {%- if message.tool_calls | length != 1 %}
            {{- raise_exception("This model only supports single tool-calls at once!") }}
        {%- endif %}
        {%- set tool_call = message.tool_calls[0].function %}
        {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}
        {{- '{"name": "' + tool_call.name + '", "parameters": ' }}
        {{- tool_call.arguments | tojson }}
        {{- "}" }}
        {{- "<|eot_id|>" }}
    {%- elif message.role in ["tool", "ipython"] %}
        {{- "<|start_header_id|>ipython<|end_header_id|>\\n\\n" }}
        {%- if message.content is mapping or message.content is iterable %}
            {{- message.content | tojson }}
        {%- else %}
            {{- message.content }}
        {%- endif %}
        {{- "<|eot_id|>" }}
    {%- endif %}
{%- endfor %}

{# Add generation prompt if specified #}
{%- if add_generation_prompt %}
    {{- '<|start_header_id|>assistant<|end_header_id|>\\n\\n' }}
{%- endif %}