From 87d9b7e84e71b097eadf1df4f9852359104f17ed Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 17 Aug 2026 00:51:04 -0700 Subject: [PATCH] refac --- backend/open_webui/routers/retrieval.py | 39 ++++++++----------- src/lib/apis/retrieval/index.ts | 11 ++++-- .../admin/Settings/Documents.svelte | 39 +++++++++++-------- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/backend/open_webui/routers/retrieval.py b/backend/open_webui/routers/retrieval.py index d81e1e4e2c..9023c8b634 100644 --- a/backend/open_webui/routers/retrieval.py +++ b/backend/open_webui/routers/retrieval.py @@ -490,19 +490,19 @@ async def get_embedding_config(request: Request, user=Depends(get_admin_user)): class OpenAIConfigForm(BaseModel): - url: str - key: str + url: str | None = None + key: str | None = None class OllamaConfigForm(BaseModel): - url: str - key: str + url: str | None = None + key: str | None = None class AzureOpenAIConfigForm(BaseModel): - url: str - key: str - version: str + url: str | None = None + key: str | None = None + version: str | None = None class EmbeddingModelUpdateForm(BaseModel): @@ -544,23 +544,18 @@ async def update_embedding_config(request: Request, form_data: EmbeddingModelUpd config.ENABLE_ASYNC_EMBEDDING = form_data.ENABLE_ASYNC_EMBEDDING config.RAG_EMBEDDING_CONCURRENT_REQUESTS = form_data.RAG_EMBEDDING_CONCURRENT_REQUESTS - if config.RAG_EMBEDDING_ENGINE in [ - 'ollama', - 'openai', - 'azure_openai', - ]: - if form_data.openai_config is not None: - config.RAG_OPENAI_API_BASE_URL = form_data.openai_config.url - config.RAG_OPENAI_API_KEY = form_data.openai_config.key + if config.RAG_EMBEDDING_ENGINE == 'openai' and form_data.openai_config is not None: + config.RAG_OPENAI_API_BASE_URL = form_data.openai_config.url or '' + config.RAG_OPENAI_API_KEY = form_data.openai_config.key or '' - if form_data.ollama_config is not None: - config.RAG_OLLAMA_BASE_URL = form_data.ollama_config.url - config.RAG_OLLAMA_API_KEY = form_data.ollama_config.key + if config.RAG_EMBEDDING_ENGINE == 'ollama' and form_data.ollama_config is not None: + config.RAG_OLLAMA_BASE_URL = form_data.ollama_config.url or '' + config.RAG_OLLAMA_API_KEY = form_data.ollama_config.key or '' - if form_data.azure_openai_config is not None: - config.RAG_AZURE_OPENAI_BASE_URL = form_data.azure_openai_config.url - config.RAG_AZURE_OPENAI_API_KEY = form_data.azure_openai_config.key - config.RAG_AZURE_OPENAI_API_VERSION = form_data.azure_openai_config.version + if config.RAG_EMBEDDING_ENGINE == 'azure_openai' and form_data.azure_openai_config is not None: + config.RAG_AZURE_OPENAI_BASE_URL = form_data.azure_openai_config.url or '' + config.RAG_AZURE_OPENAI_API_KEY = form_data.azure_openai_config.key or '' + config.RAG_AZURE_OPENAI_API_VERSION = form_data.azure_openai_config.version or '' request.app.state.ef = get_ef( config.RAG_EMBEDDING_ENGINE, diff --git a/src/lib/apis/retrieval/index.ts b/src/lib/apis/retrieval/index.ts index 99801dfedb..f913e3e868 100644 --- a/src/lib/apis/retrieval/index.ts +++ b/src/lib/apis/retrieval/index.ts @@ -188,6 +188,8 @@ type OpenAIConfigForm = { url: string; }; +type OllamaConfigForm = OpenAIConfigForm; + type AzureOpenAIConfigForm = { key: string; url: string; @@ -196,10 +198,13 @@ type AzureOpenAIConfigForm = { type EmbeddingModelUpdateForm = { openai_config?: OpenAIConfigForm; + ollama_config?: OllamaConfigForm; azure_openai_config?: AzureOpenAIConfigForm; - embedding_engine: string; - embedding_model: string; - embedding_batch_size?: number; + RAG_EMBEDDING_ENGINE: string; + RAG_EMBEDDING_MODEL: string; + RAG_EMBEDDING_BATCH_SIZE?: number; + ENABLE_ASYNC_EMBEDDING?: boolean; + RAG_EMBEDDING_CONCURRENT_REQUESTS?: number; }; export const updateEmbeddingConfig = async (token: string, payload: EmbeddingModelUpdateForm) => { diff --git a/src/lib/components/admin/Settings/Documents.svelte b/src/lib/components/admin/Settings/Documents.svelte index ba4b112a64..8425f338ea 100644 --- a/src/lib/components/admin/Settings/Documents.svelte +++ b/src/lib/components/admin/Settings/Documents.svelte @@ -122,26 +122,33 @@ }); updateEmbeddingModelLoading = true; - const res = await updateEmbeddingConfig(localStorage.token, { + const payload: Parameters[1] = { RAG_EMBEDDING_ENGINE: RAG_EMBEDDING_ENGINE, RAG_EMBEDDING_MODEL: RAG_EMBEDDING_MODEL, RAG_EMBEDDING_BATCH_SIZE: RAG_EMBEDDING_BATCH_SIZE, ENABLE_ASYNC_EMBEDDING: ENABLE_ASYNC_EMBEDDING, - RAG_EMBEDDING_CONCURRENT_REQUESTS: RAG_EMBEDDING_CONCURRENT_REQUESTS, - ollama_config: { + RAG_EMBEDDING_CONCURRENT_REQUESTS: RAG_EMBEDDING_CONCURRENT_REQUESTS + }; + + if (RAG_EMBEDDING_ENGINE === 'ollama') { + payload.ollama_config = { key: OllamaKey, url: OllamaUrl - }, - openai_config: { + }; + } else if (RAG_EMBEDDING_ENGINE === 'openai') { + payload.openai_config = { key: OpenAIKey, url: OpenAIUrl - }, - azure_openai_config: { + }; + } else if (RAG_EMBEDDING_ENGINE === 'azure_openai') { + payload.azure_openai_config = { key: AzureOpenAIKey, url: AzureOpenAIUrl, version: AzureOpenAIVersion - } - }).catch(async (error) => { + }; + } + + const res = await updateEmbeddingConfig(localStorage.token, payload).catch(async (error) => { toast.error(`${error}`); await setEmbeddingConfig(); return null; @@ -300,15 +307,15 @@ ENABLE_ASYNC_EMBEDDING = embeddingConfig.ENABLE_ASYNC_EMBEDDING ?? true; RAG_EMBEDDING_CONCURRENT_REQUESTS = embeddingConfig.RAG_EMBEDDING_CONCURRENT_REQUESTS ?? 0; - OpenAIKey = embeddingConfig.openai_config.key; - OpenAIUrl = embeddingConfig.openai_config.url; + OpenAIKey = embeddingConfig.openai_config.key ?? ''; + OpenAIUrl = embeddingConfig.openai_config.url ?? ''; - OllamaKey = embeddingConfig.ollama_config.key; - OllamaUrl = embeddingConfig.ollama_config.url; + OllamaKey = embeddingConfig.ollama_config.key ?? ''; + OllamaUrl = embeddingConfig.ollama_config.url ?? ''; - AzureOpenAIKey = embeddingConfig.azure_openai_config.key; - AzureOpenAIUrl = embeddingConfig.azure_openai_config.url; - AzureOpenAIVersion = embeddingConfig.azure_openai_config.version; + AzureOpenAIKey = embeddingConfig.azure_openai_config.key ?? ''; + AzureOpenAIUrl = embeddingConfig.azure_openai_config.url ?? ''; + AzureOpenAIVersion = embeddingConfig.azure_openai_config.version ?? ''; } }; onMount(async () => {