{{ 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 and 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 -%} {{ '' + 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'] -%} {{ '\n' + (message['thought'] | trim) + '\n\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' -%} {{ '' }} {%- elif item['type'] == 'text' -%} {{ item['text'] | trim }} {%- endif -%} {%- endfor -%} {%- endif -%} {{ '\n' }} {%- endfor -%} {%- if add_generation_prompt -%} {{ 'model\n\n' }} {%- endif -%}