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 a model matching the specified name, and uses the first one it finds. We assume that all matching models will have the same tokenizer, so we just use the first one.

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], 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, ModelFilter
        from transformers import AutoTokenizer

        return AutoTokenizer, HfApi, ModelFilter
    except ImportError:
        raise ImportError(
            """
            You are trying to use some/all of:
            HuggingFace transformers.AutoTokenizer,
            huggingface_hub.HfApi, 
            huggingface_hub.ModelFilter,
            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.
            """
        )