|begin_of_text|>{%- macro render_extra_keys(json_dict, handled_keys) -%}
{%- if json_dict is mapping %}
{%- for json_key in json_dict if json_key not in handled_keys %}
{%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}
{{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '' ~ json_key ~ '>' }}
{%- else %}
{{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '' ~ json_key ~ '>' }}
{%- endif %}
{%- endfor %}
{%- endif %}
{%- endmacro -%}
{%- macro render_tool_call(raw_tool_call) -%}
{%- if raw_tool_call.function is defined and raw_tool_call.function is mapping %}
{%- set tool_call = raw_tool_call.function %}
{%- else %}
{%- set tool_call = raw_tool_call %}
{%- endif %}
{{- '\n\n' }}
{%- if tool_call.arguments is defined and tool_call.arguments is mapping %}
{%- for args_name, args_value in tool_call.arguments.items() %}
{{- '\n' }}
{%- if args_value is mapping or (args_value is sequence and args_value is not string) %}
{{- args_value | tojson | safe }}
{%- else %}
{{- args_value | string }}
{%- endif %}
{{- '\n\n' }}
{%- endfor %}
{%- endif %}
{{- '\n' }}
{%- endmacro -%}
{%- set system_message = none %}
{%- if messages and messages[0]["role"] == "system" %}
{%- set system_message = messages[0]["content"] %}
{%- set loop_messages = messages[1:] %}
{%- else %}
{%- set loop_messages = messages %}
{%- endif %}
{%- if not tools is defined %}
{%- set tools = [] %}
{%- endif %}
{%- set has_tools = tools is iterable and tools is not string and tools | length > 0 %}
{%- if system_message is not none or has_tools %}
{{- '<|im_start|>system\n' }}
{%- if system_message is not none %}
{{- system_message }}
{%- else %}
{{- "You are Trinity Large, a helpful assistant developed by Arcee AI, that can interact with a computer to solve tasks." }}
{%- endif %}
{%- if has_tools %}
{{- "\n\n# Tools\n\nYou have access to the following functions:\n\n" }}
{%- for tool in tools %}
{%- if tool.function is defined and tool.function is mapping %}
{%- set tool = tool.function %}
{%- endif %}
{{- '\n\n' ~ (tool.name | default('') | string) ~ '' }}
{%- if tool.description is defined and tool.description is not none %}
{{- '\n' ~ (tool.description | string | trim) ~ '' }}
{%- endif %}
{{- '\n' }}
{%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
{%- for param_name, param_fields in tool.parameters.properties.items() %}
{{- '\n\n' ~ (param_name | string) ~ '' }}
{%- if param_fields is mapping and param_fields.type is defined and param_fields.type is not none %}
{{- '\n' ~ (param_fields.type | string) ~ '' }}
{%- endif %}
{%- if param_fields is mapping and param_fields.description is defined and param_fields.description is not none %}
{{- '\n' ~ (param_fields.description | string | trim) ~ '' }}
{%- endif %}
{%- if param_fields is mapping %}
{%- set handled_keys = ['name', 'type', 'description'] %}
{{- render_extra_keys(param_fields, handled_keys) }}
{%- endif %}
{{- '\n' }}
{%- endfor %}
{%- endif %}
{%- if tool.parameters is defined %}
{%- set handled_keys = ['type', 'properties'] %}
{{- render_extra_keys(tool.parameters, handled_keys) }}
{%- endif %}
{{- '\n' }}
{%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
{{- render_extra_keys(tool, handled_keys) }}
{{- '\n' }}
{%- endfor %}
{{- "\n" }}
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n\n\n\nvalue_1\n\n\nThis is the value for the second parameter\nthat can span\nmultiple lines\n\n\n\n\n\nReminder:\n- Function calls MUST follow the specified format: an inner block must be nested within XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n' }}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- for message in loop_messages %}
{%- set role = message.role | default('') %}
{%- if role == "assistant" %}
{%- set content_str = '' if message.content is none else (message.content | string) %}
{%- set trimmed_content = content_str | trim %}
{%- set has_reasoning_content = message.reasoning_content is defined %}
{%- set has_reasoning = has_reasoning_content or (message.reasoning is defined) %}
{%- if has_reasoning_content %}
{%- set reasoning_value = message.reasoning_content %}
{%- elif message.reasoning is defined %}
{%- set reasoning_value = message.reasoning %}
{%- else %}
{%- set reasoning_value = none %}
{%- endif %}
{%- set has_tool_calls = message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls is not string and message.tool_calls | length > 0 %}
{{- '<|im_start|>assistant\n' }}
{%- if has_reasoning %}
{%- if reasoning_value %}
{{- '' + (reasoning_value | string | trim) + '' }}
{%- else %}
{{- '' }}
{%- endif %}
{%- if trimmed_content %}
{{- '\n' + trimmed_content }}
{%- endif %}
{%- elif has_tool_calls %}
{%- if trimmed_content %}
{{- trimmed_content }}
{%- endif %}
{%- else %}
{{- content_str }}
{%- endif %}
{%- if has_tool_calls %}
{%- for tool_call in message.tool_calls %}
{%- set separator = '\n' if ((loop.first and (has_reasoning or trimmed_content)) or (not loop.first)) else '' -%}
{{- separator + render_tool_call(tool_call) }}
{%- endfor %}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- elif role == "tool" or role == "observation" or role == "function" %}
{%- if loop.first or loop.previtem.role not in ["tool", "observation", "function"] %}
{{- '<|im_start|>user\n' }}
{%- endif %}
{{- '\n' }}
{{- '' if message.content is none else (message.content | string) }}
{{- '\n\n' }}
{%- if loop.last or loop.nextitem.role not in ["tool", "observation", "function"] %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- else %}
{{- '<|im_start|>' + (role | string) }}
{{- '\n' + ('' if message.content is none else (message.content | string)) }}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- endif %}