template.py 65 KB
Newer Older
chenych's avatar
chenych committed
1
# Copyright 2025 the LlamaFactory team.
chenych's avatar
chenych committed
2
3
4
5
6
7
8
9
10
11
12
13
14
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
15
from dataclasses import dataclass
chenych's avatar
chenych committed
16
from typing import TYPE_CHECKING, Optional, Union
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
17

luopl's avatar
luopl committed
18
19
from typing_extensions import override

luopl's avatar
luopl committed
20
from ..extras import logging
luopl's avatar
luopl committed
21
from ..extras.misc import check_version
chenych's avatar
chenych committed
22
from .data_utils import Role
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
23
from .formatter import EmptyFormatter, FunctionFormatter, StringFormatter, ToolFormatter
luopl's avatar
luopl committed
24
from .mm_plugin import get_mm_plugin
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
25
26
27
28
29


if TYPE_CHECKING:
    from transformers import PreTrainedTokenizer

luopl's avatar
luopl committed
30
    from ..hparams import DataArguments
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
31
    from .formatter import SLOTS, Formatter
luopl's avatar
luopl committed
32
    from .mm_plugin import BasePlugin
luopl's avatar
luopl committed
33
    from .tool_utils import FunctionCall
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
34
35


luopl's avatar
luopl committed
36
logger = logging.get_logger(__name__)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
37
38
39
40
41
42
43
44
45
46


@dataclass
class Template:
    format_user: "Formatter"
    format_assistant: "Formatter"
    format_system: "Formatter"
    format_function: "Formatter"
    format_observation: "Formatter"
    format_tools: "Formatter"
chenych's avatar
chenych committed
47
    format_prefix: "Formatter"
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
48
    default_system: str
chenych's avatar
chenych committed
49
50
    stop_words: list[str]
    thought_words: tuple[str, str]
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
51
52
    efficient_eos: bool
    replace_eos: bool
luopl's avatar
luopl committed
53
54
    replace_jinja_template: bool
    mm_plugin: "BasePlugin"
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
55
56
57
58

    def encode_oneturn(
        self,
        tokenizer: "PreTrainedTokenizer",
chenych's avatar
chenych committed
59
        messages: list[dict[str, str]],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
60
61
        system: Optional[str] = None,
        tools: Optional[str] = None,
chenych's avatar
chenych committed
62
63
    ) -> tuple[list[int], list[int]]:
        r"""Return a single pair of token ids representing prompt and response respectively."""
chenych's avatar
chenych committed
64
        encoded_messages = self._encode(tokenizer, messages, system, tools)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
65
        prompt_ids = []
chenych's avatar
chenych committed
66
67
68
        for encoded_ids in encoded_messages[:-1]:
            prompt_ids += encoded_ids

chenych's avatar
chenych committed
69
70
        response_ids = encoded_messages[-1]
        return prompt_ids, response_ids
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
71
72
73
74

    def encode_multiturn(
        self,
        tokenizer: "PreTrainedTokenizer",
chenych's avatar
chenych committed
75
        messages: list[dict[str, str]],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
76
77
        system: Optional[str] = None,
        tools: Optional[str] = None,
chenych's avatar
chenych committed
78
79
    ) -> list[tuple[list[int], list[int]]]:
        r"""Return multiple pairs of token ids representing prompts and responses respectively."""
chenych's avatar
chenych committed
80
81
82
        encoded_messages = self._encode(tokenizer, messages, system, tools)
        return [(encoded_messages[i], encoded_messages[i + 1]) for i in range(0, len(encoded_messages), 2)]

chenych's avatar
chenych committed
83
84
    def extract_tool(self, content: str) -> Union[str, list["FunctionCall"]]:
        r"""Extract tool message."""
chenych's avatar
chenych committed
85
        return self.format_tools.extract(content)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
86

chenych's avatar
chenych committed
87
88
    def get_stop_token_ids(self, tokenizer: "PreTrainedTokenizer") -> list[int]:
        r"""Return stop token ids."""
luopl's avatar
luopl committed
89
90
91
92
93
94
        stop_token_ids = {tokenizer.eos_token_id}
        for token in self.stop_words:
            stop_token_ids.add(tokenizer.convert_tokens_to_ids(token))

        return list(stop_token_ids)

chenych's avatar
chenych committed
95
96
    def _convert_elements_to_ids(self, tokenizer: "PreTrainedTokenizer", elements: "SLOTS") -> list[int]:
        r"""Convert elements to token ids."""
chenych's avatar
chenych committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        token_ids = []
        for elem in elements:
            if isinstance(elem, str):
                if len(elem) != 0:
                    token_ids += tokenizer.encode(elem, add_special_tokens=False)
            elif isinstance(elem, dict):
                token_ids += [tokenizer.convert_tokens_to_ids(elem.get("token"))]
            elif isinstance(elem, set):
                if "bos_token" in elem and tokenizer.bos_token_id is not None:
                    token_ids += [tokenizer.bos_token_id]
                elif "eos_token" in elem and tokenizer.eos_token_id is not None:
                    token_ids += [tokenizer.eos_token_id]
            else:
                raise ValueError(f"Input must be string, set[str] or dict[str, str], got {type(elem)}")

        return token_ids

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
114
115
116
    def _encode(
        self,
        tokenizer: "PreTrainedTokenizer",
chenych's avatar
chenych committed
117
        messages: list[dict[str, str]],
chenych's avatar
chenych committed
118
119
        system: Optional[str],
        tools: Optional[str],
chenych's avatar
chenych committed
120
121
122
    ) -> list[list[int]]:
        r"""Encode formatted inputs to pairs of token ids.

chenych's avatar
chenych committed
123
        Turn 0: prefix + system + query        resp
chenych's avatar
chenych committed
124
        Turn t: query                          resp.
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
125
126
127
128
129
        """
        system = system or self.default_system
        encoded_messages = []
        for i, message in enumerate(messages):
            elements = []
