Skip to content

Commit 48e6306

Browse files
committed
Update chat example, prompt formats
1 parent 1f685bd commit 48e6306

File tree

2 files changed

+103
-58
lines changed

2 files changed

+103
-58
lines changed

examples/chat.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
parser.add_argument("-un", "--username", type = str, default = "User", help = "Username when using raw chat mode")
4141
parser.add_argument("-bn", "--botname", type = str, default = "Chatbort", help = "Bot name when using raw chat mode")
4242
parser.add_argument("-sp", "--system_prompt", type = str, help = "Use custom system prompt")
43+
parser.add_argument("-nsp", "--no_system_prompt", action = "store_true", help = "Do not use any system prompt")
4344

4445
parser.add_argument("-temp", "--temperature", type = float, default = 0.95, help = "Sampler temperature, default = 0.95 (1 to disable)")
4546
parser.add_argument("-smooth", "--smoothing_factor", type = float, default = 0.0, help = "Smoothing Factor, default = 0.0 (0 to disable")
@@ -90,6 +91,8 @@
9091
username = args.username
9192
botname = args.botname
9293
system_prompt = args.system_prompt
94+
if args.no_system_prompt:
95+
system_prompt = ""
9396

9497
if args.mode is None:
9598
print(" ## Error: No mode specified.")
@@ -185,7 +188,7 @@ def format_prompt(user_prompt, first):
185188
global system_prompt, prompt_format
186189

187190
if first:
188-
return prompt_format.first_prompt() \
191+
return prompt_format.first_prompt(not system_prompt) \
189192
.replace("<|system_prompt|>", system_prompt) \
190193
.replace("<|user_prompt|>", user_prompt)
191194
else:
@@ -288,9 +291,10 @@ def get_tokenized_context(max_len):
288291
# Main loop
289292

290293
print(f" -- Prompt format: {args.mode}")
291-
print(f" -- System prompt:")
292-
print()
293-
print(col_sysprompt + system_prompt.strip() + col_default)
294+
if system_prompt:
295+
print(f" -- System prompt:")
296+
print()
297+
print(col_sysprompt + system_prompt.strip() + col_default)
294298

295299
while True:
296300

examples/chat_prompts.py

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __init__(self):
1212
def default_system_prompt(self):
1313
raise NotImplementedError
1414

15-
def first_prompt(self):
15+
def first_prompt(self, sysprompt: bool):
1616
raise NotImplementedError
1717

1818
def subs_prompt(self):
@@ -44,9 +44,11 @@ def default_system_prompt(self):
4444
f"""This is a conversation between a helpful AI assistant named {self.botname} and a """ + \
4545
(f"""user named {self.username}.""" if self.username != "User" else """user.""")
4646

47-
def first_prompt(self):
48-
return \
49-
f"""<|system_prompt|>\n{self.username}: <|user_prompt|>\n{self.botname}:"""
47+
def first_prompt(self, sysprompt):
48+
if sysprompt:
49+
return f"""<|system_prompt|>\n{self.username}: <|user_prompt|>\n{self.botname}:"""
50+
else:
51+
return f"""{self.username}: <|user_prompt|>\n{self.botname}:"""
5052

5153
def subs_prompt(self):
5254
return \
@@ -61,7 +63,7 @@ def stop_conditions(self, tokenizer):
6163
tokenizer.eos_token_id]
6264

6365
def encoding_options(self):
64-
return False, False, False
66+
return True, False, False
6567

6668
def print_bot_name(self):
6769
return True
@@ -81,9 +83,11 @@ def default_system_prompt(self):
8183
"""Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. """ + \
8284
"""Please ensure that your responses are socially unbiased and positive in nature."""
8385

84-
def first_prompt(self):
85-
return \
86-
"""[INST] <<SYS>>\n<|system_prompt|>\n<</SYS>>\n\n<|user_prompt|> [/INST]"""
86+
def first_prompt(self, sysprompt):
87+
if sysprompt:
88+
return """[INST] <<SYS>>\n<|system_prompt|>\n<</SYS>>\n\n<|user_prompt|> [/INST]"""
89+
else:
90+
return """[INST] <|user_prompt|> [/INST]"""
8791

8892
def subs_prompt(self):
8993
return \
@@ -115,19 +119,23 @@ def default_system_prompt(self):
115119
"""to find the answer or suggest where to find it. Keep responses concise and relevant. Follow ethical """ + \
116120
"""guidelines and promote a safe and respectful interaction."""
117121

