chore: refac

This commit is contained in:
Timothy J. Baek 2023-12-26 12:50:52 -08:00
parent f30d16a3fd
commit cc49e0d10f
11 changed files with 239 additions and 397 deletions

View file

@ -31,7 +31,7 @@ export const createNewChat = async (token: string, chat: object) => {
return res;
};
export const getChatlist = async (token: string = '') => {
export const getChatList = async (token: string = '') => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/chats/`, {

View file

@ -69,3 +69,68 @@ export const getOllamaModels = async (
return res?.models ?? [];
};
export const generateTitle = async (
base_url: string = OLLAMA_API_BASE_URL,
token: string = '',
model: string,
prompt: string
) => {
let error = null;
const res = await fetch(`${base_url}/generate`, {
method: 'POST',
headers: {
'Content-Type': 'text/event-stream',
Authorization: `Bearer ${token}`
},
body: JSON.stringify({
model: model,
prompt: `Generate a brief 3-5 word title for this question, excluding the term 'title.' Then, please reply with only the title: ${prompt}`,
stream: false
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
console.log(err);
if ('detail' in err) {
error = err.detail;
}
return null;
});
if (error) {
throw error;
}
return res?.response ?? 'New Chat';
};
export const generateChatCompletion = async (
base_url: string = OLLAMA_API_BASE_URL,
token: string = '',
body: object
) => {
let error = null;
const res = await fetch(`${base_url}/chat`, {
method: 'POST',
headers: {
'Content-Type': 'text/event-stream',
Authorization: `Bearer ${token}`
},
body: JSON.stringify(body)
}).catch((err) => {
error = err;
return null;
});
if (error) {
throw error;
}
return res;
};

View file

@ -27,8 +27,6 @@ export const getOpenAIModels = async (
let models = Array.isArray(res) ? res : res?.data ?? null;
console.log(models);
return models
.map((model) => ({ name: model.id, external: true }))
.filter((model) => (base_url.includes('openai') ? model.name.includes('gpt') : true));

View file

@ -8,10 +8,11 @@
import auto_render from 'katex/dist/contrib/auto-render.mjs';
import 'katex/dist/katex.min.css';
import { config, db, modelfiles, settings, user } from '$lib/stores';
import { chats, config, db, modelfiles, settings, user } from '$lib/stores';
import { tick } from 'svelte';
import toast from 'svelte-french-toast';
import { getChatList, updateChatById } from '$lib/apis/chats';
export let chatId = '';
export let sendPrompt: Function;
@ -262,10 +263,12 @@
return message;
});
$db.updateChatById(chatId, {
await updateChatById(localStorage.token, chatId, {
messages: messages,
history: history
});
await chats.set(await getChatList(localStorage.token));
};
const showPreviousMessage = async (message) => {

View file

@ -1,7 +1,5 @@
<script lang="ts">
import { v4 as uuidv4 } from 'uuid';
import { goto } from '$app/navigation';
import { getChatById } from '$lib/apis/chats';
import { chatId, db, modelfiles } from '$lib/stores';
import toast from 'svelte-french-toast';
@ -10,10 +8,10 @@
export let shareEnabled: boolean = false;
const shareChat = async () => {
const chat = (await $db.getChatById($chatId)).chat;
const chat = (await getChatById(localStorage.token, $chatId)).chat;
console.log('share', chat);
toast.success('Redirecting you to OllamaHub');
toast.success('Redirecting you to OllamaHub');
const url = 'https://ollamahub.com';
// const url = 'http://localhost:5173';

View file

@ -8,6 +8,7 @@
import { page } from '$app/stores';
import { user, db, chats, showSettings, chatId } from '$lib/stores';
import { onMount } from 'svelte';
import { deleteChatById, getChatList, updateChatById } from '$lib/apis/chats';
let show = false;
let navElement;
@ -31,7 +32,7 @@
show = true;
}
await chats.set(await $db.getChats());
await chats.set(await getChatList(localStorage.token));
});
const loadChat = async (id) => {
@ -39,42 +40,46 @@
};
const editChatTitle = async (id, _title) => {
await $db.updateChatById(id, {
title = _title;
await updateChatById(localStorage.token, id, {
title: _title
});
title = _title;
await chats.set(await getChatList(localStorage.token));
};
const deleteChat = async (id) => {
goto('/');
$db.deleteChatById(id);
await deleteChatById(localStorage.token, id);
await chats.set(await getChatList(localStorage.token));
};
const deleteChatHistory = async () => {
await $db.deleteAllChat();
};
// const deleteChatHistory = async () => {
// await $db.deleteAllChat();
// };
const importChats = async (chatHistory) => {
await $db.importChats(chatHistory);
};
// const importChats = async (chatHistory) => {
// await $db.importChats(chatHistory);
// };
const exportChats = async () => {
let blob = new Blob([JSON.stringify(await $db.exportChats())], { type: 'application/json' });
saveAs(blob, `chat-export-${Date.now()}.json`);
};
// const exportChats = async () => {
// let blob = new Blob([JSON.stringify(await $db.exportChats())], { type: 'application/json' });
// saveAs(blob, `chat-export-${Date.now()}.json`);
// };
$: if (importFiles) {
console.log(importFiles);
// $: if (importFiles) {
// console.log(importFiles);
let reader = new FileReader();
reader.onload = (event) => {
let chats = JSON.parse(event.target.result);
console.log(chats);
importChats(chats);
};
// let reader = new FileReader();
// reader.onload = (event) => {
// let chats = JSON.parse(event.target.result);
// console.log(chats);
// importChats(chats);
// };
reader.readAsText(importFiles[0]);
}
// reader.readAsText(importFiles[0]);
// }
</script>
<div

View file

@ -13,6 +13,8 @@ export const WEBUI_API_BASE_URL = `${WEBUI_BASE_URL}/api/v1`;
export const WEB_UI_VERSION = 'v1.0.0-alpha-static';
export const REQUIRED_OLLAMA_VERSION = '0.1.16';
// Source: https://kit.svelte.dev/docs/modules#$env-static-public
// This feature, akin to $env/static/private, exclusively incorporates environment variables
// that are prefixed with config.kit.env.publicPrefix (usually set to PUBLIC_).

View file

@ -66,9 +66,9 @@ export const getGravatarURL = (email) => {
return `https://www.gravatar.com/avatar/${hash}`;
};
const copyToClipboard = (text) => {
export const copyToClipboard = (text) => {
if (!navigator.clipboard) {
var textArea = document.createElement('textarea');
const textArea = document.createElement('textarea');
textArea.value = text;
// Avoid scrolling to bottom
@ -81,8 +81,8 @@ const copyToClipboard = (text) => {
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
const successful = document.execCommand('copy');
const msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);