Browse Source

refac: openai allow empty key

Timothy J. Baek 11 tháng trước cách đây
mục cha
commit
e8d4e03c0d

+ 4 - 0
backend/apps/ollama/main.py

@@ -219,6 +219,8 @@ async def get_ollama_tags(
         return models
         return models
     else:
     else:
         url = app.state.config.OLLAMA_BASE_URLS[url_idx]
         url = app.state.config.OLLAMA_BASE_URLS[url_idx]
+
+        r = None
         try:
         try:
             r = requests.request(method="GET", url=f"{url}/api/tags")
             r = requests.request(method="GET", url=f"{url}/api/tags")
             r.raise_for_status()
             r.raise_for_status()
@@ -270,6 +272,8 @@ async def get_ollama_versions(url_idx: Optional[int] = None):
             )
             )
     else:
     else:
         url = app.state.config.OLLAMA_BASE_URLS[url_idx]
         url = app.state.config.OLLAMA_BASE_URLS[url_idx]
+
+        r = None
         try:
         try:
             r = requests.request(method="GET", url=f"{url}/api/version")
             r = requests.request(method="GET", url=f"{url}/api/version")
             r.raise_for_status()
             r.raise_for_status()

+ 0 - 3
backend/apps/openai/main.py

@@ -407,9 +407,6 @@ async def proxy(path: str, request: Request, user=Depends(get_verified_user)):
 
 
     target_url = f"{url}/{path}"
     target_url = f"{url}/{path}"
 
 
-    if key == "":
-        raise HTTPException(status_code=401, detail=ERROR_MESSAGES.API_KEY_NOT_FOUND)
-
     headers = {}
     headers = {}
     headers["Authorization"] = f"Bearer {key}"
     headers["Authorization"] = f"Bearer {key}"
     headers["Content-Type"] = "application/json"
     headers["Content-Type"] = "application/json"

+ 2 - 2
src/lib/apis/ollama/index.ts

@@ -135,10 +135,10 @@ export const updateOllamaUrls = async (token: string = '', urls: string[]) => {
 	return res.OLLAMA_BASE_URLS;
 	return res.OLLAMA_BASE_URLS;
 };
 };
 
 
