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

View file

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

View file

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