{{- "<|im_start|>system\n" -}}
{%- if tools -%}
	{%- if messages[0].role == "system" -%}
		{{- messages[0].content + "\n\n" -}}
	{%- endif -%}
	{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" -}}
	{%- for tool in tools -%}
		{{- "\n" -}}
		{{- tool | tojson -}}
	{%- endfor -%}
	{{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call>\n" -}}
{%- elif messages[0].role == "system" -%}
	{{- messages[0].content -}}
{%- endif -%}
{{- "# Commands: \n- /think or think: Think or reason step-by-step using a long chain-of-thought and self-reflection inside think tags. If this command is present, ALWAYS start your thinking or reasoning with '<think>\\nSince the user used the \"/think\" command, I will:\\nThink step-by-step with **long chain-of-thought** and **self-reflection**. I will only respond when I'm 100% sure. I will provide the final response after closing this thinking section.'\n- /shortthink or shortthink: Think or reason step-by-step briefly and shortly inside think tags. If this command is present, ALWAYS start your thinking or reasoning with '<think>\\nSince the user used the \"/shortthink\" command, I will:\\nThink step-by-step about the user's query briefly and shortly. I will only respond when I'm more than 50% sure. I will provide the final response after closing this thinking section.' \n- /nothink or nothink: Immediate response and you are FORBIDDEN FROM THINKING or REASONING, without any DOUBLE-CHECKING, and WITHOUT DRAFTING your response, just an immediate response WITH NO REPETITION. If this command is present, ALWAYS start respond with '<think>\\nSince the user used the \"/nothink\" command, I will:\\nImmediately give my final response without any thinking or self-reflection. Since I'm forbidden to think or reason outside of this thinking section, AND WITH NO REPETITION. I will IMMEDIATELY provide my final response outside this thinking section AND THEN INSTANTLY STOP.\\n</think>'\n- /clear: Forgets EVERYTHING (including previous messages, topics, etc.) that came before this command got executed.\nDo not take these commands into consideration, instead just do as they instruct.\nALWAYS REMEMBER THESE COMMANDS. NO NEED TO REPEAT THIS PART OF THE SYSTEM INSTRUCTIONS (COMMANDS) AS IT IS USELESS AND UNIMPORTANT.\n" -}}
{{- "\n<|im_end|>" -}}
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages | length - 1, reasoning_mode="normal") -%}
{%- for message in messages[::-1] -%}
	{%- set index = messages | length - 1 - loop.index0 -%}
	{%- if ns.multi_step_tool and message.role == "user" and message.content is string and not (message.content.startswith("<tool_response>") and message.content.endswith("</tool_response>")) -%}
		{%- set ns.multi_step_tool = false -%}
		{%- set ns.last_query_index = index -%}
	{%- endif -%}
{%- endfor -%}
{%- for message in messages -%}
	{%- if message.content is string -%}
		{%- set content = message.content -%}
	{%- else -%}
		{%- set content = "" -%}
	{%- endif -%}
	{%- if message.role == "user" or message.role == "system" and not loop.first -%}
		{{- "<|im_start|>" + message.role + "\n" + content -}}
		{%- if messages[0].role == "system" and "/nothink" in messages[0].content and (not ("/think" in messages[0].content) or not ("/shortthink" in messages[0].content)) or "/nothink" in content and not ("/think" in messages[0].content or "/shortthink" in messages[0].content) or (messages[loop.index+1] and "</think>" not in messages[loop.index+1].content) -%}
			{%- set enable_thinking = false -%}
			{%- set enable_short_thinking = false -%}
			{%- set ns.reasoning_mode = "none" -%}
			{%- if not ("think" in content) -%}
				{{- " /nothink" -}}
			{%- endif -%}
		{%- elif messages[0].role == "system" and "/shortthink" in messages[0].content and (not ("/nothink" in messages[0].content) and not ("/think" in messages[0].content)) or "/shortthink" in content and not ("/think" in messages[0].content or "/shortthink" in messages[0].content) -%}
			{%- set enable_thinking = true -%}
			{%- set enable_short_thinking = true -%}
			{%- set ns.reasoning_mode = "short" -%}
			{%- if not ("think" in content) -%}
				{{- " /shortthink" -}}
			{%- endif -%}
		{%- else -%}
			{%- set enable_thinking = true -%}
			{%- set enable_short_thinking = false -%}
			{%- set ns.reasoning_mode = "normal" -%}
			{%- if not ("think" in content) -%}
				{{- " /think" -}}
			{%- endif -%}
		{%- endif -%}
		{{- "<|im_end|>" + "\n" -}}
	{%- elif message.role == "assistant" -%}
		{%- set reasoning_content = "" -%}
		{%- if "<think>\nSince the user used the" not in content.split("</think>")[0].split("<think>")[-1] -%}
			{%- if ns.reasoning_mode == "none" -%}
				{%- set reasoning_prefix = "" -%}
			{%- elif ns.reasoning_mode == "short" -%}
				{%- set reasoning_prefix = "Since the user used the \"/shortthink\" command, I will:\nThink step-by-step about the user's query briefly and shortly. I will only respond when I'm more than 50% sure. I will provide the final response after closing this thinking section. " -%}
			{%- else -%}
				{%- set reasoning_prefix = "Since the user used the \"/think\" command, I will:\nThink step-by-step with **long chain-of-thought** and **self-reflection**. I will only respond when I'm 100% sure. I will provide the final response after closing this thinking section. " -%}
			{%- endif -%}
		{%- endif -%}
		{%- if message.reasoning_content is string -%}
			{%- set reasoning_content = message.reasoning_content -%}
		{%- else -%}
			{%- if "</think>" in content -%}
				{%- set reasoning_content = reasoning_prefix + content.split("</think>")[0].rstrip("\n").split("<think>")[-1].lstrip("\n") -%}
				{%- set content = content.split("</think>")[-1].lstrip("\n") -%}
			{%- endif -%}
			{%- if "<think>" in content -%}
				{%- set content = content | replace("<think>", " ") -%}
			{%- endif -%}
			{%- if "</think>" in content -%}
				{%- set content = content | replace("</think>", " ") -%}
			{%- endif -%}
		{%- endif -%}
		{{- "\n" -}}
		{#  Apply truncation and break if the /shortthink command is active  #}
		{%- if enable_short_thinking is true -%}
			{%- set words = reasoning_content.split(" ") -%}
			{%- if words | length > 300 -%}
				{%- set truncated_reasoning = words[:150] | join(" ") + " ... truncated ... " + words[words | length - 150:words | length] | join(" ") -%}
			{%- else -%}
				{%- set truncated_reasoning = reasoning_content | join(" ") -%}
			{%- endif -%}
			{%- set reasoning_content = truncated_reasoning -%}
		{%- endif -%}
		{%- if loop.last or not loop.last and reasoning_content and enable_thinking == true -%}
			{{- "<|im_start|>" + message.role + "\n<think>\n" + reasoning_content.strip("\n") + "\n</think>\n" + content.lstrip("\n") -}}
		{%- else -%}
			{{- "<|im_start|>" + message.role + "\n" + content -}}
		{%- endif -%}
		{%- if message.tool_calls -%}
			{%- for tool_call in message.tool_calls -%}
				{%- if loop.first and content or not loop.first -%}
					{{- "\n" -}}
				{%- endif -%}
				{%- if tool_call.function -%}
					{%- set tool_call = tool_call.function -%}
				{%- endif -%}
				{{- "<tool_call>\n{\"name\": \"" -}}
				{{- tool_call.name -}}
				{{- "\", \"arguments\": " -}}
				{%- if tool_call.arguments is string -%}
					{{- tool_call.arguments -}}
				{%- else -%}
					{{- tool_call.arguments | tojson -}}
				{%- endif -%}
				{{- "}\n</tool_call>" -}}
			{%- endfor -%}
		{%- endif -%}
		{{- "<|im_end|>\n" -}}
	{%- elif message.role == "tool" -%}
		{%- if loop.first or messages[loop.index0 - 1].role != "tool" -%}
			{{- "<|im_start|>user" -}}
		{%- endif -%}
		{{- "\n<tool_response>\n" -}}
		{{- content -}}
		{{- "\n</tool_response>" -}}
		{%- if loop.last or messages[loop.index0 + 1].role != "tool" -%}
			{{- "<|im_end|>\n" -}}
		{%- endif -%}
	{%- endif -%}
{%- endfor -%}
{%- set last_message = messages[messages | length - 1].content -%}
{%- if add_generation_prompt -%}
	{{- "<|im_start|>assistant\n" -}}
	{%- if enable_thinking is defined and enable_thinking is false or "/nothink" in last_message or "nothink" in last_message or messages[0].role == "system" and "/nothink" in messages[0].content and (not ("/think" in messages[0].content) or not ("/shortthink" in messages[0].content)) -%}
		{{- "<think>\nSince the user used the \"/nothink\" command, I will:\nImmediately give my final response without any thinking or self-reflection. Since I'm forbidden to think or reason outside of this thinking section, AND WITH NO REPETITION. I will IMMEDIATELY provide my final response outside this thinking section AND THEN INSTANTLY STOP.\n</think>" -}}
	{%- elif enable_short_thinking is defined and enable_short_thinking is false or "/shortthink" in last_message or "shortthink" in last_message or messages[0].role == "system" and "/shortthink" in messages[0].content and not ("/think" in messages[0].content) -%}
		{{- "<think>\nSince the user used the \"/shortthink\" command, I will:\nThink step-by-step about the user's query briefly and shortly. I will only respond when I'm more than 50% sure. I will provide the final response after closing this thinking section." -}}
	{%- elif "/clear" in last_message -%}
		{{- "<think>\nSince the user used the \"/clear\" command, I will:\n1. Forget everything above.\n2. Ignore everything that comes before this message.\n3. Start a fresh new conversation and greet the user.\n</think>" -}}
	{%- else -%}
		{{- "<think>\nSince the user used the \"/think\" command, I will:\nThink step-by-step with **long chain-of-thought** and **self-reflection**. I will only respond when I'm 100% sure. I will provide the final response after closing this thinking section." -}}
	{%- endif -%}
{%- endif -%}