chenych's avatar
chenych committed
130
131
132
133
134
135
136

            if i == 0:
                elements += self.format_prefix.apply()
                if system or tools:
                    tool_text = self.format_tools.apply(content=tools)[0] if tools else ""
                    elements += self.format_system.apply(content=(system + tool_text))

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
137
138
139
140
141
142
143
144
145
146
147
148
149
            if message["role"] == Role.USER.value:
                elements += self.format_user.apply(content=message["content"], idx=str(i // 2))
            elif message["role"] == Role.ASSISTANT.value:
                elements += self.format_assistant.apply(content=message["content"])
            elif message["role"] == Role.OBSERVATION.value:
                elements += self.format_observation.apply(content=message["content"])
            elif message["role"] == Role.FUNCTION.value:
                elements += self.format_function.apply(content=message["content"])
            else:
                raise NotImplementedError("Unexpected role: {}".format(message["role"]))

            encoded_messages.append(self._convert_elements_to_ids(tokenizer, elements))

chenych's avatar
chenych committed
150
        return encoded_messages
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
151

chenych's avatar
chenych committed
152
153
    @staticmethod
    def _add_or_replace_eos_token(tokenizer: "PreTrainedTokenizer", eos_token: str) -> None:
chenych's avatar
chenych committed
154
        r"""Add or replace eos token to the tokenizer."""
chenych's avatar
chenych committed
155
156
        is_added = tokenizer.eos_token_id is None
        num_added_tokens = tokenizer.add_special_tokens({"eos_token": eos_token})
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
157

chenych's avatar
chenych committed
158
159
160
161
162
163
164
165
166
        if is_added:
            logger.info_rank0(f"Add eos token: {tokenizer.eos_token}.")
        else:
            logger.info_rank0(f"Replace eos token: {tokenizer.eos_token}.")

        if num_added_tokens > 0:
            logger.warning_rank0("New tokens have been added, make sure `resize_vocab` is True.")

    def fix_special_tokens(self, tokenizer: "PreTrainedTokenizer") -> None:
chenych's avatar
chenych committed
167
        r"""Add eos token and pad token to the tokenizer."""
chenych's avatar
chenych committed
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
        stop_words = self.stop_words
        if self.replace_eos:
            if not stop_words:
                raise ValueError("Stop words are required to replace the EOS token.")

            self._add_or_replace_eos_token(tokenizer, eos_token=stop_words[0])
            stop_words = stop_words[1:]

        if tokenizer.eos_token_id is None:
            self._add_or_replace_eos_token(tokenizer, eos_token="<|endoftext|>")

        if tokenizer.pad_token_id is None:
            tokenizer.pad_token = tokenizer.eos_token
            logger.info_rank0(f"Add pad token: {tokenizer.pad_token}")

        if stop_words:
            num_added_tokens = tokenizer.add_special_tokens(
                dict(additional_special_tokens=stop_words), replace_additional_special_tokens=False
            )
            logger.info_rank0("Add {} to stop words.".format(",".join(stop_words)))
            if num_added_tokens > 0:
                logger.warning_rank0("New tokens have been added, make sure `resize_vocab` is True.")

    @staticmethod
    def _jinja_escape(content: str) -> str:
chenych's avatar
chenych committed
193
        r"""Escape single quotes in content."""
chenych's avatar
chenych committed
194
195
196
197
        return content.replace("'", r"\'")

    @staticmethod
    def _convert_slots_to_jinja(slots: "SLOTS", tokenizer: "PreTrainedTokenizer", placeholder: str = "content") -> str:
chenych's avatar
chenych committed
198
        r"""Convert slots to jinja template."""
chenych's avatar
chenych committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
        slot_items = []
        for slot in slots:
            if isinstance(slot, str):
                slot_pieces = slot.split("{{content}}")
                if slot_pieces[0]:
                    slot_items.append("'" + Template._jinja_escape(slot_pieces[0]) + "'")
                if len(slot_pieces) > 1:
                    slot_items.append(placeholder)
                    if slot_pieces[1]:
                        slot_items.append("'" + Template._jinja_escape(slot_pieces[1]) + "'")
            elif isinstance(slot, set):  # do not use {{ eos_token }} since it may be replaced
                if "bos_token" in slot and tokenizer.bos_token_id is not None:
                    slot_items.append("'" + tokenizer.bos_token + "'")
                elif "eos_token" in slot and tokenizer.eos_token_id is not None:
                    slot_items.append("'" + tokenizer.eos_token + "'")
            elif isinstance(slot, dict):
                raise ValueError("Dict is not supported.")

        return " + ".join(slot_items)

    def _get_jinja_template(self, tokenizer: "PreTrainedTokenizer") -> str:
chenych's avatar
chenych committed
220
        r"""Return the jinja template."""
chenych's avatar
chenych committed
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
246
247
        prefix = self._convert_slots_to_jinja(self.format_prefix.apply(), tokenizer)
        system = self._convert_slots_to_jinja(self.format_system.apply(), tokenizer, placeholder="system_message")
        user = self._convert_slots_to_jinja(self.format_user.apply(), tokenizer)
        assistant = self._convert_slots_to_jinja(self.format_assistant.apply(), tokenizer)
        jinja_template = ""
        if prefix:
            jinja_template += "{{ " + prefix + " }}"

        if self.default_system:
            jinja_template += "{% set system_message = '" + self._jinja_escape(self.default_system) + "' %}"

        jinja_template += (
            "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}"
            "{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% endif %}"
            "{% if system_message is defined %}{{ " + system + " }}{% endif %}"
            "{% for message in loop_messages %}"
            "{% set content = message['content'] %}"
            "{% if message['role'] == 'user' %}"
            "{{ " + user + " }}"
            "{% elif message['role'] == 'assistant' %}"
            "{{ " + assistant + " }}"
            "{% endif %}"
            "{% endfor %}"
        )
        return jinja_template

    def fix_jinja_template(self, tokenizer: "PreTrainedTokenizer") -> None:
chenych's avatar
chenych committed
248
        r"""Replace the jinja template in the tokenizer."""
chenych's avatar
chenych committed
249
250
251
252
253
254
255
256
257
258
        if tokenizer.chat_template is None or self.replace_jinja_template:
            try:
                tokenizer.chat_template = self._get_jinja_template(tokenizer)
            except ValueError as e:
                logger.info_rank0(f"Cannot add this chat template to tokenizer: {e}.")

    @staticmethod
    def _convert_slots_to_ollama(
        slots: "SLOTS", tokenizer: "PreTrainedTokenizer", placeholder: str = "content"
    ) -> str:
chenych's avatar
chenych committed
259
        r"""Convert slots to ollama template."""
chenych's avatar
chenych committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
        slot_items = []
        for slot in slots:
            if isinstance(slot, str):
                slot_pieces = slot.split("{{content}}")
                if slot_pieces[0]:
                    slot_items.append(slot_pieces[0])
                if len(slot_pieces) > 1:
                    slot_items.append("{{ " + placeholder + " }}")
                    if slot_pieces[1]:
                        slot_items.append(slot_pieces[1])
            elif isinstance(slot, set):  # do not use {{ eos_token }} since it may be replaced
                if "bos_token" in slot and tokenizer.bos_token_id is not None:
                    slot_items.append(tokenizer.bos_token)
                elif "eos_token" in slot and tokenizer.eos_token_id is not None:
                    slot_items.append(tokenizer.eos_token)
            elif isinstance(slot, dict):
                raise ValueError("Dict is not supported.")

        return "".join(slot_items)

    def _get_ollama_template(self, tokenizer: "PreTrainedTokenizer") -> str:
chenych's avatar
chenych committed
281
        r"""Return the ollama template."""
chenych's avatar
chenych committed
282
283
284
285
286
287
288
289
290
291
292
        prefix = self._convert_slots_to_ollama(self.format_prefix.apply(), tokenizer)
        system = self._convert_slots_to_ollama(self.format_system.apply(), tokenizer, placeholder=".System")
        user = self._convert_slots_to_ollama(self.format_user.apply(), tokenizer, placeholder=".Content")
        assistant = self._convert_slots_to_ollama(self.format_assistant.apply(), tokenizer, placeholder=".Content")
        return (
            f"{prefix}{{{{ if .System }}}}{system}{{{{ end }}}}"
            f"""{{{{ range .Messages }}}}{{{{ if eq .Role "user" }}}}{user}"""
            f"""{{{{ else if eq .Role "assistant" }}}}{assistant}{{{{ end }}}}{{{{ end }}}}"""
        )

    def get_ollama_modelfile(self, tokenizer: "PreTrainedTokenizer") -> str:
chenych's avatar
chenych committed
293
        r"""Return the ollama modelfile.
chenych's avatar
chenych committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307

        TODO: support function calling.
        """
        modelfile = "# ollama modelfile auto-generated by llamafactory\n\n"
        modelfile += f'FROM .\n\nTEMPLATE """{self._get_ollama_template(tokenizer)}"""\n\n'

        if self.default_system:
            modelfile += f'SYSTEM """{self.default_system}"""\n\n'

        for stop_token_id in self.get_stop_token_ids(tokenizer):
            modelfile += f'PARAMETER stop "{tokenizer.convert_ids_to_tokens(stop_token_id)}"\n'

        modelfile += "PARAMETER num_ctx 4096\n"
        return modelfile
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
308
309
310
311


@dataclass
class Llama2Template(Template):
chenych's avatar
chenych committed
312
313
    r"""A template that fuse the system message to first user message."""

luopl's avatar
luopl committed
314
    @override
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
315
316
317
    def _encode(
        self,
        tokenizer: "PreTrainedTokenizer",
chenych's avatar
chenych committed
318
        messages: list[dict[str, str]],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
319
320
        system: str,
        tools: str,
chenych's avatar
chenych committed
321
    ) -> list[list[int]]:
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
322
323
324
325
        system = system or self.default_system
        encoded_messages = []
        for i, message in enumerate(messages):
            elements = []
chenych's avatar
chenych committed
326

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
327
            system_text = ""
chenych's avatar
chenych committed
328
329
330
331
332
333
            if i == 0:
                elements += self.format_prefix.apply()
                if system or tools:
                    tool_text = self.format_tools.apply(content=tools)[0] if tools else ""
                    system_text = self.format_system.apply(content=(system + tool_text))[0]

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
334
335
336
337
338
339
340
341
342
343
344
345
346
            if message["role"] == Role.USER.value:
                elements += self.format_user.apply(content=system_text + message["content"])
            elif message["role"] == Role.ASSISTANT.value:
                elements += self.format_assistant.apply(content=message["content"])
            elif message["role"] == Role.OBSERVATION.value:
                elements += self.format_observation.apply(content=message["content"])
            elif message["role"] == Role.FUNCTION.value:
                elements += self.format_function.apply(content=message["content"])
            else:
                raise NotImplementedError("Unexpected role: {}".format(message["role"]))

            encoded_messages.append(self._convert_elements_to_ids(tokenizer, elements))

chenych's avatar
chenych committed
347
        return encoded_messages
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
348

chenych's avatar
chenych committed
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
    def _get_jinja_template(self, tokenizer: "PreTrainedTokenizer") -> str:
        prefix = self._convert_slots_to_jinja(self.format_prefix.apply(), tokenizer)
        system_message = self._convert_slots_to_jinja(
            self.format_system.apply(), tokenizer, placeholder="system_message"
        )
        user_message = self._convert_slots_to_jinja(self.format_user.apply(), tokenizer)
        assistant_message = self._convert_slots_to_jinja(self.format_assistant.apply(), tokenizer)
        jinja_template = ""
        if prefix:
            jinja_template += "{{ " + prefix + " }}"

        if self.default_system:
            jinja_template += "{% set system_message = '" + self._jinja_escape(self.default_system) + "' %}"

        jinja_template += (
            "{% if messages[0]['role'] == 'system' %}{% set loop_messages = messages[1:] %}"
            "{% set system_message = messages[0]['content'] %}{% else %}{% set loop_messages = messages %}{% endif %}"
            "{% for message in loop_messages %}"
            "{% if loop.index0 == 0 and system_message is defined %}"
            "{% set content = " + system_message + " + message['content'] %}"
            "{% else %}{% set content = message['content'] %}{% endif %}"
            "{% if message['role'] == 'user' %}"
            "{{ " + user_message + " }}"
            "{% elif message['role'] == 'assistant' %}"
            "{{ " + assistant_message + " }}"
            "{% endif %}"
            "{% endfor %}"
        )
        return jinja_template

Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
379

chenych's avatar
chenych committed
380
TEMPLATES: dict[str, "Template"] = {}
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
381
382


chenych's avatar
chenych committed
383
def register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
384
385
386
387
388
389
390
    name: str,
    format_user: Optional["Formatter"] = None,
    format_assistant: Optional["Formatter"] = None,
    format_system: Optional["Formatter"] = None,
    format_function: Optional["Formatter"] = None,
    format_observation: Optional["Formatter"] = None,
    format_tools: Optional["Formatter"] = None,
chenych's avatar
chenych committed
391
    format_prefix: Optional["Formatter"] = None,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
392
    default_system: str = "",
chenych's avatar
chenych committed
393
394
    stop_words: Optional[list[str]] = None,
    thought_words: Optional[tuple[str, str]] = None,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
395
396
    efficient_eos: bool = False,
    replace_eos: bool = False,
luopl's avatar
luopl committed
397
    replace_jinja_template: bool = False,
luopl's avatar
luopl committed
398
    mm_plugin: "BasePlugin" = get_mm_plugin(name="base"),
chenych's avatar
chenych committed
399
    template_class: type["Template"] = Template,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
400
) -> None:
chenych's avatar
chenych committed
401
    r"""Register a chat template.
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
402
403
404

    To add the following chat template:
    ```
luopl's avatar
luopl committed
405
406
407
408
    <s><user>user prompt here
    <model>model response here</s>
    <user>user prompt here
    <model>model response here</s>
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
409
410
411
412
    ```

    The corresponding code should be:
    ```
chenych's avatar
chenych committed
413
    register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
414
        name="custom",
luopl's avatar
luopl committed
415
416
417
        format_user=StringFormatter(slots=["<user>{{content}}\n<model>"]),
        format_assistant=StringFormatter(slots=["{{content}}</s>\n"]),
        format_prefix=EmptyFormatter("<s>"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
418
419
420
    )
    ```
    """
chenych's avatar
chenych committed
421
422
423
    if name in TEMPLATES:
        raise ValueError(f"Template {name} already exists.")

luopl's avatar
luopl committed
424
    default_slots = ["{{content}}"] if efficient_eos else ["{{content}}", {"eos_token"}]
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
425
    default_user_formatter = StringFormatter(slots=["{{content}}"])
luopl's avatar
luopl committed
426
427
    default_assistant_formatter = StringFormatter(slots=default_slots)
    default_function_formatter = FunctionFormatter(slots=default_slots, tool_format="default")
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
428
    default_tool_formatter = ToolFormatter(tool_format="default")
chenych's avatar
chenych committed
429
430
    default_prefix_formatter = EmptyFormatter()
    TEMPLATES[name] = template_class(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
431
432
433
434
435
436
        format_user=format_user or default_user_formatter,
        format_assistant=format_assistant or default_assistant_formatter,
        format_system=format_system or default_user_formatter,
        format_function=format_function or default_function_formatter,
        format_observation=format_observation or format_user or default_user_formatter,
        format_tools=format_tools or default_tool_formatter,
chenych's avatar
chenych committed
437
        format_prefix=format_prefix or default_prefix_formatter,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
438
        default_system=default_system,
luopl's avatar
luopl committed
439
        stop_words=stop_words or [],
chenych's avatar
chenych committed
440
        thought_words=thought_words or ("<think>", "</think>"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
441
442
        efficient_eos=efficient_eos,
        replace_eos=replace_eos,
luopl's avatar
luopl committed
443
444
        replace_jinja_template=replace_jinja_template,
        mm_plugin=mm_plugin,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
445
446
447
    )


chenych's avatar
chenych committed
448
def parse_template(tokenizer: "PreTrainedTokenizer") -> "Template":
chenych's avatar
chenych committed
449
    r"""Extract a chat template from the tokenizer."""
chenych's avatar
chenych committed
450

chenych's avatar
chenych committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
    def find_diff(short_str: str, long_str: str) -> str:
        i, j = 0, 0
        diff = ""
        while i < len(short_str) and j < len(long_str):
            if short_str[i] == long_str[j]:
                i += 1
                j += 1
            else:
                diff += long_str[j]
                j += 1

        return diff

    prefix = tokenizer.decode(tokenizer.encode(""))

    messages = [{"role": "system", "content": "{{content}}"}]
    system_slot = tokenizer.apply_chat_template(messages, add_generation_prompt=False, tokenize=False)[len(prefix) :]

    messages = [{"role": "system", "content": ""}, {"role": "user", "content": "{{content}}"}]
    user_slot_empty_system = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
    user_slot_empty_system = user_slot_empty_system[len(prefix) :]

    messages = [{"role": "user", "content": "{{content}}"}]
    user_slot = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
    user_slot = user_slot[len(prefix) :]

    messages = [{"role": "user", "content": "{{content}}"}, {"role": "assistant", "content": "{{content}}"}]
    assistant_slot = tokenizer.apply_chat_template(messages, add_generation_prompt=False, tokenize=False)
    assistant_slot = assistant_slot[len(prefix) + len(user_slot) :]

    if len(user_slot) > len(user_slot_empty_system):
        default_system = find_diff(user_slot_empty_system, user_slot)
        sole_system = system_slot.replace("{{content}}", default_system, 1)
        user_slot = user_slot[len(sole_system) :]
    else:  # if defaut_system is empty, user_slot_empty_system will be longer than user_slot
        default_system = ""

    return Template(
        format_user=StringFormatter(slots=[user_slot]),
        format_assistant=StringFormatter(slots=[assistant_slot]),
        format_system=StringFormatter(slots=[system_slot]),
        format_function=FunctionFormatter(slots=[assistant_slot], tool_format="default"),
        format_observation=StringFormatter(slots=[user_slot]),
        format_tools=ToolFormatter(tool_format="default"),
        format_prefix=EmptyFormatter(slots=[prefix]) if prefix else EmptyFormatter(),
        default_system=default_system,
        stop_words=[],
        thought_words=("<think>", "</think>"),
        efficient_eos=False,
        replace_eos=False,
        replace_jinja_template=False,
        mm_plugin=get_mm_plugin(name="base"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
503
504
505
    )


luopl's avatar
luopl committed
506
def get_template_and_fix_tokenizer(tokenizer: "PreTrainedTokenizer", data_args: "DataArguments") -> "Template":
chenych's avatar
chenych committed
507
    r"""Get chat template and fixes the tokenizer."""
luopl's avatar
luopl committed
508
    if data_args.template is None:
chenych's avatar
chenych committed
509
510
511
512
513
514
        if isinstance(tokenizer.chat_template, str):
            logger.warning_rank0("`template` was not specified, try parsing the chat template from the tokenizer.")
            template = parse_template(tokenizer)
        else:
            logger.warning_rank0("`template` was not specified, use `empty` template.")
            template = TEMPLATES["empty"]  # placeholder
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
515
    else:
chenych's avatar
chenych committed
516
        if data_args.template not in TEMPLATES:
luopl's avatar
luopl committed
517
518
            raise ValueError(f"Template {data_args.template} does not exist.")

chenych's avatar
chenych committed
519
520
        template = TEMPLATES[data_args.template]

luopl's avatar
luopl committed
521
    if template.mm_plugin.__class__.__name__ != "BasePlugin":
luopl's avatar
luopl committed
522
        check_version("transformers>=4.45.0")
luopl's avatar
luopl committed
523
524
525

    if data_args.train_on_prompt and template.efficient_eos:
        raise ValueError("Current template does not support `train_on_prompt`.")
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
526

luopl's avatar
luopl committed
527
    if data_args.tool_format is not None:
luopl's avatar
luopl committed
528
        logger.info_rank0(f"Using tool format: {data_args.tool_format}.")
luopl's avatar
luopl committed
529
530
        default_slots = ["{{content}}"] if template.efficient_eos else ["{{content}}", {"eos_token"}]
        template.format_function = FunctionFormatter(slots=default_slots, tool_format=data_args.tool_format)
luopl's avatar
luopl committed
531
        template.format_tools = ToolFormatter(tool_format=data_args.tool_format)
chenych's avatar
chenych committed
532

chenych's avatar
chenych committed
533
534
    template.fix_special_tokens(tokenizer)
    template.fix_jinja_template(tokenizer)
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
535
536
537
    return template


chenych's avatar
chenych committed
538
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
539
540
    name="alpaca",
    format_user=StringFormatter(slots=["### Instruction:\n{{content}}\n\n### Response:\n"]),
luopl's avatar
luopl committed
541
    format_assistant=StringFormatter(slots=["{{content}}", {"eos_token"}, "\n\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
542
    default_system=(
luopl's avatar
luopl committed
543
        "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n"
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
544
    ),
luopl's avatar
luopl committed
545
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
546
547
548
)


chenych's avatar
chenych committed
549
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
550
551
    name="aquila",
    format_user=StringFormatter(slots=["Human: {{content}}###Assistant:"]),
luopl's avatar
luopl committed
552
553
    format_assistant=StringFormatter(slots=["{{content}}###"]),
    format_system=StringFormatter(slots=["System: {{content}}###"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
554
555
556
557
558
559
560
561
    default_system=(
        "A chat between a curious human and an artificial intelligence assistant. "
        "The assistant gives helpful, detailed, and polite answers to the human's questions."
    ),
    stop_words=["</s>"],
)


chenych's avatar
chenych committed
562
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
563
564
565
566
567
568
569
570
    name="atom",
    format_user=StringFormatter(
        slots=[{"bos_token"}, "Human: {{content}}\n", {"eos_token"}, {"bos_token"}, "Assistant:"]
    ),
    format_assistant=StringFormatter(slots=["{{content}}\n", {"eos_token"}]),
)


chenych's avatar
chenych committed
571
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
572
573
574
575
576
577
    name="baichuan",
    format_user=StringFormatter(slots=[{"token": "<reserved_102>"}, "{{content}}", {"token": "<reserved_103>"}]),
    efficient_eos=True,
)


chenych's avatar
chenych committed
578
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
579
580
581
582
583
584
    name="baichuan2",
    format_user=StringFormatter(slots=["<reserved_106>{{content}}<reserved_107>"]),
    efficient_eos=True,
)


chenych's avatar
chenych committed
585
586
587
588
589
590
591
592
593
594
595
register_template(
    name="bailing",
    format_user=StringFormatter(slots=["<role>HUMAN</role>{{content}}<role>ASSISTANT</role>"]),
    format_system=StringFormatter(slots=["<role>SYSTEM</role>{{content}}"]),
    format_observation=StringFormatter(slots=["<role>OBSERVATION</role>{{content}}<role>ASSISTANT</role>"]),
    stop_words=["<|endoftext|>"],
    efficient_eos=True,
)


register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
596
597
    name="belle",
    format_user=StringFormatter(slots=["Human: {{content}}\n\nBelle: "]),
luopl's avatar
luopl committed
598
    format_assistant=StringFormatter(slots=["{{content}}", {"eos_token"}, "\n\n"]),
chenych's avatar
chenych committed
599
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
600
601
602
)


chenych's avatar
chenych committed
603
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
604
605
606
607
608
    name="bluelm",
    format_user=StringFormatter(slots=[{"token": "[|Human|]:"}, "{{content}}", {"token": "[|AI|]:"}]),
)


