{% set role_prefix = '<|start_header_id|>' %}
{% set role_suffix = '<|end_header_id|>\n\n' %}
{% set turn_suffix = '<|eot_id|>' %}
{% set image_token = '<|image|>' %}

{{ bos_token }}
{%- for message in messages -%}
    {{ role_prefix + message['role'] + role_suffix }}

    {%- if message['content'] is string -%}
        {{ message['content'] | trim }}
    {%- elif message['content'] is iterable -%}
        {%- for item in message['content'] -%}
            {%- if item['type'] == 'text' -%}
                {{  (item['text'] if 'text' in item else item['content']) | trim }}
            {%- elif item['type'].startswith('image') -%}
                {{  image_token }}
            {%- endif -%}
        {%- endfor -%}
    {%- endif -%}
    {{ turn_suffix }}
{%- endfor -%}

{%- if add_generation_prompt -%}
    {{ role_prefix + 'assistant' + role_suffix }}
{%- endif -%}
