Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

30.3 KB
Raw Blame
"""Sentence Transformers embedding provider."""

import errno
import re
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

from langchain_core.embeddings import Embeddings
from ....security.secure_logging import logger

from ....config.thread_settings import get_setting_from_snapshot
from ...model_cache import shared_sentence_transformer_embeddings
from ...sentence_transformer_models import (
    DEFAULT_SENTENCE_TRANSFORMER_MODEL,
    SENTENCE_TRANSFORMER_MODELS,
    get_sentence_transformer_model_spec,
)
from ..base import BaseEmbeddingProvider, Exposure


class SentenceTransformersProvider(BaseEmbeddingProvider):
    """
    Sentence Transformers embedding provider.

    Uses HuggingFace sentence-transformers models for local embeddings.
    No API key required, runs entirely locally.
    """

    provider_name = "Sentence Transformers"
    provider_key = "SENTENCE_TRANSFORMERS"
    requires_api_key = False
    supports_local = True
    egress_exposure = Exposure.CONTAINED
    default_model = DEFAULT_SENTENCE_TRANSFORMER_MODEL  # type: ignore[assignment]

    # Preserve the provider API's legacy metadata shape while centralizing the
    # curated allowlist and the default model's immutable revision.
    AVAILABLE_MODELS = {
        model_name: spec.display_metadata()
        for model_name, spec in SENTENCE_TRANSFORMER_MODELS.items()
    }

    @classmethod
    def create_embeddings(
        cls,
        model: Optional[str] = None,
        settings_snapshot: Optional[Dict[str, Any]] = None,
        **kwargs,
    ) -> Embeddings:
        """
        Create a Sentence Transformers embeddings handle.

        Args:
            model: Model name (defaults to Alibaba-NLP/gte-modernbert-base)
            settings_snapshot: Optional settings snapshot
            **kwargs: Additional parameters (device, etc.)

        Returns:
            A closeable handle to the process-wide resident model; it holds
            no weights itself.
        """
        from langchain_community.embeddings import (
            SentenceTransformerEmbeddings,
        )

        # Get model from settings if not specified
        if model is None:
            model = get_setting_from_snapshot(
                "embeddings.sentence_transformers.model",
                default=cls.default_model,
                settings_snapshot=settings_snapshot,
            )

        # Get device setting (cpu or cuda)
        device = kwargs.get("device")
        if device is None:
            device = get_setting_from_snapshot(
                "embeddings.sentence_transformers.device",
                default="cpu",
                settings_snapshot=settings_snapshot,
            )

        logger.info(
            f"Preparing local embeddings with model={model}, device={device}"
        )

        # Constructing a cache-missing model is itself an egress action. It is
        # allowed only under a resolved public scope (or the explicit
        # UNPROTECTED escape hatch), never merely because the local-embedding
        # flag is false. Snapshot-less, STRICT, PRIVATE_ONLY, and adaptive
        # contexts that resolve BOTH therefore remain cache-only.
        download_allowed = False
        if settings_snapshot is not None:
            try:
                from ....security.egress.policy import (
                    EgressScope,
                    context_from_snapshot,
                    resolve_run_primary_engine,
                )

                from ....search_system import username_from_snapshot

                # No fallback primary: an incomplete snapshot cannot widen the
                # constructor into a network-capable state.
                _primary = resolve_run_primary_engine(settings_snapshot)
                context = context_from_snapshot(
                    settings_snapshot,
                    _primary,
                    username=username_from_snapshot(settings_snapshot),
                )
                download_allowed = (
                    not context.require_local_embeddings
                    and context.scope
                    in {EgressScope.PUBLIC_ONLY, EgressScope.UNPROTECTED}
                )
            except ValueError:
                # No usable primary: fail closed to cache-only. Unknown scopes
                # raise PolicyDeniedError and intentionally propagate.
                pass

        spec = (
            get_sentence_transformer_model_spec(model)
            if isinstance(model, str)
            else None
        )
        # Keep existing explicit local models confined to the application's
        # models directory. The confined models directory is app-owned state,
        # so an operator-placed model there wins in every scope — including
        # when its name matches a catalog key or repository. That is the
        # pre-catalog behavior and the only offline-usable form for an
        # air-gapped install; resolving such a name to the Hub artifact
        # instead would silently embed with different bytes than the
        # collection was indexed with. The substitution is audited below so
        # it is never silent. (This is unrelated to
        # ``_reject_shadowed_hub_model``, which refuses an unconfined
        # working-directory match for a name that was admitted as a Hub id.)
        local_model_path = cls._confined_local_model_path(model)
        if local_model_path is not None and spec is not None:
            logger.bind(policy_audit=True).warning(
                "using the confined local model directory {!r} for "
                "embedding model {!r} instead of the curated Hub model {!r}",
                str(local_model_path),
                model,
                spec.repository,
            )
        if local_model_path is None and cls._looks_like_filesystem_path(model):
            raise ValueError(
                "Invalid embedding model path: local model paths must "
                "resolve under the application's models directory."
            )
        cache_dir = cls._model_cache_directory()
        if local_model_path is not None:
            return shared_sentence_transformer_embeddings(
                SentenceTransformerEmbeddings,
                model_name=str(local_model_path),
                cache_folder=cache_dir,
                model_kwargs={
                    "device": device,
                    "trust_remote_code": False,
                    "token": False,
                    "model_kwargs": {"use_safetensors": True},
                    "local_files_only": True,
                },
            )

        if spec is None:
            # Legacy compatibility: if the model is already cached locally
            # from a previous installation, allow loading it in local-only
            # mode. This preserves access to existing collections indexed
            # with non-catalog models without permitting new downloads of
            # unvetted artifacts.
            if isinstance(model, str) and cls._is_model_cached_locally(
                model, cache_dir=cache_dir
            ):
                cls._reject_shadowed_hub_model(model)
                logger.info(
                    "Loading legacy cached SentenceTransformer model {!r} "
                    "in local-only mode (not in catalog but already cached)",
                    model,
                )
                return shared_sentence_transformer_embeddings(
                    SentenceTransformerEmbeddings,
                    model_name=model,
                    cache_folder=cache_dir,
                    model_kwargs={
                        "device": device,
                        "trust_remote_code": False,
                        "token": False,
                        "model_kwargs": {"use_safetensors": True},
                        "local_files_only": True,
                    },
                )

            from ....security.egress.policy import (
                Decision,
                PolicyDeniedError,
            )

            # New downloads are restricted to the catalog. Existing local
            # models were handled through the confined path above.
            logger.bind(policy_audit=True).warning(
                "refusing non-curated SentenceTransformer model {!r}", model
            )
            raise PolicyDeniedError(
                Decision(False, "embeddings_model_not_curated"),
                target=str(model),
            )

        # The new GTE key has an immutable artifact revision. Legacy keys must
        # remain unpinned until collections persist a revision/digest; changing
        # the bytes behind an existing key would silently invalidate its index.
        resolved_model = spec.repository if spec.revision is not None else model
        cls._reject_shadowed_hub_model(resolved_model)
        # Resolve an already cached legacy snapshot without consulting the
        # network. Repair must use these same bytes, never a newer remote main.
        resolved_revision = spec.revision or cls._cached_model_revision(
            spec.repository, cache_dir=cache_dir
        )
        model_kwargs = {
            "device": device,
            "trust_remote_code": False,
            "token": False,
            # Forwarded by SentenceTransformer to the nested transformers
            # AutoModel.from_pretrained call. Do not accept pickle-based weights.
            "model_kwargs": {"use_safetensors": True},
        }
        if resolved_revision is not None:
            model_kwargs["revision"] = resolved_revision
            cached = cls._is_model_cached_locally(
                spec.repository, revision=resolved_revision, cache_dir=cache_dir
            )
        else:
            # Preserve the exact legacy key/alias lookup semantics. The
            # local-only constructor below reuses refs/main without checking
            # for, or advancing to, a newer remote artifact.
            cached = cls._is_model_cached_locally(model, cache_dir=cache_dir)

        if cached:
            # Probe the cache before considering policy-authorized network
            # acquisition in every scope. This prevents repeat network
            # checks and keeps a legacy collection on its existing bytes.
            model_kwargs["local_files_only"] = True
        elif not download_allowed:
            from ....security.egress.policy import (
                Decision,
                PolicyDeniedError,
            )

            logger.bind(policy_audit=True).warning(
                "refusing SentenceTransformer download for {!r} "
                "outside a resolved public egress scope",
                model,
            )
            raise PolicyDeniedError(
                Decision(False, "embeddings_model_not_cached"),
                target=str(model),
            )

        elif (
            spec.revision is None
            and resolved_revision is None
            and cls._has_model_cache_state(spec.repository, cache_dir=cache_dir)
        ):
            from ....security.egress.policy import Decision, PolicyDeniedError

            # A partially cached model is not a first install. Without a
            # trustworthy revision, a floating download could change the
            # vectors behind an existing collection's model key.
            raise PolicyDeniedError(
                Decision(False, "embeddings_cache_revision_unknown"),
                target=str(model),
            )

        try:
            return shared_sentence_transformer_embeddings(
                SentenceTransformerEmbeddings,
                model_name=resolved_model,
                cache_folder=cache_dir,
                model_kwargs=model_kwargs,
            )
        except OSError as error:
            # Metadata is evidence of a snapshot, not proof it is complete.
            # An interrupted download can leave config/modules but no weights.
            # Try the cache first; repair only under an authorized public scope
            # and at an immutable known revision (including legacy snapshots).
            #
            # An environment fault is not an incomplete snapshot. The repair
            # runs at the same commit revision, and the Hub client returns a
            # file already present in that snapshot without fetching it again,
            # so e.g. an unreadable modules.json or config (PermissionError,
            # EACCES) fails identically on the retry. The retry could only
            # reproduce the fault, now with network access enabled, and would
            # replace the original, actionable error. Surface those unchanged.
            # (Unreadable weights are different: safetensors reports them as
            # FileNotFoundError without an errno, so they enter the repair,
            # which likewise reuses the existing blob and fails the same way.)
            if isinstance(error, PermissionError) or error.errno in (
                errno.EACCES,
                errno.EPERM,
                errno.ENOSPC,
                errno.EROFS,
            ):
                raise
            if not (
                model_kwargs.get("local_files_only")
                and download_allowed
                and resolved_revision is not None
            ):
                raise
            repair_kwargs = dict(model_kwargs)
            repair_kwargs.pop("local_files_only")
            return shared_sentence_transformer_embeddings(
                SentenceTransformerEmbeddings,
                model_name=resolved_model,
                cache_folder=cache_dir,
                model_kwargs=repair_kwargs,
            )

    @staticmethod
    def _model_cache_directory() -> str:
        """Use the loader's cache root for admission, revision and repair.

        Sentence Transformers gives its own environment override precedence
        over the Hub default. Resolve it once per load and pass it explicitly
        to both cache probes and constructors.
        """
        from ....config.paths import get_sentence_transformers_home
        from huggingface_hub.constants import HF_HUB_CACHE

        cache_dir = get_sentence_transformers_home()
        return HF_HUB_CACHE if cache_dir is None else cache_dir

    @classmethod
    def _is_model_cached_locally(
        cls,
        model_name: str,
        revision: Optional[str] = None,
        *,
        cache_dir: Optional[str] = None,
    ) -> bool:
        """Check the Hub cache for an offline-loadable configuration.

        Confined local models never reach this helper: ``create_embeddings``
        resolves ``_confined_local_model_path`` first and returns from its own
        branch, so this is purely a repo-id cache probe (a filesystem-shaped
        name is a miss, not an existence probe).

        Curated models require every file in their spec's
        ``required_cache_files`` (model config, module list, pooling config,
        tokenizer config and, where the artifact publishes one, the
        Transformer module config) plus at least one file from each group in
        ``required_cache_file_alternatives`` (the tokenizer vocabulary:
        ``tokenizer.json`` or ``vocab.txt`` for the legacy WordPiece/MPNet
        models, ``tokenizer.json`` for the others). Any of them missing can
        silently change pooling, truncation, tokenizer selection or the
        vocabulary itself without the offline constructor raising, so none of
        it is left to the missing-weight repair path. Weights are not probed:
        a missing weights file does raise from the offline constructor, which
        a policy-authorized load repairs at the cache's existing revision.
        Names outside the catalog need only ``config.json`` (config-only
        vanilla transformer snapshots remain compatible).
        """
        if not model_name or not isinstance(model_name, str):
            return False
        if cls._looks_like_filesystem_path(model_name):
            return False
        try:
            from huggingface_hub import try_to_load_from_cache

            # Probe both the bare and the namespaced cache keys when the
            # input has no "/", because the upstream loader resolves bare
            # names two different ways:
            #   - names in ``sentence_transformers.util.misc``'s
            #     ``ORIGINAL_TRANSFORMER_MODELS`` edge-case list
            #     (bert-base-uncased, gpt2, t5-base, ...) are requested
            #     from the BARE repo_id;
            #   - everything else is prefixed with the model class's
            #     ``default_huggingface_organization`` — for
            #     ``SentenceTransformer`` the value re-exported as
            #     ``__MODEL_HUB_ORGANIZATION__`` (e.g. "all-MiniLM-L6-v2"
            #     -> "sentence-transformers/all-MiniLM-L6-v2").
            # The HF hub cache is keyed on whichever form the loader
            # requests, so probing both is the only way to be correct
            # for both classes without mirroring that upstream list.
            if "/" in model_name:
                candidates = [model_name]
            else:
                from sentence_transformers import (
                    __MODEL_HUB_ORGANIZATION__,
                )

                candidates = [
                    model_name,
                    f"{__MODEL_HUB_ORGANIZATION__}/{model_name}",
                ]

            if cache_dir is None:
                cache_dir = cls._model_cache_directory()
            spec = get_sentence_transformer_model_spec(model_name)
            required_files = (
                spec.required_cache_files if spec else ("config.json",)
            )
            alternatives = spec.required_cache_file_alternatives if spec else ()

            def is_cached(repo_id: str, filename: str) -> bool:
                cache_kwargs = {
                    "repo_id": repo_id,
                    "filename": filename,
                    "cache_dir": cache_dir,
                }
                if revision is not None:
                    cache_kwargs["revision"] = revision
                cached = try_to_load_from_cache(**cache_kwargs)
                return isinstance(cached, str) and bool(cached)

            # Every required file, and at least one file of every group.
            return any(
                all(is_cached(repo_id, name) for name in required_files)
                and all(
                    any(is_cached(repo_id, name) for name in group)
                    for group in alternatives
                )
                for repo_id in candidates
            )
        except Exception:  # pragma: no cover - defensive
            return False

    @classmethod
    def _cached_model_revision(
        cls, repository: str, *, cache_dir: Optional[str] = None
    ) -> Optional[str]:
        """Read a snapshot commit from local metadata or the Hub's refs/main.

        Hub cache lookup returns a lexical snapshots/<commit>/<file> path;
        resolving its symlink would lose the commit by pointing into blobs/.
        Reject ambiguous or unrecognized paths rather than repair at main.
        """
        try:
            from huggingface_hub import try_to_load_from_cache

            if cache_dir is None:
                cache_dir = cls._model_cache_directory()
            revisions = set()
            for filename in ("config.json", "modules.json"):
                cached = try_to_load_from_cache(
                    repo_id=repository, filename=filename, cache_dir=cache_dir
                )
                if not isinstance(cached, str) or not cached:
                    continue
                snapshot = Path(cached).parent
                if snapshot.parent.name != "snapshots" or not re.fullmatch(
                    r"[0-9a-f]{40}", snapshot.name
                ):
                    return None
                revisions.add(snapshot.name)
            # The Hub's local ref survives loss of both metadata anchors.
            # Read it without resolving a model file or contacting the Hub.
            reference = (
                Path(cache_dir)
                / ("models--" + repository.replace("/", "--"))
                / "refs"
                / "main"
            )
            try:
                # nosemgrep: semgrep.rules.path-traversal-risk, reason: Repository comes from the curated embedding catalog; refs/main is inside the operator model cache
                cached_revision = reference.read_text(encoding="ascii").strip()
            except FileNotFoundError:
                cached_revision = None
            if cached_revision is not None:
                if not re.fullmatch(r"[0-9a-f]{40}", cached_revision):
                    return None
                revisions.add(cached_revision)
            return next(iter(revisions)) if len(revisions) == 1 else None
        except Exception:  # pragma: no cover - cache lookup is best-effort
            return None

    @classmethod
    def _has_model_cache_state(
        cls, repository: str, *, cache_dir: Optional[str] = None
    ) -> bool:
        """Distinguish an empty cache from an unresolved existing snapshot.

        Only curated repository names reach this helper. Any existing entry
        counts as cache state; inaccessible state must not permit a floating
        download. An absent or completely empty repository cache is a new
        installation.
        """
        if cache_dir is None:
            cache_dir = cls._model_cache_directory()
        cache = Path(cache_dir) / ("models--" + repository.replace("/", "--"))
        try:
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: Repository comes from the curated embedding catalog; only its local cache directory is enumerated
            return next(cache.iterdir(), None) is not None
        except FileNotFoundError:
            return False
        except OSError:
            return True

    @staticmethod
    def _reject_shadowed_hub_model(model_name: str) -> None:
        """Prevent the loader from interpreting an admitted Hub ID as a CWD path.

        Call only after catalog/cache admission and lexical path checks, so
        arbitrary absolute server paths never reach this existence check.
        Explicit local models use the separately confined absolute path.
        """
        try:
            shadowed = Path(model_name).exists()
        except (OSError, ValueError):
            shadowed = True
        if shadowed:
            raise ValueError(
                "Embedding model identifier conflicts with a local path. "
                "Use an explicit path under the application's models directory "
                "for a local model."
            )

    @classmethod
    def _confined_local_model_path(
        cls,
        model_name: Optional[str],
        models_dir: Optional[Union[str, Path]] = None,
    ) -> Optional[Path]:
        """Resolve ``model_name`` to a real local model path IFF it denotes an
        EXISTING location confined UNDER the application's models directory.

        Returns the resolved :class:`~pathlib.Path` for a legitimate local
        model (a file or directory living under the models dir), or ``None``
        for everything else — HuggingFace repo ids and, crucially, any
        absolute/relative path that escapes the models directory.

        Security: a value that is NOT confined under the models dir is
        rejected by LEXICAL containment (``is_relative_to``) and is never
        probed on disk with ``Path(model_name).exists()``. This is what closes
        the arbitrary-filesystem existence oracle. Only a value already
        confined under the models dir is resolved and stat-ed.
        """
        if not isinstance(model_name, str):
            return None
        text = model_name.strip()
        if not text:
            return None

        from ....security.path_validator import (
            PathValidator,
            resolve_allow_missing,
        )

        try:
            if models_dir is None:
                from ....config.paths import get_models_directory

                models_dir = get_models_directory()
            # Two views of the models root are needed:
            #   * ``models_root_lexical`` -- absolute + lexically normalized
            #     but NOT symlink-resolved. Used for the LEXICAL containment
            #     check on absolute inputs so a legitimate in-tree model still
            #     matches when an ANCESTOR of the models dir is a symlink
            #     (NFS-mounted homes, a symlinked ``LDR_DATA_DIR``, macOS
            #     ``/tmp`` -> ``/private/tmp``). Resolving the root but NOT the
            #     user path is precisely the mismatch that wrongly refused
            #     legitimate models.
            #   * ``models_root`` -- fully symlink-resolved. Used ONLY for the
            #     secondary post-resolution escape re-check below, which
            #     collapses any symlink in the confined candidate and confirms
            #     it still lands inside the resolved tree (catches an in-tree
            #     symlink that points OUT of the models dir).
            #
            # ``.absolute()`` makes the path absolute WITHOUT resolving
            # symlinks (pathlib already collapses ``.`` and redundant
            # separators on construction); it is used deliberately here in
            # place of ``.resolve()`` so the lexical comparison below stays
            # symlink-agnostic.
            models_root_lexical = Path(models_dir).absolute()
            models_root = resolve_allow_missing(Path(models_dir))
        except Exception:  # pragma: no cover - defensive
            return None

        try:
            if Path(text).is_absolute():
                # Absolute reference: decide containment LEXICALLY first so a
                # path outside the models dir is rejected without any
                # filesystem access. Compare ABSOLUTE-but-UN-RESOLVED paths on
                # BOTH sides (``lexical`` here vs ``models_root_lexical`` above,
                # never ``.resolve()``) so a symlinked ANCESTOR of the models
                # dir does not spuriously break the match for a legitimate
                # in-tree model. The ``..`` guard runs first, on the parts,
                # so a traversal segment is refused regardless. Only a path
                # already lexically inside is then resolved (collapsing any
                # symlink escape) and re-checked against the resolved root
                # below.
                lexical = Path(text)
                if ".." in lexical.parts or not lexical.is_relative_to(
                    models_root_lexical
                ):
                    return None
                candidate = resolve_allow_missing(lexical)
            else:
                # Relative reference: safe_join confines it under the models
                # dir, rejecting traversal / absolute inputs (returns None or
                # raises ValueError). A bare HF id like "all-MiniLM-L6-v2" or
                # "sentence-transformers/all-MiniLM-L6-v2" confines cleanly but
                # simply won't exist under the models dir -> falls through to
                # None and is handled as a repo id by the caller.
                confined = PathValidator.validate_safe_path(text, models_root)
                if confined is None:
                    return None
                candidate = resolve_allow_missing(confined)
        except (ValueError, OSError, RuntimeError):
            # resolve_allow_missing() raises OSError for a symlink LOOP
            # (e.g. a -> b -> a), for a symlink chain whose resolved form
            # outgrows PATH_MAX, and for any other component it cannot
            # resolve, where a bare non-strict resolve() would return a
            # half-resolved path. RuntimeError is still caught defensively.
            # Every case fails closed with a clean refusal instead of
            # propagating out of this function / ``create_embeddings``.
            return None

        # Secondary post-resolution escape re-check (defence in depth): both
        # ``candidate`` (each branch above resolved it) and ``models_root`` are
        # fully symlink-resolved here, so this catches an in-tree symlink whose
        # target points OUT of the models dir — which the lexical check above
        # deliberately cannot see. Also require a STRICT subpath (never the
        # models root itself). is_relative_to stays a pure lexical check.
        if candidate == models_root or not candidate.is_relative_to(
            models_root
        ):
            return None

        try:
            if candidate.exists():
                return candidate
        except OSError:  # pragma: no cover - defensive
            return None
        return None

    @staticmethod
    def _looks_like_filesystem_path(model_name: Optional[str]) -> bool:
        """Pure string classification: does ``model_name`` look like a
        filesystem path rather than a HuggingFace repo id?

        Returns True only for the unambiguous path shapes used to escape the
        models directory — absolute paths, home-relative (``~``) refs, Windows
        drive paths, and any reference containing a parent-traversal (``..``)
        segment. HuggingFace repo ids such as ``all-MiniLM-L6-v2`` or
        ``sentence-transformers/all-MiniLM-L6-v2`` return False. Performs no
        filesystem access.
        """
        if not isinstance(model_name, str):
            return False
        text = model_name.strip()
        if not text:
            return False
        if text.startswith(("/", "\\", "~")) or Path(text).is_absolute():
            return True
        # Windows drive-letter absolute path, e.g. "C:\\models".
        if len(text) >= 2 and text[1] == ":":
            return True
        # Parent-directory traversal in any separator form.
        if ".." in re.split(r"[\\/]+", text):
            return True
        return False

    @classmethod
    def is_available(
        cls, settings_snapshot: Optional[Dict[str, Any]] = None
    ) -> bool:
        """
        Check if Sentence Transformers is available.

        Since sentence-transformers is a required dependency, this always returns True.
        This method exists for API consistency with other providers.
        """
        return True

    @classmethod
    def get_available_models(
        cls, settings_snapshot: Optional[Dict[str, Any]] = None
    ) -> List[Dict[str, Any]]:
        """
        Get list of available Sentence Transformer models.

        The catalog controls new Hub downloads. Existing non-catalog Hub
        snapshots and models confined to the application's models directory
        remain available in local-only mode.
        """
        return [
            {
                "value": model,
                "label": f"{model} ({info['dimensions']}d) - {info['description']}",
            }
            for model, info in cls.AVAILABLE_MODELS.items()
        ]