stefan-it commited on
Commit
904e303
·
verified ·
1 Parent(s): a78aa9b

feat: add chat template

Browse files
Files changed (1) hide show
  1. chat_template.jinja +51 -0
chat_template.jinja ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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' -%}
16
+ {{- '<|user_start|>' }}
17
+ {%- if loop.first and first_user_has_system -%}
18
+ {{- system_content + '\n\n' }}
19
+ {%- endif -%}
20
+ {%- if message['content'] is not string -%}
21
+ {{- raise_exception("User messages must contain string content") }}
22
+ {%- endif -%}
23
+ {{- message['content'] }}
24
+ {{- '<|user_end|>' }}
25
+ {%- elif message['role'] == 'assistant' -%}
26
+ {{- '<|assistant_start|>' }}
27
+ {%- if message['content'] is string -%}
28
+ {{- message['content'] }}
29
+ {%- elif message['content'] is iterable -%}
30
+ {%- for part in message['content'] -%}
31
+ {%- if part['type'] == 'text' -%}
32
+ {{- part.get('text', '') }}
33
+ {%- elif part['type'] == 'python' -%}
34
+ {{- '<|python_start|>' + part.get('text', '') + '<|python_end|>' }}
35
+ {%- elif part['type'] == 'python_output' -%}
36
+ {{- '<|output_start|>' + part.get('text', '') + '<|output_end|>' }}
37
+ {%- else -%}
38
+ {{- raise_exception("Unknown assistant content part: " + part['type']) }}
39
+ {%- endif -%}
40
+ {%- endfor -%}
41
+ {%- else -%}
42
+ {{- raise_exception("Unsupported assistant content type") }}
43
+ {%- endif -%}
44
+ {{- '<|assistant_end|>' }}
45
+ {%- else -%}
46
+ {{- raise_exception("Unexpected message role: " + message['role'] + ". Only 'user' and 'assistant' roles are supported after the optional initial system message.") }}
47
+ {%- endif -%}
48
+ {%- endfor -%}
49
+ {%- if add_generation_prompt -%}
50
+ {{- '<|assistant_start|>' }}
51
+ {%- endif -%}