118-
def first_prompt(self):
119-
return \
120-
"""<|start_header_id|>system<|end_header_id|>\n\n""" + \
121-
"""<|system_prompt|><|eot_id|>""" + \
122+
def first_prompt(self, sysprompt):
123+
r = ""
124+
if sysprompt:
125+
r += \
126+
"""<|start_header_id|>system<|end_header_id|>\n\n""" + \
127+
"""<|system_prompt|><|eot_id|>"""
128+
r += \
122129
"""<|start_header_id|>user<|end_header_id|>\n\n""" + \
123130
"""<|user_prompt|><|eot_id|>""" + \
124-
"""<|start_header_id|>assistant<|end_header_id|>"""
131+
"""<|start_header_id|>assistant<|end_header_id|>\n\n"""
132+
return r
125133

126134
def subs_prompt(self):
127135
return \
128136
"""<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n""" + \
129137
"""<|user_prompt|><|eot_id|>""" + \
130-
"""<|start_header_id|>assistant<|end_header_id|>"""
138+
"""<|start_header_id|>assistant<|end_header_id|>\n\n"""
131139

132140
def stop_conditions(self, tokenizer):
133141
return \
@@ -136,7 +144,7 @@ def stop_conditions(self, tokenizer):
136144
tokenizer.single_id("<|start_header_id|>")]
137145

138146
def encoding_options(self):
139-
return False, False, True
147+
return True, False, True
140148

141149
def print_extra_newline(self):
142150
return True
@@ -154,14 +162,18 @@ def default_system_prompt(self):
154162
return \
155163
"""You are a helpful AI assistant."""
156164

157-
def first_prompt(self):
158-
return \
159-
"""<s><|system|>\n""" + \
160-
"""<|system_prompt|>""" + \
161-
"""<|end|>\n""" + \
165+
def first_prompt(self, sysprompt):
166+
r = """<s>"""
167+
if sysprompt:
168+
r += \
169+
"""<|system|>\n""" + \
170+
"""<|system_prompt|>""" + \
171+
"""<|end|>\n"""
172+
r += \
162173
"""<|user|>\n""" + \
163174
"""<|user_prompt|><|end|>\n""" + \
164175
"""<|assistant|>\n"""
176+
return r
165177

166178
def subs_prompt(self):
167179
return \
@@ -210,14 +222,18 @@ def default_system_prompt(self):
210222
return \
211223
f"""You are {self.botname}, a large language model. Answer as concisely as possible."""
212224

213-
def first_prompt(self):
214-
return \
215-
"""<|im_start|>system\n""" + \
216-
"""<|system_prompt|>\n""" + \
217-
"""<|im_end|>\n""" + \
225+
def first_prompt(self, sysprompt):
226+
r = ""
227+
if sysprompt:
228+
r += \
229+
"""<|im_start|>system\n""" + \
230+
"""<|system_prompt|>\n""" + \
231+
"""<|im_end|>\n"""
232+
r += \
218233
"""<|im_start|>user\n""" + \
219234
"""<|user_prompt|><|im_end|>\n""" + \
220235
"""<|im_start|>assistant\n"""
236+
return r
221237

222238
def subs_prompt(self):
223239
return \
@@ -259,14 +275,18 @@ def default_system_prompt(self):
259275
return \
260276
f"""You are {self.botname}, a large language model. Answer as concisely as possible."""
261277

262-
def first_prompt(self):
263-
return \
264-
"""<|system|>\n""" + \
265-
"""<|system_prompt|>\n""" + \
266-
"""</s>\n""" + \
278+
def first_prompt(self, sysprompt):
279+
r = ""
280+
if sysprompt:
281+
r += \
282+
"""<|system|>\n""" + \
283+
"""<|system_prompt|>\n""" + \
284+
"""</s>\n"""
285+
r += \
267286
"""<|user|>\n""" + \
268287
"""<|user_prompt|></s>\n""" + \
269288
"""<|assistant|>\n"""
289+
return r
270290

271291
def subs_prompt(self):
272292
return \
@@ -299,12 +319,15 @@ def default_system_prompt(self):
299319
return \
300320
f"""You are an AI programming assistant, utilizing the Deepseek Coder model, developed by Deepseek Company, and you only answer questions related to computer science. For politically sensitive questions, security and privacy issues, and other non-computer science questions, you will refuse to answer."""
301321

302-
def first_prompt(self):
303-
return \
304-
"""<|system_prompt|>\n""" + \
322+
def first_prompt(self, sysprompt):
323+
r = ""
324+
if sysprompt:
325+
r += """<|system_prompt|>\n"""
326+
r += \
305327
"""### Instruction:\n""" + \
306328
"""<|user_prompt|>\n""" + \
307329
"""### Response:\n"""
330+
return r
308331

