| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Provides lazy import of the vllm.tokenizers.mistral module.""" |
| 4 | |
| 5 | from __future__ import annotations |
| 6 | |
| 7 | from typing import TYPE_CHECKING, TypeGuard |
| 8 | |
| 9 | from vllm.tokenizers import TokenizerLike |
| 10 | from vllm.utils.import_utils import LazyLoader |
| 11 | |
| 12 | if TYPE_CHECKING: |
| 13 | # if type checking, eagerly import the module |
| 14 | import vllm.tokenizers.mistral as mt |
| 15 | else: |
| 16 | mt = LazyLoader("mt", globals(), "vllm.tokenizers.mistral") |
| 17 | |
| 18 | |
| 19 | def is_mistral_tokenizer(obj: TokenizerLike | None) -> TypeGuard[mt.MistralTokenizer]: |
| 20 | """Return true if the tokenizer is a MistralTokenizer instance.""" |
| 21 | cls = type(obj) |
| 22 | # Check for special class attribute, this avoids importing the class to |
| 23 | # do an isinstance() check. If the attribute is True, do an isinstance |
| 24 | # check to be sure we have the correct type. |
| 25 | return bool( |
| 26 | getattr(cls, "IS_MISTRAL_TOKENIZER", False) |
| 27 | and isinstance(obj, mt.MistralTokenizer) |
| 28 | ) |
| 29 | |
| 30 | |
| 31 | def is_mistral_tool_parser(cls: type | None) -> bool: |
| 32 | """Return true if *cls* carries the ``IS_MISTRAL_TOOL_PARSER`` marker. |
| 33 | |
| 34 | The marker is set on the engine-adapter subclass registered under the |
| 35 | ``"mistral"`` tool-parser key. No import of the parser module is |
| 36 | required. |
| 37 | """ |
| 38 | return bool(getattr(cls, "IS_MISTRAL_TOOL_PARSER", False)) |