Skip to content

vllm.transformers_utils.chat_templates.registry

CHAT_TEMPLATES_DIR module-attribute

CHAT_TEMPLATES_DIR = parent

ChatTemplatePath module-attribute

ChatTemplatePath = Union[
    Path, Callable[[str], Optional[Path]]
]

_MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK module-attribute

_MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK: dict[
    str, ChatTemplatePath
] = {
    "blip-2": CHAT_TEMPLATES_DIR / "template_blip2.jinja",
    "chameleon": CHAT_TEMPLATES_DIR
    / "template_basic.jinja",
    "deepseek_vl_v2": CHAT_TEMPLATES_DIR
    / "template_deepseek_vl2.jinja",
    "florence2": CHAT_TEMPLATES_DIR
    / "template_basic.jinja",
    "fuyu": CHAT_TEMPLATES_DIR / "template_fuyu.jinja",
    "paligemma": CHAT_TEMPLATES_DIR
    / "template_basic.jinja",
    "qwen": _get_qwen_chat_template_fallback,
}

logger module-attribute

logger = init_logger(__file__)

_get_qwen_chat_template_fallback

_get_qwen_chat_template_fallback(
    tokenizer_name_or_path: str,
) -> Optional[Path]
Source code in vllm/transformers_utils/chat_templates/registry.py
def _get_qwen_chat_template_fallback(
        tokenizer_name_or_path: str) -> Optional[Path]:
    if tokenizer_name_or_path.endswith("-Chat"):
        return CHAT_TEMPLATES_DIR / "template_chatml.jinja"

    return CHAT_TEMPLATES_DIR / "template_basic.jinja"

get_chat_template_fallback_path

get_chat_template_fallback_path(
    model_type: str, tokenizer_name_or_path: str
) -> Optional[Path]
Source code in vllm/transformers_utils/chat_templates/registry.py
def get_chat_template_fallback_path(
    model_type: str,
    tokenizer_name_or_path: str,
) -> Optional[Path]:
    chat_template = _MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK.get(model_type)
    if callable(chat_template):
        chat_template = chat_template(tokenizer_name_or_path)

    if chat_template is None:
        return None

    return chat_template

register_chat_template_fallback_path

register_chat_template_fallback_path(
    model_type: str, chat_template: ChatTemplatePath
) -> None
Source code in vllm/transformers_utils/chat_templates/registry.py
def register_chat_template_fallback_path(
    model_type: str,
    chat_template: ChatTemplatePath,
) -> None:
    if model_type in _MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK:
        logger.warning(
            "Model type %s already has a chat template registered. "
            "It will be overwritten by the new chat template %s.", model_type,
            chat_template)

    _MODEL_TYPE_TO_CHAT_TEMPLATE_FALLBACK[model_type] = chat_template