Archived
106 lines
4.5 KiB
Django/Jinja
106 lines
4.5 KiB
Django/Jinja
{%- if messages | length == 2 and messages[0]['role'] == 'query' and messages[1]['role'] == 'document' -%}
|
||
{# Render the checkpoint's query/document training format without BOS/EOS. #}
|
||
{%- macro text_content(content) -%}
|
||
{%- if content is string -%}
|
||
{{- content | replace('[', '[') | replace('<', '<') -}}
|
||
{%- else -%}
|
||
{%- set ns = namespace(first=true) -%}
|
||
{%- for block in content -%}
|
||
{%- if block['type'] == 'text' -%}
|
||
{%- if not ns.first -%}{{- '\n\n' -}}{%- endif -%}
|
||
{{- block['text'] | replace('[', '[') | replace('<', '<') -}}
|
||
{%- set ns.first = false -%}
|
||
{%- elif block['type'] not in ['image', 'image_url'] -%}
|
||
{{- raise_exception('Only text and image documents are supported') -}}
|
||
{%- endif -%}
|
||
{%- endfor -%}
|
||
{%- endif -%}
|
||
{%- endmacro -%}
|
||
{%- if messages | length != 2 or messages[0]['role'] != 'query' or messages[1]['role'] != 'document' -%}
|
||
{{- raise_exception('Reranking requires a query followed by a document') -}}
|
||
{%- endif -%}
|
||
{%- set ns = namespace(images=0) -%}
|
||
{%- if messages[0]['content'] is not string -%}
|
||
{%- for block in messages[0]['content'] -%}
|
||
{%- if block['type'] != 'text' -%}{{- raise_exception('Queries must be text') -}}{%- endif -%}
|
||
{%- endfor -%}
|
||
{%- endif -%}
|
||
{%- if messages[1]['content'] is not string -%}
|
||
{%- for block in messages[1]['content'] -%}
|
||
{%- if block['type'] in ['image', 'image_url'] -%}{%- set ns.images = ns.images + 1 -%}{%- endif -%}
|
||
{%- endfor -%}
|
||
{%- endif -%}
|
||
{%- if ns.images > 1 -%}{{- raise_exception('At most one image per document is supported') -}}{%- endif -%}
|
||
{%- set rendered -%}
|
||
{%- if ns.images -%}{{- '[IMG] ' -}}{%- endif -%}
|
||
{{- 'query: ' -}}{{- text_content(messages[0]['content']) -}}
|
||
{{- '\n\npassage: ' -}}{{- text_content(messages[1]['content']) -}}
|
||
{%- endset -%}
|
||
{{- rendered | trim if ns.images else rendered -}}
|
||
|
||
{%- else -%}
|
||
{# nemo-mistral-retrieval-v1
|
||
Derived from the Mistral Ministral-3 instruct chat template render_content
|
||
macro and its image-before-text canonicalization. Retrieval adaptation:
|
||
system content is the retrieval prefix; generation wrappers and BOS/EOS are
|
||
deliberately omitted so message inference matches training token for token.
|
||
#}
|
||
{%- macro mark_user_text(text) -%}
|
||
{{- text | replace("[", "[") | replace("<", "<") -}}
|
||
{%- endmacro -%}
|
||
{%- macro render_text_content(content, context_name) -%}
|
||
{%- if content is string -%}
|
||
{{- content -}}
|
||
{%- elif content is sequence -%}
|
||
{%- set ns = namespace(has_output=false) -%}
|
||
{%- for block in content -%}
|
||
{%- if block["type"] == "text" -%}
|
||
{%- if ns.has_output %}{{ "\n\n" }}{% endif -%}
|
||
{{- block["text"] -}}
|
||
{%- set ns.has_output = true -%}
|
||
{%- else -%}
|
||
{{- raise_exception(context_name + " may contain only text blocks") -}}
|
||
{%- endif -%}
|
||
{%- endfor -%}
|
||
{%- else -%}
|
||
{{- raise_exception(context_name + " must be a string or a list of text blocks") -}}
|
||
{%- endif -%}
|
||
{%- endmacro -%}
|
||
{%- macro render_user_content(content) -%}
|
||
{%- if content is string -%}
|
||
{{- mark_user_text(content) -}}
|
||
{%- elif content is sequence -%}
|
||
{%- set ns = namespace(has_output=false) -%}
|
||
{%- for block in content -%}
|
||
{%- if block["type"] in ["image", "image_url"] -%}
|
||
{%- if ns.has_output %}{{ " " }}{% endif -%}
|
||
{{- "[IMG]" -}}
|
||
{%- set ns.has_output = true -%}
|
||
{%- endif -%}
|
||
{%- endfor -%}
|
||
{%- for block in content -%}
|
||
{%- if block["type"] == "text" -%}
|
||
{%- if ns.has_output %}{{ " " }}{% endif -%}
|
||
{{- mark_user_text(block["text"]) -}}
|
||
{%- set ns.has_output = true -%}
|
||
{%- elif block["type"] not in ["image", "image_url"] -%}
|
||
{{- raise_exception("User content supports only text and image blocks") -}}
|
||
{%- endif -%}
|
||
{%- endfor -%}
|
||
{%- else -%}
|
||
{{- raise_exception("User content must be a string or a list of text and image blocks") -}}
|
||
{%- endif -%}
|
||
{%- endmacro -%}
|
||
{%- if messages | length == 1 and messages[0]["role"] == "user" -%}
|
||
{{- render_user_content(messages[0]["content"]) -}}
|
||
{%- elif messages | length == 2
|
||
and messages[0]["role"] == "system"
|
||
and messages[1]["role"] == "user" -%}
|
||
{{- render_text_content(messages[0]["content"], "System content") -}}
|
||
{{- " " -}}
|
||
{{- render_user_content(messages[1]["content"]) -}}
|
||
{%- else -%}
|
||
{{- raise_exception("Retrieval inputs require one user message with an optional leading system prefix") -}}
|
||
{%- endif -%}
|
||
{%- endif -%}
|