9
9
10
10
11
11
@lru_cache
12
- def _compile_jinja_template (chat_template ):
12
+ def _compile_jinja_template (chat_template : str ):
13
13
try :
14
14
from jinja2 .exceptions import TemplateError
15
15
from jinja2 .sandbox import ImmutableSandboxedEnvironment
@@ -64,8 +64,15 @@ def apply_chat_template(
64
64
return rendered
65
65
66
66
@property
67
- def template (self ):
68
- raise NotImplementedError
67
+ def template (self ) -> str :
68
+ return (
69
+ "{% for message in messages %}"
70
+ "{{ '<|im_start|>' + message['role'] + '\\ n' + message['content'] + '<|im_end|>' + '\\ n' }}"
71
+ "{% endfor %}"
72
+ "{% if add_generation_prompt %}"
73
+ "{{ '<|im_start|>assistant\\ n' }}"
74
+ "{% endif %}"
75
+ )
69
76
70
77
def postprocess_messages (
71
78
self ,
@@ -80,8 +87,8 @@ def parse_assistant_response(
80
87
output : str ,
81
88
functions : Optional [Union [Dict [str , Any ], List [Dict [str , Any ]]]] = None ,
82
89
tools : Optional [List [Dict [str , Any ]]] = None ,
83
- ) -> Tuple [str , Union [str , Dict [str , Any ]]]:
84
- raise NotImplementedError
90
+ ) -> Tuple [str , Optional [ Union [str , Dict [str , Any ] ]]]:
91
+ return output , None
85
92
86
93
87
94
# A global registry for all prompt adapters
@@ -119,7 +126,7 @@ class QwenTemplate(BaseTemplate):
119
126
function_call_available = True
120
127
121
128
@property
122
- def template (self ):
129
+ def template (self ) -> str :
123
130
""" This template formats inputs in the standard ChatML format. See
124
131
https://github.com/openai/openai-python/blob/main/chatml.md
125
132
"""
@@ -138,7 +145,7 @@ def parse_assistant_response(
138
145
output : str ,
139
146
functions : Optional [Union [Dict [str , Any ], List [Dict [str , Any ]]]] = None ,
140
147
tools : Optional [List [Dict [str , Any ]]] = None ,
141
- ) -> Tuple [str , Union [str , Dict [str , Any ]]]:
148
+ ) -> Tuple [str , Optional [ Union [str , Dict [str , Any ] ]]]:
142
149
func_name , func_args = "" , ""
143
150
i = output .rfind ("\n Action:" )
144
151
j = output .rfind ("\n Action Input:" )
@@ -177,6 +184,7 @@ def parse_assistant_response(
177
184
178
185
179
186
class Llama2Template (BaseTemplate ):
187
+
180
188
name = "llama2"
181
189
system_prompt = "You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe." \
182
190
"Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content." \
@@ -189,7 +197,7 @@ class Llama2Template(BaseTemplate):
189
197
}
190
198
191
199
@property
192
- def template (self ):
200
+ def template (self ) -> str :
193
201
"""
194
202
LLaMA uses [INST] and [/INST] to indicate user messages, and <<SYS>> and <</SYS>> to indicate system messages.
195
203
Assistant messages do not have special tokens, because LLaMA chat models are generally trained with strict
@@ -257,7 +265,7 @@ def match(self, name) -> bool:
257
265
return name == "chatglm"
258
266
259
267
@property
260
- def template (self ):
268
+ def template (self ) -> str :
261
269
""" The output should look something like:
262
270
263
271
[Round 0]
@@ -292,7 +300,7 @@ def match(self, name) -> bool:
292
300
return name == "chatglm2"
293
301
294
302
@property
295
- def template (self ):
303
+ def template (self ) -> str :
296
304
""" The output should look something like:
297
305
298
306
[Round 1]
@@ -337,7 +345,7 @@ def match(self, name) -> bool:
337
345
return name == "chatglm3"
338
346
339
347
@property
340
- def template (self ):
348
+ def template (self ) -> str :
341
349
"""
342
350
The reference for this chat template is [this code
343
351
snippet](https://huggingface.co/THUDM/chatglm3-6b/blob/main/modeling_chatglm.py)
@@ -410,7 +418,7 @@ def parse_assistant_response(
410
418
output : str ,
411
419
functions : Optional [Union [Dict [str , Any ], List [Dict [str , Any ]]]] = None ,
412
420
tools : Optional [List [Dict [str , Any ]]] = None ,
413
- ) -> Tuple [str , Union [str , Dict [str , Any ]]]:
421
+ ) -> Tuple [str , Optional [ Union [str , Dict [str , Any ] ]]]:
414
422
content = ""
415
423
for response in output .split ("<|assistant|>" ):
416
424
if "\n " in response :
@@ -471,7 +479,7 @@ class MossTemplate(BaseTemplate):
471
479
}
472
480
473
481
@property
474
- def template (self ):
482
+ def template (self ) -> str :
475
483
""" The output should look something like:
476
484
477
485
<|Human|>: {Prompt}<eoh>
@@ -501,7 +509,7 @@ class PhoenixTemplate(BaseTemplate):
501
509
allow_models = ["phoenix" ]
502
510
503
511
@property
504
- def template (self ):
512
+ def template (self ) -> str :
505
513
""" The output should look something like:
506
514
507
515
Human: <s>{Prompt}</s>Assistant: <s>{Answer}</s>
@@ -536,7 +544,7 @@ class AlpacaTemplate(BaseTemplate):
536
544
}
537
545
538
546
@property
539
- def template (self ):
547
+ def template (self ) -> str :
540
548
""" The output should look something like:
541
549
542
550
### Instruction:
@@ -573,7 +581,7 @@ class FireflyTemplate(BaseTemplate):
573
581
allow_models = ["firefly" ]
574
582
575
583
@property
576
- def template (self ):
584
+ def template (self ) -> str :
577
585
""" The output should look something like:
578
586
579
587
<s>{Prompt}</s>{Answer}</s>{Prompt}</s>
@@ -597,7 +605,7 @@ class FireflyForQwenTemplate(BaseTemplate):
597
605
allow_models = ["firefly-qwen" ]
598
606
599
607
@property
600
- def template (self ):
608
+ def template (self ) -> str :
601
609
""" The output should look something like:
602
610
603
611
<|endoftext|>{Prompt}<|endoftext|>{Answer}<|endoftext|>{Prompt}<|endoftext|>
@@ -620,7 +628,7 @@ class BelleTemplate(BaseTemplate):
620
628
allow_models = ["belle" ]
621
629
622
630
@property
623
- def template (self ):
631
+ def template (self ) -> str :
624
632
""" The output should look something like:
625
633
626
634
Human: {Prompt}
@@ -658,7 +666,7 @@ class OpenBuddyTemplate(BaseTemplate):
658
666
"""
659
667
660
668
@property
661
- def template (self ):
669
+ def template (self ) -> str :
662
670
""" The output should look something like:
663
671
664
672
User: {Prompt}
@@ -692,7 +700,7 @@ class InternLMTemplate(BaseTemplate):
692
700
}
693
701
694
702
@property
695
- def template (self ):
703
+ def template (self ) -> str :
696
704
""" The output should look something like:
697
705
698
706
<s><|User|>:{Prompt}<eoh>
@@ -721,7 +729,7 @@ class BaiChuanTemplate(BaseTemplate):
721
729
}
722
730
723
731
@property
724
- def template (self ):
732
+ def template (self ) -> str :
725
733
""" The output should look something like:
726
734
727
735
<reserved_102>{Prompt}<reserved_103>{Answer}<reserved_102>{Prompt}<reserved_103>
@@ -747,7 +755,7 @@ class BaiChuan2Template(BaseTemplate):
747
755
}
748
756
749
757
@property
750
- def template (self ):
758
+ def template (self ) -> str :
751
759
""" The output should look something like:
752
760
753
761
<reserved_106>{Prompt}<reserved_107>{Answer}<reserved_106>{Prompt}<reserved_107>
@@ -773,7 +781,7 @@ class StarChatTemplate(BaseTemplate):
773
781
}
774
782
775
783
@property
776
- def template (self ):
784
+ def template (self ) -> str :
777
785
""" The output should look something like:
778
786
779
787
<|user|>
@@ -809,7 +817,7 @@ class AquilaChatTemplate(BaseTemplate):
809
817
}
810
818
811
819
@property
812
- def template (self ):
820
+ def template (self ) -> str :
813
821
""" The output should look something like:
814
822
815
823
Human: {Prompt}###
@@ -850,7 +858,7 @@ class OctopackTemplate(BaseTemplate):
850
858
allow_models = ["starcoder-self-instruct" ]
851
859
852
860
@property
853
- def template (self ):
861
+ def template (self ) -> str :
854
862
""" The output should look something like:
855
863
856
864
Question:{Prompt}
@@ -878,7 +886,7 @@ class XverseTemplate(BaseTemplate):
878
886
allow_models = ["xverse" ]
879
887
880
888
@property
881
- def template (self ):
889
+ def template (self ) -> str :
882
890
""" The output should look something like:
883
891
884
892
Human: {Prompt}
@@ -905,7 +913,7 @@ class VicunaTemplate(BaseTemplate):
905
913
allow_models = ["vicuna" , "xwin" ]
906
914
907
915
@property
908
- def template (self ):
916
+ def template (self ) -> str :
909
917
""" The output should look something like:
910
918
911
919
USER: {Prompt} ASSISTANT: {Answer}</s>USER: {Prompt} ASSISTANT:
@@ -933,7 +941,7 @@ class XuanYuanTemplate(BaseTemplate):
933
941
allow_models = ["xuanyuan" ]
934
942
935
943
@property
936
- def template (self ):
944
+ def template (self ) -> str :
937
945
""" The output should look something like:
938
946
939
947
Human: {Prompt} Assistant: {Answer}</s>Human: {Prompt} Assistant:
@@ -964,7 +972,7 @@ class PhindTemplate(BaseTemplate):
964
972
}
965
973
966
974
@property
967
- def template (self ):
975
+ def template (self ) -> str :
968
976
return (
969
977
"{% if messages[0]['role'] == 'system' %}"
970
978
"{{ messages[0]['content'] }}"
@@ -1001,7 +1009,7 @@ def match(self, name) -> bool:
1001
1009
return name == "deepseek-coder"
1002
1010
1003
1011
@property
1004
- def template (self ):
1012
+ def template (self ) -> str :
1005
1013
return (
1006
1014
"{% if messages[0]['role'] == 'system' %}"
1007
1015
"{{ messages[0]['content'] }}"
@@ -1028,7 +1036,7 @@ class DeepseekTemplate(BaseTemplate):
1028
1036
}
1029
1037
1030
1038
@property
1031
- def template (self ):
1039
+ def template (self ) -> str :
1032
1040
return (
1033
1041
"{{ '<|begin▁of▁sentence|>' }}"
1034
1042
"{% for message in messages %}"
@@ -1052,7 +1060,7 @@ class BlueLMTemplate(BaseTemplate):
1052
1060
}
1053
1061
1054
1062
@property
1055
- def template (self ):
1063
+ def template (self ) -> str :
1056
1064
return (
1057
1065
"{% for message in messages %}"
1058
1066
"{% if message['role'] == 'system' %}"
@@ -1072,7 +1080,7 @@ class ZephyrTemplate(BaseTemplate):
1072
1080
allow_models = ["zephyr" ]
1073
1081
1074
1082
@property
1075
- def template (self ):
1083
+ def template (self ) -> str :
1076
1084
return (
1077
1085
"{% for message in messages %}"
1078
1086
"{% if message['role'] == 'system' %}"
@@ -1100,7 +1108,7 @@ class HuatuoTemplate(BaseTemplate):
1100
1108
}
1101
1109
1102
1110
@property
1103
- def template (self ):
1111
+ def template (self ) -> str :
1104
1112
return (
1105
1113
"{% if messages[0]['role'] == 'system' %}"
1106
1114
"{{ messages[0]['content'] }}"
@@ -1129,7 +1137,7 @@ class OrionStarTemplate(BaseTemplate):
1129
1137
}
1130
1138
1131
1139
@property
1132
- def template (self ):
1140
+ def template (self ) -> str :
1133
1141
return (
1134
1142
"{{ '<|startoftext|>' }}"
1135
1143
"{% for message in messages %}"
@@ -1153,7 +1161,7 @@ class YiAITemplate(BaseTemplate):
1153
1161
}
1154
1162
1155
1163
@property
1156
- def template (self ):
1164
+ def template (self ) -> str :
1157
1165
return (
1158
1166
"{% for message in messages %}"
1159
1167
"{{ '<|im_start|>' + message['role'] + '\\ n' + message['content'] + '<|im_end|>' + '\\ n' }}"
@@ -1203,6 +1211,6 @@ def template(self):
1203
1211
{"role" : "assistant" , "content" : "I'm doing great. How can I help you today?" },
1204
1212
{"role" : "user" , "content" : "I'd like to show off how chat templating works!" },
1205
1213
]
1206
- template = get_prompt_adapter (prompt_name = "deepseek " )
1214
+ template = get_prompt_adapter (prompt_name = "yi " )
1207
1215
messages = template .postprocess_messages (chat )
1208
1216
print (template .apply_chat_template (messages ))
0 commit comments