{%- set training = messages | length == 2 -%} {%- if messages | length > 2 or messages[0]['role'] != 'user' or (training and messages[1]['role'] != 'assistant') -%} {{- raise_exception('Gradium Phonon TTS takes one `user` message per conversation, followed by the `assistant` target audio to train on.') -}} {%- endif -%} {%- if training -%} {%- set target = messages[1]['content'] -%} {%- if target is string or target | length != 1 or target[0]['type'] != 'audio' -%} {{- raise_exception('The `assistant` turn holds one `audio` item, the target.') -}} {%- endif -%} {%- endif -%} {%- set content = messages[0]['content'] -%} {%- if content is string -%} {%- set texts = [content] -%} {%- set num_audio = 0 -%} {%- else -%} {%- set texts = content | selectattr('type', 'equalto', 'text') | map(attribute='text') | list -%} {%- set num_audio = content | selectattr('type', 'equalto', 'audio') | list | length -%} {%- endif -%} {%- if texts | length > 1 or (texts | length == 0 and not training) or num_audio > 1 -%} {{- raise_exception('The `user` turn holds one `text` item and at most one `audio` item (the voice).') -}} {%- endif -%} {%- set voice = '<|audio_prompt|>' if num_audio else '' -%} {%- set text = (texts[0] if texts else '') | trim -%} {%- if training -%} {{- voice ~ text ~ '<|audio_bos|><|audio_prompt|>' -}} {%- else -%} {{- voice ~ text -}} {%- if add_generation_prompt -%} {{- '<|audio_bos|>' -}} {%- endif -%} {%- endif -%}