309332
def subs_prompt(self):
310333
return \
@@ -335,13 +358,17 @@ def default_system_prompt(self):
335358
return \
336359
f"""You are an AI assistant."""
337360

338-
def first_prompt(self):
339-
return \
340-
"""### System\n""" + \
341-
"""<|system_prompt|>\n\n""" + \
361+
def first_prompt(self, sysprompt):
362+
r = ""
363+
if sysprompt:
364+
r += \
365+
"""### System\n""" + \
366+
"""<|system_prompt|>\n\n"""
367+
r += \
342368
"""### User:\n""" + \
343369
"""<|user_prompt|>\n\n""" + \
344370
"""### Assistant:\n"""
371+
return r
345372

346373
def subs_prompt(self):
347374
return \
@@ -374,9 +401,13 @@ def default_system_prompt(self):
374401
return \
375402
f"""You are an AI assistant."""
376403

377-
def first_prompt(self):
378-
return \
379-
"""<|system_prompt|><|end_of_turn|>GPT4 Correct User:<|user_prompt|><|end_of_turn|>GPT4 Correct Assistant:"""
404+
def first_prompt(self, sysprompt):
405+
if sysprompt:
406+
return \
407+
"""<|system_prompt|><|end_of_turn|>GPT4 Correct User:<|user_prompt|><|end_of_turn|>GPT4 Correct Assistant:"""
408+
else:
409+
return \
410+
"""GPT4 Correct User:<|user_prompt|><|end_of_turn|>GPT4 Correct Assistant:"""
380411

381412
def subs_prompt(self):
382413
return \
@@ -408,12 +439,15 @@ def default_system_prompt(self):
408439
return \
409440
f"""Perform the task to the best of your ability."""
410441

411-
def first_prompt(self):
412-
return \
413-
"""<|system_prompt|>\n\n""" + \
442+
def first_prompt(self, sysprompt):
443+
r = ""
444+
if sysprompt:
445+
r += """<|system_prompt|>\n\n"""
446+
r += \
414447
"""USER:\n""" + \
415448
"""<|user_prompt|>\n\n""" + \
416449
"""ASSISTANT:\n"""
450+
return r
417451

418452
def subs_prompt(self):
419453
return \
@@ -444,7 +478,7 @@ def __init__(self):
444478
def default_system_prompt(self):
445479
return ""
446480

447-
def first_prompt(self):
481+
def first_prompt(self, sysprompt):
448482
return \
449483
"""<bos><start_of_turn>user\n""" + \
450484
"""<|user_prompt|><end_of_turn>\n""" + \
@@ -481,13 +515,17 @@ def __init__(self):
481515
def default_system_prompt(self):
482516
return "You are an AI coding assistant."
483517

484-
def first_prompt(self):
485-
return \
486-
"""System:\n""" + \
487-
"""<|system_prompt|>\n\n""" + \
518+
def first_prompt(self, sysprompt):
519+
r = ""
520+
if sysprompt:
521+
r += \
522+
"""System:\n""" + \
523+
"""<|system_prompt|>\n\n"""
524+
r += \
488525
"""Question:\n""" + \
489526
"""<|user_prompt|>\n\n""" + \
490527
"""Answer:\n"""
528+
return r
491529

492530
def subs_prompt(self):
493531
return \
@@ -520,16 +558,19 @@ def default_system_prompt(self):
520558
return \
521559
f"""You are a helpful AI assistant."""
522560

523-
def first_prompt(self):
524-
return \
525-
"""<BOS_TOKEN>""" + \
526-
"""<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>""" + \
527-
"""<|system_prompt|>""" + \
528-
"""<|END_OF_TURN_TOKEN|>""" + \
561+
def first_prompt(self, sysprompt):
562+
r = """<BOS_TOKEN>"""
563+
if sysprompt:
564+
r += \
565+
"""<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>""" + \
566+
"""<|system_prompt|>""" + \
567+
"""<|END_OF_TURN_TOKEN|>"""
568+
r += \
529569
"""<|START_OF_TURN_TOKEN|><|USER_TOKEN|>""" + \
530570
"""<|user_prompt|>""" + \
531571
"""<|END_OF_TURN_TOKEN|>""" + \
532572
"""<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"""
573+
return r
533574

534575
def subs_prompt(self):
535576
return \

0 commit comments

Comments
 (0)