DavidAU's picture
Upload 2 files
af192a0 verified
{{ bos_token }}
{# Define the hardcoded thinking instructions #}
{%- set system_instruction = "Think deeply and carefully about the user's request. Compose your thoughts about the user's prompt between <think> and </think> tags, then output the final answer based on your thoughts." -%}
{%- if messages[0]['role'] == 'system' -%}
{# If the user provided a system prompt, prepend our instruction to it #}
{%- if messages[0]['content'] is string -%}
{%- set first_user_prefix = system_instruction + '\n\n' + messages[0]['content'] + '\n\n' -%}
{%- else -%}
{%- set first_user_prefix = system_instruction + '\n\n' + messages[0]['content'][0]['text'] + '\n\n' -%}
{%- endif -%}
{%- set loop_messages = messages[1:] -%}
{%- else -%}
{# If no system prompt exists, just use our instruction #}
{%- set first_user_prefix = system_instruction + '\n\n' -%}
{%- set loop_messages = messages -%}
{%- endif -%}
{%- for message in loop_messages -%}
{%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
{{ raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
{%- endif -%}
{%- if (message['role'] == 'assistant') -%}
{%- set role = "model" -%}
{%- else -%}
{%- set role = message['role'] -%}
{%- endif -%}
{{ '<start_of_turn>' + role + '\n' }}
{# Inject the system prefix only into the very first turn #}
{%- if loop.first -%}
{{ first_user_prefix }}
{%- endif -%}
{# Check for a 'thought' key to wrap in tags if it exists in history #}
{%- if message['thought'] is defined and message['thought'] -%}
{{ '<think>\n' + (message['thought'] | trim) + '\n</think>\n' }}
{%- endif -%}
{# Render message content #}
{%- if message['content'] is string -%}
{{ message['content'] | trim }}
{%- elif message['content'] is iterable -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'image' -%}
{{ '<image>' }}
{%- elif item['type'] == 'text' -%}
{{ item['text'] | trim }}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{{ '<end_of_turn>\n' }}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{ '<start_of_turn>model\n<think>\n' }}
{%- endif -%}