| {%- set today = strftime_now("%Y-%m-%d") %} | |
| {%- set default_think_system_message = "You are a helpful assistant. Think deeply before answering the user's question. Do the thinking inside <think>...</think> tags." %} | |
| {%- set default_nothink_system_message = "You are a helpful assistant." %} | |
| {%- set think_system_message_addition = "Think deeply before answering the user's question. Do the thinking inside <think>...</think> tags." %} | |
| {{- bos_token }} | |
| {%- if messages[0].role == 'system' %} | |
| {%- if enable_thinking is defined and enable_thinking is false %} | |
| {%- set system_message = messages[0].content %} | |
| {%- else %} | |
| {%- set system_message = think_system_message_addition +'\n\n'+ messages[0].content %} | |
| {%- endif %} | |
| {%- set loop_messages = messages[1:] %} | |
| {%- else %} | |
| {%- if enable_thinking is defined and enable_thinking is false %} | |
| {%- set system_message = default_nothink_system_message %} | |
| {%- else %} | |
| {%- set system_message = default_think_system_message %} | |
| {%- endif %} | |
| {%- set loop_messages = messages %} | |
| {%- endif %} | |
| {{- '[SYSTEM_PROMPT]' + system_message + '[/SYSTEM_PROMPT]' }} | |
| {%- for message in loop_messages %} | |
| {%- if message.role == 'user' %} | |
| {%- if loop.index0 % 2 == 1 %} | |
| {{- raise_exception('User and assistant turns must alternate starting with user turn!') }} | |
| {%- endif %} | |
| {%- if loop.last and not (enable_thinking is defined and enable_thinking is false) %} | |
| {{- '[INST]' + message.content + '[/INST]<think>\n' }} | |
| {%- else %} | |
| {{- '[INST]' + message.content + '[/INST]' }} | |
| {%- endif %} | |
| {%- elif message.role == 'system' %} | |
| {{- raise_exception('System message can only be the first message!') }} | |
| {%- elif message.role == 'assistant' %} | |
| {%- if loop.index0 % 2 == 0 %} | |
| {{- raise_exception('User and assistant turns must alternate starting with user turn!') }} | |
| {%- endif %} | |
| {%- set content = message.content %} | |
| {%- set reasoning_content = '' %} | |
| {%- if message.reasoning_content is defined and message.reasoning_content is not none %} | |
| {%- set reasoning_content = message.reasoning_content %} | |
| {%- else %} | |
| {%- if '</think>' in message.content %} | |
| {%- set content = message.content.split('</think>')[-1].lstrip('\n') %} | |
| {%- set reasoning_content = message.content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %} | |
| {%- endif %} | |
| {%- endif %} | |
| {%- if loop.last and reasoning_content and not (enable_thinking is defined and enable_thinking is false) %} | |
| {{- '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') + eos_token }} | |
| {%- else %} | |
| {{- content + eos_token }} | |
| {%- endif %} | |
| {%- else %} | |
| {{- raise_exception('Only user, system and assistant roles are supported!') }} | |
| {%- endif %} | |
| {%- endfor %} |