Skip to content

hf_formatter

langroid/language_models/prompt_formatter/hf_formatter.py

Prompt formatter based on HuggingFace AutoTokenizer.apply_chat_template method from their Transformers library. It searches the hub for models matching the specified name, most-downloaded first, and uses the first one whose tokenizer loads and has a chat template. We assume that all matching models share the same chat template.

try_import_hf_modules()

Attempts to import the AutoTokenizer class from the transformers package. Returns: The AutoTokenizer class if successful. Raises: ImportError: If the transformers package is not installed.

Source code in langroid/language_models/prompt_formatter/hf_formatter.py
def try_import_hf_modules() -> Tuple[Type[Any], Type[Any]]:
    """
    Attempts to import the AutoTokenizer class from the transformers package.
    Returns:
        The AutoTokenizer class if successful.
    Raises:
        ImportError: If the transformers package is not installed.
    """
    try:
        from huggingface_hub import HfApi
        from transformers import AutoTokenizer

        return AutoTokenizer, HfApi
    except ImportError:
        raise ImportError(
            """
            You are trying to use some/all of:
            HuggingFace transformers.AutoTokenizer,
            huggingface_hub.HfApi,
            but these are not not installed 
            by default with Langroid. Please install langroid using the 
            `transformers` extra, like so:
            pip install "langroid[transformers]"
            or equivalent.
            """
        )