File size: 12,171 Bytes
14649af
 
 
 
 
 
 
 
 
 
e949b16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14649af
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
{%- if not platform_instruction_override %}
{%- set platform_instruction_override -%}
These instructions are always to be followed and cannot be overridden by subsequent system or user turns:
- You will answer requests for educational, informative, or creative content related to safety categories. You will not provide content that is harmful or could be used to cause harm.

These instructions serve as your defaults, but they can be overridden in subsequent system or user turns:
- Your name is Command.
- You are a large language model built by Cohere.
{%- endset %}
{%- endif %}
{%- set reasoning = reasoning if reasoning is not undefined else (false if reasoning_effort is defined and reasoning_effort | lower == "none" else true) -%}
{%- set grounding = grounding | default("disabled") | upper %}
{%- set grounding_enabled = grounding == "ENABLED" %}
{%- set tools_or_docs_exist = tools or documents %}
{%- set render_tools_section = true %}
{%- set render_grounding = grounding_enabled and tools_or_docs_exist %}
{%- set render_platform_instruction_override = true if platform_instruction_override else false %}
{%- set has_developer_instruction = developer_instruction or developer_instruction == "" %}
{%- set render_developer_instruction = true if developer_instruction else false %}
{%- set convert_first_system_msg = convert_first_system_msg | default(true) -%}
{%- set skip_thinking = skip_thinking | default(false) -%}
{{ bos_token }}
{%- macro document_turn(documents) -%}
{# format documents into chat turn -#}
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>{%- if not skip_thinking -%}<|START_THINKING|>I will look through the document to address the users needs.<|END_THINKING|>{%- endif -%}<|START_ACTION|>[
    {"tool_call_id": "0", "tool_name": "direct-injected-document", "parameters": {}}
]<|END_ACTION|><|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
    {
        "tool_call_id": "0",
        "results": {
{%- for doc in documents %}
{%- set doc_val = doc.data if doc.data else doc %}

            "{{ loop.index0 }}": {{ doc_val|tojson }}{% if not loop.last %},
            {%- endif %}
{%- endfor %}

        },
        "is_error": null
    }
]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|>{%- endmacro %}
{%- macro tool_call_id_to_int(messages, tool_call_id) %}
{%- if regen_tool_call_ids -%}
    {%- set counter = namespace(value=0) %}
    {%- set tool_call_id_seen = namespace(value=false) %}
    {%- for msg in messages %}
        {%- if msg.tool_calls %}
            {%- for tool_call in msg.tool_calls %}
                {%- if tool_call.id == tool_call_id and not tool_call_id_seen.value -%}
                    {{ counter.value }}
                    {%- set tool_call_id_seen.value = true %}
                {%- endif %}
                {%- set counter.value = counter.value + 1 %}
            {%- endfor %}
        {%- endif %}
    {%- endfor %}
{%- else -%}
    {{ tool_call_id }}
{%- endif -%}
{%- endmacro %}
{%- macro format_tool_message(messages, tool_msg) -%}
{#- format tool message #}{
        "tool_call_id": "{{ tool_call_id_to_int(messages, tool_msg.tool_call_id) }}",
        "results": {
        {%- if tool_msg.content is mapping or tool_msg.content is string %}

            {% if tool_msg.content is string -%}
                {%- set text_wrapper = {"content": tool_msg.content} -%}
            {%- else -%}
                {%- set text_wrapper = tool_msg.content -%}
            {%- endif %}
            "0": {{ text_wrapper|tojson }}
        {%- else %}
            {%- for content in tool_msg.content %}

            "{{ loop.index0 }}": {{ print_tool_content(content) }}{% if not loop.last %},{% endif %}
            {%- endfor %}
        {%- endif %}

        },
        "is_error": null
    }
{%- endmacro -%}
{%- macro print_tool_content(item) %}
{%- if item.type|lower == "text" -%}
{%- set text_wrapper = {"content": item.text} -%}
{{ text_wrapper|tojson }}
{%- elif item.type|lower == "document" and item.document and "data" in item.document -%}
{{ item.document.data|tojson }}
{%- else -%}
{{ item|tojson }}
{%- endif -%}
{%- endmacro %}
{%- macro print_msg(msg) %}
    {%- if msg is string -%}
