Allow API endpoints to be changed in env

This commit is contained in:
Webifi 2023-06-07 11:50:36 -05:00
parent fdd46fdd06
commit a51cea24d4
4 changed files with 17 additions and 9 deletions

2
.env
View File

@ -1,2 +1,4 @@
# Uncomment the following line to use the mocked API
#VITE_API_BASE=http://localhost:5174
#VITE_ENDPOINT_COMPLETIONS=/v1/chat/completions
#VITE_ENDPOINT_COMPLETIONS=/v1/models

10
src/lib/ApiUtil.svelte Normal file
View File

@ -0,0 +1,10 @@
<script context="module" lang="ts">
// This makes it possible to override the OpenAI API base URL in the .env file
const apiBase = import.meta.env.VITE_API_BASE || 'https://api.openai.com'
const endpointCompletions = import.meta.env.VITE_ENDPOINT_COMPLETIONS || '/v1/chat/completions'
const endpointModels = import.meta.env.VITE_ENDPOINT_COMPLETIONS || '/v1/models'
export const getApiBase = ():string => apiBase
export const getEndpointCompletions = ():string => endpointCompletions
export const getEndpointModels = ():string => endpointModels
</script>

View File

@ -50,9 +50,7 @@
import PromptInput from './PromptInput.svelte'
import { ChatCompletionResponse } from './ChatCompletionResponse.svelte'
import { fetchEventSource } from '@microsoft/fetch-event-source'
// This makes it possible to override the OpenAI API base URL in the .env file
const apiBase = import.meta.env.VITE_API_BASE || 'https://api.openai.com'
import { getApiBase, getEndpointCompletions } from './ApiUtil.svelte'
export let params = { chatId: '' }
const chatId: number = parseInt(params.chatId)
@ -394,7 +392,7 @@
updatingMessage = ''
scrollToBottom()
})
fetchEventSource(apiBase + '/v1/chat/completions', {
fetchEventSource(getApiBase() + getEndpointCompletions(), {
...fetchOptions,
onmessage (ev) {
// Remove updating indicator
@ -423,7 +421,7 @@
scrollToBottom()
})
} else {
const response = await fetch(apiBase + '/v1/chat/completions', fetchOptions)
const response = await fetch(getApiBase() + getEndpointCompletions(), fetchOptions)
const json = await response.json()
// Remove updating indicator

View File

@ -35,13 +35,11 @@
import { replace } from 'svelte-spa-router'
import { openModal } from 'svelte-modals'
import PromptConfirm from './PromptConfirm.svelte'
import { getApiBase, getEndpointModels } from './ApiUtil.svelte'
export let chatId:number
export const show = () => { showSettings() }
// This makes it possible to override the OpenAI API base URL in the .env file
const apiBase = import.meta.env.VITE_API_BASE || 'https://api.openai.com'
let showSettingsModal = 0
let showProfileMenu:boolean = false
let profileFileInput
@ -179,7 +177,7 @@
// Load available models from OpenAI
const allModels = (await (
await fetch(apiBase + '/v1/models', {
await fetch(getApiBase() + getEndpointModels(), {
method: 'GET',
headers: {
Authorization: `Bearer ${$apiKeyStorage}`,