diff --git a/src/lib/Home.svelte b/src/lib/Home.svelte index 4cf79dd2..66e5045c 100644 --- a/src/lib/Home.svelte +++ b/src/lib/Home.svelte @@ -6,17 +6,18 @@ import { getPetalsBase, getPetalsWebsocket } from './ApiUtil.svelte' import { set as setOpenAI } from './providers/openai/util.svelte' import { hasActiveModels } from './Models.svelte' + import { get } from 'svelte/store' $: apiKey = $apiKeyStorage const openAiEndpoint = $globalStorage.openAiEndpoint || '' let showPetalsSettings = $globalStorage.enablePetals let pedalsEndpoint = $globalStorage.pedalsEndpoint let hasModels = hasActiveModels() + let apiError: string = '' onMount(() => { if (!$started) { $started = true - // console.log('started', apiKey, $lastChatId, getChat($lastChatId)) if (hasActiveModels() && getChat($lastChatId)) { const chatId = $lastChatId $lastChatId = 0 @@ -39,13 +40,30 @@ hasModels = hasActiveModels() } + async function testApiEndpoint (baseUri: string): Promise { + try { + const response = await fetch(`${baseUri}/v1/models`, { + headers: { Authorization: `Bearer ${get(apiKeyStorage)}` } + }) + if (!response.ok) { + apiError = `There was an error connecting to this endpoint: ${response.statusText}` + return false + } + apiError = '' + return true + } catch (error) { + console.error('Failed to connect:', error) + apiError = `There was an error connecting to this endpoint: ${error.message}` + return false + } + }
-

- ChatGPT-web +

+ ChatGPT-web is a simple one-page web interface to the OpenAI ChatGPT API. To use it, you need to register for an OpenAI API key first. OpenAI bills per token (usage-based), which means it is a lot cheaper than @@ -64,7 +82,7 @@

{ + on:submit|preventDefault={async (event) => { let val = '' if (event.target && event.target[0].value) { val = (event.target[0].value).trim() @@ -80,7 +98,8 @@ autocomplete="off" class="input" class:is-danger={!hasModels} - class:is-warning={!apiKey} class:is-info={apiKey} + class:is-warning={!apiKey} + class:is-info={apiKey} value={apiKey} />

@@ -100,18 +119,18 @@
-
+
Set the API BASE URI for alternative OpenAI-compatible endpoints: { + on:submit|preventDefault={async (event) => { let val = '' if (event.target && event.target[0].value) { val = (event.target[0].value).trim() + } + if (await testApiEndpoint(val)) { setGlobalSettingValueByKey('openAiEndpoint', val) - } else { - setGlobalSettingValueByKey('openAiEndpoint', '') } }} > @@ -120,6 +139,7 @@ aria-label="API BASE URI" type="text" class="input" + class:is-danger={apiError} placeholder="https://api.openai.com" value={openAiEndpoint} /> @@ -128,20 +148,22 @@

+ {#if apiError} +

{apiError}

+ {/if}
- - +
{#if showPetalsSettings}