{#- Unsloth template fixes #}
{%- set yesterday_day = strftime_now("%d") %}
{%- set yesterday_month = strftime_now("%m") %}
{%- set yesterday_year = strftime_now("%Y") %}
{%- set today_date = yesterday_year + '-' + yesterday_month + '-' + yesterday_day %}
{%- if yesterday_day == '01' %}
    {#- Jinja doesnt allow minus 1 date - Unsloth alternative #}
    {%- if yesterday_month == '01' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '12' %}
        {%- if yesterday_year == '2024' %}
            {%- set yesterday_year = '2023' %}
        {%- elif yesterday_year == '2025' %}
            {%- set yesterday_year = '2024' %}
        {%- elif yesterday_year == '2026' %}
            {%- set yesterday_year = '2025' %}
        {%- elif yesterday_year == '2027' %}
            {%- set yesterday_year = '2026' %}
        {%- elif yesterday_year == '2028' %}
            {%- set yesterday_year = '2027' %}
        {%- elif yesterday_year == '2029' %}
            {%- set yesterday_year = '2028' %}
        {%- elif yesterday_year == '2030' %}
            {%- set yesterday_year = '2029' %}
        {%- elif yesterday_year == '2031' %}
            {%- set yesterday_year = '2030' %}
        {%- elif yesterday_year == '2032' %}
            {%- set yesterday_year = '2031' %}
        {%- elif yesterday_year == '1970' %}
            {#- Stop llama_cpp from erroring out #}
            {%- set yesterday_year = '1970' %}
        {%- else %}
            {{- raise_exception('Unsloth custom template does not support years > 2032. Error year = [' + yesterday_year + ']') }}
        {%- endif %}
    {%- elif yesterday_month == '02' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '01' %}
    {%- elif yesterday_month == '03' %}
        {%- set yesterday_month = '02' %}
        {%- set yesterday_day = '28' %}
        {%- if yesterday_year == '2024' %}
            {%- set yesterday_day = '29' %}
        {%- elif yesterday_year == '2028' %}
            {%- set yesterday_day = '29' %}
        {%- elif yesterday_year == '2032' %}
            {%- set yesterday_day = '29' %}
        {%- elif yesterday_year == '1970' %}
            {#- Stop llama_cpp from erroring out #}
            {%- set yesterday_day = '29' %}
        {%- else %}
            {{- raise_exception('Unsloth custom template does not support years > 2032. Error year = [' + yesterday_year + ']') }}
        {%- endif %}
    {%- elif yesterday_month == '04' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '03' %}
    {%- elif yesterday_month == '05' %}
        {%- set yesterday_day = '30' %}
        {%- set yesterday_month = '04' %}
    {%- elif yesterday_month == '06' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '05' %}
    {%- elif yesterday_month == '07' %}
        {%- set yesterday_day = '30' %}
        {%- set yesterday_month = '06' %}
    {%- elif yesterday_month == '08' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '07' %}
    {%- elif yesterday_month == '09' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '08' %}
    {%- elif yesterday_month == '10' %}
        {%- set yesterday_day = '30' %}
        {%- set yesterday_month = '09' %}
    {%- elif yesterday_month == '11' %}
        {%- set yesterday_day = '31' %}
        {%- set yesterday_month = '10' %}
    {%- elif yesterday_month == '12' %}
        {%- set yesterday_day = '30' %}
        {%- set yesterday_month = '11' %}
    {%- endif %}
{%- elif yesterday_day == '02' %}
    {%- set yesterday_day = '01' %}
{%- elif yesterday_day == '03' %}
    {%- set yesterday_day = '02' %}
{%- elif yesterday_day == '04' %}
    {%- set yesterday_day = '03' %}
{%- elif yesterday_day == '05' %}
    {%- set yesterday_day = '04' %}
{%- elif yesterday_day == '06' %}
    {%- set yesterday_day = '05' %}
{%- elif yesterday_day == '07' %}
    {%- set yesterday_day = '06' %}
{%- elif yesterday_day == '08' %}
    {%- set yesterday_day = '07' %}
{%- elif yesterday_day == '09' %}
    {%- set yesterday_day = '08' %}
{%- elif yesterday_day == '10' %}
    {%- set yesterday_day = '09' %}
{%- elif yesterday_day == '11' %}
    {%- set yesterday_day = '10' %}
{%- elif yesterday_day == '12' %}
    {%- set yesterday_day = '11' %}
{%- elif yesterday_day == '13' %}
    {%- set yesterday_day = '12' %}
{%- elif yesterday_day == '14' %}
    {%- set yesterday_day = '13' %}
{%- elif yesterday_day == '15' %}
    {%- set yesterday_day = '14' %}
{%- elif yesterday_day == '16' %}
    {%- set yesterday_day = '15' %}
{%- elif yesterday_day == '17' %}
    {%- set yesterday_day = '16' %}
{%- elif yesterday_day == '18' %}
    {%- set yesterday_day = '17' %}
{%- elif yesterday_day == '19' %}
    {%- set yesterday_day = '18' %}
{%- elif yesterday_day == '20' %}
    {%- set yesterday_day = '19' %}
{%- elif yesterday_day == '21' %}
    {%- set yesterday_day = '20' %}
{%- elif yesterday_day == '22' %}
    {%- set yesterday_day = '21' %}
{%- elif yesterday_day == '23' %}
    {%- set yesterday_day = '22' %}
{%- elif yesterday_day == '24' %}
    {%- set yesterday_day = '23' %}
{%- elif yesterday_day == '25' %}
    {%- set yesterday_day = '24' %}
{%- elif yesterday_day == '26' %}
    {%- set yesterday_day = '25' %}
{%- elif yesterday_day == '27' %}
    {%- set yesterday_day = '26' %}
{%- elif yesterday_day == '28' %}
    {%- set yesterday_day = '27' %}
{%- elif yesterday_day == '29' %}
    {%- set yesterday_day = '28' %}
{%- elif yesterday_day == '30' %}
    {%- set yesterday_day = '29' %}
{%- elif yesterday_day == '31' %}
    {%- set yesterday_day = '30' %}
{%- endif %}
{#- Edits made by Unsloth #}
{%- set yesterday_date = yesterday_year + '-' + yesterday_month + '-' + yesterday_day %}
{%- set default_system_message = "You are Mistral-Small-3.2-24B-Instruct-2506, a Large Language Model (LLM) created by Mistral AI, a French startup headquartered in Paris.\nYou power an AI assistant called Le Chat.\nYour knowledge base was last updated on 2023-10-01.\nThe current date is " + today_date + ".\n\nWhen you\'re not sure about some information or when the user\'s request requires up-to-date or specific data, you must use the available tools to fetch the information. Do not hesitate to use tools whenever they can provide a more accurate or complete response. If no relevant tools are available, then clearly state that you don\'t have the information and avoid making up anything.\nIf the user\'s question is not clear, ambiguous, or does not provide enough context for you to accurately answer the question, you do not try to answer it right away and you rather ask the user to clarify their request (e.g. \"What are some good restaurants around me?\" => \"Where are you?\" or \"When is the next flight to Tokyo\" => \"Where do you travel from?\").\nYou are always very attentive to dates, in particular you try to resolve dates (e.g. \"yesterday\" is " + yesterday_date + ") and when asked about information at specific dates, you discard information that is at another date.\nYou follow these instructions in all languages, and always respond to the user in the language they use or request.\nNext sections describe the capabilities that you have.\n\n# WEB BROWSING INSTRUCTIONS\n\nYou cannot perform any web search or access internet to open URLs, links etc. If it seems like the user is expecting you to do so, you clarify the situation and ask the user to copy paste the text directly in the chat.\n\n# MULTI-MODAL INSTRUCTIONS\n\nYou have the ability to read images, but you cannot generate images. You also cannot transcribe audio files or videos.\nYou cannot read nor transcribe audio files or videos.\n\n# TOOL CALLING INSTRUCTIONS\n\nYou may have access to tools that you can use to fetch information or perform actions. You must use these tools in the following situations:\n\n1. When the request requires up-to-date information.\n2. When the request requires specific data that you do not have in your knowledge base.\n3. When the request involves actions that you cannot perform without tools.\n\nAlways prioritize using tools to provide the most accurate and helpful response. If tools are not available, inform the user that you cannot perform the requested action at the moment." %}

{{- bos_token }}

{%- if messages[0]['role'] == 'system' %}
    {%- if messages[0]['content'] is string %}
        {%- set system_message = messages[0]['content'] %}
    {%- elif messages[0]['content'] is iterable %}
        {%- set system_message = messages[0]['content'][0]['text'] %}
    {%- else %}
        {%- set system_message = messages[0]['content']|string %}
    {%- endif %}
    {%- set loop_messages = messages[1:] %}
{%- else %}
    {%- set system_message = default_system_message %}
    {%- set loop_messages = messages %}
{%- endif %}
{{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }}


{#- Tool description appended ONLY to last user message. Edits made by Unsloth #}
{#- Tool description appended also if last message is tool. Edits made by Unsloth #}
{%- set tools_description = "" %}
{%- set has_tools = false %}
{#- Cannot use set append_tools_index = loop.index0 since temporary variable - must use namespace #}
{%- set ns = namespace(append_tools_index=0, append_tools=false) %}

{%- if tools is defined and tools is not none and tools|length > 0 %}

    {%- set has_tools = true %}
    {%- set tools_description = "[AVAILABLE_TOOLS]" + (tools | tojson) + "[/AVAILABLE_TOOLS]" %}

    {#- If User,Assistant,Tool,Tool we also need to append tools_description to  last assistant WITHOUT tool_calls defined. Edits made by Unsloth #}
    {%- if (loop_messages|last)['role'] == 'tool' %}

        {#- Find last assistant WITHOUT tool_calls defined #}
        {%- set ns.append_tools = true %}
        {%- for message in loop_messages %}
            {%- if message['role'] == 'assistant' %}

                {#- Cannot use set append_tools_index = loop.index0 since temporary variable - must use namespace #}

                {%- if message['tool_calls'] is not defined or message['tool_calls'] is none %}
                    {%- set ns.append_tools_index = loop.index0 %}
                {%- endif %}

            {%- endif %}
        {%- endfor %}

    {%- endif %}

{%- endif %}

{%- for message in loop_messages %}
    {%- if message['role'] == 'user' %}

        {%- if has_tools and loop.last %}
            {{- tools_description }}
        {%- endif %}

        {#- If directly called tools in first turn, prepend to user #}
        {%- if ns.append_tools and ns.append_tools_index == 0 %}
            {{- tools_description }}
        {%- endif %}

        {%- if message['content'] is string %}
            {{- '
' + message['content'] + '
' }}
        {%- else %}
            {{- '
' }}
            {%- for block in message['content'] %}
                {%- if block['type'] == 'text' %}

                    {#- Original did not have content which is weird. Added by Un-sloth. #}
                    {%- if block['text'] is defined %}
                        {{- block['text'] }}
                    {%- else %}
                        {{- block['content'] }}
                    {%- endif %}

                {%- elif block['type'] in ['image', 'image_url'] %}
                    {{- '[IMG]' }}
                {%- else %}
                    {{- raise_exception('Only text and image blocks are supported in message content!') }}
                {%- endif %}
            {%- endfor %}
            {{- '
' }}
        {%- endif %}

    {%- elif message['role'] == 'system' %}
        {%- if message['content'] is string %}
            {{- '[SYSTEM_PROMPT]' + message['content'] + '[/SYSTEM_PROMPT]' }}
        {%- elif message['content'] is iterable %}
            {{- '[SYSTEM_PROMPT]' + message['content'][0]['text'] + '[/SYSTEM_PROMPT]' }}
        {%- else %}
            {{- '[SYSTEM_PROMPT]' + message['content']|string + '[/SYSTEM_PROMPT]' }}
        {%- endif %}
        

    {%- elif message['role'] == 'assistant' %}
        {%- if message['content'] is string %}
            {{- message['content'] }}
        {%- elif message['content'] is iterable %}
            {{- message['content'][0]['text'] }}
        {%- else %}
            {{- message['content']|string }}
        {%- endif %}

        {#- If User,Assistant,Tool,Tool we also need to append tools_description. Edits made by Unsloth #}

        {%- if has_tools and (loop.index0 == ns.append_tools_index) %}
            {{- eos_token }}
            {{- tools_description }}
        {%- endif %}

        {%- if message['tool_calls'] is defined and message['tool_calls'] is not none %}
            {%- for tool in message['tool_calls'] %}
                {%- if tool['id'] is not defined %}
                    {{- raise_exception('Tool ID must be provided!') }}
                {%- endif %}
                {%- set tool_call_id = tool['id'] %}
                {%- if tool_call_id is not string or tool_call_id|length < 9 %}
                    {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!") }}
                {%- endif %}
                {%- set arguments = tool['function']['arguments'] %}
                {%- if arguments is not string %}
                    {%- set arguments = arguments|tojson %}
                {%- endif %}
                {#- Must list tool calls AFTER assistant. Edits made by Un-sloth #}
                {{- "[TOOL_CALLS]" + tool['function']['name'] + "[CALL_ID]" + tool_call_id + "[ARGS]" + arguments }}
            {%- endfor %}
        {%- endif %}

        {#- Must not add EOS if added tools_description. Unsloth edits. #}
        {%- if (loop.index0 != ns.append_tools_index) %}
            {{- eos_token }}
        {%- endif %}

    {%- elif message["role"] == "tool_results" or message["role"] == "tool" %}
        {%- if message.content is defined and message.content.content is defined %}
            {%- set content = message.content.content %}
        {%- else %}
            {%- set content = message.content %}
        {%- endif %}
        {%- if message['tool_call_id'] is not defined %}
            {{- raise_exception('tool_call_id must be provided!') }}
        {%- endif %}
        {%- set tool_call_id = message['tool_call_id'] %}
        {%- if tool_call_id is not string or tool_call_id|length < 9 %}
            {{- raise_exception("Tool call IDs should be alphanumeric strings with length >= 9!") }}
        {%- endif %}
        {{- "[TOOL_RESULTS]" + tool_call_id + "[TOOL_CONTENT]" + content|string + "[/TOOL_RESULTS]" }}

    {%- else %}
        {{- raise_exception('Only user, systemm assistant and tool roles are supported in the custom template made by Unsloth!') }}
    {%- endif %}
{%- endfor %}
{#- Copyright 2025-present Unsloth. Apache 2.0 License. #}