| {%- if messages | length != 1 -%} | |
| {{- raise_exception("This chat template requires exactly one message, but got " ~ (messages | length) ~ ".") -}} | |
| {%- endif -%} | |
| {%- for message in messages -%} | |
| {%- if message['role'] != 'user' -%} | |
| {{- raise_exception("The message role must be 'user', but got '" ~ message['role'] ~ "'.") -}} | |
| {%- endif -%} | |
| {%- set content = message['content'] -%} | |
| {%- if content is not iterable or content is string -%} | |
| {{- raise_exception("The 'content' field must be a list.") -}} | |
| {%- endif -%} | |
| {%- if content | length != 1 -%} | |
| {{- raise_exception("The 'content' list must contain exactly one item, but got " ~ (content|length) ~ ".") -}} | |
| {%- endif -%} | |
| {%- set item = content[0] -%} | |
| {%- if 'type' not in item or item['type'] != 'text' -%} | |
| {{- raise_exception("The content item must have a 'type' of 'text'.") -}} | |
| {%- endif -%} | |
| {%- if 'text' not in item or not item['text'] is string -%} | |
| {{- raise_exception("The content item must have a 'text' key with a string value.") -}} | |
| {%- endif -%} | |
| {%- endfor -%} | |
| {%- for message in messages -%} | |
| {{- bos_token -}} | |
| {{- '[0]' -}} | |
| {%- set text = message['content'][0]['text'] -%} | |
| {%- if text.endswith('.') -%} | |
| {{- text[:-1] -}} | |
| {%- else -%} | |
| {{- text -}} | |
| {%- endif -%} | |
| {{- eos_token -}} | |
| {%- endfor -%} |