File size: 15,430 Bytes
4147729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""Create chat models with Cloudflare, Groq, Gemini, and custom endpoints."""

from __future__ import annotations

import logging
import os
import re
import threading
import time
from collections import deque
from typing import Any, Optional
from langchain_core.rate_limiters import BaseRateLimiter, InMemoryRateLimiter
from backend.integrations.cloudflare_transport import CloudflareTransport, AsyncCloudflareTransport, ReachableCloudflareRoutes

logger = logging.getLogger(__name__)

# Gemini 3.1 Flash model name as of 2026-07
# Verified via Google AI documentation: gemini-3.1-flash-lite is the correct name.
# The base 'gemini-3.1-flash' does not exist in the API.
GEMINI_MODEL = "gemini-3.1-flash-lite"
GROQ_MODEL = os.getenv("GROQ_MODEL", "openai/gpt-oss-20b")
CLOUDFLARE_MODEL = os.getenv("CLOUDFLARE_MODEL", "@cf/zai-org/glm-4.7-flash")
class CloudflareRateLimiter(BaseRateLimiter):
    """Conservative per-process pacing and rolling daily attempt budget."""

    def __init__(self, daily_limit: int = 50):
        self.pacer = InMemoryRateLimiter(requests_per_second=0.2, check_every_n_seconds=0.1, max_bucket_size=1)
        self.daily_limit = daily_limit
        self.attempts = deque()
        self.lock = threading.Lock()

    def _reserve(self):
        now = time.monotonic()
        with self.lock:
            while self.attempts and self.attempts[0] <= now - 86400:
                self.attempts.popleft()
            if len(self.attempts) >= self.daily_limit:
                raise ValueError("Cloudflare local daily request budget reached; try later or choose another provider.")
            self.attempts.append(now)

    def acquire(self, *, blocking: bool = True) -> bool:
        if not self.pacer.acquire(blocking=blocking):
            return False
        self._reserve()
        return True

    async def aacquire(self, *, blocking: bool = True) -> bool:
        if not await self.pacer.aacquire(blocking=blocking):
            return False
        self._reserve()
        return True


# Shared by reviewers and evaluation in this process. This request budget is
# a conservative application cap, not a measurement of Cloudflare neurons.
_cloudflare_limiter = CloudflareRateLimiter(max(1, int(os.getenv("CLOUDFLARE_DAILY_REQUEST_LIMIT", "50"))))
_cloudflare_routes = ReachableCloudflareRoutes()
DEFAULT_TEMPERATURE = 0.0
DEFAULT_REQUEST_TIMEOUT = float(os.getenv("OCR_MODEL_TIMEOUT_SECONDS", "60"))
DEFAULT_OUTPUT_TOKENS = int(os.getenv("OCR_MODEL_MAX_OUTPUT_TOKENS", "2000"))


def _get_gemini_chat(**kwargs: Any) -> Optional[Any]:
    """Create a ``ChatGoogleGenerativeAI`` instance if ``GEMINI_API_KEY`` is set.

    Returns ``None`` silently when the key is missing or the package is not
    installed β€” callers are expected to fall back.
    """
    kwargs.setdefault("timeout", DEFAULT_REQUEST_TIMEOUT)
    kwargs.setdefault("max_retries", 0)
    kwargs.setdefault("max_tokens", DEFAULT_OUTPUT_TOKENS)
    api_key = os.environ.get("GEMINI_API_KEY", "").strip()
    if not api_key:
        return None

    try:
        from langchain_google_genai import ChatGoogleGenerativeAI

        return ChatGoogleGenerativeAI(
            model=kwargs.pop("model", GEMINI_MODEL),
            temperature=kwargs.pop("temperature", DEFAULT_TEMPERATURE),
            google_api_key=api_key,
            **kwargs,
        )
    except ImportError:
        logger.warning("langchain-google-genai not installed β€” skipping Gemini")
        return None
    except Exception as exc:
        logger.warning("Failed to initialise Gemini: %s", exc)
        return None


def _get_groq_chat(**kwargs: Any) -> Optional[Any]:
    """Create a ``ChatGroq`` instance if ``GROQ_API_KEY`` is set."""
    kwargs.setdefault("timeout", DEFAULT_REQUEST_TIMEOUT)
    kwargs.setdefault("max_retries", 0)
    kwargs.setdefault("max_tokens", DEFAULT_OUTPUT_TOKENS)
    api_key = os.environ.get("GROQ_API_KEY", "").strip()
    if not api_key:
        return None

    try:
        from langchain_groq import ChatGroq

        return ChatGroq(
            model=kwargs.pop("model", GROQ_MODEL),
            temperature=kwargs.pop("temperature", DEFAULT_TEMPERATURE),
            api_key=api_key,
            **kwargs,
        )
    except ImportError:
        logger.warning("langchain-groq not installed")
        return None
    except Exception as exc:
        logger.warning("Failed to initialise Groq: %s", exc)
        return None


def _get_cloudflare_chat(**kwargs: Any) -> Optional[Any]:
    """Use Workers AI when an account ID and API token are configured."""
    api_key = os.environ.get("CLOUDFLARE_API_TOKEN", "").strip()
    account_id = os.environ.get("CLOUDFLARE_ACCOUNT_ID", "").strip()
    gateway_id = os.environ.get("CLOUDFLARE_GATEWAY_ID", "").strip()
    gateway_token = os.environ.get("CLOUDFLARE_GATEWAY_TOKEN", "").strip()
    if not api_key or not account_id:
        return None
    if not re.fullmatch(r"[0-9a-fA-F]{32}", account_id):
        raise ValueError("CLOUDFLARE_ACCOUNT_ID must be a 32-character hexadecimal account ID.")
    if gateway_id and not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", gateway_id):
        raise ValueError("CLOUDFLARE_GATEWAY_ID contains invalid characters.")
    from langchain_openai import ChatOpenAI
    import httpx
    timeout = kwargs.pop("timeout", DEFAULT_REQUEST_TIMEOUT)
    if isinstance(timeout, (int, float)):
        timeout = httpx.Timeout(timeout, connect=min(float(timeout), 4.0))
    # Retry only failures while establishing a connection, before an HTTP
    # request is sent. Preserve normal TLS certificate and hostname checks.
    # IPv6 is unavailable on the hosted Space; keep DNS and TLS SNI intact.
    if gateway_id:
        # Cloudflare's AI Gateway is an official alternate route to Workers AI.
        # Request no gateway cache or stored logs for potentially private code.
        headers = dict(kwargs.pop("default_headers", {}) or {})
        headers.update({"cf-aig-authorization": f"Bearer {gateway_token or api_key}", "cf-aig-collect-log": "false", "cf-aig-skip-cache": "true"})
        kwargs["default_headers"] = headers
        # The gateway's hostname route was reachable with normal TLS/SNI, while
        # pinning its resolved IPs produced intermittent connect errors from HF.
        # Let HTTPX resolve the official hostname and retry connection setup.
        kwargs.setdefault("http_client", httpx.Client(transport=httpx.HTTPTransport(retries=2), timeout=timeout))
        kwargs.setdefault("http_async_client", httpx.AsyncClient(transport=httpx.AsyncHTTPTransport(retries=2), timeout=timeout))
        base_url = f"https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/compat"
    else:
        kwargs.setdefault("http_client", httpx.Client(transport=CloudflareTransport(_cloudflare_routes), timeout=timeout))
        kwargs.setdefault("http_async_client", httpx.AsyncClient(transport=AsyncCloudflareTransport(_cloudflare_routes), timeout=timeout))
        base_url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/v1"
    kwargs["timeout"] = timeout
    kwargs.setdefault("max_retries", 0)
    kwargs["max_tokens"] = max(int(kwargs.get("max_tokens") or DEFAULT_OUTPUT_TOKENS), 4096)
    kwargs.setdefault("reasoning_effort", "low")
    kwargs.setdefault("rate_limiter", _cloudflare_limiter)
    model = kwargs.pop("model", CLOUDFLARE_MODEL)
    if gateway_id and model.startswith("@cf/"):
        model = f"workers-ai/{model}"
    return ChatOpenAI(model=model, api_key=api_key,
                      base_url=base_url,
                      temperature=kwargs.pop("temperature", DEFAULT_TEMPERATURE), **kwargs)


def create_endpoint_llm(endpoint, **kwargs: Any) -> Any:
    """Create a chat model from a user-configured custom endpoint.

    Supports OpenAI-compatible APIs (DeepSeek, OpenRouter, vLLM, Ollama,
    Groq, Together, ...), Anthropic Claude, and Google Gemini.

    Parameters
    ----------
    endpoint : EndpointConfig
        The user's endpoint config (name, provider, api_key, base_url, model).
    **kwargs
        Passed through to the underlying LangChain constructor (e.g.
        ``temperature``).  ``model`` / ``api_key`` / ``base_url`` come from
        the endpoint and cannot be overridden here.

    Returns
    -------
    Any
        A LangChain chat model instance (``ChatOpenAI``, ``ChatAnthropic``
        or ``ChatGoogleGenerativeAI``).

    Raises
    ------
    ValueError
        If the endpoint's provider package is not installed or the config
        is incomplete.
    """
    if not endpoint.api_key or not endpoint.model:
        raise ValueError(
            f"Endpoint '{endpoint.name}' is missing an API key or model name."
        )

    kwargs.setdefault("timeout", DEFAULT_REQUEST_TIMEOUT)
    kwargs.setdefault("max_retries", 0)
    kwargs.setdefault("max_tokens", DEFAULT_OUTPUT_TOKENS)
    provider = endpoint.provider
    model = endpoint.model
    # MiniMax M3.1 requires reasoning and OpenRouter defaults to max effort.
    # Reserve room for reasoning + visible JSON rather than truncating at 1200.
    if 'minimax-m3.1' in model.lower():
        kwargs['max_tokens'] = max(int(kwargs.get('max_tokens') or 0), 8192)
        if endpoint.base_url and 'openrouter.ai' in endpoint.base_url:
            extra = dict(kwargs.get('extra_body') or {})
            extra['reasoning'] = {'effort': 'low'}
            kwargs['extra_body'] = extra
    temperature = kwargs.pop("temperature", DEFAULT_TEMPERATURE)

    if provider == "anthropic":
        try:
            from langchain_anthropic import ChatAnthropic
        except ImportError:
            raise ValueError(
                "langchain-anthropic is not installed β€” cannot use endpoint "
                f"'{endpoint.name}'. Run: pip install langchain-anthropic"
            ) from None
        params: dict[str, Any] = {
            "model": model,
            "api_key": endpoint.api_key,
            "temperature": temperature,
        }
        if endpoint.base_url:
            params["base_url"] = endpoint.base_url
        params.update(kwargs)
        return ChatAnthropic(**params)

    if provider == "google":
        try:
            from langchain_google_genai import ChatGoogleGenerativeAI
        except ImportError:
            raise ValueError(
                "langchain-google-genai is not installed β€” cannot use endpoint "
                f"'{endpoint.name}'. Run: pip install langchain-google-genai"
            ) from None
        params = {
            "model": model,
            "google_api_key": endpoint.api_key,
            "temperature": temperature,
        }
        params.update(kwargs)
        return ChatGoogleGenerativeAI(**params)

    # Default: OpenAI-compatible
    try:
        from langchain_openai import ChatOpenAI
    except ImportError:
        raise ValueError(
            "langchain-openai is not installed β€” cannot use endpoint "
            f"'{endpoint.name}'. Run: pip install langchain-openai"
        ) from None
    params = {
        "model": model,
        "api_key": endpoint.api_key,
        "temperature": temperature,
    }
    if endpoint.base_url:
        params["base_url"] = endpoint.base_url
        if "api.cloudflare.com/client/v4/accounts/" in endpoint.base_url.lower():
            kwargs.setdefault("rate_limiter", _cloudflare_limiter)
    params.update(kwargs)
    return ChatOpenAI(**params)


def create_llm(endpoint=None, **kwargs: Any) -> Any:
    """Create a chat LLM β€” selected endpoint first, else Gemini β†’ Groq β†’ Cloudflare.

    Parameters
    ----------
    endpoint : EndpointConfig or str or None
        A custom endpoint to run on (``EndpointConfig`` object or its name).
        ``None`` (default) falls back to the built-in providers.
    **kwargs
        Passed through to the underlying LangChain constructor.
        Common overrides: ``model``, ``temperature``.

    Returns
    -------
    Any
        A LangChain chat model instance (``ChatOpenAI``/``ChatAnthropic``/
        ``ChatGoogleGenerativeAI`` from a custom endpoint, or the built-in
        ``ChatGoogleGenerativeAI`` / ``ChatGroq``).

    Raises
    ------
    ValueError
        If a custom endpoint is named but not found, or no provider key is set.
    """
    if endpoint:
        from backend.integrations.endpoints import EndpointConfig, get_endpoint

        # The browser historically sent lowercase builtin ids while the
        # endpoint registry uses display names. Accept both for API clients.
        if isinstance(endpoint, str):
            endpoint = {"gemini": "Gemini", "groq": "Grok", "cloudflare": "Cloudflare"}.get(endpoint.lower(), endpoint)
        ep = endpoint if isinstance(endpoint, EndpointConfig) else get_endpoint(endpoint)
        if ep is not None:
            if getattr(ep, "builtin", False):
                if ep.name == "Cloudflare":
                    llm = _get_cloudflare_chat(**kwargs)
                    if llm is not None:
                        return llm
                    raise ValueError("Cloudflare selected but CLOUDFLARE_ACCOUNT_ID or CLOUDFLARE_API_TOKEN is not set.")
                # Built-in providers use their native constructors so the
                # behaviour is identical to the default (no-endpoint) path.
                if ep.name == "Gemini":
                    llm = _get_gemini_chat(**kwargs)
                    if llm is not None:
                        logger.info(
                            "Using built-in Gemini endpoint (model=%s)",
                            kwargs.get("model", GEMINI_MODEL),
                        )
                        return llm
                    raise ValueError(
                        "Gemini selected but GEMINI_API_KEY is not set."
                    )
                if ep.name == "Grok":
                    llm = _get_groq_chat(**kwargs)
                    if llm is not None:
                        logger.info(
                            "Using built-in Grok endpoint (model=%s)",
                            kwargs.get("model", GROQ_MODEL),
                        )
                        return llm
                    raise ValueError(
                        "Grok selected but GROQ_API_KEY is not set."
                    )
            try:
                llm = create_endpoint_llm(ep, **kwargs)
                logger.info("Using custom endpoint %s (%s/%s)", ep.name, ep.provider, ep.model)
                return llm
            except ValueError:
                raise
        else:
            raise ValueError(
                f"Custom endpoint '{endpoint}' not found. "
                "Configure it via OCR_ENDPOINT_* environment variables "
                "or add it in the UI."
            )

    llm = _get_gemini_chat(**kwargs)
    if llm is not None:
        logger.debug("Using Gemini (model=%s)", kwargs.get("model", GEMINI_MODEL))
        return llm

    llm = _get_groq_chat(**kwargs)
    if llm is not None:
        logger.debug("Falling back to Groq (model=%s)", kwargs.get("model", GROQ_MODEL))
        return llm

    llm = _get_cloudflare_chat(**kwargs)
    if llm is not None:
        logger.debug("Falling back to Cloudflare (model=%s)", kwargs.get("model", CLOUDFLARE_MODEL))
        return llm

    raise ValueError(
        "No LLM available. Set GEMINI_API_KEY, GROQ_API_KEY, or both "
        "CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN."
    )