Skip to content

Commit

Permalink
fix: clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
gaozixiang committed Nov 12, 2024
1 parent cfff8ee commit 4641d39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
56 changes: 34 additions & 22 deletions lmdeploy/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,27 +891,29 @@ def match(cls, model_path: str) -> Optional[str]:
) or 'llama3.2-' in model_path.lower():
return 'llama3_1'


@MODELS.register_module(name='qwen2d5')
class Qwen2halfChat(BaseChatTemplate):
"""Chat template for Qwen-7B-Chat."""

def __init__(self,
system='<|im_start|>system\n',
meta_instruction='You are Qwen, created by Alibaba Cloud. 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',
tools="""
def __init__(
self,
system='<|im_start|>system\n',
meta_instruction='You are Qwen, created by Alibaba Cloud. 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',
tools="""
\n\n#Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>
""",
eotools="""
eotools="""
\n\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{"name": <function-name>, "arguments": <args-json-object>}\n</tool_call><|im_end|>\
""",
stop_words=['<|im_end|>'],
**kwargs):
stop_words=['<|im_end|>'],
**kwargs):

self.tools = tools
self.eotools = eotools
Expand All @@ -925,11 +927,15 @@ def __init__(self,
separator=separator,
stop_words=stop_words,
**kwargs)
def messages2prompt(self, messages, sequence_start=True,
tools=None, **kwargs):

def messages2prompt(self,
messages,
sequence_start=True,
tools=None,
**kwargs):
"""Return the prompt that is concatenated with other elements in the
chat template.
Args:
messages (str | List): user's input prompt
Returns:
Expand All @@ -948,16 +954,19 @@ def messages2prompt(self, messages, sequence_start=True,
if len(messages) and messages[0]['role'] == 'system':
ret += f"{messages[0]['content']}{self.tools}{tool_prompt}{self.eotools}{self.meta_instruction}{self.eosys}"
else:
ret += f"{self.system}{self.meta_instruction}{self.tools}{tool_prompt}{self.eotools}{self.eosys}"
ret += f'{self.system}{self.meta_instruction}{self.tools}{tool_prompt}{self.eotools}{self.eosys}'
else:
if self.meta_instruction is not None and sequence_start:
if len(messages) and messages[0]['role'] == 'system':
ret += f"{self.system}{messages[0]['content']}{self.eosys}"
else:
ret += f"{self.system}{self.meta_instruction}{self.eosys}"
ret += f'{self.system}{self.meta_instruction}{self.eosys}'

for index, message in enumerate(messages):
if (message['role'] == 'user' or (message['role'] == 'system' and index != 0) or (message['role'] == 'assistant' and message['tools_call'] is not None)):
if (message['role'] == 'user'
or (message['role'] == 'system' and index != 0)
or (message['role'] == 'assistant'
and message['tools_call'] is not None)):
ret += f"{box_map[message['role']]}\n{message['content']}\n{self.eoh}"
if message['role'] == 'assistant':
ret += f"{box_map[message['role']]}"
Expand All @@ -971,10 +980,11 @@ def messages2prompt(self, messages, sequence_start=True,
ret += f'\n<tool_call>\n{{"name": "{toolCall["name"]}, "arguments": {json.dumps(tools["arguments"])}"\n</toolcall>}}'
if message['role'] == 'tool':
if index == 0 or messages[index - 1]['role'] != 'tool':
ret += f"{self.user}"
ret += f'{self.user}'
ret += f"\n<tool_response>\n{message['content']}\n</tool_response>"
if index == len(messages) - 1 or messages[index + 1]['role'] != 'tool':
ret += f"{self.eoh}"
if index == len(messages) - 1 or messages[index +
1]['role'] != 'tool':
ret += f'{self.eoh}'
ret += f'{self.assistant}'
return ret

Expand All @@ -988,6 +998,7 @@ def match(cls, model_path: str) -> Optional[str]:
if 'qwen2.5' in model_path.lower():
return 'qwen2d5'


@MODELS.register_module(name='minicpmv-2d6')
@MODELS.register_module(name='minicpm3')
@MODELS.register_module(name='qwen')
Expand Down Expand Up @@ -1023,7 +1034,8 @@ def match(cls, model_path: str) -> Optional[str]:
Args:
model_path (str): the model path used for matching.
"""
if 'qwen' in model_path.lower() and 'qwen2.5' not in model_path.lower():
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'
Expand Down
5 changes: 2 additions & 3 deletions lmdeploy/serve/openai/api_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,8 @@ async def completion_stream_generator() -> AsyncGenerator[str, None]:
final_logprobs.extend(res.logprobs)

tool_calls = None
if request.tool_choice != 'none' and ('<|plugin|>' in text
or '<function=' in text
or '<tool_call>' in text):
if request.tool_choice != 'none' and ('<|plugin|>' in text or '<function='
in text or '<tool_call>' in text):
if final_res.finish_reason == 'stop':
final_res.finish_reason = 'tool_calls'
try: # TODO add json_schema guidance to turbomind
Expand Down

0 comments on commit 4641d39

Please sign in to comment.