Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

DeepSeek conversation template support #810

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 51 additions & 11 deletions docs/source/examples/supported_conversation_template.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Supported Conversation Template

## Table of Contents
- [ChatGLM-3](#chatglm-3)
- [InternLM2](#internlm2)
- [Llama-2](#llama-2)
- [Llama-3](#llama-3)
- [Mixtral 8x22B](#mixtral-8x22b)
- [Mixtral 8x7B](#mixtral-8x7b)
- [Qwen-2](#qwen-2)
- [Phi-3](#phi-3)
- [Yi](#yi)
- [Supported Conversation Template](#supported-conversation-template)
- [ChatGLM-3](#chatglm-3)
- [ChatML](#chatml)
- [DeepSeek](#deepseek)
- [InternLM2](#internlm2)
- [Llama-2](#llama-2)
- [Llama-3](#llama-3)
- [Mixtral 8x22B](#mixtral-8x22b)
- [Mixtral 8x7B](#mixtral-8x7b)
- [Phi-3](#phi-3)
- [Qwen-2](#qwen-2)
- [Yi](#yi)


## ChatGLM-3
Expand Down Expand Up @@ -58,6 +60,44 @@ This template is not preseted in LMFlow currently. We are working on it and will
```


## DeepSeek
```{admonition} **Work in Progress**
:class: info

We are working on building test cases for this template. We will add this template into switch case once the tests pass.
```
**With a system message**
```
<|begin▁of▁sentence|>{{system_message}}\n\nUser: {{user_message_0}}\n\n
```

**Without a system message**
```
<|begin▁of▁sentence|>User: {{user_message_0}}\n\n
```

**A complete conversation**
```
<|begin▁of▁sentence|>{{system_message}}\n\nUser: {{user_message_0}}\n\nAssistant: {{assistant_reply_0}}<|end▁of▁sentence|>
```

**Multiple rounds**
```
<|begin▁of▁sentence|>{{system_message}}\n\nUser: {{user_message_0}}\n\nAssistant: {{assistant_reply_0}}<|end▁of▁sentence|>User: {{user_message_1}}\n\nAssistant: {{assistant_reply_1}}<|end▁of▁sentence|>
```

**jinja template**
[[Reference](https://huggingface.co/deepseek-ai/DeepSeek-V2-Chat/blob/941577e8236164bc96829096d20c61568630d7bc/tokenizer_config.json#L34)]
```
{% if not add_generation_prompt is defined %}{% set add_generation_prompt = false %}{% endif %}{{ bos_token }}{% for message in messages %}{% if message['role'] == 'user' %}{{ 'User: ' + message['content'] + '\n\n' }}{% elif message['role'] == 'assistant' %}{{ 'Assistant: ' + message['content'] + eos_token }}{% elif message['role'] == 'system' %}{{ message['content'] + '\n\n' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}
```

**Filled Example**
```
<|begin▁of▁sentence|>You are a chatbot developed by LMFlow team.\n\nUser: Who are you?\n\nAssistant: I am a chatbot developed by LMFlow team.<|end▁of▁sentence|>User: How old are you?\n\nAssistant: I don't age like humans do. I exist as a piece of software, so I don't have a concept of age in the traditional sense.<|end▁of▁sentence|>
```


## InternLM2
```{admonition} **Work in Progress**
:class: info
Expand Down Expand Up @@ -196,7 +236,7 @@ The conversation template for Mixtral 8x7B is slightly different from the templa
<s><|system|>\n{{system_message}}<|end|>\n<|user|>\n{{user_message_0}}<|end|>\n<|assistant|>\n{{assistant_reply_0}}<|end|>\n<|user|>\n{{user_message_1}}<|end|>\n<|assistant|>\n{{assistant_reply_1}}<|end|>\n<|endoftext|>
```

**jinja template**
**jinja template**
[[Reference]](https://huggingface.co/microsoft/Phi-3-mini-4k-instruct/blob/3a811845d89f3c1b3f41b341d0f9f05104769f35/tokenizer_config.json#L338)
```
{{ bos_token }}{% for message in messages %}{{'<|' + message['role'] + '|>' + '\n' + message['content'] + '<|end|>\n' }}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% else %}{{ eos_token }}{% endif %}
Expand Down
21 changes: 21 additions & 0 deletions src/lmflow/utils/conversation_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ class ChatMLConversationTemplate(ConversationTemplate):
TemplateComponent(type='string', content='<|im_start|>system\n{{content}}<|im_end|>\n')
]
)


@dataclass
class DeepSeekConversationTemplate(ConversationTemplate):
user_formatter: Formatter = StringFormatter(
template=[
TemplateComponent(type='string', content='User: {{content}}\n\n')
]
)
assistant_formatter: Formatter = StringFormatter(
template=[
TemplateComponent(type='string', content='Assistant: {{content}}'),
TemplateComponent(type='token', content='eos_token')
]
)
system_formatter: Formatter = StringFormatter(
template=[
TemplateComponent(type='string', content='{{content}}\n\n')
]
)
special_starter: TemplateComponent = TemplateComponent(type='token', content='bos_token')


@dataclass
Expand Down
Loading