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.

2.6 KB
Raw Blame
"""OpenAI embedding clients with explicit, retryable resource cleanup."""

import asyncio
import threading
from typing import Any

from langchain_openai import OpenAIEmbeddings
from pydantic import ConfigDict, PrivateAttr

from ....security.secure_logging import logger


async def _aclose_http_clients(
    http_client: Any, http_async_client: Any
) -> bool:
    closed = True
    try:
        http_client.close()
    except Exception:
        closed = False
        logger.warning("Failed to close OpenAI embeddings sync httpx client")
    try:
        await http_async_client.aclose()
    except Exception:
        closed = False
        logger.warning("Failed to close OpenAI embeddings async httpx client")
    return closed


def close_openai_embedding_clients(
    http_client: Any, http_async_client: Any
) -> bool:
    try:
        asyncio.get_running_loop()
    except RuntimeError:
        try:
            return asyncio.run(
                _aclose_http_clients(http_client, http_async_client)
            )
        except Exception:
            logger.warning("Failed to close OpenAI embeddings httpx clients")
            return False

    closed = False

    def _close_in_thread() -> None:
        nonlocal closed
        try:
            closed = asyncio.run(
                _aclose_http_clients(http_client, http_async_client)
            )
        except Exception:
            logger.warning("Failed to close OpenAI embeddings httpx clients")

    close_thread = threading.Thread(
        target=_close_in_thread,
        daemon=True,
        name="ldr-async-openai-embedding-close",
    )
    close_thread.start()
    close_thread.join(timeout=5)
    if close_thread.is_alive():
        logger.warning("OpenAI embeddings async httpx close exceeded 5s")
        return False
    return closed


class OwnedOpenAIEmbeddings(OpenAIEmbeddings):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    http_client: Any
    http_async_client: Any
    _closed: bool = PrivateAttr(default=False)

    def close(self) -> None:
        """Close from synchronous code; use ``aclose`` on the owning loop.

        The fallback for a running loop waits up to five seconds. A client
        whose transport cannot close on another loop remains retryable.
        """
        if self._closed:
            return
        if close_openai_embedding_clients(
            self.http_client, self.http_async_client
        ):
            self._closed = True

    async def aclose(self) -> None:
        if self._closed:
            return
        if await _aclose_http_clients(self.http_client, self.http_async_client):
            self._closed = True