Focus-Red / chat_template.jinja
Jaidchen's picture
Publish Focus-Red
85f197c unverified
Raw
History Blame Contribute Delete
9.71 kB
{%- macro render_content(content) %}
{%- if content is string %}
{{- content }}
{%- elif content is iterable and content is not mapping %}
{%- for item in content %}
{%- if item is mapping %}
{%- if ('type' in item and (item.type == 'image' or item.type == 'image_url')) or 'image' in item or 'image_url' in item %}
{{- '[image]' }}
{%- elif ('type' in item and item.type == 'video') or 'video' in item %}
{{- '[video]' }}
{%- elif 'text' in item %}
{{- item.text }}
{%- else %}
{{- raise_exception('Unexpected item type in content.') }}
{%- endif %}
{%- else %}
{{- item | string }}
{%- endif %}
{%- endfor %}
{%- elif content is none or content is undefined %}
{{- '' }}
{%- else %}
{{- raise_exception('Unexpected content type.') }}
{%- endif %}
{%- endmacro %}
{%- if not messages %}
{{- raise_exception('No messages provided.') }}
{%- endif %}
{%- set reasoning_instructions = '' %}
{%- if enable_thinking is undefined or enable_thinking is true %}
{%- set resolved_reasoning_effort = reasoning_effort|default('xhigh') %}
{%- if resolved_reasoning_effort not in ('xhigh', 'medium', 'low') %}
{{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.') }}
{%- endif %}
{%- if resolved_reasoning_effort == 'xhigh' %}
{%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}
{%- elif resolved_reasoning_effort == 'low' %}
{%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}
{%- endif %}
{%- endif %}
{%- set _preserve_thinking = preserve_thinking if preserve_thinking is defined else true %}
{%- set leading = namespace(active=true, count=0, content='') %}
{%- for message in messages %}
{%- if leading.active and (message.role == 'system' or message.role == 'developer') %}
{%- set rendered = render_content(message.content) | trim %}
{%- if rendered %}
{%- if leading.content %}
{%- set leading.content = leading.content + '\n' + rendered %}
{%- else %}
{%- set leading.content = rendered %}
{%- endif %}
{%- endif %}
{%- set leading.count = leading.count + 1 %}
{%- else %}
{%- set leading.active = false %}
{%- endif %}
{%- endfor %}
{%- set _has_tools = tools is defined and tools and tools is iterable and tools is not mapping %}
{%- if _has_tools %}
{{- '<|im_start|>system\n' }}
{%- if reasoning_instructions %}
{{- reasoning_instructions + '\n\n' }}
{%- endif %}
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
{%- for tool in tools %}
{{- "\n" }}
{{- tool | tojson }}
{%- 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>' }}
{%- if leading.content %}
{{- '\n\n' + leading.content }}
{%- endif %}
{{- '<|im_end|>\n' }}
{%- else %}
{%- if leading.content %}
{{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + leading.content + '<|im_end|>\n' }}
{%- elif reasoning_instructions %}
{{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
{%- endif %}
{%- endif %}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
{%- for message in messages[::-1] %}
{%- set index = (messages|length - 1) - loop.index0 %}
{%- if ns.multi_step_tool and message.role == "user" %}
{%- set content = render_content(message.content)|trim %}
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
{%- set ns.multi_step_tool = false %}
{%- set ns.last_query_index = index %}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- for message in messages %}
{%- set content = render_content(message.content) | trim %}
{%- if loop.index0 < leading.count %}
{%- elif message.role == 'system' or message.role == 'developer' %}
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
{%- elif message.role == 'user' %}
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
{%- elif message.role == "assistant" %}
{%- set reasoning_content = '' %}
{%- if message.reasoning_content is defined and message.reasoning_content is string %}
{%- set reasoning_content = message.reasoning_content %}
{%- elif message.thinking is defined and message.thinking is string %}
{%- set reasoning_content = message.thinking %}
{%- elif content.startswith('</think>') %}
{%- set content = (content.split('</think>')[1:] | join('</think>')).lstrip('\n') %}
{%- elif '\n</think>' in content %}
{%- set reasoning_content = content.split('\n</think>')[0] %}
{%- if reasoning_content.startswith('<think>\n') %}
{%- set reasoning_content = reasoning_content[8:] %}
{%- elif reasoning_content.startswith('<think>') %}
{%- set reasoning_content = reasoning_content[7:].lstrip('\n') %}
{%- endif %}
{%- set content = (content.split('\n</think>')[1:] | join('\n</think>')).lstrip('\n') %}
{%- endif %}
{%- set reasoning_content = reasoning_content | trim %}
{%- if reasoning_content and (_preserve_thinking or loop.index0 > ns.last_query_index) %}
{{- '<|im_start|>assistant\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
{%- else %}
{{- '<|im_start|>assistant\n' + content }}
{%- endif %}
{%- if message.tool_calls is defined and message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
{%- for tool_call in message.tool_calls %}
{%- set call = tool_call.function if tool_call.function is defined and tool_call.function is not none else tool_call %}
{%- if loop.first %}
{%- if content | trim %}
{{- '\n\n<tool_call>\n<function=' + call.name + '>\n' }}
{%- else %}
{{- '<tool_call>\n<function=' + call.name + '>\n' }}
{%- endif %}
{%- else %}
{{- '\n<tool_call>\n<function=' + call.name + '>\n' }}
{%- endif %}
{%- if call.arguments is defined and call.arguments is mapping %}
{%- for args_name in call.arguments %}
{%- set args_value = call.arguments[args_name] %}
{{- '<parameter=' + args_name + '>\n' }}
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
{{- args_value }}
{{- '\n</parameter>\n' }}
{%- endfor %}
{%- elif call.arguments is defined and call.arguments is string and call.arguments %}
{{- call.arguments }}
{{- '\n' }}
{%- endif %}
{{- '</function>\n</tool_call>' }}
{%- endfor %}
{%- endif %}
{%- if not (loop.last and continue_final_message is defined and continue_final_message) %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- elif message.role == 'tool' %}
{%- set previous_role = messages[loop.index0 - 1].role if loop.index0 > 0 else '' %}
{%- if previous_role != 'tool' %}
{{- '<|im_start|>user' }}
{%- endif %}
{{- '\n<tool_response>\n' + content + '\n</tool_response>' }}
{%- set next_role = messages[loop.index0 + 1].role if not loop.last else '' %}
{%- if loop.last or next_role != 'tool' %}
{{- '<|im_end|>\n' }}
{%- endif %}
{%- else %}
{{- raise_exception('Unexpected message role: ' + message.role) }}
{%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
{{- '<|im_start|>assistant\n' }}
{%- if enable_thinking is defined and enable_thinking is false %}
{{- '<think>\n\n</think>\n\n' }}
{%- else %}
{{- '<think>\n' }}
{%- endif %}
{%- endif %}