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.

19.4 KB
Raw Blame
"""OpenAI embedding provider."""

import os
from typing import Any, Dict, Final, List, Optional
from urllib.parse import urlparse

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

from ....config.thread_settings import get_setting_from_snapshot
from ....security.log_sanitizer import redact_secrets
from ....security.ssrf_validator import assert_base_url_safe
from ....security.url_redaction import redact_url_credentials
from ....settings.exceptions import SettingsValueError
from ....utilities.url_utils import normalize_url
from ..base import BaseEmbeddingProvider, Exposure


_DEFAULT_CHUNK_SIZE: Final[int] = 5


class OpenAIEmbeddingsProvider(BaseEmbeddingProvider):
    """
    OpenAI embedding provider.

    Targets the OpenAI cloud API by default, and any OpenAI-compatible
    endpoint (LM Studio, vLLM, llama.cpp server, etc.) when
    ``embeddings.openai.base_url`` is configured. An API key is required
    for the cloud, but optional for keyless local servers — the
    ``base_url``-set, ``api_key``-empty configuration falls back to a
    placeholder key so the OpenAI client request still goes out.
    """

    provider_name = "OpenAI"
    provider_key = "OPENAI"
    # Not strictly required: the OpenAI cloud needs a key, but
    # OpenAI-compatible local servers (LM Studio, vLLM, llama.cpp)
    # don't. ``is_available`` and ``create_embeddings`` enforce the
    # cloud-needs-key rule at runtime when no base_url is set.
    # Inherits ``requires_api_key = False`` from BaseEmbeddingProvider.
    supports_local = False
    egress_exposure = Exposure.EXPOSING
    default_model = "text-embedding-3-small"  # type: ignore[assignment]
    # Placeholder key used when targeting an OpenAI-compatible local
    # server (api_key empty, base_url set). Mirrors the LLM-side
    # LMStudio provider's keyless-fallback pattern.
    _PLACEHOLDER_API_KEY = "lm-studio"

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

        Args:
            model: Model name (defaults to text-embedding-3-small)
            settings_snapshot: Optional settings snapshot
            **kwargs: Additional parameters (api_key, base_url,
                dimensions, chunk_size, openai_proxy)

        Returns:
            OpenAIEmbeddings instance

        Raises:
            ValueError: If API key is not configured, or if a custom
                base_url fails SSRF validation (a blocked address, or a
                host the local resolver cannot resolve -- the check
                resolves locally even when a proxy is configured)
        """
        from langchain_openai import OpenAIEmbeddings
        from openai import DefaultAsyncHttpxClient, DefaultHttpxClient, Timeout

        # Get API key + base_url. Read base_url first so we can decide
        # whether a missing api_key is fatal (cloud) or just a keyless
        # local-server signal (OpenAI-compatible endpoint).
        base_url = kwargs.get("base_url")
        if base_url is None:
            base_url = get_setting_from_snapshot(
                "embeddings.openai.base_url",
                default=None,
                settings_snapshot=settings_snapshot,
            )
        if not base_url:
            # Resolve the same fallbacks as LangChain and then the OpenAI
            # SDK before validation and client construction. Otherwise an
            # environment endpoint bypasses the custom-URL protections.
            base_url = os.environ.get("OPENAI_API_BASE") or os.environ.get(
                "OPENAI_BASE_URL"
            )

        api_key = kwargs.get("api_key")
        if api_key is None:
            api_key = get_setting_from_snapshot(
                "embeddings.openai.api_key",
                default=None,
                settings_snapshot=settings_snapshot,
            )

        if not api_key:
            if base_url:
                # OpenAI-compatible local server (LM Studio, vLLM,
                # llama.cpp). The server ignores the key but the
                # OpenAI client requires the field to be non-empty.
                logger.info(
                    "OpenAI embeddings: no API key set but base_url={} "
                    "is configured — using placeholder key for the "
                    "OpenAI-compatible endpoint.",
                    redact_url_credentials(base_url),
                )
                api_key = cls._PLACEHOLDER_API_KEY
            else:
                logger.error("OpenAI API key not found in settings")
                raise ValueError(
                    "OpenAI API key not configured. "
                    "Please set embeddings.openai.api_key in settings, "
                    "or set embeddings.openai.base_url to point at an "
                    "OpenAI-compatible local server."
                )

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

        dimensions = kwargs.get("dimensions")
        if dimensions is None:
            dimensions = get_setting_from_snapshot(
                "embeddings.openai.dimensions",
                default=None,
                settings_snapshot=settings_snapshot,
            )

        chunk_size = kwargs.get("chunk_size")
        if chunk_size is None:
            chunk_size = get_setting_from_snapshot(
                "embeddings.openai.chunk_size",
                default=_DEFAULT_CHUNK_SIZE,
                settings_snapshot=settings_snapshot,
            )
        if chunk_size is None:
            chunk_size = _DEFAULT_CHUNK_SIZE

        embedding_dimensions = None
        if dimensions and model.startswith("text-embedding-3"):
            embedding_dimensions = int(dimensions)
        # Convert early so invalid numeric settings raise before any
        # custom HTTP client is constructed in the base_url branch below.
        embedding_chunk_size = int(chunk_size)

        # Same bound as the LLM side: langchain otherwise forwards an
        # explicit ``timeout=None`` that overrides the SDK default, leaving
        # embedding reads unbounded, and the SDK's own 2 retries would
        # multiply the wait. Imported lazily — the ``llm.providers``
        # package runs provider auto-discovery at import time. Resolved
        # here, before the base_url branch builds any HTTP client, so a
        # bad timeout/retry setting cannot leak those clients.
        from ....llm.providers._helpers import (
            build_httpx_timeout,
            resolve_max_retries,
            resolve_request_timeout,
        )

        # An SDK ``Timeout`` object, not the chat path's hashable tuple:
        # ``OpenAIEmbeddings`` never consults langchain's ``@lru_cache``'d
        # ``_get_default_httpx_client``, so every instance owns its httpx
        # clients whatever the timeout's type. The object keeps the SDK's
        # informational ``x-stainless-read-timeout`` header well formed
        # while the effective bounds stay identical. ``openai.Timeout`` is
        # the httpx2 flavour the SDK ships; ``httpx.Timeout`` is a
        # different class and would not be recognised. The SDK applies an
        # explicit ``timeout`` even when an ``http_client`` is injected.
        request_timeout = build_httpx_timeout(
            resolve_request_timeout(settings_snapshot), Timeout
        )
        max_retries = resolve_max_retries(settings_snapshot)

        # Proxy precedence matches langchain's own ``openai_proxy`` field:
        # an explicit kwarg wins, else the ``OPENAI_PROXY`` env var.
        openai_proxy = kwargs.get("openai_proxy") or os.environ.get(
            "OPENAI_PROXY"
        )

        # Build parameters. Annotated as Dict[str, Any] so the
        # heterogeneous values (str for model/key/base_url, int for
        # dimensions) and the **params unpack into OpenAIEmbeddings
        # type-check under mypy.
        params: Dict[str, Any] = {
            "model": model,
            "openai_api_key": api_key,
        }

        http_client = None
        http_async_client = None
        if base_url:
            # Normalize first so a scheme-less entry like "api.openai.com"
            # parses to a hostname (urlparse otherwise returns hostname=None
            # for bare hosts, which would silently drop the ctx-length guard
            # for the real OpenAI endpoint). Mirrors the LLM-side OpenAI
            # provider, which already normalizes via the same helper.
            base_url = assert_base_url_safe(
                normalize_url(base_url),
                setting_key="embeddings.openai.base_url",
            )
            params["openai_api_base"] = base_url
            from .owned_openai_embeddings import (
                OwnedOpenAIEmbeddings,
                close_openai_embedding_clients,
            )

            # Own the HTTP clients for a custom base_url so redirects are
            # refused explicitly: the SDK's default client follows them,
            # which would let an allowed host bounce embedding requests
            # (and the bearer key) to a metadata endpoint that
            # assert_base_url_safe() above refuses directly.
            #
            # A configured proxy is routed through these same clients
            # rather than through langchain's ``openai_proxy`` wiring:
            # langchain raises if ``openai_proxy`` and an explicit
            # ``http_client`` are both set, and its ``openai_proxy``
            # field reads ``OPENAI_PROXY`` from the env by default, so it
            # is pinned to None below. Every custom-base_url instance
            # therefore gets follow_redirects=False, proxy or not, and
            # its clients are closed by OwnedOpenAIEmbeddings.close().
            #
            # Neither path does DNS pinning -- assert_base_url_safe()
            # validates once at construction time; like every other LLM
            # SDK integration (see SECURITY.md "LLM Provider URL
            # Validation"), the OpenAI SDK's HTTP client re-resolves the
            # hostname on every call. Egress restriction at the firewall
            # is the operator-side mitigation for that residual window.
            # The check also resolves with the LOCAL resolver, so the
            # host must resolve locally even under a proxy; with a proxy
            # it is advisory, since the proxy resolves the name itself.
            client_options: Dict[str, Any] = {"follow_redirects": False}
            if openai_proxy:
                client_options["proxy"] = openai_proxy
            http_client = DefaultHttpxClient(**client_options)
            try:
                http_async_client = DefaultAsyncHttpxClient(**client_options)
            except Exception:
                try:
                    http_client.close()
                except Exception:
                    logger.warning(
                        "Failed to close OpenAI embeddings sync httpx client"
                    )
                raise
            params["http_client"] = http_client
            params["http_async_client"] = http_async_client
            params["openai_proxy"] = None
            # Disable client-side context length checks only for non-OpenAI
            # hosts (LM Studio, vLLM, llama.cpp, etc.) which may lack tiktoken
            # model entries or reject tokenized inputs. Keep the LangChain
            # default for api.openai.com so the guard stays in place for users
            # who set base_url explicitly to the real OpenAI endpoint.
            if urlparse(base_url).hostname != "api.openai.com":
                params["check_embedding_ctx_length"] = False
        elif openai_proxy:
            # Fixed public endpoint: langchain builds the proxy-routed
            # client itself. Forwarded explicitly so a proxy passed as a
            # kwarg is not dropped (langchain only reads the env var).
            params["openai_proxy"] = openai_proxy

        # For text-embedding-3 models, dimensions can be customized
        if embedding_dimensions is not None:
            params["dimensions"] = embedding_dimensions

        if embedding_chunk_size <= 0:
            logger.warning(
                "OpenAI embeddings chunk_size={} is not positive; "
                "using safe default chunk_size={}",
                chunk_size,
                _DEFAULT_CHUNK_SIZE,
            )
            embedding_chunk_size = _DEFAULT_CHUNK_SIZE
        params["chunk_size"] = embedding_chunk_size
        params["request_timeout"] = request_timeout
        params["max_retries"] = max_retries

        embeddings: Embeddings
        if http_client is not None:
            try:
                embeddings = OwnedOpenAIEmbeddings(**params)
            except Exception:
                close_openai_embedding_clients(http_client, http_async_client)
                raise
        else:
            embeddings = OpenAIEmbeddings(**params)
        logger.info(
            "Creating OpenAIEmbeddings with model={}, chunk_size={}",
            model,
            embeddings.chunk_size,
        )
        return embeddings

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

        Available when either an API key (cloud) or a custom base URL
        (OpenAI-compatible local server) is configured. A blank
        installation still reports unavailable so the UI doesn't list
        the provider on first launch.
        """
        api_key = None
        try:
            api_key = get_setting_from_snapshot(
                "embeddings.openai.api_key",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            if api_key and str(api_key).strip():
                return True
            base_url = get_setting_from_snapshot(
                "embeddings.openai.base_url",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            return bool(base_url and str(base_url).strip())
        except SettingsValueError:
            raise
        except Exception as e:
            # Drop exc_info — the traceback would render the exception's
            # cause chain, which may embed the api_key value if a
            # settings-layer error message ever surfaces it. Interpolate a
            # redacted exception message so debugging is still possible.
            safe_msg = redact_secrets(str(e), api_key)
            logger.debug(
                f"Error checking OpenAI embedding availability: {safe_msg}"
            )
            return False

    @classmethod
    def get_available_models(
        cls, settings_snapshot: Optional[Dict[str, Any]] = None
    ) -> List[Dict[str, Any]]:
        """Get every model the configured endpoint reports.

        No filtering: ``/v1/models`` doesn't expose a reliable "is this
        an embedding model?" signal — neither cloud OpenAI nor
        OpenAI-compatible local servers (LM Studio, vLLM, llama.cpp).
        Earlier versions guessed from the model name and ended up
        hiding real embedding models whose names didn't match the
        heuristic (e.g. ``nomic-embed-text-v1.5`` was dropped because
        it lacks the trailing ``-ing``). The dropdown now shows every
        model the endpoint returns so the user can pick the one they
        actually loaded.
        """
        # Initialized before the try so the except block can redact it
        # even if an early statement (e.g. the openai import) raises.
        api_key = None
        http_client = None
        client_entered = False
        try:
            from openai import DefaultHttpxClient, OpenAI, Timeout

            api_key = get_setting_from_snapshot(
                "embeddings.openai.api_key",
                default=None,
                settings_snapshot=settings_snapshot,
            )
            base_url = get_setting_from_snapshot(
                "embeddings.openai.base_url",
                default=None,
                settings_snapshot=settings_snapshot,
            )

            if not base_url:
                # Discovery uses the SDK directly; OPENAI_API_BASE is a
                # LangChain-only fallback and does not apply here.
                base_url = os.environ.get("OPENAI_BASE_URL")

            if not api_key:
                if base_url:
                    # Keyless OpenAI-compatible local server — use a
                    # placeholder so the client request can proceed.
                    api_key = cls._PLACEHOLDER_API_KEY
                else:
                    logger.warning("OpenAI API key not configured")
                    return []

            client_kwargs: Dict[str, Any] = {"api_key": api_key}
            # Bounded discovery request — mirrors the LLM-side provider.
            # Resolved before any HTTP client is built below.
            from ....llm.providers._helpers import (
                MODEL_DISCOVERY_TIMEOUT_SECONDS,
                build_httpx_timeout,
            )

            # Fresh client per call, outside langchain's cached factory —
            # see ``create_embeddings`` above for why the object form,
            # not the tuple, is right here.
            client_kwargs["timeout"] = build_httpx_timeout(
                MODEL_DISCOVERY_TIMEOUT_SECONDS, Timeout
            )
            client_kwargs["max_retries"] = 0
            if base_url:
                client_kwargs["base_url"] = assert_base_url_safe(
                    normalize_url(base_url),
                    setting_key="embeddings.openai.base_url",
                )
                http_client = DefaultHttpxClient(follow_redirects=False)
                client_kwargs["http_client"] = http_client
            with OpenAI(**client_kwargs) as client:
                client_entered = True
                models_response = client.models.list()

            # No name-based filtering — see method docstring (#4195).
            models: List[Dict[str, Any]] = []
            for model in models_response.data:
                model_id = model.id
                # Skip only blank ids (malformed entry); never skip
                # based on what the name looks like.
                if not model_id:
                    continue
                models.append({"value": model_id, "label": model_id})

            logger.info(
                "Fetched {} models from OpenAI endpoint{}",
                len(models),
                f" at {redact_url_credentials(base_url)}" if base_url else "",
            )
            return models

        except SettingsValueError:
            raise
        except Exception as e:
            if http_client is not None and not client_entered:
                try:
                    http_client.close()
                except Exception:
                    logger.warning(
                        "Failed to close OpenAI model discovery httpx client"
                    )
            # Use logger.warning rather than logger.exception so the
            # exception's cause chain (which may embed the api_key in a
            # URL or auth header echoed back in the error body) is not
            # written to log sinks. The api_key value is also redacted
            # from str(e) as defense-in-depth.
            safe_msg = redact_secrets(str(e), api_key)
            logger.warning(
                f"Error fetching OpenAI embedding models: {safe_msg}"
            )
            return []