burtenshaw HF Staff commited on
Commit
58abf66
·
verified ·
1 Parent(s): 1febc4b

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +52 -0
chat_template.jinja ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{- bos_token }}
2
+ {%- if messages[0]['role'] == 'system' -%}
3
+ {%- if messages[1] is not defined or messages[1]['role'] != 'user' -%}
4
+ {{- raise_exception("System message must be followed by a user message") }}
5
+ {%- endif -%}
6
+ {%- set system_content = messages[0]['content'] -%}
7
+ {%- set loop_messages = messages[1:] -%}
8
+ {%- set first_user_has_system = true -%}
9
+ {%- else -%}
10
+ {%- set system_content = "" -%}
11
+ {%- set loop_messages = messages -%}
12
+ {%- set first_user_has_system = false -%}
13
+ {%- endif -%}
14
+ {%- for message in loop_messages -%}
15
+ {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
16
+ {{- raise_exception("Conversation roles must alternate user/assistant/user/assistant/...") }}
17
+ {%- endif -%}
18
+ {%- if message['role'] == 'user' -%}
19
+ {{- '<|user_start|>' }}
20
+ {%- if loop.first and first_user_has_system -%}
21
+ {{- system_content + '\n\n' }}
22
+ {%- endif -%}
23
+ {%- if message['content'] is not string -%}
24
+ {{- raise_exception("User messages must contain string content") }}
25
+ {%- endif -%}
26
+ {{- message['content'] }}
27
+ {{- '<|user_end|>' }}
28
+ {%- elif message['role'] == 'assistant' -%}
29
+ {{- '<|assistant_start|>' }}
30
+ {%- if message['content'] is string -%}
31
+ {{- message['content'] }}
32
+ {%- elif message['content'] is iterable -%}
33
+ {%- for part in message['content'] -%}
34
+ {%- if part['type'] == 'text' -%}
35
+ {{- part.get('text', '') }}
36
+ {%- elif part['type'] == 'python' -%}
37
+ {{- '<|python_start|>' + part.get('text', '') + '<|python_end|>' }}
38
+ {%- elif part['type'] == 'python_output' -%}
39
+ {{- '<|output_start|>' + part.get('text', '') + '<|output_end|>' }}
40
+ {%- else -%}
41
+ {{- raise_exception("Unknown assistant content part: " + part['type']) }}
42
+ {%- endif -%}
43
+ {%- endfor -%}
44
+ {%- else -%}
45
+ {{- raise_exception("Unsupported assistant content type") }}
46
+ {%- endif -%}
47
+ {{- '<|assistant_end|>' }}
48
+ {%- endif -%}
49
+ {%- endfor -%}
50
+ {%- if add_generation_prompt -%}
51
+ {{- '<|assistant_start|>' }}
52
+ {%- endif -%}