{%- set user_messages = messages | selectattr('role', 'equalto', 'user') | list %}
{%- macro output_available_tools(tools, message) %}
{%- if tools and (message == user_messages[-1]) %}
{{- '<|available_tools|>[' }}
{%- for tool in tools %}
{%- set tool = tool.function %}
{{- "{" }}
{%- for key, val in tool.items() if key != "return" %}
{%- if val is string %}
{{- "'" + key + "': '" + val + "'" }}
{%- else %}
{{- "'" + key + "': " + val|string }}
{%- endif %}
{%- if not loop.last %}
{{- ", " }}
{%- endif %}
{%- endfor %}
{{- "}" }}
{%- if not loop.last %}
{{- ", " }}
{%- else %}
{{- "]" }}
{%- endif %}
{%- endfor %}
{{- eos_token -}}
{%- endif %}
{%- endmacro %}
{%- macro output_tool_results(tool_results) %}
{{- '<|tool_results|>[' }}
{%- for tool_result in tool_results %}
{{- "{'content': " + tool_result.content|string + ", 'call_id': '" + tool_result.call_id + "'}" }}
{%- endfor %}
{{- ']' }}
{{- eos_token -}}
{%- endmacro %}
{%- macro output_tool_calls(tool_calls) %}
{{- '<|tool_calls|>[' }}
{%- for tool_call in tool_calls %}
{{- "{'id': '" + tool_call.id + "', 'name': '" + tool_call.name + "', 'arguments': " + tool_call.arguments|string + '}' }}
{%- endfor %}
{{- ']' }}
{%- endmacro %}
{%- if messages[0]['role'] == 'system' %}
{{- '<|system|>' + messages[0]['content'] + eos_token -}}
{%- else %}
{{- '<|system|>' + 'You are a helpful AI assistant. Your role as an assistant involves thoroughly exploring questions through a systematic thinking process before providing the final precise and accurate solutions. This requires engaging in a comprehensive cycle of analysis, summarizing, exploration, reassessment, reflection, backtracking, and iteration to develop well-considered thinking process. Please structure your response into two main sections: Thought and Solution using the specified format: Thought section Solution section. In the Thought section, detail your reasoning process in steps. Each step should include detailed considerations such as analysing questions, summarizing relevant findings, brainstorming new ideas, verifying the accuracy of the current steps, refining any errors, and revisiting previous steps. In the Solution section, based on various attempts, explorations, and reflections from the Thought section, systematically present the final solution that you deem correct. The Solution section should be logical, accurate, and concise and detail necessary steps needed to reach the conclusion.' + eos_token }}
{%- endif %}
{%- for message in messages %}
{%- if message['role'] == 'user' %}
{%- if tools is defined %}
{{- output_available_tools(tools, message) }}
{%- endif %}
{{- '<|user|>' + message['content'] + eos_token -}}
{%- elif message['role'] == 'assistant' %}
{% set assistant_content = "" %}
{%- if message.content is defined %}
{% set assistant_content = message.content %}
{%- endif %}
{%- if message.tool_calls is defined and message.tool_calls -%}
{{- '<|assistant|>' + assistant_content + output_tool_calls(message['tool_calls']) + eos_token -}}
{%- else %}
{{- '<|assistant|>' + assistant_content + eos_token }}
{%- endif %}
{%- elif message['role'] == 'tool_results' %}
{{- output_tool_results(message.tool_results) }}
{%- endif %}
{%- if loop.last and add_generation_prompt -%}
{{- '<|assistant|>' + '\n' -}}
{%- endif -%}
{%- endfor %}