Allow API endpoints to be changed in env
This commit is contained in:
parent
fdd46fdd06
commit
a51cea24d4
2
.env
2
.env
|
@ -1,2 +1,4 @@
|
||||||
# Uncomment the following line to use the mocked API
|
# Uncomment the following line to use the mocked API
|
||||||
#VITE_API_BASE=http://localhost:5174
|
#VITE_API_BASE=http://localhost:5174
|
||||||
|
#VITE_ENDPOINT_COMPLETIONS=/v1/chat/completions
|
||||||
|
#VITE_ENDPOINT_COMPLETIONS=/v1/models
|
||||||
|
|
|
@ -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>
|
|
@ -50,9 +50,7 @@
|
||||||
import PromptInput from './PromptInput.svelte'
|
import PromptInput from './PromptInput.svelte'
|
||||||
import { ChatCompletionResponse } from './ChatCompletionResponse.svelte'
|
import { ChatCompletionResponse } from './ChatCompletionResponse.svelte'
|
||||||
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
||||||
|
import { getApiBase, getEndpointCompletions } from './ApiUtil.svelte'
|
||||||
// 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'
|
|
||||||
|
|
||||||
export let params = { chatId: '' }
|
export let params = { chatId: '' }
|
||||||
const chatId: number = parseInt(params.chatId)
|
const chatId: number = parseInt(params.chatId)
|
||||||
|
@ -394,7 +392,7 @@
|
||||||
updatingMessage = ''
|
updatingMessage = ''
|
||||||
scrollToBottom()
|
scrollToBottom()
|
||||||
})
|
})
|
||||||
fetchEventSource(apiBase + '/v1/chat/completions', {
|
fetchEventSource(getApiBase() + getEndpointCompletions(), {
|
||||||
...fetchOptions,
|
...fetchOptions,
|
||||||
onmessage (ev) {
|
onmessage (ev) {
|
||||||
// Remove updating indicator
|
// Remove updating indicator
|
||||||
|
@ -423,7 +421,7 @@
|
||||||
scrollToBottom()
|
scrollToBottom()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
const response = await fetch(apiBase + '/v1/chat/completions', fetchOptions)
|
const response = await fetch(getApiBase() + getEndpointCompletions(), fetchOptions)
|
||||||
const json = await response.json()
|
const json = await response.json()
|
||||||
|
|
||||||
// Remove updating indicator
|
// Remove updating indicator
|
||||||
|
|
|
@ -35,12 +35,10 @@
|
||||||
import { replace } from 'svelte-spa-router'
|
import { replace } from 'svelte-spa-router'
|
||||||
import { openModal } from 'svelte-modals'
|
import { openModal } from 'svelte-modals'
|
||||||
import PromptConfirm from './PromptConfirm.svelte'
|
import PromptConfirm from './PromptConfirm.svelte'
|
||||||
|
import { getApiBase, getEndpointModels } from './ApiUtil.svelte'
|
||||||
|
|
||||||
export let chatId:number
|
export let chatId:number
|
||||||
export const show = () => { showSettings() }
|
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 showSettingsModal = 0
|
||||||
let showProfileMenu:boolean = false
|
let showProfileMenu:boolean = false
|
||||||
|
@ -179,7 +177,7 @@
|
||||||
|
|
||||||
// Load available models from OpenAI
|
// Load available models from OpenAI
|
||||||
const allModels = (await (
|
const allModels = (await (
|
||||||
await fetch(apiBase + '/v1/models', {
|
await fetch(getApiBase() + getEndpointModels(), {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${$apiKeyStorage}`,
|
Authorization: `Bearer ${$apiKeyStorage}`,
|
||||||
|
|
Loading…
Reference in New Issue