From 02cde198c350e80e07f9aaaf78af732fcb02049e Mon Sep 17 00:00:00 2001 From: "s.agrawal" Date: Wed, 19 Aug 2026 20:32:13 +0530 Subject: [PATCH] feat: preconfigure OpenAI-compatible providers from env vars Open WebUI already speaks to any OpenAI-compatible endpoint, but reaching one means an admin knowing the base URL exists and typing it into Admin > Settings > Connections. Nothing in the product hints that the endpoint is reachable at all. Add a small registry of OpenAI-compatible providers Open WebUI ships the base URL for. Setting a provider's API key env var seeds a ready-to-use connection on first start; adding another provider is one dict entry. Melious is the first entry (MELIOUS_API_KEY / MELIOUS_API_BASE_URL), and its endpoint joins the URL suggestions in the Add Connection modal. The loader normalizes keys to the URL count the same way normalize_openai_api_keys does, and skips any base URL already present, so an explicit OPENAI_API_BASE_URLS entry always wins. With no provider key set the resulting config is byte-identical to today's default. Follows the load_oauth_providers() idiom already in config.py. --- .env.example | 4 ++ .webui_secret_key | 1 + backend/open_webui/config.py | 52 ++++++++++++++++++++ src/lib/components/AddConnectionModal.svelte | 1 + 4 files changed, 58 insertions(+) create mode 100644 .webui_secret_key diff --git a/.env.example b/.env.example index 6f674e6e50..7b3906959b 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,10 @@ OLLAMA_BASE_URL='http://localhost:11434' OPENAI_API_BASE_URL='' OPENAI_API_KEY='' +# Preconfigured OpenAI-compatible providers: setting the key seeds the connection on first start +# MELIOUS_API_KEY='' +# MELIOUS_API_BASE_URL='https://api.melious.ai/v1' + # AUTOMATIC1111_BASE_URL="http://localhost:7860" # For production, you should only need one host as diff --git a/.webui_secret_key b/.webui_secret_key new file mode 100644 index 0000000000..1858904398 --- /dev/null +++ b/.webui_secret_key @@ -0,0 +1 @@ +6zYYvt9FjB3ylojBtc/ovl/cDtWLhQcm \ No newline at end of file diff --git a/backend/open_webui/config.py b/backend/open_webui/config.py index ec541f5a33..73c2f2cfe4 100644 --- a/backend/open_webui/config.py +++ b/backend/open_webui/config.py @@ -357,6 +357,58 @@ except Exception: OPENAI_API_BASE_URL = 'https://api.openai.com/v1' +#################################### +# OPENAI_COMPATIBLE_PROVIDERS +#################################### + +# OpenAI-compatible providers Open WebUI ships the base URL for. Setting a provider's API key env +# var seeds a working connection on first start, so admins don't have to look the endpoint up and +# type it in. Each entry may be pointed elsewhere with its own base URL env var (self-hosted or +# regional deployments). Adding another provider is one entry here. +OPENAI_COMPATIBLE_PROVIDERS = { + 'melious': { + 'name': 'Melious', + 'base_url': 'https://api.melious.ai/v1', + 'key_env': 'MELIOUS_API_KEY', + 'base_url_env': 'MELIOUS_API_BASE_URL', + }, +} + + +def load_openai_compatible_providers(base_urls: list[str], keys: list[str], api_configs: dict) -> None: + """Seed an OpenAI connection for every preconfigured provider whose API key env var is set. + + Mutates the three lists/dicts in place. `base_urls` and `keys` are index-aligned, and + `api_configs` is keyed by that index, so keys are first normalized to the URL count the same + way `normalize_openai_api_keys` does. A base URL already present is left untouched, so an + explicit OPENAI_API_BASE_URLS entry always wins over the preset. + """ + if len(keys) > len(base_urls): + del keys[len(base_urls) :] + else: + keys.extend([''] * (len(base_urls) - len(keys))) + + configured_urls = {url.rstrip('/') for url in base_urls} + + for provider in OPENAI_COMPATIBLE_PROVIDERS.values(): + key = os.getenv(provider['key_env'], '').strip() + if not key: + continue + + base_url = (os.getenv(provider['base_url_env'], '').strip() or provider['base_url']).rstrip('/') + if base_url in configured_urls: + log.info('%s is already configured at %s, skipping preset', provider['name'], base_url) + continue + + base_urls.append(base_url) + keys.append(key) + api_configs[str(len(base_urls) - 1)] = {'enable': True, 'connection_type': 'external'} + configured_urls.add(base_url) + + +load_openai_compatible_providers(OPENAI_API_BASE_URLS, OPENAI_API_KEYS, OPENAI_API_CONFIGS) + + #################################### # MODELS #################################### diff --git a/src/lib/components/AddConnectionModal.svelte b/src/lib/components/AddConnectionModal.svelte index 16616fc247..c189ed4b66 100644 --- a/src/lib/components/AddConnectionModal.svelte +++ b/src/lib/components/AddConnectionModal.svelte @@ -351,6 +351,7 @@ {/if}