Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AI-32] Check /models endpoint before saving API URL #2

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions src/lib/Home.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,13 +40,30 @@
hasModels = hasActiveModels()
}

async function testApiEndpoint (baseUri: string): Promise<boolean> {
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
}
}
</script>

<section class="section">
<article class="message">
<div class="message-body">
<p class="mb-4">
<strong><a href="https://github.com/Niek/chatgpt-web" target="_blank">ChatGPT-web</a></strong>
<p class="mb-4">
<strong><a href="https://github.com/Niek/chatgpt-web" target="_blank">ChatGPT-web</a></strong>
is a simple one-page web interface to the OpenAI ChatGPT API. To use it, you need to register for
<a href="https://platform.openai.com/account/api-keys" target="_blank" rel="noreferrer">an OpenAI API key</a>
first. OpenAI bills per token (usage-based), which means it is a lot cheaper than
Expand All @@ -64,7 +82,7 @@

<form
class="field has-addons has-addons-right"
on:submit|preventDefault={(event) => {
on:submit|preventDefault={async (event) => {
let val = ''
if (event.target && event.target[0].value) {
val = (event.target[0].value).trim()
Expand All @@ -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}
/>
</p>
Expand All @@ -100,18 +119,18 @@
</div>
</article>

<article class="message" class:is-danger={!hasModels} class:is-warning={!openAiEndpoint} class:is-info={openAiEndpoint}>
<article class="message" class:is-danger={!hasModels || apiError} class:is-warning={!openAiEndpoint} class:is-info={openAiEndpoint && !apiError}>
<div class="message-body">
Set the API BASE URI for alternative OpenAI-compatible endpoints:
<form
class="field has-addons has-addons-right"
on:submit|preventDefault={(event) => {
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', '')
}
}}
>
Expand All @@ -120,6 +139,7 @@
aria-label="API BASE URI"
type="text"
class="input"
class:is-danger={apiError}
placeholder="https://api.openai.com"
value={openAiEndpoint}
/>
Expand All @@ -128,20 +148,22 @@
<button class="button is-info" type="submit">Save</button>
</p>
</form>
{#if apiError}
<p class:is-danger={apiError}>{apiError}</p>
{/if}
</div>
</article>



<article class="message" class:is-danger={!hasModels} class:is-warning={!showPetalsSettings} class:is-info={showPetalsSettings}>
<div class="message-body">
<label class="label" for="enablePetals">
<input
type="checkbox"
class="checkbox"
id="enablePetals"
checked={!!$globalStorage.enablePetals}
on:click={setPetalsEnabled}
>
type="checkbox"
class="checkbox"
id="enablePetals"
checked={!!$globalStorage.enablePetals}
on:click={setPetalsEnabled}
>
Use Petals API and Models (Llama 2)
</label>
{#if showPetalsSettings}
Expand Down