From d49b6dd240098792ce7604beadbca384273e7361 Mon Sep 17 00:00:00 2001 From: gaozixiang Date: Wed, 13 Nov 2024 15:55:22 +0800 Subject: [PATCH] fix: adopting review suggestions --- lmdeploy/model.py | 96 +++++++++++++++++----------------- lmdeploy/serve/async_engine.py | 5 +- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/lmdeploy/model.py b/lmdeploy/model.py index bc85c7516..7f6d69866 100644 --- a/lmdeploy/model.py +++ b/lmdeploy/model.py @@ -892,10 +892,54 @@ def match(cls, model_path: str) -> Optional[str]: return 'llama3_1' -@MODELS.register_module(name='qwen2d5') -class Qwen2halfChat(BaseChatTemplate): +@MODELS.register_module(name='minicpmv-2d6') +@MODELS.register_module(name='minicpm3') +@MODELS.register_module(name='qwen') +class Qwen7BChat(BaseChatTemplate): """Chat template for Qwen-7B-Chat.""" + def __init__(self, + system='<|im_start|>system\n', + meta_instruction='You are a helpful assistant.', + eosys='<|im_end|>\n', + user='<|im_start|>user\n', + eoh='<|im_end|>\n', + assistant='<|im_start|>assistant\n', + eoa='<|im_end|>', + separator='\n', + stop_words=['<|im_end|>'], + **kwargs): + super().__init__(system=system, + meta_instruction=meta_instruction, + eosys=eosys, + user=user, + eoh=eoh, + assistant=assistant, + eoa=eoa, + separator=separator, + stop_words=stop_words, + **kwargs) + + @classmethod + def match(cls, model_path: str) -> Optional[str]: + """Return the model_name that was registered to MODELS. + + Args: + model_path (str): the model path used for matching. + """ + if 'qwen' in model_path.lower() and 'qwen2.5' not in model_path.lower( + ): + return 'qwen' + if 'minicpm-v-2_6' in model_path.lower(): + return 'minicpmv-2d6' + if 'minicpm3-' in model_path.lower(): + return 'minicpm3' + + +@MODELS.register_module(name='qwen2d5') +class Qwen2d5Chat(Qwen7BChat): + """Chat template for Qwen2.5-Instruct series.""" + def __init__( self, system='<|im_start|>system\n', @@ -969,8 +1013,8 @@ def messages2prompt(self, if message.get('content') is not None: ret += f"\n{message['content']}" if message.get('tools_call') is not None: - toolsCall = message['tools_call'] - for toolCall in toolsCall: + tools_call = message['tools_call'] + for toolCall in tools_call: if toolCall.get('function') is not None: toolCall = toolCall['function'] ret += f'\n\n{{"name": "{toolCall["name"]}, "arguments": {json.dumps(tools["arguments"])}"\n}}' @@ -995,50 +1039,6 @@ def match(cls, model_path: str) -> Optional[str]: return 'qwen2d5' -@MODELS.register_module(name='minicpmv-2d6') -@MODELS.register_module(name='minicpm3') -@MODELS.register_module(name='qwen') -class Qwen7BChat(BaseChatTemplate): - """Chat template for Qwen-7B-Chat.""" - - def __init__(self, - system='<|im_start|>system\n', - meta_instruction='You are a helpful assistant.', - eosys='<|im_end|>\n', - user='<|im_start|>user\n', - eoh='<|im_end|>\n', - assistant='<|im_start|>assistant\n', - eoa='<|im_end|>', - separator='\n', - stop_words=['<|im_end|>'], - **kwargs): - super().__init__(system=system, - meta_instruction=meta_instruction, - eosys=eosys, - user=user, - eoh=eoh, - assistant=assistant, - eoa=eoa, - separator=separator, - stop_words=stop_words, - **kwargs) - - @classmethod - def match(cls, model_path: str) -> Optional[str]: - """Return the model_name that was registered to MODELS. - - Args: - model_path (str): the model path used for matching. - """ - if 'qwen' in model_path.lower() and 'qwen2.5' not in model_path.lower( - ): - return 'qwen' - if 'minicpm-v-2_6' in model_path.lower(): - return 'minicpmv-2d6' - if 'minicpm3-' in model_path.lower(): - return 'minicpm3' - - @MODELS.register_module(name='codellama') class CodeLlama(Llama2): diff --git a/lmdeploy/serve/async_engine.py b/lmdeploy/serve/async_engine.py index 6e4dac58c..0bef79237 100644 --- a/lmdeploy/serve/async_engine.py +++ b/lmdeploy/serve/async_engine.py @@ -660,8 +660,9 @@ def parse_tool_response(self, text, tools, **kwargs): else: text, action = action action = json.loads(action) - name, parameters = action['name'], json.dumps( - action.get('parameters', action.get('arguments', {}))) + name, parameters = action['name'], json.dumps(action.get( + 'parameters', action.get('arguments', {})), + ensure_ascii=False) else: raise RuntimeError(f'Unexpected model response: {text}') action_id = [tool.function.name for tool in tools].index(name)