|
"template": "{%- set _mode = mode | default('generate', true) -%}\n{%- set stop_token = '<|eot_id|>' -%}\n{%- set message_roles = ['SYSTEM', 'USER', 'ASSISTANT'] -%}\n{%- set ns = namespace(initial_system_message_handled=false, last_assistant_index_for_eos=-1, messages=messages) -%}\n{%- for message in ns.messages -%}\n {%- if not message.get('role') -%}\n {{ raise_exception('Key [role] is missing. Original input: ' + message|tojson) }}\n {%- endif -%}\n {%- if message['role'] | upper not in message_roles -%}\n {{ raise_exception('Invalid role ' + message['role']|tojson + '. Only ' + message_roles|tojson + ' are supported.') }}\n {%- endif -%}\n {%- if 'content' not in message -%}\n {{ raise_exception('Key [content] is missing. Original input: ' + message|tojson) }}\n {%- endif -%}\n {%- if loop.last and message['role'] | upper == 'ASSISTANT' -%}\n {%- set ns.last_assistant_index_for_eos = loop.index0 -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if _mode == 'generate' -%}\n {{ bos_token }}\n{%- endif -%}\n{%- for message in ns.messages -%}\n {%- if message['role'] | upper == 'SYSTEM' and not ns.initial_system_message_handled -%}\n {%- set ns.initial_system_message_handled = true -%}\n {{ '<|start_header_id|>system<|end_header_id|>\n\n' + message['content'] + stop_token }}\n {%- elif message['role'] | upper != 'SYSTEM' -%}\n {%- if (message['role'] | upper == 'USER') != ((loop.index0 - (1 if ns.initial_system_message_handled else 0)) % 2 == 0) -%}\n {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}\n {%- endif -%}\n {%- if message['role'] | upper == 'USER' -%}\n {{ '<|start_header_id|>user<|end_header_id|>\n\n' + message['content'] + stop_token }}\n {%- elif message['role'] | upper == 'ASSISTANT' -%}\n {%- if _mode == 'train' -%}\n {{ '<|start_header_id|>assistant<|end_header_id|>\n\n' + unk_token + message['content'] + stop_token + unk_token }}\n {%- else -%}\n {{ '<|start_header_id|>assistant<|end_header_id|>\n\n' + message['content'] + (stop_token if loop.index0 != ns.last_assistant_index_for_eos else '') }}\n {%- endif -%}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if _mode == 'generate' and ns.last_assistant_index_for_eos == -1 -%}\n {{ '<|start_header_id|>assistant<|end_header_id|>' }}\n{%- endif -%}" |