fix: model list update

This commit is contained in:
Timothy J. Baek 2023-11-19 18:00:53 -08:00
parent 2342c5036b
commit e366d1137f
3 changed files with 44 additions and 48 deletions

View file

@ -4,7 +4,7 @@
import { WEB_UI_VERSION, OLLAMA_API_BASE_URL as BUILD_TIME_API_BASE_URL } from '$lib/constants'; import { WEB_UI_VERSION, OLLAMA_API_BASE_URL as BUILD_TIME_API_BASE_URL } from '$lib/constants';
import toast from 'svelte-french-toast'; import toast from 'svelte-french-toast';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import { config, settings, user } from '$lib/stores'; import { config, models, settings, user } from '$lib/stores';
import { splitStream, getGravatarURL } from '$lib/utils'; import { splitStream, getGravatarURL } from '$lib/utils';
export let show = false; export let show = false;
@ -50,7 +50,7 @@
if (API_BASE_URL === '') { if (API_BASE_URL === '') {
API_BASE_URL = BUILD_TIME_API_BASE_URL; API_BASE_URL = BUILD_TIME_API_BASE_URL;
} }
const res = await getModelTags(API_BASE_URL, 'ollama'); const res = await getModels(API_BASE_URL, 'ollama');
if (res) { if (res) {
toast.success('Server connection verified'); toast.success('Server connection verified');
@ -97,6 +97,7 @@
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'text/event-stream', 'Content-Type': 'text/event-stream',
...($settings.authHeader && { Authorization: $settings.authHeader }),
...($user && { Authorization: `Bearer ${localStorage.token}` }) ...($user && { Authorization: `Bearer ${localStorage.token}` })
}, },
body: JSON.stringify({ body: JSON.stringify({
@ -150,7 +151,7 @@
} }
modelTag = ''; modelTag = '';
await getModelTags(); models.set(await getModels());
}; };
const deleteModelHandler = async () => { const deleteModelHandler = async () => {
@ -158,6 +159,7 @@
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'Content-Type': 'text/event-stream', 'Content-Type': 'text/event-stream',
...($settings.authHeader && { Authorization: $settings.authHeader }),
...($user && { Authorization: `Bearer ${localStorage.token}` }) ...($user && { Authorization: `Bearer ${localStorage.token}` })
}, },
body: JSON.stringify({ body: JSON.stringify({
@ -203,7 +205,7 @@
} }
deleteModelTag = ''; deleteModelTag = '';
await getModelTags(); models.set(await getModels());
}; };
$: if (show) { $: if (show) {
@ -226,14 +228,14 @@
OPENAI_API_KEY = settings.OPENAI_API_KEY ?? ''; OPENAI_API_KEY = settings.OPENAI_API_KEY ?? '';
} }
const getModelTags = async (url = null, type = 'all') => { const getModels = async (url = '', type = 'all') => {
let models = []; let models = [];
const res = await fetch(`${url === null ? API_BASE_URL : url}/tags`, { const res = await fetch(`${url ? url : $settings?.API_BASE_URL ?? OLLAMA_API_BASE_URL}/tags`, {
method: 'GET', method: 'GET',
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...(settings.authHeader && { Authorization: settings.authHeader }), ...($settings.authHeader && { Authorization: $settings.authHeader }),
...($user && { Authorization: `Bearer ${localStorage.token}` }) ...($user && { Authorization: `Bearer ${localStorage.token}` })
} }
}) })
@ -250,17 +252,17 @@
} }
return null; return null;
}); });
console.log(res); console.log(res);
models.push(...(res?.models ?? []));
if (type === 'all') { // If OpenAI API Key exists
if (settings.OPENAI_API_KEY) { if (type === 'all' && $settings.OPENAI_API_KEY) {
// Validate OPENAI_API_KEY // Validate OPENAI_API_KEY
const openaiModelRes = await fetch(`https://api.openai.com/v1/models`, { const openaiModelRes = await fetch(`https://api.openai.com/v1/models`, {
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Authorization: `Bearer ${settings.OPENAI_API_KEY}` Authorization: `Bearer ${$settings.OPENAI_API_KEY}`
} }
}) })
.then(async (res) => { .then(async (res) => {
@ -272,28 +274,22 @@
toast.error(`OpenAI: ${error?.error?.message ?? 'Network Problem'}`); toast.error(`OpenAI: ${error?.error?.message ?? 'Network Problem'}`);
return null; return null;
}); });
const openaiModels = openaiModelRes?.data ?? null;
if (openaiModels) { const openAIModels = openaiModelRes?.data ?? null;
models = [
...(res?.models ?? []), models.push(
...(openAIModels
? [
{ name: 'hr' }, { name: 'hr' },
...openAIModels
...openaiModels
.map((model) => ({ name: model.id, label: 'OpenAI' })) .map((model) => ({ name: model.id, label: 'OpenAI' }))
.filter((model) => model.name.includes('gpt')) .filter((model) => model.name.includes('gpt'))
]; ]
} else { : [])
models = res?.models ?? []; );
}
} else {
models = res?.models ?? [];
} }
return models; return models;
} else {
return res?.models ?? null;
}
}; };
onMount(() => { onMount(() => {

View file

@ -93,6 +93,8 @@
await goto('/auth'); await goto('/auth');
} }
await settings.set(JSON.parse(localStorage.getItem('settings') ?? JSON.stringify($settings)));
let _models = await getModels(); let _models = await getModels();
await models.set(_models); await models.set(_models);
let _db = await getDB(); let _db = await getDB();

View file

@ -62,8 +62,6 @@
messages: {}, messages: {},
currentId: null currentId: null
}; };
await settings.set(JSON.parse(localStorage.getItem('settings') ?? JSON.stringify($settings)));
selectedModels = $settings.models ?? ['']; selectedModels = $settings.models ?? [''];
}; };