diff --git a/src/lib/apis/openai/index.ts b/src/lib/apis/openai/index.ts index a801bcdbb..53f369e01 100644 --- a/src/lib/apis/openai/index.ts +++ b/src/lib/apis/openai/index.ts @@ -241,33 +241,62 @@ export const getOpenAIModels = async (token: string, urlIdx?: number) => { export const verifyOpenAIConnection = async ( token: string = '', url: string = 'https://api.openai.com/v1', - key: string = '' + key: string = '', + direct: boolean = false ) => { + if (!url) { + throw 'OpenAI: URL is required'; + } + let error = null; + let res = null; - const res = await fetch(`${OPENAI_API_BASE_URL}/verify`, { - method: 'POST', - headers: { - Accept: 'application/json', - Authorization: `Bearer ${token}`, - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - url, - key + if (direct) { + res = await fetch(`${url}/models`, { + method: 'GET', + headers: { + Accept: 'application/json', + Authorization: `Bearer ${key}`, + 'Content-Type': 'application/json' + } }) - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); - }) - .catch((err) => { - error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`; - return []; - }); + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`; + return []; + }); - if (error) { - throw error; + if (error) { + throw error; + } + } else { + res = await fetch(`${OPENAI_API_BASE_URL}/verify`, { + method: 'POST', + headers: { + Accept: 'application/json', + Authorization: `Bearer ${token}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + url, + key + }) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + error = `OpenAI: ${err?.error?.message ?? 'Network Problem'}`; + return []; + }); + + if (error) { + throw error; + } } return res; diff --git a/src/lib/components/AddConnectionModal.svelte b/src/lib/components/AddConnectionModal.svelte index a8726a546..95074c258 100644 --- a/src/lib/components/AddConnectionModal.svelte +++ b/src/lib/components/AddConnectionModal.svelte @@ -20,7 +20,9 @@ export let show = false; export let edit = false; + export let ollama = false; + export let direct = false; export let connection = null; @@ -46,9 +48,11 @@ }; const verifyOpenAIHandler = async () => { - const res = await verifyOpenAIConnection(localStorage.token, url, key).catch((error) => { - toast.error(`${error}`); - }); + const res = await verifyOpenAIConnection(localStorage.token, url, key, direct).catch( + (error) => { + toast.error(`${error}`); + } + ); if (res) { toast.success($i18n.t('Server connection verified')); diff --git a/src/lib/components/chat/Settings/Connections.svelte b/src/lib/components/chat/Settings/Connections.svelte index 699bebf6f..3700f90e4 100644 --- a/src/lib/components/chat/Settings/Connections.svelte +++ b/src/lib/components/chat/Settings/Connections.svelte @@ -14,6 +14,8 @@ import Plus from '$lib/components/icons/Plus.svelte'; import Connection from './Connections/Connection.svelte'; + import AddConnectionModal from '$lib/components/AddConnectionModal.svelte'; + const getModels = async () => { const models = await _getModels(localStorage.token); return models; @@ -25,82 +27,73 @@ onMount(async () => {}); + const addConnectionHandler = async (connection) => {}; + const submitHandler = async () => {}; const updateHandler = async () => {}; - +
-
-
-
{$i18n.t('Direct Connections')}
+
+
+
+
{$i18n.t('Manage Direct Connections')}
+ + + + +
+ +
+ {#each config?.OPENAI_API_BASE_URLS ?? [] as url, idx} + { + updateHandler(); + }} + onDelete={() => { + config.OPENAI_API_BASE_URLS = config.OPENAI_API_BASE_URLS.filter( + (url, urlIdx) => idx !== urlIdx + ); + config.OPENAI_API_KEYS = config.OPENAI_API_KEYS.filter( + (key, keyIdx) => idx !== keyIdx + ); + + let newConfig = {}; + config.OPENAI_API_BASE_URLS.forEach((url, newIdx) => { + newConfig[newIdx] = + config.OPENAI_API_CONFIGS[newIdx < idx ? newIdx : newIdx + 1]; + }); + config.OPENAI_API_CONFIGS = newConfig; + }} + /> + {/each} +
+
+
{$i18n.t('Connect to your own OpenAI compatible API endpoints.')}
- - {#if false} -
- -
-
-
{$i18n.t('Manage Connections')}
- - - - -
- -
- {#each config?.OPENAI_API_BASE_URLS ?? [] as url, idx} - { - updateHandler(); - }} - onDelete={() => { - config.OPENAI_API_BASE_URLS = config.OPENAI_API_BASE_URLS.filter( - (url, urlIdx) => idx !== urlIdx - ); - config.OPENAI_API_KEYS = config.OPENAI_API_KEYS.filter( - (key, keyIdx) => idx !== keyIdx - ); - - let newConfig = {}; - config.OPENAI_API_BASE_URLS.forEach((url, newIdx) => { - newConfig[newIdx] = - config.OPENAI_API_CONFIGS[newIdx < idx ? newIdx : newIdx + 1]; - }); - config.OPENAI_API_CONFIGS = newConfig; - }} - /> - {/each} -
-
- {/if}
- -