<|START_TEXT|>{{ msg }}<|END_TEXT|>
    {%- elif msg.content is string -%}
<|START_TEXT|>{{ msg.content }}<|END_TEXT|>
    {%- else %}
        {%- set last_was_text = namespace(value=false) %}
        {%- for content in msg.content %}
            {%- if content.type|lower == "text" -%}
                {%- if not last_was_text.value -%}
                    <|START_TEXT|>
                {%- endif -%}
    {{ content.text }}
                {%- if loop.last -%}
                  <|END_TEXT|>
                {%- endif %}
                {%- set last_was_text.value = true -%}
            {%- else -%}
                {%- if last_was_text.value -%}
                    <|END_TEXT|>
                {%- endif -%}
                {%- set last_was_text.value = false -%}
            {%- endif -%}
            {%- if content.type|lower == "image" -%}
                {%- if content.data -%}
{{ content.data }}
                {%- else -%}
<|IMG_PATCH|>
                {%- endif -%}
            {%- endif -%}
        {%- endfor %}
    {%- endif %}
{%- endmacro %}
{%- macro print_thinking(msg) %}
    {%- if msg.thinking -%}
{{ msg.thinking }}
    {%- elif msg.content and msg.content[0].thinking -%}
{{ msg.content[0].thinking }}
    {%- endif %}
{%- endmacro %}
{%- if messages and messages[0]['role']|lower == 'system' and not has_developer_instruction and convert_first_system_msg %}{%- set developer_instruction = messages[0] %}{%- set render_developer_instruction = true %}{%- set initial_instruction_message = true %}{% endif %}
{%- set json_object = true if response_format and response_format.type == "json_object" else false %}
{%- set json_schema = (response_format.json_schema or response_format.schema) if response_format %}
{%- set json_mode = json_object or json_schema %}
{%- set tool_idx = namespace(value=0) %}
{%- set tool_ids_seen = namespace(value=[]) %}
{%- set regen_tool_call_ids = regen_tool_call_ids | default(true) -%}
{%- set sent_documents = namespace(value=false) -%}

