diff --git a/setup.py b/setup.py index 89cb5a080b..33f6db4559 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,6 @@ def get_version() -> str: extras["inference"] = [ "aiohttp", # for AsyncInferenceClient - "minijinja>=1.0", # for chat-completion if not TGI-served ] extras["torch"] = [ diff --git a/src/huggingface_hub/errors.py b/src/huggingface_hub/errors.py index 5252319de2..1dae6ddf97 100644 --- a/src/huggingface_hub/errors.py +++ b/src/huggingface_hub/errors.py @@ -120,13 +120,6 @@ class NotASafetensorsRepoError(Exception): """ -# TEMPLATING ERRORS - - -class TemplateError(Exception): - """Any error raised while trying to fetch or render a chat template.""" - - # TEXT GENERATION ERRORS diff --git a/src/huggingface_hub/inference/_templating.py b/src/huggingface_hub/inference/_templating.py deleted file mode 100644 index 954b203908..0000000000 --- a/src/huggingface_hub/inference/_templating.py +++ /dev/null @@ -1,102 +0,0 @@ -from functools import lru_cache -from typing import Callable, Dict, List, Optional, Union - -from ..errors import HfHubHTTPError, RepositoryNotFoundError, TemplateError -from ..utils import is_minijinja_available - - -def _import_minijinja(): - if not is_minijinja_available(): - raise ImportError("Cannot render template. Please install minijinja using `pip install minijinja`.") - import minijinja # noqa: F401 - - return minijinja - - -def render_chat_prompt( - *, - model_id: str, - messages: List[Dict[str, str]], - token: Union[str, bool, None] = None, - add_generation_prompt: bool = True, - **kwargs, -) -> str: - """Render a chat prompt using a model's chat template. - - Args: - model_id (`str`): - The model id. - messages (`List[Dict[str, str]]`): - The list of messages to render. - token (`str` or `bool`, *optional*): - Hugging Face token. Will default to the locally saved token if not provided. - - Returns: - `str`: The rendered chat prompt. - - Raises: - `TemplateError`: If there's any issue while fetching, compiling or rendering the chat template. - """ - minijinja = _import_minijinja() - template = _fetch_and_compile_template(model_id=model_id, token=token) - - try: - return template(messages=messages, add_generation_prompt=add_generation_prompt, **kwargs) - except minijinja.TemplateError as e: - raise TemplateError(f"Error while trying to render chat prompt for model '{model_id}': {e}") from e - - -@lru_cache # TODO: lru_cache for raised exceptions -def _fetch_and_compile_template(*, model_id: str, token: Union[str, None]) -> Callable: - """Fetch and compile a model's chat template. - - Method is cached to avoid fetching the same model's config multiple times. - - Args: - model_id (`str`): - The model id. - token (`str` or `bool`, *optional*): - Hugging Face token. Will default to the locally saved token if not provided. - - Returns: - `Callable`: A callable that takes a list of messages and returns the rendered chat prompt. - """ - from huggingface_hub.hf_api import HfApi - - minijinja = _import_minijinja() - - # 1. fetch config from API - try: - config = HfApi(token=token).model_info(model_id).config - except RepositoryNotFoundError as e: - raise TemplateError(f"Cannot render chat template: model '{model_id}' not found.") from e - except HfHubHTTPError as e: - raise TemplateError(f"Error while trying to fetch chat template for model '{model_id}': {e}") from e - - # 2. check config validity - if config is None: - raise TemplateError(f"Config not found for model '{model_id}'.") - tokenizer_config = config.get("tokenizer_config") - if tokenizer_config is None: - raise TemplateError(f"Tokenizer config not found for model '{model_id}'.") - if tokenizer_config.get("chat_template") is None: - raise TemplateError(f"Chat template not found in tokenizer_config for model '{model_id}'.") - chat_template = tokenizer_config["chat_template"] - if not isinstance(chat_template, str): - raise TemplateError(f"Chat template must be a string, not '{type(chat_template)}' (model: {model_id}).") - - special_tokens: Dict[str, Optional[str]] = {} - for key, value in tokenizer_config.items(): - if "token" in key: - if isinstance(value, str): - special_tokens[key] = value - elif isinstance(value, dict) and value.get("__type") == "AddedToken": - special_tokens[key] = value.get("content") - - # 3. compile template and return - env = minijinja.Environment() - try: - env.add_template("chat_template", chat_template) - except minijinja.TemplateError as e: - raise TemplateError(f"Error while trying to compile chat template for model '{model_id}': {e}") from e - return lambda **kwargs: env.render_template("chat_template", **kwargs, **special_tokens) diff --git a/src/huggingface_hub/utils/__init__.py b/src/huggingface_hub/utils/__init__.py index 8ad45734f1..b9715dc0ad 100644 --- a/src/huggingface_hub/utils/__init__.py +++ b/src/huggingface_hub/utils/__init__.py @@ -73,7 +73,6 @@ get_hf_hub_version, get_hf_transfer_version, get_jinja_version, - get_minijinja_version, get_numpy_version, get_pillow_version, get_pydantic_version, @@ -92,7 +91,6 @@ is_graphviz_available, is_hf_transfer_available, is_jinja_available, - is_minijinja_available, is_notebook, is_numpy_available, is_package_available, diff --git a/src/huggingface_hub/utils/_runtime.py b/src/huggingface_hub/utils/_runtime.py index 72b4dd4a2b..c8d82d4129 100644 --- a/src/huggingface_hub/utils/_runtime.py +++ b/src/huggingface_hub/utils/_runtime.py @@ -38,7 +38,6 @@ "hf_transfer": {"hf_transfer"}, "jinja": {"Jinja2"}, "keras": {"keras"}, - "minijinja": {"minijinja"}, "numpy": {"numpy"}, "pillow": {"Pillow"}, "pydantic": {"pydantic"}, @@ -161,15 +160,6 @@ def get_keras_version() -> str: return _get_version("keras") -# Minijinja -def is_minijinja_available() -> bool: - return is_package_available("minijinja") - - -def get_minijinja_version() -> str: - return _get_version("minijinja") - - # Numpy def is_numpy_available() -> bool: return is_package_available("numpy")