{{- bos_token }} {%- if not tools is defined %} {%- set tools = none %} {%- endif %} {%- if not enable_thinking is defined %} {%- set enable_thinking = false %} {%- endif %} {#- This block extracts the system message, so we can slot it into the right place. #} {%- if messages[0]['role'] == 'system' %} {%- if messages[0]['content'] is string %} {%- set system_message = messages[0]['content']|trim %} {%- else %} {%- set system_message = messages[0]['content'][0]['text']|trim %} {%- endif %} {%- set messages = messages[1:] %} {%- else %} {%- set system_message = "" %} {%- endif %} {#- Set the system message. If enable_thinking is true, add the "Enable deep thinking subroutine." #} {%- if enable_thinking %} {%- if system_message != "" %} {%- set system_message = "Enable deep thinking subroutine. " ~ system_message %} {%- else %} {%- set system_message = "Enable deep thinking subroutine." %} {%- endif %} {%- endif %} {#- System message + tools #} {%- if tools is not none or system_message != '' %} {{- "<|header_start|>system<|header_end|> " }} {{- system_message }} {%- if tools is not none %} {%- if system_message != "" %} {{- " " }} {%- endif %} {{- "Available Tools: " }} {%- for t in tools %} {{- t | tojson(indent=4) }} {{- " " }} {%- endfor %} {%- endif %} {{- "<|eot|>" }} {%- endif %} {#- Rest of the messages #} {%- for message in messages %} {#- Case 1 - Usual, non tool related message. #} {%- if not (message.role == "ipython" or message.role == "tool" or message.role == "tool_results" or (message.tool_calls is defined and message.tool_calls is not none)) %} {{- '<|header_start|>' + message['role'] + '<|header_end|> ' }} {%- 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|>" }} {#- Case 2 - the response is from the assistant, but has a tool call returned. #} {%- elif message.tool_calls is defined and message.tool_calls is not none %} {{- "<|header_start|>assistant<|header_end|> " }} {%- if message['content'] is string %} {{- message['content'] }} {%- if message['content'] | trim != "" %} {{- " " }} {%- endif %} {%- else %} {%- for content in message['content'] %} {%- if content['type'] == 'image' %} {{- '<|image|>' }} {%- elif content['type'] == 'text' %} {{- content['text'] }} {%- if content['text'] | trim != "" %} {{- " " }} {%- endif %} {%- endif %} {%- endfor %} {%- endif %} {{- "[" }} {%- for tool_call in message.tool_calls %} {%- if tool_call.function is defined %} {%- set out = tool_call.function|tojson %} {%- if not tool_call.id is defined %} {{- out }} {%- else %} {{- out[:-1] }} {{- ', "id": "' + tool_call.id + '"}' }} {%- endif %} {%- else %} {{- tool_call|tojson }} {%- endif %} {%- if not loop.last %} {{- ", " }} {%- else %} {{- "]<|eot|>" }} {%- endif %} {%- endfor %} {#- Case 3 - the response is from a tool call. #} {%- elif message.role == "ipython" or message["role"] == "tool_results" or message["role"] == "tool" %} {{- "<|header_start|>ipython<|header_end|> " }} {%- if message.tool_call_id is defined and message.tool_call_id != '' %} {{- '{"content": ' }} {%- if message.content is mapping or (message.content is iterable and not message.content is string) %} {{- message.content | tojson }} {%- else %} {{- '"' ~ message.content ~ '"' }} {%- endif %} {{- ', "call_id": "' ~ message.tool_call_id ~ '"}' }} {%- else %} {%- if message.content is mapping or (message.content is iterable and not message.content is string) %} {{- message.content | tojson }} {%- else %} {{- message.content }} {%- endif %} {%- endif %} {{- "<|eot|>" }} {%- endif %} {%- endfor %} {%- if add_generation_prompt %} {{- '<|header_start|>assistant<|header_end|>\n\n' }} {%- if enable_thinking %} {{- '\n' }} {%- endif %} {%- endif %}