{%- set image_count = namespace(value=0) -%} {%- set video_count = namespace(value=0) -%} {{- '<|begin_of_sentence|>' }} {%- for message in messages -%} {%- if message.role in ['system', 'user'] -%} {%- if message.role == 'user' -%} {{- 'User: ' -}} {%- endif -%} {%- if message.content is string -%} {{- message.content -}} {%- else -%} {%- for content_item in message.content -%} {%- if content_item.type == 'text' -%} {{- content_item.text -}} {%- elif content_item.type in ['image_url', 'image'] -%} {%- set image_count.value = image_count.value + 1 -%} Picture {{ image_count.value }}:<|IMAGE_START|><|image@placeholder|><|IMAGE_END|> {%- elif content_item.type in ['video_url', 'video'] -%} {%- set video_count.value = video_count.value + 1 -%} Video {{ video_count.value }}:<|VIDEO_START|><|video@placeholder|><|VIDEO_END|> {%- endif -%} {%- endfor -%} {%- endif -%} {%- if message.role == 'system' -%} {{- ' ' -}} {%- endif -%} {%- elif message.role == 'assistant' -%} {%- macro extract_text_content(content_field) -%} {%- if content_field is string -%} {{- content_field -}} {%- elif content_field is iterable and content_field is not string -%} {%- set ns = namespace(text_parts=[]) -%} {%- set text_parts = [] -%} {%- for item in content_field -%} {%- if item.type == 'text' -%} {%- set ns.text_parts = ns.text_parts + [item.text] -%} {%- endif -%} {%- endfor -%} {{- ns.text_parts | join('') -}} {%- else -%} {{- '' -}} {%- endif -%} {%- endmacro -%} {%- set reasoning_content = extract_text_content(message.reasoning_content) -%} {%- set content = extract_text_content(message.content) -%} {%- if '' in content %} {%- set reasoning_content = content.split('')[0].rstrip(' ').split('')[-1].lstrip(' ') %} {%- set content = content.split('')[-1].lstrip(' ') %} {%- endif %} {%- if reasoning_content %} {{- ' ' + 'Assistant: ' + ' ' + reasoning_content.strip(' ') + ' ' + content.lstrip(' ') }} {%- else %} {{- ' ' + 'Assistant: ' + content }} {%- endif %} {{- '<|end_of_sentence|>' }} {%- endif -%} {%- endfor -%} {%- if add_generation_prompt is not defined or add_generation_prompt is true %} {{- ' Assistant: ' -}} {%- if enable_thinking is defined and enable_thinking is false %} {{- ' ' }} {%- endif %} {%- if enable_thinking is not defined or enable_thinking is true %} {{- '' }} {%- endif %} {%- endif %}