4
4
5
5
6
6
def _get_system_message (
7
- messages : List [llama_types .ChatCompletionRequestMessage ],
7
+ messages : List [llama_types .ChatCompletionRequestMessage ],
8
8
) -> str :
9
9
"""Get the first system message."""
10
10
for message in messages :
@@ -14,7 +14,7 @@ def _get_system_message(
14
14
15
15
16
16
def _map_roles (
17
- messages : List [llama_types .ChatCompletionRequestMessage ], role_map : Dict [str , str ]
17
+ messages : List [llama_types .ChatCompletionRequestMessage ], role_map : Dict [str , str ]
18
18
) -> List [Tuple [str , Optional [str ]]]:
19
19
"""Map the message roles."""
20
20
output : List [Tuple [str , Optional [str ]]] = []
@@ -26,7 +26,7 @@ def _map_roles(
26
26
27
27
28
28
def _format_llama2 (
29
- system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
29
+ system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
30
30
) -> str :
31
31
"""Format the prompt with the llama2 style."""
32
32
ret = system_message + sep
@@ -39,7 +39,7 @@ def _format_llama2(
39
39
40
40
41
41
def _format_add_colon_single (
42
- system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
42
+ system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
43
43
) -> str :
44
44
"""Format the prompt with the add-colon-single style."""
45
45
ret = system_message + sep
@@ -52,7 +52,7 @@ def _format_add_colon_single(
52
52
53
53
54
54
def _format_add_colon_two (
55
- system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str , sep2 : str
55
+ system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str , sep2 : str
56
56
) -> str :
57
57
"""Format the prompt with the add-colon-two style."""
58
58
seps = [sep , sep2 ]
@@ -66,7 +66,7 @@ def _format_add_colon_two(
66
66
67
67
68
68
def _format_no_colon_single (
69
- system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
69
+ system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
70
70
) -> str :
71
71
"""Format the prompt with the no-colon-single style."""
72
72
ret = system_message
@@ -79,7 +79,7 @@ def _format_no_colon_single(
79
79
80
80
81
81
def _format_add_colon_space_single (
82
- system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
82
+ system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
83
83
) -> str :
84
84
"""Format the prompt with the add-colon-space-single style."""
85
85
ret = system_message + sep
@@ -92,7 +92,7 @@ def _format_add_colon_space_single(
92
92
93
93
94
94
def _format_chatml (
95
- system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
95
+ system_message : str , messages : List [Tuple [str , Optional [str ]]], sep : str
96
96
) -> str :
97
97
"""Format the prompt with the chatml style."""
98
98
ret = "" if system_message == "" else system_message + sep + "\n "
@@ -112,9 +112,9 @@ class ChatFormatterResponse:
112
112
113
113
class ChatFormatter (Protocol ):
114
114
def __call__ (
115
- self ,
116
- messages : List [llama_types .ChatCompletionRequestMessage ],
117
- ** kwargs : Any ,
115
+ self ,
116
+ messages : List [llama_types .ChatCompletionRequestMessage ],
117
+ ** kwargs : Any ,
118
118
) -> ChatFormatterResponse :
119
119
...
120
120
@@ -141,8 +141,8 @@ def get_chat_format(name: str):
141
141
142
142
@register_chat_format ("llama-2" )
143
143
def format_llama2 (
144
- messages : List [llama_types .ChatCompletionRequestMessage ],
145
- ** kwargs : Any ,
144
+ messages : List [llama_types .ChatCompletionRequestMessage ],
145
+ ** kwargs : Any ,
146
146
) -> ChatFormatterResponse :
147
147
_system_template = "[INST] <<SYS>>\n {system_message}\n <</SYS>>\n \n "
148
148
_roles = dict (user = "[INST]" , assistant = "[/INST]" )
@@ -157,8 +157,8 @@ def format_llama2(
157
157
158
158
@register_chat_format ("alpaca" )
159
159
def format_alpaca (
160
- messages : List [llama_types .ChatCompletionRequestMessage ],
161
- ** kwargs : Any ,
160
+ messages : List [llama_types .ChatCompletionRequestMessage ],
161
+ ** kwargs : Any ,
162
162
) -> ChatFormatterResponse :
163
163
_roles = dict (user = "### Instruction" , assistant = "### Response" )
164
164
_sep = "\n \n "
@@ -171,8 +171,8 @@ def format_alpaca(
171
171
172
172
@register_chat_format ("vicuna" )
173
173
def format (
174
- messages : List [llama_types .ChatCompletionRequestMessage ],
175
- ** kwargs : Any ,
174
+ messages : List [llama_types .ChatCompletionRequestMessage ],
175
+ ** kwargs : Any ,
176
176
) -> ChatFormatterResponse :
177
177
_system_message = "A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions."
178
178
_roles = dict (user = "USER" , assistant = "ASSISTANT" )
@@ -187,8 +187,8 @@ def format(
187
187
188
188
@register_chat_format ("oasst_llama" )
189
189
def format_oasst_llama (
190
- messages : List [llama_types .ChatCompletionRequestMessage ],
191
- ** kwargs : Any ,
190
+ messages : List [llama_types .ChatCompletionRequestMessage ],
191
+ ** kwargs : Any ,
192
192
) -> ChatFormatterResponse :
193
193
_system_template = "[INST] <<SYS>>\n {system_message}\n <</SYS>>\n \n "
194
194
_roles = dict (user = "<|prompter|>" , assistant = "<|assistant|>" )
@@ -203,8 +203,8 @@ def format_oasst_llama(
203
203
204
204
@register_chat_format ("openbuddy" )
205
205
def format_openbuddy (
206
- messages : List [llama_types .ChatCompletionRequestMessage ],
207
- ** kwargs : Any ,
206
+ messages : List [llama_types .ChatCompletionRequestMessage ],
207
+ ** kwargs : Any ,
208
208
) -> ChatFormatterResponse :
209
209
_system_message = """Consider a conversation between User (a human) and Assistant (named Buddy).
210
210
Buddy is an INTP-T, a friendly, intelligent and multilingual AI assistant, by OpenBuddy team. GitHub: https://github.com/OpenBuddy/OpenBuddy
@@ -228,8 +228,8 @@ def format_openbuddy(
228
228
229
229
@register_chat_format ("redpajama-incite" )
230
230
def format_redpajama_incite (
231
- messages : List [llama_types .ChatCompletionRequestMessage ],
232
- ** kwargs : Any ,
231
+ messages : List [llama_types .ChatCompletionRequestMessage ],
232
+ ** kwargs : Any ,
233
233
) -> ChatFormatterResponse :
234
234
_system_message = _get_system_message (messages )
235
235
_roles = dict (user = "<human>" , assistant = "<bot>" )
@@ -244,8 +244,8 @@ def format_redpajama_incite(
244
244
245
245
@register_chat_format ("snoozy" )
246
246
def format_snoozy (
247
- messages : List [llama_types .ChatCompletionRequestMessage ],
248
- ** kwargs : Any ,
247
+ messages : List [llama_types .ChatCompletionRequestMessage ],
248
+ ** kwargs : Any ,
249
249
) -> ChatFormatterResponse :
250
250
system_template = "### Instruction:\n {system_message}"
251
251
default_system_message = "The prompt below is a question to answer, a task to complete, or a conversation to respond to; decide which and write an appropriate response."
@@ -266,8 +266,8 @@ def format_snoozy(
266
266
267
267
@register_chat_format ("phind" )
268
268
def format_phind (
269
- messages : List [llama_types .ChatCompletionRequestMessage ],
270
- ** kwargs : Any ,
269
+ messages : List [llama_types .ChatCompletionRequestMessage ],
270
+ ** kwargs : Any ,
271
271
) -> ChatFormatterResponse :
272
272
_roles = dict (user = "### User Message" , assistant = "### Assistant" )
273
273
_sep = "\n \n "
@@ -280,8 +280,8 @@ def format_phind(
280
280
281
281
@register_chat_format ("open-orca" )
282
282
def format_open_orca (
283
- messages : List [llama_types .ChatCompletionRequestMessage ],
284
- ** kwargs : Any ,
283
+ messages : List [llama_types .ChatCompletionRequestMessage ],
284
+ ** kwargs : Any ,
285
285
) -> ChatFormatterResponse :
286
286
system_template = "{system_message}"
287
287
system_message = (
@@ -307,8 +307,8 @@ def format_open_orca(
307
307
308
308
@register_chat_format ("chatml" )
309
309
def format_chatml (
310
- messages : List [llama_types .ChatCompletionRequestMessage ],
311
- ** kwargs : Any ,
310
+ messages : List [llama_types .ChatCompletionRequestMessage ],
311
+ ** kwargs : Any ,
312
312
) -> ChatFormatterResponse :
313
313
system_template = """<|im_start|>system
314
314
{system_message}"""
@@ -319,4 +319,4 @@ def format_chatml(
319
319
_messages = _map_roles (messages , _roles )
320
320
_messages .append ((_roles ["assistant" ], None ))
321
321
_prompt = _format_chatml (system_message , _messages , _sep )
322
- return ChatFormatterResponse (prompt = _prompt )
322
+ return ChatFormatterResponse (prompt = _prompt , stop = _sep )
0 commit comments