Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add system role to deepseek chat template #3031

Merged
merged 8 commits into from
Jan 27, 2025
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
50 changes: 33 additions & 17 deletions lmdeploy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,16 @@ def __init__(
stop_words=stop_words,
**kwargs)

def get_prompt(self, prompt, sequence_start=True):
if self.capability == 'chat':
return super().get_prompt(prompt, sequence_start)[:-1]
return super().get_prompt(prompt, sequence_start)

def messages2prompt(self, messages, sequence_start=True, **kwargs):
if isinstance(messages, str):
return self.get_prompt(messages, sequence_start)
return super().messages2prompt(messages, sequence_start, **kwargs)[:-1]

@classmethod
def match(cls, model_path: str) -> Optional[str]:
"""Return the model_name that was registered to MODELS.
Expand All @@ -337,12 +347,6 @@ def __init__(
**kwargs):
super().__init__(meta_instruction=meta_instruction, **kwargs)

def get_prompt(self, prompt, sequence_start=True):
return super().get_prompt(prompt, sequence_start)[:-1]

def messages2prompt(self, messages, sequence_start=True, **kwargs):
return super().messages2prompt(messages, sequence_start, **kwargs)[:-1]

@classmethod
def match(cls, model_path: str) -> Optional[str]:
"""Return the model_name that was registered to MODELS.
Expand All @@ -365,12 +369,6 @@ class MiniGemini(Vicuna):
def __init__(self, **kwargs):
super().__init__(**kwargs)

def get_prompt(self, prompt, sequence_start=True):
return super().get_prompt(prompt, sequence_start)[:-1]

def messages2prompt(self, messages, sequence_start=True, **kwargs):
return super().messages2prompt(messages, sequence_start, **kwargs)[:-1]

@classmethod
def match(cls, model_path: str) -> Optional[str]:
"""Return the model_name that was registered to MODELS.
Expand Down Expand Up @@ -1325,13 +1323,23 @@ def match(cls, model_path: str) -> Optional[str]:
@MODELS.register_module(name=['deepseek'])
class Deepseek(BaseChatTemplate):

def __init__(self, user='User: ', eoh='\n\n', assistant='Assistant: ', eoa='<|end▁of▁sentence|>', **kwargs):
super().__init__(user=user, eoh=eoh, assistant=assistant, eoa=eoa, **kwargs)
def __init__(self,
eosys='\n\n',
user='User: ',
eoh='\n\n',
assistant='Assistant: ',
eoa='<|end▁of▁sentence|>',
**kwargs):
super().__init__(eosys=eosys, user=user, eoh=eoh, assistant=assistant, eoa=eoa, **kwargs)

def get_prompt(self, prompt, sequence_start=True):
return super().get_prompt(prompt, sequence_start)[:-1]
if self.capability == 'chat':
return super().get_prompt(prompt, sequence_start)[:-1]
return super().get_prompt(prompt, sequence_start)

def messages2prompt(self, messages, sequence_start=True, **kwargs):
if isinstance(messages, str):
return self.get_prompt(messages, sequence_start)
return super().messages2prompt(messages, sequence_start, **kwargs)[:-1]

@classmethod
Expand All @@ -1353,9 +1361,13 @@ def __init__(self, user='<human>: ', eoh=' ', assistant='<bot>: ', eoa='</s>', *
super().__init__(user=user, eoh=eoh, assistant=assistant, eoa=eoa, **kwargs)

def get_prompt(self, prompt, sequence_start=True):
return super().get_prompt(prompt, sequence_start)[:-1]
if self.capability == 'chat':
return super().get_prompt(prompt, sequence_start)[:-1]
return super().get_prompt(prompt, sequence_start)

def messages2prompt(self, messages, sequence_start=True, **kwargs):
if isinstance(messages, str):
return self.get_prompt(messages, sequence_start)
return super().messages2prompt(messages, sequence_start, **kwargs)[:-1]

@classmethod
Expand Down Expand Up @@ -1391,9 +1403,13 @@ def __init__(
**kwargs)

def get_prompt(self, prompt, sequence_start=True):
return super().get_prompt(prompt, sequence_start)[:-1]
if self.capability == 'chat':
return super().get_prompt(prompt, sequence_start)[:-1]
return super().get_prompt(prompt, sequence_start)

def messages2prompt(self, messages, sequence_start=True, **kwargs):
if isinstance(messages, str):
return self.get_prompt(messages, sequence_start)
return super().messages2prompt(messages, sequence_start, **kwargs)[:-1]

@classmethod
Expand Down
22 changes: 22 additions & 0 deletions tests/test_lmdeploy/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,28 @@ def test_codellama_others():
assert model is None


def test_deepseek():
model = MODELS.get('deepseek')()
messages = [{
'role': 'system',
'content': 'you are a helpful assistant'
}, {
'role': 'user',
'content': 'who are you'
}, {
'role': 'assistant',
'content': 'I am an AI'
}, {
'role': 'user',
'content': 'hi'
}]
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('deepseek-ai/DeepSeek-V2-Lite', trust_remote_code=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding test cases for DeepSeek V3 and DeepSeek R1 here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in #3072

ref = tokenizer.apply_chat_template(messages, tokenize=False)
res = '<|begin▁of▁sentence|>' + model.messages2prompt(messages)
assert res.startswith(ref)


def test_deepseek_coder():
model = MODELS.get('deepseek-coder')()
messages = [{
Expand Down