from __future__ import annotations

import os
import warnings
from functools import lru_cache

warnings.filterwarnings(
    "ignore",
    message=r"Core Pydantic V1 functionality isn't compatible with Python 3\.14 or greater\.",
    category=UserWarning,
)

try:
    from langchain_huggingface import HuggingFaceEmbeddings
except ImportError:  # pragma: no cover - compatibility fallback
    from langchain_community.embeddings import HuggingFaceEmbeddings
    warnings.filterwarnings(
        "ignore",
        message=r"The class `HuggingFaceEmbeddings` was deprecated in LangChain 0\.2\.2.*",
        category=Warning,
    )


@lru_cache(maxsize=8)
def get_hf_embeddings(model_name: str) -> HuggingFaceEmbeddings:
    """Return a cached embeddings model to avoid repeated heavyweight loads."""
    return HuggingFaceEmbeddings(
        model_name=model_name,
        model_kwargs={"device": os.getenv("EMBEDDINGS_DEVICE", "cpu")},
        show_progress=False,
    )
