huggingface.py
1 from __future__ import annotations 2 3 """HuggingFace embedding model implementation.""" 4 5 from typing import Any 6 7 from langchain_huggingface import HuggingFaceEmbeddings 8 9 from .constants import DEFAULT_HUGGINGFACE_MODEL 10 from .protocol import Embeddings 11 12 13 def create_huggingface_embedding(config: dict[str, Any]) -> Embeddings: 14 """Create HuggingFace embedding model. 15 16 Parameters 17 ---------- 18 config 19 Configuration dictionary. Common parameters include: 20 - model_name: str (optional) - Model name (defaults to DEFAULT_HUGGINGFACE_MODEL) 21 - model_kwargs: dict (optional) - Additional model arguments 22 - encode_kwargs: dict (optional) - Additional encoding arguments 23 - Other parameters supported by HuggingFaceEmbeddings constructor. 24 Invalid parameters will be caught by HuggingFaceEmbeddings and raise clear errors. 25 26 Returns 27 ------- 28 Embeddings instance. 29 30 Raises 31 ------ 32 ValueError 33 If model creation fails or invalid parameters are provided. 34 """ 35 36 if "model_name" not in config: 37 config = {**config, "model_name": DEFAULT_HUGGINGFACE_MODEL} 38 39 try: 40 return HuggingFaceEmbeddings(**config) 41 except TypeError as e: 42 raise ValueError( 43 f"Invalid parameter for HuggingFace embedding model: {e}. " 44 "Check HuggingFaceEmbeddings documentation for valid parameters." 45 ) from e 46 except Exception as e: 47 raise ValueError(f"Failed to create HuggingFace embedding model: {e}") from e