{%- 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" }} {%- for tool in tools %} {{- "\n" }} {{- tool | tojson }} {%- 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' }} {%- 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('') and content.endswith('')) %} {%- 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('') %} {%- set content = (content.split('')[1:] | join('')).lstrip('\n') %} {%- elif '\n' in content %} {%- set reasoning_content = content.split('\n')[0] %} {%- if reasoning_content.startswith('\n') %} {%- set reasoning_content = reasoning_content[8:] %} {%- elif reasoning_content.startswith('') %} {%- set reasoning_content = reasoning_content[7:].lstrip('\n') %} {%- endif %} {%- set content = (content.split('\n')[1:] | join('\n')).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\n' + reasoning_content + '\n\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\n\n' }} {%- else %} {{- '\n\n' }} {%- endif %} {%- else %} {{- '\n\n\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] %} {{- '\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\n' }} {%- endfor %} {%- elif call.arguments is defined and call.arguments is string and call.arguments %} {{- call.arguments }} {{- '\n' }} {%- endif %} {{- '\n' }} {%- 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\n' + content + '\n' }} {%- 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 %} {{- '\n\n\n\n' }} {%- else %} {{- '\n' }} {%- endif %} {%- endif %}