diff --git a/lmdeploy/cli/utils.py b/lmdeploy/cli/utils.py index 1dda62a7e8..79e611e5a6 100644 --- a/lmdeploy/cli/utils.py +++ b/lmdeploy/cli/utils.py @@ -7,10 +7,6 @@ from collections import defaultdict from typing import Any -from lmdeploy.utils import get_logger - -logger = get_logger('lmdeploy') - class DefaultsAndTypesHelpFormatter(argparse.HelpFormatter): """Formatter to output default value and type in help information.""" @@ -68,12 +64,12 @@ def get_lora_adapters(adapters: list[str]): return output -def get_chat_template(chat_template: str, model_path: str = None): +def get_chat_template(chat_template: str | None, model_path: str | None = None): """Get chat template config. Args: - chat_template(str): it could be a builtin chat template name, or a chat template json file - model_path(str): the model path, used to check deprecated chat template names + chat_template(str | None): it could be a builtin chat template name, or a chat template json file + model_path(str | None): the model path, passed through to the chat template config """ import os @@ -82,14 +78,7 @@ def get_chat_template(chat_template: str, model_path: str = None): if os.path.isfile(chat_template): return ChatTemplateConfig.from_json(chat_template) else: - from lmdeploy.model import DEPRECATED_CHAT_TEMPLATE_NAMES, MODELS, REMOVED_CHAT_TEMPLATE_NAMES - if chat_template in REMOVED_CHAT_TEMPLATE_NAMES: - raise ValueError(f"The chat template '{chat_template}' has been removed. " - f'Please refer to the latest chat templates in ' - f'https://lmdeploy.readthedocs.io/en/latest/advance/chat_template.html') - if chat_template in DEPRECATED_CHAT_TEMPLATE_NAMES: - logger.warning(f"The chat template '{chat_template}' is deprecated and fallback to hf chat template.") - chat_template = 'hf' + from lmdeploy.model import MODELS assert chat_template in MODELS.module_dict.keys(), \ f"chat template '{chat_template}' is not " \ f'registered. The builtin chat templates are: ' \ diff --git a/tests/test_lmdeploy/test_utils.py b/tests/test_lmdeploy/test_utils.py index 060c6a5ea5..856e9d264f 100644 --- a/tests/test_lmdeploy/test_utils.py +++ b/tests/test_lmdeploy/test_utils.py @@ -1,3 +1,4 @@ +import pytest import torch from transformers import AutoConfig @@ -87,3 +88,30 @@ def test_get_and_verify_max_len(): assert (_get_and_verify_max_len(config, None) == 32768) assert (_get_and_verify_max_len(config, 1024) == 1024) assert (_get_and_verify_max_len(config, 102400) == 102400) + + +def test_get_chat_template_builtin_name(): + """A builtin chat-template name must resolve without crashing. + + Regression for #4533: get_chat_template imported DEPRECATED_CHAT_TEMPLATE_NAMES / REMOVED_CHAT_TEMPLATE_NAMES from + lmdeploy.model, which were removed in #4252, so any non-file --chat-template value raised ImportError before + reaching the registry check. + """ + from lmdeploy.cli.utils import get_chat_template + cfg = get_chat_template('llama2') + assert cfg is not None + assert cfg.model_name == 'llama2' + + +def test_get_chat_template_unregistered_name(): + """An unregistered name is rejected by the registry check, not by an + ImportError.""" + from lmdeploy.cli.utils import get_chat_template + with pytest.raises(AssertionError): + get_chat_template('not-a-registered-template') + + +def test_get_chat_template_none(): + """Passing no chat template returns None.""" + from lmdeploy.cli.utils import get_chat_template + assert get_chat_template(None) is None