{%- set reasoning_instruction = "Think deeply inside <think> </think> tags before responding to the user query.\n\nFormat:\n\n<think>\nYour internal thought process here.\n</think>\n\nYour response to the user here." -%}
{%- set system_message = "" -%}
{%- if messages[0].role == "system" -%}
	{%- set system_message = messages[0].content.rstrip() + "\n\n" -%}
	{%- set messages = messages[1:] -%}
{%- endif -%}
{%- set system_message = system_message + reasoning_instruction -%}
{{- "<|im_start|>system\n" + system_message.lstrip() + "<|im_end|>\n" -}}
{%- for message in messages -%}
	{{- "<|im_start|>" + message.role + "\n" -}}	
	{%- set content = message.content -%}
	{%- if message.role == "assistant" and not loop.last -%}
		{%- if "</think>" in message.content -%}
			{%- set content = content.split("</think>")[-1].strip() -%}
		{%- endif -%}
	{%- endif -%}
	{{- content + "<|im_end|>\n" -}}
{%- endfor -%}
{%- if add_generation_prompt -%}
	{{- "<|im_start|>assistant\n<think>\n" -}}
{%- endif -%}