diff --git a/backend/apps/openai/main.py b/backend/apps/openai/main.py index 74e516714..3b55d4d16 100644 --- a/backend/apps/openai/main.py +++ b/backend/apps/openai/main.py @@ -184,13 +184,10 @@ async def speech(request: Request, user=Depends(get_verified_user)): async def fetch_url(url, key): timeout = aiohttp.ClientTimeout(total=5) try: - if key != "": - headers = {"Authorization": f"Bearer {key}"} - async with aiohttp.ClientSession(timeout=timeout) as session: - async with session.get(url, headers=headers) as response: - return await response.json() - else: - return None + headers = {"Authorization": f"Bearer {key}"} + async with aiohttp.ClientSession(timeout=timeout) as session: + async with session.get(url, headers=headers) as response: + return await response.json() except Exception as e: # Handle connection error here log.error(f"Connection error: {e}") @@ -277,11 +274,16 @@ async def get_models(url_idx: Optional[int] = None, user=Depends(get_current_use return models else: url = app.state.config.OPENAI_API_BASE_URLS[url_idx] + key = app.state.config.OPENAI_API_KEYS[url_idx] + + headers = {} + headers["Authorization"] = f"Bearer {key}" + headers["Content-Type"] = "application/json" r = None try: - r = requests.request(method="GET", url=f"{url}/models") + r = requests.request(method="GET", url=f"{url}/models", headers=headers) r.raise_for_status() response_data = r.json() diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index 04d3c4d62..ddeab495c 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -206,14 +206,17 @@ export const updateOpenAIKeys = async (token: string = '', keys: string[]) => { export const getOpenAIModels = async (token: string, urlIdx?: number) => { let error = null; - const res = await fetch(`${OPENAI_API_BASE_URL}/models${urlIdx ? `/${urlIdx}` : ''}`, { - method: 'GET', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json', - ...(token && { authorization: `Bearer ${token}` }) + const res = await fetch( + `${OPENAI_API_BASE_URL}/models${typeof urlIdx === 'number' ? `/${urlIdx}` : ''}`, + { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } } - }) + ) .then(async (res) => { if (!res.ok) throw await res.json(); return res.json(); diff --git a/src/lib/components/chat/Settings/Connections.svelte b/src/lib/components/chat/Settings/Connections.svelte index 85237c7a8..35345a76f 100644 --- a/src/lib/components/chat/Settings/Connections.svelte +++ b/src/lib/components/chat/Settings/Connections.svelte @@ -34,6 +34,8 @@ let OPENAI_API_KEYS = ['']; let OPENAI_API_BASE_URLS = ['']; + let pipelineUrls = {}; + let ENABLE_OPENAI_API = null; let ENABLE_OLLAMA_API = null; @@ -48,7 +50,9 @@ if (res) { toast.success($i18n.t('Server connection verified')); - console.log(res); + if (res.pipelines) { + pipelineUrls[OPENAI_API_BASE_URLS[idx]] = true; + } } }; @@ -100,6 +104,13 @@ })() ]); + OPENAI_API_BASE_URLS.forEach(async (url, idx) => { + const res = await getOpenAIModels(localStorage.token, idx); + if (res.pipelines) { + pipelineUrls[url] = true; + } + }); + const ollamaConfig = await getOllamaConfig(localStorage.token); const openaiConfig = await getOpenAIConfig(localStorage.token); @@ -139,13 +150,38 @@