File size: 1,402 Bytes
ac19a8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{%- 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 -%}