-export const getOllamaVersion = async (token: string = '') => {
+export const getOllamaVersion = async (token: string, urlIdx?: number) => {
 	let error = null;
 	let error = null;
 
 
-	const res = await fetch(`${OLLAMA_API_BASE_URL}/api/version`, {
+	const res = await fetch(`${OLLAMA_API_BASE_URL}/api/version${urlIdx ? `/${urlIdx}` : ''}`, {
 		method: 'GET',
 		method: 'GET',
 		headers: {
 		headers: {
 			Accept: 'application/json',
 			Accept: 'application/json',

+ 3 - 16
src/lib/apis/openai/index.ts

@@ -203,10 +203,10 @@ export const updateOpenAIKeys = async (token: string = '', keys: string[]) => {
 	return res.OPENAI_API_KEYS;
 	return res.OPENAI_API_KEYS;
 };
 };
 
 
-export const getOpenAIModels = async (token: string = '') => {
+export const getOpenAIModels = async (token: string, urlIdx?: number) => {
 	let error = null;
 	let error = null;
 
 
-	const res = await fetch(`${OPENAI_API_BASE_URL}/models`, {
+	const res = await fetch(`${OPENAI_API_BASE_URL}/models${urlIdx ? `/${urlIdx}` : ''}`, {
 		method: 'GET',
 		method: 'GET',
 		headers: {
 		headers: {
 			Accept: 'application/json',
 			Accept: 'application/json',
@@ -227,20 +227,7 @@ export const getOpenAIModels = async (token: string = '') => {
 		throw error;
 		throw error;
 	}
 	}
 
 
-	const models = Array.isArray(res) ? res : res?.data ?? null;
-
-	return models
-		? models
-				.map((model) => ({
-					id: model.id,
-					name: model.name ?? model.id,
-					external: true,
-					custom_info: model.custom_info
-				}))
-				.sort((a, b) => {
-					return a.name.localeCompare(b.name);
-				})
-		: models;
+	return res;
 };
 };
 
 
 export const getOpenAIModelsDirect = async (
 export const getOpenAIModelsDirect = async (

+ 82 - 23
src/lib/components/chat/Settings/Connections.svelte

@@ -13,6 +13,7 @@
 	import {
 	import {
 		getOpenAIConfig,
 		getOpenAIConfig,
 		getOpenAIKeys,
 		getOpenAIKeys,
+		getOpenAIModels,
 		getOpenAIUrls,
 		getOpenAIUrls,
 		updateOpenAIConfig,
 		updateOpenAIConfig,
 		updateOpenAIKeys,
 		updateOpenAIKeys,
@@ -21,6 +22,7 @@
 	import { toast } from 'svelte-sonner';
 	import { toast } from 'svelte-sonner';
 	import Switch from '$lib/components/common/Switch.svelte';
 	import Switch from '$lib/components/common/Switch.svelte';
 	import Spinner from '$lib/components/common/Spinner.svelte';
 	import Spinner from '$lib/components/common/Spinner.svelte';
+	import Tooltip from '$lib/components/common/Tooltip.svelte';
 
 
 	const i18n = getContext('i18n');
 	const i18n = getContext('i18n');
 
 
@@ -35,6 +37,34 @@
 	let ENABLE_OPENAI_API = null;
 	let ENABLE_OPENAI_API = null;
 	let ENABLE_OLLAMA_API = null;
 	let ENABLE_OLLAMA_API = null;
 
 
+	const verifyOpenAIHandler = async (idx) => {
+		OPENAI_API_BASE_URLS = await updateOpenAIUrls(localStorage.token, OPENAI_API_BASE_URLS);
+		OPENAI_API_KEYS = await updateOpenAIKeys(localStorage.token, OPENAI_API_KEYS);
+
+		const res = await getOpenAIModels(localStorage.token, idx).catch((error) => {
+			toast.error(error);
+			return null;
+		});
+
+		if (res) {
+			toast.success($i18n.t('Server connection verified'));
+			console.log(res);
+		}
+	};
+
+	const verifyOllamaHandler = async (idx) => {
+		OLLAMA_BASE_URLS = await updateOllamaUrls(localStorage.token, OLLAMA_BASE_URLS);
+
+		const res = await getOllamaVersion(localStorage.token, idx).catch((error) => {
+			toast.error(error);
+			return null;
+		});
+
+		if (res) {
+			toast.success($i18n.t('Server connection verified'));
+		}
+	};
+
 	const updateOpenAIHandler = async () => {
 	const updateOpenAIHandler = async () => {
 		OPENAI_API_BASE_URLS = await updateOpenAIUrls(localStorage.token, OPENAI_API_BASE_URLS);
 		OPENAI_API_BASE_URLS = await updateOpenAIUrls(localStorage.token, OPENAI_API_BASE_URLS);
 		OPENAI_API_KEYS = await updateOpenAIKeys(localStorage.token, OPENAI_API_KEYS);
 		OPENAI_API_KEYS = await updateOpenAIKeys(localStorage.token, OPENAI_API_KEYS);
@@ -83,6 +113,8 @@
 	class="flex flex-col h-full justify-between text-sm"
 	class="flex flex-col h-full justify-between text-sm"
 	on:submit|preventDefault={() => {
 	on:submit|preventDefault={() => {
 		updateOpenAIHandler();
 		updateOpenAIHandler();
+		updateOllamaUrlsHandler();
+
 		dispatch('save');
 		dispatch('save');
 	}}
 	}}
 >
 >
@@ -167,6 +199,31 @@
 											</button>
 											</button>
 										{/if}
 										{/if}
 									</div>
 									</div>
+
+									<div class="flex">
+										<Tooltip content="Verify connection" className="self-start mt-0.5">
+											<button
+												class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
+												on:click={() => {
+													verifyOpenAIHandler(idx);
+												}}
+												type="button"
+											>
+												<svg
+													xmlns="http://www.w3.org/2000/svg"
+													viewBox="0 0 20 20"
+													fill="currentColor"
+													class="w-4 h-4"
+												>
+													<path
+														fill-rule="evenodd"
+														d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
+														clip-rule="evenodd"
+													/>
+												</svg>
+											</button>
+										</Tooltip>
+									</div>
 								</div>
 								</div>
 								<div class=" mb-1 text-xs text-gray-400 dark:text-gray-500">
 								<div class=" mb-1 text-xs text-gray-400 dark:text-gray-500">
 									{$i18n.t('WebUI will make requests to')}
 									{$i18n.t('WebUI will make requests to')}
@@ -245,32 +302,34 @@
 											</button>
 											</button>
 										{/if}
 										{/if}
 									</div>
 									</div>
+
+									<div class="flex">
+										<Tooltip content="Verify connection" className="self-start mt-0.5">
+											<button
+												class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
+												on:click={() => {
+													verifyOllamaHandler(idx);
+												}}
+												type="button"
+											>
+												<svg
+													xmlns="http://www.w3.org/2000/svg"
+													viewBox="0 0 20 20"
+													fill="currentColor"
+													class="w-4 h-4"
+												>
+													<path
+														fill-rule="evenodd"
+														d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
+														clip-rule="evenodd"
+													/>
+												</svg>
+											</button>
+										</Tooltip>
+									</div>
 								</div>
 								</div>
 							{/each}
 							{/each}
 						</div>
 						</div>
-
-						<div class="flex">
-							<button
-								class="self-center p-2 bg-gray-200 hover:bg-gray-300 dark:bg-gray-900 dark:hover:bg-gray-850 rounded-lg transition"
-								on:click={() => {
-									updateOllamaUrlsHandler();
-								}}
-								type="button"
-							>
-								<svg
-									xmlns="http://www.w3.org/2000/svg"
-									viewBox="0 0 20 20"
-									fill="currentColor"
-									class="w-4 h-4"
-								>
-									<path
-										fill-rule="evenodd"
-										d="M15.312 11.424a5.5 5.5 0 01-9.201 2.466l-.312-.311h2.433a.75.75 0 000-1.5H3.989a.75.75 0 00-.75.75v4.242a.75.75 0 001.5 0v-2.43l.31.31a7 7 0 0011.712-3.138.75.75 0 00-1.449-.39zm1.23-3.723a.75.75 0 00.219-.53V2.929a.75.75 0 00-1.5 0V5.36l-.31-.31A7 7 0 003.239 8.188a.75.75 0 101.448.389A5.5 5.5 0 0113.89 6.11l.311.31h-2.432a.75.75 0 000 1.5h4.243a.75.75 0 00.53-.219z"
-										clip-rule="evenodd"
-									/>
-								</svg>
-							</button>
-						</div>
 					</div>
 					</div>
 
 
 					<div class="mt-2 text-xs text-gray-400 dark:text-gray-500">
 					<div class="mt-2 text-xs text-gray-400 dark:text-gray-500">