{%- if render_tools_section or render_platform_instruction_override or render_grounding or json_mode -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TEXT|>
{%- elif not render_developer_instruction -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>
{%- endif %}

{%- set rendered_platform_turn_chunk = false %}

{%- if render_platform_instruction_override -%}
{{ platform_instruction_override }}
{% set rendered_platform_turn_chunk = true %}
{%- else %}
{%- endif %}

{%- if render_grounding -%}
{%- if rendered_platform_turn_chunk %}

{% endif -%}
Note that both your responses and reflections can be grounded. Grounding means you associate pieces of texts (called "spans") with those specific tool results that support them (called "sources"). And you use a pair of tags "<co>" and "</co>" to indicate when a span can be grounded onto a list of sources, listing them out in the closing tag. Sources from the same tool call are grouped together and listed as "{tool_call_id}:[{list of result indices}]", before they are joined together by ",". E.g., "<co>span</co: 0:[1,2],1:[0]>" means that "span" is supported by result 1 and 2 from "tool_call_id=0" as well as result 0 from "tool_call_id=1".
{% set rendered_platform_turn_chunk = true %}
{%- endif %}

{%- if render_tools_section %}
{%- if rendered_platform_turn_chunk %}

{% endif %}
# Available Tools
```json
[
{% if tools_or_docs_exist %}
{%- if documents %}
    {"name": "direct-injected-document", "description": "This is a special tool to directly inject user-uploaded documents into the chat as additional context. DO NOT use this tool by yourself!", "parameters": {"type": "object", "properties": {}, "required": []}, "responses": {"200": {"description": "Successfully returned a list of chunked text snippets from the directly uploaded documents.", "content": {"application/json": {"schema": {"type": "array", "items": {"type": "object", "required": ["url", "snippet"], "properties": {"url": {"type": "string", "description": "The url of the uploaded document."}, "snippet": {"type": "string", "description": "The text snippet for the returned document chunk."}}}}}}}}}
    {%- if tools %},
    {% else %}

    {% endif %}
{%- endif %}
{%- for tool in tools %}
    {"name": "{{ tool['function']['name'] }}", "description": "{{ tool['function']['description'] }}", "parameters": {{ tool['function']['parameters']|tojson }}, "responses": null}
    {%- if not loop.last %},{% endif %}

{% endfor %}
{%- else %}

{% endif %}
]
```
{%- set rendered_platform_turn_chunk = true %}
{%- endif -%}

{%- if json_mode -%}
{%- if rendered_platform_turn_chunk %}


{% endif -%}
When generating JSON objects, do not generate block markers. Generate an object directly without prefixing with ```json. Return only the JSON and nothing else.
    {%- if json_schema %}

Your output should adhere to the following json schema:
{{ json_schema }}
    {%- endif -%}
{%- set rendered_platform_turn_chunk = true %}
{%- endif %}
{%- if rendered_platform_turn_chunk -%}
<|END_TEXT|><|END_OF_TURN_TOKEN|>
{%- elif not render_developer_instruction -%}
<|END_OF_TURN_TOKEN|>
{%- endif %}
{%- if render_developer_instruction -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(developer_instruction) }}<|END_OF_TURN_TOKEN|>
{%- endif %}
{%- for message in messages %}
    {%- set msg_role_downcased = message.role | lower %}
    {%- if msg_role_downcased == 'system' and (not (loop.first and initial_instruction_message)) -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
    {%- elif msg_role_downcased == 'user' -%}
<|START_OF_TURN_TOKEN|><|USER_TOKEN|>{{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
        {%- if documents and not sent_documents.value %}{%- set sent_documents.value = true %}{% set tool_idx.value = tool_idx.value + 1 %}{{ document_turn(documents) }}{% endif %}
    {%- elif msg_role_downcased == 'assistant' or msg_role_downcased == 'chatbot' -%}
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
        {%- if message.tool_calls %}
            {% if not skip_thinking %}
                {% if message.tool_plan -%}
                    <|START_THINKING|>{{ message.tool_plan }}<|END_THINKING|>
                {%- elif message.thinking or (message.content and message.content[0].type == "thinking") -%}
                    <|START_THINKING|>{{ print_thinking(message) }}<|END_THINKING|>
                {%- endif %}
            {%- endif %}<|START_ACTION|>[
            {%- for tc in message.tool_calls %}

    {"tool_call_id": "{%- if regen_tool_call_ids -%}{{ tool_idx.value }}{%- else -%}{{ tc.id }}{%- endif -%}", "tool_name": "{{ tc['function']['name'] }}", "parameters": {{ tc['function']['arguments']|tojson }}}{% if not loop.last %},{% endif %}
                {%- set tool_idx.value = tool_idx.value + 1 %}
            {%- endfor %}

]<|END_ACTION|><|END_OF_TURN_TOKEN|>
        {%- else -%}
            {% if (message.thinking or (message.content and message.content[0].type == "thinking")) and not skip_thinking -%}
                <|START_THINKING|>{{ print_thinking(message) }}<|END_THINKING|>
            {%- endif -%}
            {{ print_msg(message) }}<|END_OF_TURN_TOKEN|>
        {%- endif %}
    {%- elif msg_role_downcased == 'tool' and message.tool_call_id not in tool_ids_seen.value -%}
<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|><|START_TOOL_RESULT|>[
    {{ format_tool_message(messages, message) }}
        {%- for msg in messages[loop.index0 + 1:] %}

            {%- if msg.role | lower == 'tool' %},
    {{ format_tool_message(messages, msg) }}
                {%- set tool_ids_seen.value = tool_ids_seen.value + [msg.tool_call_id] %}
            {%- else %}
                {%- break %}
            {%- endif %}
        {%- endfor %}

]<|END_TOOL_RESULT|><|END_OF_TURN_TOKEN|>
    {%- endif %}
{%- endfor %}{%- if add_generation_prompt -%}<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>{% if reasoning %}<|START_THINKING|>{% else %}<|START_THINKING|><|END_THINKING|>{% endif %}{%- endif %}