| <|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 %} |
| {{- '<tool_call>\n<function=' + (tool_call.name | default('') | string) + '>\n' }} |
| {%- if tool_call.arguments is defined and tool_call.arguments is mapping %} |
| {%- for args_name, args_value in tool_call.arguments.items() %} |
| {{- '<parameter=' + (args_name | string) + '>\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</parameter>\n' }} |
| {%- endfor %} |
| {%- endif %} |
| {{- '</function>\n</tool_call>' }} |
| {%- 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<tools>" }} |
| {%- for tool in tools %} |
| {%- if tool.function is defined and tool.function is mapping %} |
| {%- set tool = tool.function %} |
| {%- endif %} |
| {{- '\n<function>\n<name>' ~ (tool.name | default('') | string) ~ '</name>' }} |
| {%- if tool.description is defined and tool.description is not none %} |
| {{- '\n<description>' ~ (tool.description | string | trim) ~ '</description>' }} |
| {%- endif %} |
| {{- '\n<parameters>' }} |
| {%- 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<parameter>\n<name>' ~ (param_name | string) ~ '</name>' }} |
| {%- if param_fields is mapping and param_fields.type is defined and param_fields.type is not none %} |
| {{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }} |
| {%- endif %} |
| {%- if param_fields is mapping and param_fields.description is defined and param_fields.description is not none %} |
| {{- '\n<description>' ~ (param_fields.description | string | trim) ~ '</description>' }} |
| {%- endif %} |
| {%- if param_fields is mapping %} |
| {%- set handled_keys = ['name', 'type', 'description'] %} |
| {{- render_extra_keys(param_fields, handled_keys) }} |
| {%- endif %} |
| {{- '\n</parameter>' }} |
| {%- endfor %} |
| {%- endif %} |
| {%- if tool.parameters is defined %} |
| {%- set handled_keys = ['type', 'properties'] %} |
| {{- render_extra_keys(tool.parameters, handled_keys) }} |
| {%- endif %} |
| {{- '\n</parameters>' }} |
| {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %} |
| {{- render_extra_keys(tool, handled_keys) }} |
| {{- '\n</function>' }} |
| {%- endfor %} |
| {{- "\n</tools>" }} |
| {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> 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</IMPORTANT>' }} |
| {%- 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 %} |
| {{- '<think>' + (reasoning_value | string | trim) + '</think>' }} |
| {%- else %} |
| {{- '<think></think>' }} |
| {%- 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 %} |
| {{- '<tool_response>\n' }} |
| {{- '' if message.content is none else (message.content | string) }} |
| {{- '\n</tool_response>\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<think>' }} |
| {%- endif %} |