1{%- set loop_messages = messages -%}
2{%- set message_roles = ['system', 'user', 'assistant', 'tool'] -%}
3{%- set system_prompt_suffix -%}
4{%- filter trim -%}
5In addition to plain text responses, you can chose to call one or more of the provided functions.
6
7Use the following rule to decide when to call a function:
8 * if the response can be generated from your internal knowledge (e.g., as in the case of queries like "What is the capital of Poland?"), do so
9 * if you need external information that can be obtained by calling one or more of the provided functions, generate a function calls
10
11If you decide to call functions:
12 * prefix function calls with functools marker (no closing marker required)
13 * all function calls should be generated in a single JSON list formatted as functools[{"name": [function name], "arguments": [function arguments as JSON]}, ...]
14 * follow the provided JSON schema. Do not hallucinate arguments or values. Do to blindly copy values from the provided samples
15 * respect the argument type formatting. E.g., if the type if number and format is float, write value 7 as 7.0
16 * make sure you pick the right functions that match the user intent
17
18Available functions as JSON spec:
19{%- endfilter -%}
20{%- endset -%}
21{%- set system_prompt_suffix = system_prompt_suffix + "\n" + functions -%}
22{%- set system_prompt_suffix = system_prompt_suffix + '\nToday is ' + datetime + '.' -%}
23{%- set ns = namespace(role='', content='') -%}
24{#- Basic consistency checks -#}
25{%- if not loop_messages -%}
26 {{ raise_exception('Expected non-empty messages') }}
27{%- endif -%}
28{%- for message in loop_messages -%}
29 {%- set ns.role = message['role'] | lower -%}
30 {%- if ns.role not in message_roles -%}
31 {%- set message_roles_string = message_roles | join(', ') -%}
32 {{ raise_exception('Invalid role ' + message['role'] + '. Only ' + message_roles_string + ' are supported.') }}
33 {%- endif -%}
34 {%- set msg_content = message['content'] | default('', true) | trim -%}
35 {%- if loop.index0 == 0 -%}
36 {%- if ns.role == 'system' -%}
37 {%- set system_prompt = '<|start_header_id|>' + 'system' + '<|end_header_id|>\n\n' + message['content'] | trim + '\n' + system_prompt_suffix + '<|eot_id|>' -%}
38 {%- else -%}
39 {%- set system_prompt = '<|start_header_id|>' + 'system' + '<|end_header_id|>\n\nYou are a helpful assistant with access to functions.\n' + system_prompt_suffix + '<|eot_id|>' -%}
40 {%- endif -%}
41 {%- set ns.content = bos_token + system_prompt -%}
42 {{- ns.content -}}
43 {%- endif -%}
44 {%- if loop.index0 > 0 or ns.role != 'system' -%}
45 {%- set ns.content = '<|start_header_id|>' + ns.role + '<|end_header_id|>\n\n' + msg_content -%}
46 {%- if 'tool_calls' in message and message['tool_calls'] -%}
47 {%- set tool = namespace(calls=[]) -%}
48 {%- for call in message['tool_calls'] -%}
49 {%- set tool.calls = tool.calls + ['{"name": "' + call['function']['name'] + '", "arguments": ' + call['function']['arguments'] + '}'] -%}
50 {%- endfor -%}
51 {%- set ns.content = ns.content + ' functools[' + tool.calls | join(', ') + ']' -%}
52 {%- endif -%}
53 {%- set ns.content = ns.content + '<|eot_id|>' -%}
54 {{- ns.content -}}
55 {%- endif -%}
56{%- endfor -%}
57{{- '<|start_header_id|>assistant<|end_header_id|>\n\n' -}}