{% set image_placeholder = '<|vision_start|><|image_pad|><|vision_end|>' %} {% for message in messages %} {#--- Handle User Messages with Template and Examples ---#} {%- if message['role'] == 'user' and template -%} {% if loop.first and message['role'] != 'system' %} {{- '<|im_start|>system You are NuExtract, an information extraction tool created by NuMind.<|im_end|>' }} {% endif %} {{- '<|im_start|>' + message['role'] -}} {#--- Template Section ---#} {{ ' # Template:' }} {{- ' ' + template + ' ' }} {#--- Examples Section (if provided) ---#} {% if examples -%} {{- '# Examples:' }} {% for example in examples %} {{- '## Input: ' }} {#--- Handle image examples ---#} {% if example['input'] is mapping and example['input']['type'] == 'image' %} {{- image_placeholder | trim -}} {% elif example['input'] == '' %} {{- image_placeholder | trim -}} {% else %} {{- example['input'] -}} {% endif %} {{- ' ## Output: ' ~ example['output'] }} {% endfor %} {%- endif %} {#--- Context Section: Handle various content types ---#} {{- '# Context: ' }} {%- if message['content'] is string -%} {#--- Simple string content ---#} {{- message['content'] | trim -}} {%- elif message['content'] is mapping and message['content']['type'] == 'image' -%} {#--- Single image document ---#} {{- image_placeholder | trim -}} {%- else -%} {#--- List of content items (mixed text/images) ---#} {#--- First, determine what the actual input content is (not ICL images) ---#} {%- set ns = namespace(has_text_input=false, text_content='') -%} {#--- Count content types and identify actual input document ---#} {%- for content in message['content'] -%} {%- if content is mapping and content.get('type') == 'text' -%} {%- if content.get('text') != '' -%} {%- set ns.has_text_input = true -%} {%- set ns.text_content = content['text'] -%} {%- endif -%} {%- elif content is string -%} {%- if content != '' -%} {%- set ns.has_text_input = true -%} {%- set ns.text_content = content -%} {%- endif -%} {%- endif -%} {%- endfor -%} {#--- Determine what to output based on actual input type ---#} {%- if ns.has_text_input -%} {#--- Main input is text, so output the text content ---#} {{- ns.text_content | trim -}} {%- else -%} {#--- Main input is image or placeholder ---#} {%- set ns2 = namespace(found_image=false) -%} {%- for content in message['content'] -%} {%- if content is mapping and content.get('type') == 'image' and not ns2.found_image -%} {{- image_placeholder | trim -}} {%- set ns2.found_image = true -%} {%- elif content is mapping and content.get('type') == 'text' and content.get('text') == '' and not ns2.found_image -%} {{- image_placeholder | trim -}} {%- set ns2.found_image = true -%} {%- elif content is string and content == '' and not ns2.found_image -%} {{- image_placeholder | trim -}} {%- set ns2.found_image = true -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- endif -%} {{- '<|im_end|> '}} {#--- Handle All Other Messages (Assistant, System, etc.) ---#} {% else %} {% if loop.first and message['role'] != 'system' %} {{- '<|im_start|>system You are a helpful assistant.<|im_end|>' }} {% endif %} {{- '<|im_start|>' + message['role'] + ' ' }} {#--- Same content handling logic as above but without template/examples ---#} {%- if message['content'] is string -%} {{- message['content'] | trim }} {%- elif message['content'] is mapping and message['content']['type'] == 'image' -%} {{- image_placeholder | trim }} {%- else -%} {%- for content in message['content'] -%} {%- if content is string -%} {{- content | trim -}} {%- elif content is mapping and content.get('type') == 'text' and content.get('text') == '' -%} {{- image_placeholder | trim }} {%- elif content is mapping and content.get('type') == 'text' -%} {{- content['text'] | trim -}} {%- elif content is mapping and content.get('type') == 'image' -%} {# Skip adding image placeholder - it's already in the text #} {%- endif -%} {%- endfor -%} {%- endif -%} {{- '<|im_end|>'}} {% endif %} {% endfor -%} {#--- Add Generation Prompt if Requested ---#} {%- if add_generation_prompt %} {{- '<|im_start|>assistant' }} {% endif -%}