template.py 7.38 KB
Newer Older
chenzk's avatar
v1.0  
chenzk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from dataclasses import dataclass
from typing import Dict


@dataclass
class Template:
    template_name:str
    system_format: str
    user_format: str
    assistant_format: str
    system: str
    stop_word: str
    # stop_token_id: int


template_dict: Dict[str, Template] = dict()


def register_template(template_name, system_format, user_format, assistant_format, system, stop_word=None):
    template_dict[template_name] = Template(
        template_name=template_name,
        system_format=system_format,
        user_format=user_format,
        assistant_format=assistant_format,
        system=system,
        stop_word=stop_word,
        # stop_token_id=stop_token_id
    )


# 注册template
register_template(
    template_name='default',
    system_format='System: {content}\n\n',
    user_format='User: {content}\nAssistant: ',
    assistant_format='{content} {stop_token}',
    system=None,
    stop_word=None
)

register_template(
    template_name='internlm',
    system_format="<|System|>:{content}\n",
    user_format='<|User|>:{content}\n<|Bot|>:',
    assistant_format='{content}</s>\n',
    system="You are an AI assistant whose name is InternLM (书生·浦语).\n"
        "- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.\n"
        "- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.",
    stop_word='</s>'
)

register_template(
    template_name='internlm2',
    system_format='<|im_start|>system\n{content}<|im_end|>\n',
    user_format='<|im_start|>user\n{content}<|im_end|>\n<|im_start|>assistant\n',
    assistant_format='{content}<|im_end|>\n',
    system="You are an AI assistant whose name is InternLM (书生·浦语).\n"
        "- InternLM (书生·浦语) is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.\n"
        "- InternLM (书生·浦语) can understand and communicate fluently in the language chosen by the user such as English and 中文.",
    stop_word='<|im_end|>'
)

register_template(
    template_name='qwen',
    system_format='<|im_start|>system\n{content}<|im_end|>\n',
    user_format='<|im_start|>user\n{content}<|im_end|>\n<|im_start|>assistant\n',
    assistant_format='{content}<|im_end|>\n',
    system="You are a helpful assistant.",
    stop_word='<|im_end|>'
)

register_template(
    template_name='yi',
    system_format='<|im_start|>system\n{content}<|im_end|>\n',
    user_format='<|im_start|>user\n{content}<|im_end|>\n<|im_start|>assistant\n',
    assistant_format='{content}<|im_end|>\n',
    system=None,
    stop_word='<|im_end|>'
)

register_template(
    template_name="orion",
    system_format='<s>',
    user_format='Human: {content}\n\nAssistant: </s>',
    assistant_format='{content}</s>',
    system='',
    stop_word='</s>',
)

register_template(
    template_name='deepseek',
    system_format=None,
    user_format='User: {content}\n\nAssistant: ',
    assistant_format='{content}<|end▁of▁sentence|>',
    system=None,
    stop_word='<|end▁of▁sentence|>'
)

# todo 更优雅的实现方式
register_template(
    template_name='chatglm2',
    system_format=None,
    user_format='[Round {idx}]\n\n问:{content}\n\n答:',
    assistant_format='{content}',
    system=None,
    stop_word='</s>',
)

register_template(
    template_name='chatglm3',
    system_format='{content}',
    user_format='{content}',
    assistant_format='{content}',
    system="You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.",
    stop_word='</s>',
)

register_template(
    template_name='ziya2',
    system_format=None,
    user_format='<human>:{content} <bot>:',
    assistant_format='{content}</s>',
    system=None,
    stop_word='</s>',
)

register_template(
    template_name="xverse",
    system_format=None,
    user_format='Human: {content}\n\nAssistant: ',
    assistant_format='{content}<|endoftext|>',
    system=None,
    stop_word='<|endoftext|>',
)

register_template(
    template_name='minicpm',
    system_format=None,
    user_format='<用户>{content}<AI>',
    assistant_format='{content}</s>',
    system=None,
    stop_word='</s>'
)

register_template(
    template_name='zephyr',
    system_format='<|system|>\n{content}</s>',
    user_format='<|user|>\n{content}</s>\n<|assistant|>\n',
    assistant_format='{content}</s>\n',
    system=None,
    stop_word='</s>'
)

register_template(
    template_name='mistral',
    system_format='<s>',
    user_format='[INST]{content}[/INST]',
    assistant_format='{content}</s>',
    system='',
    stop_word='</s>'
)

register_template(
    template_name='mixtral',
    system_format='<s>',
    user_format='[INST]{content}[/INST]',
    assistant_format='{content}</s>',
    system='',
    stop_word='</s>'
)

register_template(
    template_name='baichuan',
    system_format=None,
    user_format='<reserved_102>{content}<reserved_103>',
    assistant_format='{content}</s>',
    system=None,
    stop_word='</s>'
)

register_template(
    template_name='baichuan2',
    system_format=None,
    user_format='<reserved_106>{content}<reserved_107>',
    assistant_format='{content}</s>',
    system=None,
    stop_word='</s>'
)

register_template(
    template_name='vicuna',
    system_format='{content}\n',
    user_format='USER: {content} ASSISTANT:',
    assistant_format='{content}</s>',
    system="A chat between a curious user and an artificial intelligence assistant. "
        "The assistant gives helpful, detailed, and polite answers to the user's questions.",
    stop_word='</s>'
)

register_template(
    template_name='llama2',
    system_format='<<SYS>>\n{content}\n<</SYS>>\n\n',
    user_format='[INST]{content}[/INST]',
    assistant_format='{content} </s>',
    system="You are a helpful, respectful and honest assistant. "
        "Always answer as helpfully as possible, while being safe. "
        "Your answers should not include any harmful, unethical, "
        "racist, sexist, toxic, dangerous, or illegal content. "
        "Please ensure that your responses are socially unbiased and positive in nature.\n\n"
        "If a question does not make any sense, or is not factually coherent, "
        "explain why instead of answering something not correct. "
        "If you don't know the answer to a question, please don't share false information.",
    stop_word='</s>'
)

register_template(
    template_name='llama3',
    system_format='<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{content}<|eot_id|>',
    user_format='<|start_header_id|>user<|end_header_id|>\n\n{content}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n',
    assistant_format='{content}<|eot_id|>',
    system=None,
    stop_word='<|eot_id|>'
)

register_template(
    template_name='gemma',
    system_format='<bos>',
    user_format='<start_of_turn>user\n{content}<end_of_turn>\n<start_of_turn>model\n',
    assistant_format='{content}<eos>\n',
    system='',
    stop_word='<eos>'
)

register_template(
    template_name='phi3',
    system_format=None,
    user_format='<|user|>\n{content}<|end|>\n<|assistant|>',
    assistant_format='{content}<|end|>\n',
    system=None,
    stop_word='<|end|>'
)

# if __name__ == '__main__':
#     model_name_or_path = ''