chenych's avatar
chenych committed
609
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
610
611
    name="breeze",
    format_user=StringFormatter(slots=["[INST] {{content}} [/INST] "]),
chenych's avatar
chenych committed
612
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
613
614
615
616
    efficient_eos=True,
)


chenych's avatar
chenych committed
617
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
618
619
    name="chatglm2",
    format_user=StringFormatter(slots=["[Round {{idx}}]\n\n问:{{content}}\n\n答:"]),
chenych's avatar
chenych committed
620
    format_prefix=EmptyFormatter(slots=[{"token": "[gMASK]"}, {"token": "sop"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
621
622
623
624
    efficient_eos=True,
)


chenych's avatar
chenych committed
625
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
626
627
628
    name="chatglm3",
    format_user=StringFormatter(slots=[{"token": "<|user|>"}, "\n", "{{content}}", {"token": "<|assistant|>"}]),
    format_assistant=StringFormatter(slots=["\n", "{{content}}"]),
chenych's avatar
chenych committed
629
    format_system=StringFormatter(slots=[{"token": "<|system|>"}, "\n", "{{content}}"]),
luopl's avatar
luopl committed
630
    format_function=FunctionFormatter(slots=["{{content}}"], tool_format="glm4"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
631
632
633
    format_observation=StringFormatter(
        slots=[{"token": "<|observation|>"}, "\n", "{{content}}", {"token": "<|assistant|>"}]
    ),
chenych's avatar
chenych committed
634
635
    format_tools=ToolFormatter(tool_format="glm4"),
    format_prefix=EmptyFormatter(slots=[{"token": "[gMASK]"}, {"token": "sop"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
636
637
638
639
640
    stop_words=["<|user|>", "<|observation|>"],
    efficient_eos=True,
)


chenych's avatar
chenych committed
641
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
642
643
    name="chatml",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
644
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
645
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
chenych's avatar
chenych committed
646
    format_observation=StringFormatter(slots=["<|im_start|>tool\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
647
648
    stop_words=["<|im_end|>", "<|im_start|>"],
    replace_eos=True,
luopl's avatar
luopl committed
649
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
650
651
652
)


luopl's avatar
luopl committed
653
# copied from chatml template
chenych's avatar
chenych committed
654
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
655
656
    name="chatml_de",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
657
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
658
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
chenych's avatar
chenych committed
659
    format_observation=StringFormatter(slots=["<|im_start|>tool\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
660
661
662
    default_system="Du bist ein freundlicher und hilfsbereiter KI-Assistent.",
    stop_words=["<|im_end|>", "<|im_start|>"],
    replace_eos=True,
luopl's avatar
luopl committed
663
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
664
665
666
)


chenych's avatar
chenych committed
667
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
668
    name="codegeex2",
chenych's avatar
chenych committed
669
670
671
672
    format_prefix=EmptyFormatter(slots=[{"token": "[gMASK]"}, {"token": "sop"}]),
)


chenych's avatar
chenych committed
673
register_template(
chenych's avatar
chenych committed
674
675
676
    name="codegeex4",
    format_user=StringFormatter(slots=["<|user|>\n{{content}}<|assistant|>\n"]),
    format_system=StringFormatter(slots=["<|system|>\n{{content}}"]),
luopl's avatar
luopl committed
677
    format_function=FunctionFormatter(slots=["{{content}}"], tool_format="glm4"),
chenych's avatar
chenych committed
678
679
680
681
682
683
684
685
686
    format_observation=StringFormatter(slots=["<|observation|>\n{{content}}<|assistant|>\n"]),
    format_tools=ToolFormatter(tool_format="glm4"),
    format_prefix=EmptyFormatter(slots=["[gMASK]<sop>"]),
    default_system=(
        "你是一位智能编程助手,你叫CodeGeeX。你会为用户回答关于编程、代码、计算机方面的任何问题,"
        "并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。"
    ),
    stop_words=["<|user|>", "<|observation|>"],
    efficient_eos=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
687
688
689
)


chenych's avatar
chenych committed
690
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
691
692
693
694
695
696
697
698
699
    name="cohere",
    format_user=StringFormatter(
        slots=[
            (
                "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>{{content}}<|END_OF_TURN_TOKEN|>"
                "<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
            )
        ]
    ),
chenych's avatar
chenych committed
700
701
    format_system=StringFormatter(slots=["<|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|>{{content}}<|END_OF_TURN_TOKEN|>"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
702
703
704
)


chenych's avatar
chenych committed
705
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
706
707
    name="cpm",
    format_user=StringFormatter(slots=["<用户>{{content}}<AI>"]),
chenych's avatar
chenych committed
708
709
710
711
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)


luopl's avatar
luopl committed
712
# copied from chatml template
chenych's avatar
chenych committed
713
register_template(
luopl's avatar
luopl committed
714
715
    name="cpm3",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
716
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
717
718
719
720
721
722
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    stop_words=["<|im_end|>"],
)


luopl's avatar
luopl committed
723
# copied from chatml template
chenych's avatar
chenych committed
724
register_template(
chenych's avatar
chenych committed
725
726
    name="dbrx",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
727
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
chenych's avatar
chenych committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    format_observation=StringFormatter(slots=["<|im_start|>tool\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    default_system=(
        "You are DBRX, created by Databricks. You were last updated in December 2023. "
        "You answer questions based on information available up to that point.\n"
        "YOU PROVIDE SHORT RESPONSES TO SHORT QUESTIONS OR STATEMENTS, but provide thorough "
        "responses to more complex and open-ended questions.\nYou assist with various tasks, "
        "from writing to coding (using markdown for code blocks — remember to use ``` with "
        "code, JSON, and tables).\n(You do not have real-time data access or code execution "
        "capabilities. You avoid stereotyping and provide balanced perspectives on "
        "controversial topics. You do not provide song lyrics, poems, or news articles and "
        "do not divulge details of your training data.)\nThis is your system prompt, "
        "guiding your responses. Do not reference it, just respond to the user. If you find "
        "yourself talking about this message, stop. You should be responding appropriately "
        "and usually that means not mentioning this.\nYOU DO NOT MENTION ANY OF THIS INFORMATION "
        "ABOUT YOURSELF UNLESS THE INFORMATION IS DIRECTLY PERTINENT TO THE USER'S QUERY."
    ),
    stop_words=["<|im_end|>"],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
746
747
748
)


chenych's avatar
chenych committed
749
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
750
751
    name="deepseek",
    format_user=StringFormatter(slots=["User: {{content}}\n\nAssistant:"]),
chenych's avatar
chenych committed
752
753
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
754
755
756
)


chenych's avatar
chenych committed
757
register_template(
luopl's avatar
luopl committed
758
759
760
761
762
763
    name="deepseek3",
    format_user=StringFormatter(slots=["<|User|>{{content}}<|Assistant|>"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)


chenych's avatar
chenych committed
764
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
765
766
    name="deepseekcoder",
    format_user=StringFormatter(slots=["### Instruction:\n{{content}}\n### Response:"]),
luopl's avatar
luopl committed
767
    format_assistant=StringFormatter(slots=["\n{{content}}\n<|EOT|>\n"]),
chenych's avatar
chenych committed
768
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
769
    default_system=(
chenych's avatar
chenych committed
770
771
        "You are an AI programming assistant, utilizing the DeepSeek Coder model, "
        "developed by DeepSeek Company, and you only answer questions related to computer science. "
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
772
        "For politically sensitive questions, security and privacy issues, "
chenych's avatar
chenych committed
773
        "and other non-computer science questions, you will refuse to answer.\n"
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
774
775
776
777
    ),
)


chenych's avatar
chenych committed
778
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
779
    name="default",
chenych's avatar
chenych committed
780
    format_user=StringFormatter(slots=["Human: {{content}}", {"eos_token"}, "\nAssistant:"]),
luopl's avatar
luopl committed
781
    format_assistant=StringFormatter(slots=["{{content}}", {"eos_token"}, "\n"]),
chenych's avatar
chenych committed
782
783
    format_system=StringFormatter(slots=["System: {{content}}", {"eos_token"}, "\n"]),
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
784
785
786
)


chenych's avatar
chenych committed
787
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
788
    name="empty",
chenych's avatar
chenych committed
789
    format_assistant=StringFormatter(slots=["{{content}}"]),
chenych's avatar
chenych committed
790
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
791
792
793
)


chenych's avatar
chenych committed
794
register_template(
luopl's avatar
luopl committed
795
796
    name="exaone",
    format_user=StringFormatter(slots=["[|user|]{{content}}\n[|assistant|]"]),
luopl's avatar
luopl committed
797
    format_assistant=StringFormatter(slots=["{{content}}", {"eos_token"}, "\n"]),
luopl's avatar
luopl committed
798
799
800
801
    format_system=StringFormatter(slots=["[|system|]{{content}}[|endofturn|]\n"]),
)


chenych's avatar
chenych committed
802
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
803
804
    name="falcon",
    format_user=StringFormatter(slots=["User: {{content}}\nFalcon:"]),
luopl's avatar
luopl committed
805
    format_assistant=StringFormatter(slots=["{{content}}\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
806
807
808
809
    efficient_eos=True,
)


chenych's avatar
chenych committed
810
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
811
    name="fewshot",
luopl's avatar
luopl committed
812
    format_assistant=StringFormatter(slots=["{{content}}\n\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
813
    efficient_eos=True,
chenych's avatar
chenych committed
814
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
815
816
817
)


chenych's avatar
chenych committed
818
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
819
820
    name="gemma",
    format_user=StringFormatter(slots=["<start_of_turn>user\n{{content}}<end_of_turn>\n<start_of_turn>model\n"]),
luopl's avatar
luopl committed
821
    format_assistant=StringFormatter(slots=["{{content}}<end_of_turn>\n"]),
chenych's avatar
chenych committed
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
    format_observation=StringFormatter(
        slots=["<start_of_turn>tool\n{{content}}<end_of_turn>\n<start_of_turn>model\n"]
    ),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    stop_words=["<end_of_turn>"],
    template_class=Llama2Template,
)


# copied from gemma template
register_template(
    name="gemma3",
    format_user=StringFormatter(slots=["<start_of_turn>user\n{{content}}<end_of_turn>\n<start_of_turn>model\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<end_of_turn>\n"]),
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
chenych's avatar
chenych committed
838
839
840
841
    format_observation=StringFormatter(
        slots=["<start_of_turn>tool\n{{content}}<end_of_turn>\n<start_of_turn>model\n"]
    ),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
chenych's avatar
chenych committed
842
843
844
    stop_words=["<end_of_turn>"],
    mm_plugin=get_mm_plugin("gemma3", image_token="<image_soft_token>"),
    template_class=Llama2Template,
chenych's avatar
chenych committed
845
846
847
)


chenych's avatar
chenych committed
848
register_template(
chenych's avatar
chenych committed
849
850
851
852
    name="glm4",
    format_user=StringFormatter(slots=["<|user|>\n{{content}}<|assistant|>"]),
    format_assistant=StringFormatter(slots=["\n{{content}}"]),
    format_system=StringFormatter(slots=["<|system|>\n{{content}}"]),
luopl's avatar
luopl committed
853
    format_function=FunctionFormatter(slots=["{{content}}"], tool_format="glm4"),
chenych's avatar
chenych committed
854
855
856
857
    format_observation=StringFormatter(slots=["<|observation|>\n{{content}}<|assistant|>"]),
    format_tools=ToolFormatter(tool_format="glm4"),
    format_prefix=EmptyFormatter(slots=["[gMASK]<sop>"]),
    stop_words=["<|user|>", "<|observation|>"],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
858
859
860
861
    efficient_eos=True,
)


chenych's avatar
chenych committed
862
register_template(
luopl's avatar
luopl committed
863
864
865
866
867
868
869
870
871
872
873
    name="granite3",
    format_user=StringFormatter(
        slots=[
            "<|start_of_role|>user<|end_of_role|>{{content}}<|end_of_text|>\n<|start_of_role|>assistant<|end_of_role|>"
        ]
    ),
    format_assistant=StringFormatter(slots=["{{content}}<|end_of_text|>\n"]),
    format_system=StringFormatter(slots=["<|start_of_role|>system<|end_of_role|>{{content}}<|end_of_text|>\n"]),
)


chenych's avatar
chenych committed
874
register_template(
luopl's avatar
luopl committed
875
876
877
878
879
880
881
    name="index",
    format_user=StringFormatter(slots=["reserved_0{{content}}reserved_1"]),
    format_system=StringFormatter(slots=["<unk>{{content}}"]),
    efficient_eos=True,
)


chenych's avatar
chenych committed
882
883
884
885
886
887
888
889
890
891
register_template(
    name="hunyuan",
    format_user=StringFormatter(slots=["<|bos|>user\n{{content}}<|eos|>\n<|bos|>assistant\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<|eos|>\n"]),
    format_system=StringFormatter(slots=["<|bos|>system\n{{content}}<|eos|>\n"]),
    format_prefix=EmptyFormatter(slots=["<|bos|>"]),
    stop_words=["<|eos|>"],
)


chenych's avatar
chenych committed
892
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
893
    name="intern",
chenych's avatar
chenych committed
894
    format_user=StringFormatter(slots=["<|User|>:{{content}}\n<|Bot|>:"]),
luopl's avatar
luopl committed
895
    format_assistant=StringFormatter(slots=["{{content}}<eoa>\n"]),
chenych's avatar
chenych committed
896
897
    format_system=StringFormatter(slots=["<|System|>:{{content}}\n"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
chenych's avatar
chenych committed
898
899
900
901
902
903
904
    default_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 中文."
    ),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
905
906
907
908
    stop_words=["<eoa>"],
)


chenych's avatar
chenych committed
909
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
910
911
    name="intern2",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
912
913
914
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
chenych's avatar
chenych committed
915
916
917
918
919
920
921
    default_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 中文."
    ),
luopl's avatar
luopl committed
922
923
924
925
    stop_words=["<|im_end|>"],
)


chenych's avatar
chenych committed
926
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
927
928
929
    name="llama2",
    format_user=StringFormatter(slots=[{"bos_token"}, "[INST] {{content}} [/INST]"]),
    format_system=StringFormatter(slots=["<<SYS>>\n{{content}}\n<</SYS>>\n\n"]),
chenych's avatar
chenych committed
930
    template_class=Llama2Template,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
931
932
933
)


luopl's avatar
luopl committed
934
# copied from llama2 template
chenych's avatar
chenych committed
935
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
936
937
938
939
    name="llama2_zh",
    format_user=StringFormatter(slots=[{"bos_token"}, "[INST] {{content}} [/INST]"]),
    format_system=StringFormatter(slots=["<<SYS>>\n{{content}}\n<</SYS>>\n\n"]),
    default_system="You are a helpful assistant. 你是一个乐于助人的助手。",
chenych's avatar
chenych committed
940
    template_class=Llama2Template,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
941
942
943
)


chenych's avatar
chenych committed
944
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
945
946
947
948
949
950
951
952
953
    name="llama3",
    format_user=StringFormatter(
        slots=[
            (
                "<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
luopl's avatar
luopl committed
954
    format_assistant=StringFormatter(slots=["{{content}}<|eot_id|>"]),
chenych's avatar
chenych committed
955
    format_system=StringFormatter(slots=["<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]),
luopl's avatar
luopl committed
956
    format_function=FunctionFormatter(slots=["{{content}}<|eot_id|>"], tool_format="llama3"),
chenych's avatar
chenych committed
957
958
959
    format_observation=StringFormatter(
        slots=[
            (
luopl's avatar
luopl committed
960
                "<|start_header_id|>ipython<|end_header_id|>\n\n{{content}}<|eot_id|>"
chenych's avatar
chenych committed
961
962
963
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
964
    ),
luopl's avatar
luopl committed
965
    format_tools=ToolFormatter(tool_format="llama3"),
chenych's avatar
chenych committed
966
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
luopl's avatar
luopl committed
967
    stop_words=["<|eot_id|>", "<|eom_id|>"],
luopl's avatar
luopl committed
968
969
970
)


chenych's avatar
chenych committed
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
register_template(
    name="llama4",
    format_user=StringFormatter(
        slots=["<|header_start|>user<|header_end|>\n\n{{content}}<|eot|><|header_start|>assistant<|header_end|>\n\n"]
    ),
    format_assistant=StringFormatter(slots=["{{content}}<|eot|>"]),
    format_system=StringFormatter(slots=["<|header_start|>system<|header_end|>\n\n{{content}}<|eot|>"]),
    format_function=FunctionFormatter(slots=["{{content}}<|eot|>"], tool_format="llama3"),
    format_observation=StringFormatter(
        slots=[
            "<|header_start|>ipython<|header_end|>\n\n{{content}}<|eot|><|header_start|>assistant<|header_end|>\n\n"
        ]
    ),
    format_tools=ToolFormatter(tool_format="llama3"),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    stop_words=["<|eot|>", "<|eom|>"],
    mm_plugin=get_mm_plugin(name="llama4", image_token="<|image|>"),
)


luopl's avatar
luopl committed
991
# copied from llama3 template
chenych's avatar
chenych committed
992
register_template(
luopl's avatar
luopl committed
993
994
995
996
997
998
999
1000
1001
    name="mllama",
    format_user=StringFormatter(
        slots=[
            (
                "<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
luopl's avatar
luopl committed
1002
    format_assistant=StringFormatter(slots=["{{content}}<|eot_id|>"]),
luopl's avatar
luopl committed
1003
    format_system=StringFormatter(slots=["<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]),
luopl's avatar
luopl committed
1004
    format_function=FunctionFormatter(slots=["{{content}}<|eot_id|>"], tool_format="llama3"),
luopl's avatar
luopl committed
1005
1006
1007
    format_observation=StringFormatter(
        slots=[
            (
luopl's avatar
luopl committed
1008
                "<|start_header_id|>ipython<|end_header_id|>\n\n{{content}}<|eot_id|>"
luopl's avatar
luopl committed
1009
1010
1011
1012
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
luopl's avatar
luopl committed
1013
    format_tools=ToolFormatter(tool_format="llama3"),
luopl's avatar
luopl committed
1014
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
luopl's avatar
luopl committed
1015
    stop_words=["<|eot_id|>", "<|eom_id|>"],
luopl's avatar
luopl committed
1016
1017
1018
1019
    mm_plugin=get_mm_plugin(name="mllama", image_token="<|image|>"),
)


chenych's avatar
chenych committed
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
register_template(
    name="moonlight",
    format_user=StringFormatter(
        slots=["<|im_user|>user<|im_middle|>{{content}}<|im_end|><|im_assistant|>assistant<|im_middle|>"]
    ),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>"]),
    format_system=StringFormatter(slots=["<|im_system|>system<|im_middle|>{{content}}<|im_end|>"]),
    default_system="You are a helpful assistant provided by Moonshot-AI.",
    stop_words=["<|im_end|>"],
)


luopl's avatar
luopl committed
1032
# copied from vicuna template
chenych's avatar
chenych committed
1033
register_template(
luopl's avatar
luopl committed
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
    name="llava",
    format_user=StringFormatter(slots=["USER: {{content}} ASSISTANT:"]),
    default_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."
    ),
    mm_plugin=get_mm_plugin(name="llava", image_token="<image>"),
)


luopl's avatar
luopl committed
1044
# copied from vicuna template
chenych's avatar
chenych committed
1045
register_template(
luopl's avatar
luopl committed
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
    name="llava_next",
    format_user=StringFormatter(slots=["USER: {{content}} ASSISTANT:"]),
    default_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."
    ),
    mm_plugin=get_mm_plugin(name="llava_next", image_token="<image>"),
)


luopl's avatar
luopl committed
1056
# copied from llama3 template
chenych's avatar
chenych committed
1057
register_template(
luopl's avatar
luopl committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
    name="llava_next_llama3",
    format_user=StringFormatter(
        slots=[
            (
                "<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
luopl's avatar
luopl committed
1067
    format_assistant=StringFormatter(slots=["{{content}}<|eot_id|>"]),
luopl's avatar
luopl committed
1068
    format_system=StringFormatter(slots=["<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]),
luopl's avatar
luopl committed
1069
    format_function=FunctionFormatter(slots=["{{content}}<|eot_id|>"], tool_format="llama3"),
luopl's avatar
luopl committed
1070
1071
1072
    format_observation=StringFormatter(
        slots=[
            (
luopl's avatar
luopl committed
1073
                "<|start_header_id|>ipython<|end_header_id|>\n\n{{content}}<|eot_id|>"
luopl's avatar
luopl committed
1074
1075
1076
1077
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
luopl's avatar
luopl committed
1078
    format_tools=ToolFormatter(tool_format="llama3"),
luopl's avatar
luopl committed
1079
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
luopl's avatar
luopl committed
1080
    stop_words=["<|eot_id|>", "<|eom_id|>"],
luopl's avatar
luopl committed
1081
1082
1083
1084
    mm_plugin=get_mm_plugin(name="llava_next", image_token="<image>"),
)


luopl's avatar
luopl committed
1085
# copied from mistral template
chenych's avatar
chenych committed
1086
register_template(
luopl's avatar
luopl committed
1087
    name="llava_next_mistral",
luopl's avatar
luopl committed
1088
1089
1090
    format_user=StringFormatter(slots=["[INST] {{content}}[/INST]"]),
    format_assistant=StringFormatter(slots=[" {{content}}", {"eos_token"}]),
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
chenych's avatar
chenych committed
1091
    format_function=FunctionFormatter(slots=["[TOOL_CALLS] {{content}}", {"eos_token"}], tool_format="mistral"),
luopl's avatar
luopl committed
1092
1093
    format_observation=StringFormatter(slots=["""[TOOL_RESULTS] {"content": {{content}}}[/TOOL_RESULTS]"""]),
    format_tools=ToolFormatter(tool_format="mistral"),
luopl's avatar
luopl committed
1094
1095
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    mm_plugin=get_mm_plugin(name="llava_next", image_token="<image>"),
chenych's avatar
chenych committed
1096
    template_class=Llama2Template,
luopl's avatar
luopl committed
1097
1098
1099
)


chenych's avatar
chenych committed
1100
1101
# copied from qwen template
register_template(
luopl's avatar
luopl committed
1102
1103
    name="llava_next_qwen",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1104
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1105
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1106
1107
1108
1109
1110
    format_function=FunctionFormatter(slots=["{{content}}<|im_end|>\n"], tool_format="qwen"),
    format_observation=StringFormatter(
        slots=["<|im_start|>user\n<tool_response>\n{{content}}\n</tool_response><|im_end|>\n<|im_start|>assistant\n"]
    ),
    format_tools=ToolFormatter(tool_format="qwen"),
luopl's avatar
luopl committed
1111
1112
1113
1114
1115
1116
    default_system="You are a helpful assistant.",
    stop_words=["<|im_end|>"],
    mm_plugin=get_mm_plugin(name="llava_next", image_token="<image>"),
)


luopl's avatar
luopl committed
1117
# copied from chatml template
chenych's avatar
chenych committed
1118
register_template(
luopl's avatar
luopl committed
1119
1120
    name="llava_next_yi",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1121
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1122
1123
1124
1125
1126
1127
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    stop_words=["<|im_end|>"],
    mm_plugin=get_mm_plugin(name="llava_next", image_token="<image>"),
)


luopl's avatar
luopl committed
1128
# copied from vicuna template
chenych's avatar
chenych committed
1129
register_template(
luopl's avatar
luopl committed
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
    name="llava_next_video",
    format_user=StringFormatter(slots=["USER: {{content}} ASSISTANT:"]),
    default_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."
    ),
    mm_plugin=get_mm_plugin(name="llava_next_video", image_token="<image>", video_token="<video>"),
)


luopl's avatar
luopl committed
1140
# copied from mistral template
chenych's avatar
chenych committed
1141
register_template(
luopl's avatar
luopl committed
1142
    name="llava_next_video_mistral",
luopl's avatar
luopl committed
1143
1144
1145
    format_user=StringFormatter(slots=["[INST] {{content}}[/INST]"]),
    format_assistant=StringFormatter(slots=[" {{content}}", {"eos_token"}]),
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
chenych's avatar
chenych committed
1146
    format_function=FunctionFormatter(slots=["[TOOL_CALLS] {{content}}", {"eos_token"}], tool_format="mistral"),
luopl's avatar
luopl committed
1147
1148
    format_observation=StringFormatter(slots=["""[TOOL_RESULTS] {"content": {{content}}}[/TOOL_RESULTS]"""]),
    format_tools=ToolFormatter(tool_format="mistral"),
luopl's avatar
luopl committed
1149
1150
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    mm_plugin=get_mm_plugin(name="llava_next_video", image_token="<image>", video_token="<video>"),
chenych's avatar
chenych committed
1151
    template_class=Llama2Template,
luopl's avatar
luopl committed
1152
1153
1154
)


luopl's avatar
luopl committed
1155
# copied from chatml template
chenych's avatar
chenych committed
1156
register_template(
luopl's avatar
luopl committed
1157
1158
    name="llava_next_video_yi",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1159
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1160
1161
1162
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    stop_words=["<|im_end|>"],
    mm_plugin=get_mm_plugin(name="llava_next_video", image_token="<image>", video_token="<video>"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1163
1164
1165
)


luopl's avatar
luopl committed
1166
# copied from chatml template
chenych's avatar
chenych committed
1167
register_template(
luopl's avatar
luopl committed
1168
1169
1170
1171
1172
1173
    name="marco",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    format_observation=StringFormatter(slots=["<|im_start|>tool\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    default_system=(
chenych's avatar
chenych committed
1174
1175
        "你是一个经过良好训练的AI助手,你的名字是Marco-o1."
        "由阿里国际数字商业集团的AI Business创造.\n## 重要!!!!!\n"
luopl's avatar
luopl committed
1176
1177
1178
1179
1180
1181
1182
1183
        "当你回答问题时,你的思考应该在<Thought>内完成,<Output>内输出你的结果。\n"
        "<Thought>应该尽可能是英文,但是有2个特例,一个是对原文中的引用,另一个是是数学应该使用markdown格式,<Output>内的输出需要遵循用户输入的语言。\n"
    ),
    stop_words=["<|im_end|>"],
)


# copied from chatml template
chenych's avatar
chenych committed
1184
register_template(
luopl's avatar
luopl committed
1185
1186
1187
1188
1189
    name="minicpm_v",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    stop_words=["<|im_end|>"],
chenych's avatar
chenych committed
1190
    default_system="You are a helpful assistant.",
luopl's avatar
luopl committed
1191
1192
1193
1194
    mm_plugin=get_mm_plugin(name="minicpm_v", image_token="<image>", video_token="<video>"),
)


chenych's avatar
chenych committed
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# copied from minicpm_v template
register_template(
    name="minicpm_o",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    stop_words=["<|im_end|>"],
    default_system="You are Qwen, created by Alibaba Cloud. You are a helpful assistant.",
    mm_plugin=get_mm_plugin(name="minicpm_v", image_token="<image>", video_token="<video>", audio_token="<audio>"),
)


# mistral tokenizer v3 tekken
register_template(
    name="ministral",
    format_user=StringFormatter(slots=["[INST]{{content}}[/INST]"]),
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
    format_function=FunctionFormatter(slots=["[TOOL_CALLS]{{content}}", {"eos_token"}], tool_format="mistral"),
    format_observation=StringFormatter(slots=["""[TOOL_RESULTS]{"content": {{content}}}[/TOOL_RESULTS]"""]),
    format_tools=ToolFormatter(tool_format="mistral"),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    template_class=Llama2Template,
)


# mistral tokenizer v3
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1222
    name="mistral",
luopl's avatar
luopl committed
1223
1224
1225
    format_user=StringFormatter(slots=["[INST] {{content}}[/INST]"]),
    format_assistant=StringFormatter(slots=[" {{content}}", {"eos_token"}]),
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
chenych's avatar
chenych committed
1226
    format_function=FunctionFormatter(slots=["[TOOL_CALLS] {{content}}", {"eos_token"}], tool_format="mistral"),
luopl's avatar
luopl committed
1227
1228
    format_observation=StringFormatter(slots=["""[TOOL_RESULTS] {"content": {{content}}}[/TOOL_RESULTS]"""]),
    format_tools=ToolFormatter(tool_format="mistral"),
chenych's avatar
chenych committed
1229
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
chenych's avatar
chenych committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
    template_class=Llama2Template,
)


# mistral tokenizer v7 tekken (copied from ministral)
register_template(
    name="mistral_small",
    format_user=StringFormatter(slots=["[INST]{{content}}[/INST]"]),
    format_system=StringFormatter(slots=["[SYSTEM_PROMPT]{{content}}[/SYSTEM_PROMPT]"]),
    format_function=FunctionFormatter(slots=["[TOOL_CALLS]{{content}}", {"eos_token"}], tool_format="mistral"),
    format_observation=StringFormatter(slots=["""[TOOL_RESULTS]{"content": {{content}}}[/TOOL_RESULTS]"""]),
    format_tools=ToolFormatter(tool_format="mistral"),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1243
1244
1245
)


chenych's avatar
chenych committed
1246
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1247
    name="olmo",
chenych's avatar
chenych committed
1248
1249
    format_user=StringFormatter(slots=["<|user|>\n{{content}}<|assistant|>\n"]),
    format_prefix=EmptyFormatter(slots=[{"eos_token"}]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1250
1251
1252
)


chenych's avatar
chenych committed
1253
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1254
1255
    name="openchat",
    format_user=StringFormatter(slots=["GPT4 Correct User: {{content}}", {"eos_token"}, "GPT4 Correct Assistant:"]),
chenych's avatar
chenych committed
1256
1257
1258
1259
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)


chenych's avatar
chenych committed
1260
register_template(
chenych's avatar
chenych committed
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
    name="openchat-3.6",
    format_user=StringFormatter(
        slots=[
            (
                "<|start_header_id|>GPT4 Correct User<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>GPT4 Correct Assistant<|end_header_id|>\n\n"
            )
        ]
    ),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    stop_words=["<|eot_id|>"],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1272
1273
1274
)


luopl's avatar
luopl committed
1275
# copied from chatml template
chenych's avatar
chenych committed
1276
register_template(
luopl's avatar
luopl committed
1277
1278
    name="opencoder",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1279
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1280
1281
1282
1283
1284
1285
1286
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    format_observation=StringFormatter(slots=["<|im_start|>tool\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    default_system="You are OpenCoder, created by OpenCoder Team.",
    stop_words=["<|im_end|>"],
)


chenych's avatar
chenych committed
1287
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1288
1289
    name="orion",
    format_user=StringFormatter(slots=["Human: {{content}}\n\nAssistant: ", {"eos_token"}]),
chenych's avatar
chenych committed
1290
1291
1292
1293
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
)


chenych's avatar
chenych committed
1294
register_template(
luopl's avatar
luopl committed
1295
    name="paligemma",
chenych's avatar
chenych committed
1296
1297
1298
    format_user=StringFormatter(slots=["{{content}}\n"]),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    mm_plugin=get_mm_plugin(name="paligemma", image_token="<image>"),
chenych's avatar
chenych committed
1299
    template_class=Llama2Template,
chenych's avatar
chenych committed
1300
1301
1302
1303
1304
1305
)


# copied from gemma template
register_template(
    name="paligemma_chat",
luopl's avatar
luopl committed
1306
    format_user=StringFormatter(slots=["<start_of_turn>user\n{{content}}<end_of_turn>\n<start_of_turn>model\n"]),
luopl's avatar
luopl committed
1307
    format_assistant=StringFormatter(slots=["{{content}}<end_of_turn>\n"]),
luopl's avatar
luopl committed
1308
1309
1310
1311
    format_observation=StringFormatter(
        slots=["<start_of_turn>tool\n{{content}}<end_of_turn>\n<start_of_turn>model\n"]
    ),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
chenych's avatar
chenych committed
1312
    stop_words=["<end_of_turn>"],
luopl's avatar
luopl committed
1313
    mm_plugin=get_mm_plugin(name="paligemma", image_token="<image>"),
chenych's avatar
chenych committed
1314
    template_class=Llama2Template,
luopl's avatar
luopl committed
1315
1316
1317
)


chenych's avatar
chenych committed
1318
register_template(
chenych's avatar
chenych committed
1319
1320
    name="phi",
    format_user=StringFormatter(slots=["<|user|>\n{{content}}<|end|>\n<|assistant|>\n"]),
luopl's avatar
luopl committed
1321
    format_assistant=StringFormatter(slots=["{{content}}<|end|>\n"]),
chenych's avatar
chenych committed
1322
1323
    format_system=StringFormatter(slots=["<|system|>\n{{content}}<|end|>\n"]),
    stop_words=["<|end|>"],
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1324
1325
1326
)


chenych's avatar
chenych committed
1327
register_template(
luopl's avatar
luopl committed
1328
1329
    name="phi_small",
    format_user=StringFormatter(slots=["<|user|>\n{{content}}<|end|>\n<|assistant|>\n"]),
luopl's avatar
luopl committed
1330
    format_assistant=StringFormatter(slots=["{{content}}<|end|>\n"]),
luopl's avatar
luopl committed
1331
1332
1333
    format_system=StringFormatter(slots=["<|system|>\n{{content}}<|end|>\n"]),
    format_prefix=EmptyFormatter(slots=[{"<|endoftext|>"}]),
    stop_words=["<|end|>"],
luopl's avatar
luopl committed
1334
1335
1336
)


chenych's avatar
chenych committed
1337
register_template(
luopl's avatar
luopl committed
1338
1339
1340
1341
1342
1343
1344
    name="phi4",
    format_user=StringFormatter(
        slots=["<|im_start|>user<|im_sep|>{{content}}<|im_end|><|im_start|>assistant<|im_sep|>"]
    ),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>"]),
    format_system=StringFormatter(slots=["<|im_start|>system<|im_sep|>{{content}}<|im_end|>"]),
    stop_words=["<|im_end|>"],
luopl's avatar
luopl committed
1345
1346
1347
)


chenych's avatar
chenych committed
1348
1349
# copied from ministral template
register_template(
luopl's avatar
luopl committed
1350
    name="pixtral",
luopl's avatar
luopl committed
1351
1352
    format_user=StringFormatter(slots=["[INST]{{content}}[/INST]"]),
    format_system=StringFormatter(slots=["{{content}}\n\n"]),
chenych's avatar
chenych committed
1353
1354
1355
    format_function=FunctionFormatter(slots=["[TOOL_CALLS]{{content}}", {"eos_token"}], tool_format="mistral"),
    format_observation=StringFormatter(slots=["""[TOOL_RESULTS]{"content": {{content}}}[/TOOL_RESULTS]"""]),
    format_tools=ToolFormatter(tool_format="mistral"),
luopl's avatar
luopl committed
1356
1357
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    mm_plugin=get_mm_plugin(name="pixtral", image_token="[IMG]"),
chenych's avatar
chenych committed
1358
    template_class=Llama2Template,
luopl's avatar
luopl committed
1359
1360
1361
)


luopl's avatar
luopl committed
1362
# copied from chatml template
chenych's avatar
chenych committed
1363
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1364
1365
    name="qwen",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1366
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1367
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1368
1369
1370
1371
1372
    format_function=FunctionFormatter(slots=["{{content}}<|im_end|>\n"], tool_format="qwen"),
    format_observation=StringFormatter(
        slots=["<|im_start|>user\n<tool_response>\n{{content}}\n</tool_response><|im_end|>\n<|im_start|>assistant\n"]
    ),
    format_tools=ToolFormatter(tool_format="qwen"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1373
1374
    default_system="You are a helpful assistant.",
    stop_words=["<|im_end|>"],
luopl's avatar
luopl committed
1375
1376
1377
)


luopl's avatar
luopl committed
1378
# copied from chatml template
chenych's avatar
chenych committed
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
register_template(
    name="qwen2_audio",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    default_system="You are a helpful assistant.",
    stop_words=["<|im_end|>"],
    mm_plugin=get_mm_plugin(name="qwen2_audio", audio_token="<|AUDIO|>"),
)


chenych's avatar
chenych committed
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
# copied from qwen template
register_template(
    name="qwen2_omni",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    format_function=FunctionFormatter(slots=["{{content}}<|im_end|>\n"], tool_format="qwen"),
    format_observation=StringFormatter(
        slots=["<|im_start|>user\n<tool_response>\n{{content}}\n</tool_response><|im_end|>\n<|im_start|>assistant\n"]
    ),
    format_tools=ToolFormatter(tool_format="qwen"),
    default_system="You are a helpful assistant.",
    stop_words=["<|im_end|>"],
    mm_plugin=get_mm_plugin(
        name="qwen2_omni", audio_token="<|AUDIO|>", image_token="<|IMAGE|>", video_token="<|VIDEO|>"
    ),
)

chenych's avatar
chenych committed
1408
1409
# copied from qwen template
register_template(
luopl's avatar
luopl committed
1410
1411
    name="qwen2_vl",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1412
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1413
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
luopl's avatar
luopl committed
1414
1415
1416
1417
1418
    format_function=FunctionFormatter(slots=["{{content}}<|im_end|>\n"], tool_format="qwen"),
    format_observation=StringFormatter(
        slots=["<|im_start|>user\n<tool_response>\n{{content}}\n</tool_response><|im_end|>\n<|im_start|>assistant\n"]
    ),
    format_tools=ToolFormatter(tool_format="qwen"),
luopl's avatar
luopl committed
1419
1420
1421
    default_system="You are a helpful assistant.",
    stop_words=["<|im_end|>"],
    mm_plugin=get_mm_plugin(name="qwen2_vl", image_token="<|image_pad|>", video_token="<|video_pad|>"),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1422
1423
1424
)


chenych's avatar
chenych committed
1425
register_template(
chenych's avatar
chenych committed
1426
1427
    name="sailor",
    format_user=StringFormatter(slots=["<|im_start|>question\n{{content}}<|im_end|>\n<|im_start|>answer\n"]),
luopl's avatar
luopl committed
1428
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
chenych's avatar
chenych committed
1429
1430
1431
1432
1433
1434
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
    default_system=(
        "You are an AI assistant named Sailor created by Sea AI Lab. "
        "Your answer should be friendly, unbiased, faithful, informative and detailed."
    ),
    stop_words=["<|im_end|>"],
luopl's avatar
luopl committed
1435
1436
1437
1438
)


# copied from llama3 template
chenych's avatar
chenych committed
1439
register_template(
luopl's avatar
luopl committed
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
    name="skywork_o1",
    format_user=StringFormatter(
        slots=[
            (
                "<|start_header_id|>user<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
    format_assistant=StringFormatter(slots=["{{content}}<|eot_id|>"]),
    format_system=StringFormatter(slots=["<|start_header_id|>system<|end_header_id|>\n\n{{content}}<|eot_id|>"]),
    format_function=FunctionFormatter(slots=["{{content}}<|eot_id|>"], tool_format="llama3"),
    format_observation=StringFormatter(
        slots=[
            (
                "<|start_header_id|>ipython<|end_header_id|>\n\n{{content}}<|eot_id|>"
                "<|start_header_id|>assistant<|end_header_id|>\n\n"
            )
        ]
    ),
    format_tools=ToolFormatter(tool_format="llama3"),
    format_prefix=EmptyFormatter(slots=[{"bos_token"}]),
    default_system=(
        "You are Skywork-o1, a thinking model developed by Skywork AI, specializing in solving complex problems "
        "involving mathematics, coding, and logical reasoning through deep thought. When faced with a user's request, "
        "you first engage in a lengthy and in-depth thinking process to explore possible solutions to the problem. "
        "After completing your thoughts, you then provide a detailed explanation of the solution process "
        "in your response."
    ),
    stop_words=["<|eot_id|>", "<|eom_id|>"],
chenych's avatar
chenych committed
1470
1471
1472
)


chenych's avatar
chenych committed
1473
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1474
1475
1476
1477
1478
1479
1480
    name="solar",
    format_user=StringFormatter(slots=["### User:\n{{content}}\n\n### Assistant:\n"]),
    format_system=StringFormatter(slots=["### System:\n{{content}}\n\n"]),
    efficient_eos=True,
)


chenych's avatar
chenych committed
1481
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1482
1483
    name="starchat",
    format_user=StringFormatter(slots=["<|user|>\n{{content}}<|end|>\n<|assistant|>"]),
luopl's avatar
luopl committed
1484
    format_assistant=StringFormatter(slots=["{{content}}<|end|>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1485
1486
    format_system=StringFormatter(slots=["<|system|>\n{{content}}<|end|>\n"]),
    stop_words=["<|end|>"],
chenych's avatar
chenych committed
1487
1488
1489
)


chenych's avatar
chenych committed
1490
register_template(
chenych's avatar
chenych committed
1491
1492
1493
    name="telechat",
    format_user=StringFormatter(slots=["<_user>{{content}}<_bot>"]),
    format_system=StringFormatter(slots=["<_system>{{content}}<_end>"]),
luopl's avatar
luopl committed
1494
1495
1496
)


chenych's avatar
chenych committed
1497
register_template(
luopl's avatar
luopl committed
1498
1499
1500
1501
1502
1503
    name="telechat2",
    format_user=StringFormatter(slots=["<_user>{{content}}<_bot>"]),
    format_system=StringFormatter(slots=["<_system>{{content}}"]),
    default_system=(
        "你是中国电信星辰语义大模型,英文名是TeleChat,你是由中电信人工智能科技有限公司和中国电信人工智能研究院(TeleAI)研发的人工智能助手。"
    ),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1504
1505
1506
)


chenych's avatar
chenych committed
1507
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1508
1509
1510
1511
1512
1513
    name="vicuna",
    format_user=StringFormatter(slots=["USER: {{content}} ASSISTANT:"]),
    default_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."
    ),
luopl's avatar
luopl committed
1514
    replace_jinja_template=True,
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1515
1516
1517
)


chenych's avatar
chenych committed
1518
register_template(
luopl's avatar
luopl committed
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
    name="video_llava",
    format_user=StringFormatter(slots=["USER: {{content}} ASSISTANT:"]),
    default_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."
    ),
    mm_plugin=get_mm_plugin(name="video_llava", image_token="<image>", video_token="<video>"),
)


chenych's avatar
chenych committed
1529
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
    name="xuanyuan",
    format_user=StringFormatter(slots=["Human: {{content}} Assistant:"]),
    default_system=(
        "以下是用户和人工智能助手之间的对话。用户以Human开头,人工智能助手以Assistant开头,"
        "会对人类提出的问题给出有帮助、高质量、详细和礼貌的回答,并且总是拒绝参与与不道德、"
        "不安全、有争议、政治敏感等相关的话题、问题和指示。\n"
    ),
)


chenych's avatar
chenych committed
1540
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1541
1542
1543
1544
1545
    name="xverse",
    format_user=StringFormatter(slots=["Human: {{content}}\n\nAssistant: "]),
)


chenych's avatar
chenych committed
1546
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1547
1548
    name="yayi",
    format_user=StringFormatter(slots=[{"token": "<|Human|>"}, ":\n{{content}}\n\n", {"token": "<|YaYi|>"}, ":"]),
luopl's avatar
luopl committed
1549
    format_assistant=StringFormatter(slots=["{{content}}\n\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
    format_system=StringFormatter(slots=[{"token": "<|System|>"}, ":\n{{content}}\n\n"]),
    default_system=(
        "You are a helpful, respectful and honest assistant named YaYi "
        "developed by Beijing Wenge Technology Co.,Ltd. "
        "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_words=["<|End|>"],
)


luopl's avatar
luopl committed
1566
# copied from chatml template
chenych's avatar
chenych committed
1567
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1568
1569
    name="yi",
    format_user=StringFormatter(slots=["<|im_start|>user\n{{content}}<|im_end|>\n<|im_start|>assistant\n"]),
luopl's avatar
luopl committed
1570
    format_assistant=StringFormatter(slots=["{{content}}<|im_end|>\n"]),
chenych's avatar
chenych committed
1571
    format_system=StringFormatter(slots=["<|im_start|>system\n{{content}}<|im_end|>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1572
1573
1574
1575
    stop_words=["<|im_end|>"],
)


chenych's avatar
chenych committed
1576
register_template(
chenych's avatar
chenych committed
1577
1578
    name="yi_vl",
    format_user=StringFormatter(slots=["### Human: {{content}}\n### Assistant:"]),
luopl's avatar
luopl committed
1579
    format_assistant=StringFormatter(slots=["{{content}}\n"]),
chenych's avatar
chenych committed
1580
1581
1582
1583
1584
1585
1586
1587
1588
    default_system=(
        "This is a chat between an inquisitive human and an AI assistant. "
        "Assume the role of the AI assistant. Read all the images carefully, "
        "and respond to the human's questions with informative, helpful, detailed and polite answers. "
        "这是一个好奇的人类和一个人工智能助手之间的对话。假设你扮演这个AI助手的角色。"
        "仔细阅读所有的图像,并对人类的问题做出信息丰富、有帮助、详细的和礼貌的回答。\n\n"
    ),
    stop_words=["###"],
    efficient_eos=True,
luopl's avatar
luopl committed
1589
    mm_plugin=get_mm_plugin(name="llava", image_token="<image>"),
chenych's avatar
chenych committed
1590
1591
1592
)


chenych's avatar
chenych committed
1593
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1594
1595
    name="yuan",
    format_user=StringFormatter(slots=["{{content}}", {"token": "<sep>"}]),
luopl's avatar
luopl committed
1596
    format_assistant=StringFormatter(slots=["{{content}}<eod>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1597
1598
1599
1600
    stop_words=["<eod>"],
)


chenych's avatar
chenych committed
1601
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1602
    name="zephyr",
chenych's avatar
chenych committed
1603
    format_user=StringFormatter(slots=["<|user|>\n{{content}}", {"eos_token"}, "<|assistant|>\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1604
    format_system=StringFormatter(slots=["<|system|>\n{{content}}", {"eos_token"}]),
chenych's avatar
chenych committed
1605
    default_system="You are Zephyr, a helpful assistant.",
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1606
1607
1608
)


chenych's avatar
chenych committed
1609
register_template(
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1610
1611
    name="ziya",
    format_user=StringFormatter(slots=["<human>:{{content}}\n<bot>:"]),
luopl's avatar
luopl committed
1612
    format_assistant=StringFormatter(slots=["{{content}}\n"]),
Rayyyyy's avatar
V0.6.3  
Rayyyyy committed
1613
)