Spaces:
Running
Running
File size: 1,039 Bytes
8f1b6ce | 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 | """Drop-in OpenAI clients. Gateway controls retries by default."""
import os
from typing import Any
from openai import AsyncOpenAI, OpenAI
def options(
base_url: str | None, api_key: str | None, kwargs: dict[str, Any]
) -> dict[str, Any]:
url = base_url or os.environ.get("GATEWAY_BASE_URL")
key = api_key or os.environ.get("GATEWAY_API_KEY")
if not url or not key:
raise ValueError(
"Set GATEWAY_BASE_URL and GATEWAY_API_KEY or pass both explicitly."
)
return {"base_url": url.rstrip("/"), "api_key": key, "max_retries": 0, **kwargs}
class Gateway(OpenAI):
def __init__(
self, *, base_url: str | None = None, api_key: str | None = None, **kwargs: Any
):
super().__init__(**options(base_url, api_key, kwargs))
class AsyncGateway(AsyncOpenAI):
def __init__(
self, *, base_url: str | None = None, api_key: str | None = None, **kwargs: Any
):
super().__init__(**options(base_url, api_key, kwargs))
__all__ = ["Gateway", "AsyncGateway"]
|