| {%- set image_placeholder = '<|vision_start|><|image_pad|><|vision_end|>' -%} |
| {%- for message in messages -%} |
| {%- if message['role'] == 'system' -%} |
| {{- '<|im_start|>system |
| ' -}} |
| {%- if message['content'] is string -%} |
| {{- message['content'] | trim -}} |
| {%- endif -%} |
| {{- '<|im_end|> |
| ' -}} |
| {%- elif message['role'] == 'user' -%} |
| {%- if loop.first -%} |
| {{- '<|im_start|>system |
| ' -}} |
| {%- if template -%} |
| |
| {{- 'You are NuExtract, an information extraction tool created by NuMind.' -}} |
| {%- else -%} |
| |
| {{- 'You are a helpful assistant.' -}} |
| {%- endif -%} |
| {{ '<|im_end|> |
| ' }} |
| {%- endif -%} |
| {{- '<|im_start|>' + message['role'] + ' |
| ' -}} |
| {%- if template -%} |
| |
| {{- '# Template: |
| ' -}} |
| {{- template -}} |
| {{- ' |
| ' -}} |
| |
| {%- if examples -%} |
| |
| {{- '# Examples: |
| ' -}} |
| {%- for example in examples -%} |
| {{- '## Input: |
| ' -}} |
| {%- if example['input'] is mapping and (example['input']['type'] == 'image' or example['input']['type'] == 'image_url') -%} |
| {{- image_placeholder | trim -}} |
| {%- elif example['input'] == '<image>' -%} |
| |
| {{- image_placeholder | trim -}} |
| {%- else -%} |
| |
| {{- example['input'] -}} |
| {%- endif -%} |
| {{- ' |
| ' -}} |
| {{- '## Output: |
| ' -}} |
| {{- example['output'] -}} |
| {{- ' |
| ' -}} |
| {%- endfor -%} |
| {%- endif -%} |
| {{- '# Context: |
| ' -}} |
| {%- endif -%} |
| |
| {%- if message['content'] is string -%} |
| |
| {{- message['content'] | trim -}} |
| {%- elif message['content'] is mapping and (message['content']['type'] == 'image' or message['content']['type'] == 'image_url') -%} |
| {{- image_placeholder | trim -}} |
| {%- else -%} |
| |
| |
| {%- set ns = namespace(has_text_input=false, text_content='') -%} |
| |
| |
| {%- for content in message['content'] -%} |
| {%- if content is mapping and content.get('type') == 'text' -%} |
| {%- if content.get('text') != '<image>' -%} |
| |
| {%- set ns.has_text_input = true -%} |
| {%- set ns.text_content = content['text'] -%} |
| {%- endif -%} |
| {%- elif content is string -%} |
| {%- if content != '<image>' -%} |
| |
| {%- set ns.has_text_input = true -%} |
| {%- set ns.text_content = content -%} |
| {%- endif -%} |
| {%- endif -%} |
| {%- endfor -%} |
| |
| |
| {%- if ns.has_text_input -%} |
| |
| {{- ns.text_content | trim -}} |
| {%- else -%} |
| |
| {%- set ns2 = namespace(found_image=false) -%} |
| {%- for content in message['content'] -%} |
| {%- if content is mapping and (content.get('type') == 'image' or content.get('type') == 'image_url') 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') == '<image>' and not ns2.found_image -%} |
| |
| {{- image_placeholder | trim -}} |
| {%- set ns2.found_image = true -%} |
| {%- elif content is string and content == '<image>' and not ns2.found_image -%} |
| |
| {{- image_placeholder | trim -}} |
| {%- set ns2.found_image = true -%} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- endif -%} |
| {%- endif -%} |
| {{- '<|im_end|> |
| '}} |
| |
| {%- elif message['role'] == 'assistant' -%} |
| {{- '<|im_start|>assistant |
| ' -}} |
| {%- if message['content'] is string -%} |
| {{- message['content'] | trim -}} |
| {%- elif message['content'] is iterable and message['content'] is not string -%} |
| {%- for content in message['content'] -%} |
| {%- if content is mapping and content.get('type') == 'text' -%} |
| {{- content['text'] | trim -}} |
| {%- elif content is string -%} |
| {{- content | trim -}} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- endif -%} |
| {{- '<|im_end|> |
| ' -}} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- if add_generation_prompt -%} |
| {{- '<|im_start|>assistant |
| ' -}} |
| {%- endif -%} |