diff --git a/lmdeploy/model.py b/lmdeploy/model.py index 8920c4308e..b699fe4242 100644 --- a/lmdeploy/model.py +++ b/lmdeploy/model.py @@ -988,7 +988,7 @@ def messages2prompt(self, system=self.system) ret = '' tool_prompt = '' - if tools is not None: + if tools is not None and len(tools) > 0: for tool in tools: tool_prompt += self.separator tool_prompt += f'{{"type": "function", "function": {json.dumps(tool, ensure_ascii=False)}}}' diff --git a/tests/test_lmdeploy/test_model.py b/tests/test_lmdeploy/test_model.py index c4faf77761..ae21053308 100644 --- a/tests/test_lmdeploy/test_model.py +++ b/tests/test_lmdeploy/test_model.py @@ -291,6 +291,28 @@ def test_qwen2d5(): assert model.get_prompt(prompt, sequence_start=False) == prompt model = MODELS.get('qwen2d5')(capability='chat') + + # No tool call + messages = [ + dict(role='user', + content='What\'s the temperature in San Francisco now?') + ] + no_tool_prompt = ('<|im_start|>system\nYou are Qwen, created by Alibaba ' + 'Cloud. You are a helpful ' + "assistant.<|im_end|>\n<|im_start|>user\nWhat's the " + 'temperature in San Francisco ' + 'now?<|im_end|>\n<|im_start|>assistant\n') + assert model.messages2prompt(messages) == no_tool_prompt + assert model.messages2prompt(messages, tools=[]) == no_tool_prompt + + messages.append({'role': 'assistant', 'content': 'I don\'t know.'}) + no_tool_prompt = ('<|im_start|>system\nYou are Qwen, created by Alibaba ' + 'Cloud. You are a helpful ' + "assistant.<|im_end|>\n<|im_start|>user\nWhat's the " + 'temperature in San Francisco ' + "now?<|im_end|>\n<|im_start|>assistant\nI don't " + 'know.<|im_end|>\n<|im_start|>assistant\n') + assert model.messages2prompt(messages) == no_tool_prompt # Single tool call tools = [{ 'name': 'get_current_temperature',