|
|
{% set image_placeholder = '<|vision_start|><|image_pad|><|vision_end|>' %} |
|
|
{% for message in messages %} |
|
|
|
|
|
{%- 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:' }} |
|
|
{{- ' |
|
|
' + template + ' |
|
|
' }} |
|
|
|
|
|
|
|
|
{% if examples -%} |
|
|
{{- '# Examples:' }} |
|
|
{% for example in examples %} |
|
|
{{- '## Input: |
|
|
' }} |
|
|
|
|
|
{% if example['input'] is mapping and example['input']['type'] == 'image' %} |
|
|
{{- image_placeholder | trim -}} |
|
|
{% elif example['input'] == '<image>' %} |
|
|
{{- image_placeholder | trim -}} |
|
|
{% else %} |
|
|
{{- example['input'] -}} |
|
|
{% endif %} |
|
|
{{- ' |
|
|
## Output: |
|
|
' ~ example['output'] }} |
|
|
{% endfor %} |
|
|
{%- endif %} |
|
|
|
|
|
|
|
|
{{- '# Context: |
|
|
' }} |
|
|
{%- if message['content'] is string -%} |
|
|
|
|
|
{{- message['content'] | trim -}} |
|
|
{%- elif message['content'] is mapping and message['content']['type'] == 'image' -%} |
|
|
|
|
|
{{- 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' 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|> |
|
|
'}} |
|
|
|
|
|
|
|
|
{% else %} |
|
|
{% if loop.first and message['role'] != 'system' %} |
|
|
{{- '<|im_start|>system |
|
|
You are a helpful assistant.<|im_end|>' }} |
|
|
{% endif %} |
|
|
|
|
|
{{- '<|im_start|>' + message['role'] + ' |
|
|
' }} |
|
|
|
|
|
|
|
|
{%- 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>' -%} |
|
|
{{- image_placeholder | trim }} |
|
|
{%- elif content is mapping and content.get('type') == 'text' -%} |
|
|
{{- content['text'] | trim -}} |
|
|
{%- elif content is mapping and content.get('type') == 'image' -%} |
|
|
|
|
|
{%- endif -%} |
|
|
{%- endfor -%} |
|
|
{%- endif -%} |
|
|
{{- '<|im_end|>'}} |
|
|
{% endif %} |
|
|
{% endfor -%} |
|
|
|
|
|
{%- if add_generation_prompt %} |
|
|
{{- '<|im_start|>assistant' }} |
|
|
{% endif -%} |