1{# version=v3-llama3.1 #}{%- if not tools is defined -%}
 2    {%- set tools = none -%}
 3{%- endif -%}
 4
 5{%- set has_code_interpreter = tools | selectattr("type", "equalto", "code_interpreter") | list | length > 0 -%}
 6{%- if has_code_interpreter -%}
 7    {%- set tools = tools | rejectattr("type", "equalto", "code_interpreter") | list -%}
 8{%- endif -%}
 9
10{#- System message + builtin tools #}
11{{- bos_token + "<|start_header_id|>system<|end_header_id|>\n\n" }}
12{%- if has_code_interpreter %}
13    {{- "Environment: ipython\n\n" }}
14{%- else -%}
15    {{ "\n"}}
16{%- endif %}
17{{- "Cutting Knowledge Date: December 2023\n\n" }}
18{%- if tools %}
19    {{- "\nYou have access to the following functions:\n\n" }}
20    {%- for t in tools %}
21        {%- if "type" in t -%}
22            {{ "Use the function '"|safe + t["function"]["name"] + "' to '"|safe + t["function"]["description"] + "'\n"|safe + t["function"] | tojson() }}
23        {%- else -%}
24            {{ "Use the function '"|safe + t["name"] + "' to '"|safe + t["description"] + "'\n"|safe + t | tojson() }}
25        {%- endif -%}
26        {{- "\n\n" }}
27    {%- endfor %}
28    {{- '\nThink very carefully before calling functions.\nIf a you choose to call a function ONLY reply in the following format:\n<{start_tag}={function_name}>{parameters}{end_tag}\nwhere\n\nstart_tag => `<function`\nparameters => a JSON dict with the function argument name as key and function argument value as value.\nend_tag => `</function>`\n\nHere is an example,\n<function=example_function_name>{"example_name": "example_value"}</function>\n\nReminder:\n- If looking for real time information use relevant functions before falling back to brave_search\n- Function calls MUST follow the specified format, start with <function= and end with </function>\n- Required parameters MUST be specified\n- Only call one function at a time\n- Put the entire function call reply on one line\n\n' -}}
29{%- endif %}
30{{- "<|eot_id|>" -}}
31
32{%- for message in messages -%}
33    {%- if message['role'] == 'user' or message['role'] == 'system' -%}
34        {{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
35    {%- elif message['role'] == 'tool' -%}
36        {{ '<|start_header_id|>ipython<|end_header_id|>\n\n' + message['content'] + '<|eot_id|>' }}
37    {%- else -%}
38        {{ '<|start_header_id|>' + message['role'] + '<|end_header_id|>\n\n'}}
39        {%- if message['content'] -%}
40            {{ message['content'] }}
41        {%- endif -%}
42        {%- if 'tool_calls' in message and message['tool_calls'] -%}
43            {%- for tool_call in message['tool_calls'] -%}
44                {%- if tool_call["function"]["name"] == "python" -%}
45                    {{ '<|python_tag|>' + tool_call['function']['arguments'] }}
46                {%- else -%}
47                    {{ '<function=' + tool_call['function']['name'] + '>' + tool_call['function']['arguments'] + '</function>' }}
48                {%- endif -%}
49            {%- endfor -%}
50            {{ '<|eom_id|>' }}
51        {%- else -%}
52            {{ '<|eot_id|>' }}
53        {%- endif -%}
54    {%- endif -%}
55{%- endfor -%}
56{%- if add_generation_prompt -%}
57    {{ '<|start_header_id|>assistant<|end_header_id|>\n\n' }}
58{%- endif -%}