From 3b3b7fb46a91c1fc058d23e3f7471796e8516c56 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 6 Nov 2023 01:12:27 -0800 Subject: [PATCH] fix: ollama custom url support --- src/lib/components/chat/SettingsModal.svelte | 4 +- src/routes/+page.svelte | 67 +++++++++++--------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index 8305655c..f23b9bb7 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -65,7 +65,7 @@ if (API_BASE_URL === '') { API_BASE_URL = BUILD_TIME_API_BASE_URL; } - const res = await getModelTags(API_BASE_URL); + const res = await getModelTags(API_BASE_URL, 'ollama'); if (res) { toast.success('Server connection verified'); @@ -774,7 +774,7 @@
followers diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 6ceb612b..65eb2dd7 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -236,6 +236,7 @@ console.log(updated); settings = { ...settings, ...updated }; localStorage.setItem('settings', JSON.stringify(settings)); + API_BASE_URL = updated?.API_BASE_URL ?? API_BASE_URL; await getModelTags(); }; @@ -374,7 +375,7 @@ // Ollama functions ////////////////////////// - const getModelTags = async (url = null) => { + const getModelTags = async (url = null, type = 'all') => { const res = await fetch(`${url === null ? API_BASE_URL : url}/tags`, { method: 'GET', headers: { @@ -394,43 +395,47 @@ console.log(res); - if (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}` - } - }) - .then(async (res) => { - if (!res.ok) throw await res.json(); - return res.json(); + if (type === 'all') { + if (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}` + } }) - .catch((error) => { - console.log(error); - toast.error(`OpenAI: ${error?.error?.message ?? 'Network Problem'}`); - return null; - }); - const openaiModels = openaiModelRes?.data ?? null; + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((error) => { + console.log(error); + toast.error(`OpenAI: ${error?.error?.message ?? 'Network Problem'}`); + return null; + }); + const openaiModels = openaiModelRes?.data ?? null; - if (openaiModels) { - models = [ - ...(res?.models ?? []), - { name: 'hr' }, + if (openaiModels) { + models = [ + ...(res?.models ?? []), + { name: 'hr' }, - ...openaiModels - .map((model) => ({ name: model.id, label: 'OpenAI' })) - .filter((model) => model.name.includes('gpt')) - ]; + ...openaiModels + .map((model) => ({ name: model.id, label: 'OpenAI' })) + .filter((model) => model.name.includes('gpt')) + ]; + } else { + models = res?.models ?? []; + } } else { models = res?.models ?? []; } - } else { - models = res?.models ?? []; - } - return models; + return models; + } else { + return res?.models ?? null; + } }; const sendPrompt = async (userPrompt) => {