Merge pull request #185 from ollama-webui/main

dev
This commit is contained in:
Timothy Jaeryang Baek 2023-12-06 11:24:41 -05:00 committed by GitHub
commit d94455cef0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 42 deletions

View File

@ -664,7 +664,7 @@
</div> </div>
{/if} {/if}
<button <!-- <button
class="invisible group-hover:visible p-1 rounded dark:hover:bg-gray-800 transition" class="invisible group-hover:visible p-1 rounded dark:hover:bg-gray-800 transition"
on:click={() => { on:click={() => {
editMessageHandler(message.id); editMessageHandler(message.id);
@ -684,7 +684,7 @@
d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L6.832 19.82a4.5 4.5 0 01-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 011.13-1.897L16.863 4.487zm0 0L19.5 7.125"
/> />
</svg> </svg>
</button> </button> -->
<button <button
class="{messageIdx + 1 === messages.length class="{messageIdx + 1 === messages.length

View File

@ -46,6 +46,8 @@
currentMessage.parentId !== null ? history.messages[currentMessage.parentId] : null; currentMessage.parentId !== null ? history.messages[currentMessage.parentId] : null;
} }
messages = _messages; messages = _messages;
} else {
messages = [];
} }
onMount(async () => { onMount(async () => {
@ -80,13 +82,13 @@
// Ollama functions // Ollama functions
////////////////////////// //////////////////////////
const sendPrompt = async (userPrompt, parentId) => { const sendPrompt = async (userPrompt, parentId, _chatId) => {
await Promise.all( await Promise.all(
selectedModels.map(async (model) => { selectedModels.map(async (model) => {
if (model.includes('gpt-')) { if (model.includes('gpt-')) {
await sendPromptOpenAI(model, userPrompt, parentId); await sendPromptOpenAI(model, userPrompt, parentId, _chatId);
} else { } else {
await sendPromptOllama(model, userPrompt, parentId); await sendPromptOllama(model, userPrompt, parentId, _chatId);
} }
}) })
); );
@ -94,7 +96,7 @@
await chats.set(await $db.getChats()); await chats.set(await $db.getChats());
}; };
const sendPromptOllama = async (model, userPrompt, parentId) => { const sendPromptOllama = async (model, userPrompt, parentId, _chatId) => {
console.log('sendPromptOllama'); console.log('sendPromptOllama');
let responseMessageId = uuidv4(); let responseMessageId = uuidv4();
@ -231,7 +233,7 @@
window.scrollTo({ top: document.body.scrollHeight }); window.scrollTo({ top: document.body.scrollHeight });
} }
await $db.updateChatById($chatId, { await $db.updateChatById(_chatId, {
title: title === '' ? 'New Chat' : title, title: title === '' ? 'New Chat' : title,
models: selectedModels, models: selectedModels,
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
@ -256,12 +258,12 @@
} }
if (messages.length == 2 && messages.at(1).content !== '') { if (messages.length == 2 && messages.at(1).content !== '') {
window.history.replaceState(history.state, '', `/c/${$chatId}`); window.history.replaceState(history.state, '', `/c/${_chatId}`);
await generateChatTitle($chatId, userPrompt); await generateChatTitle(_chatId, userPrompt);
} }
}; };
const sendPromptOpenAI = async (model, userPrompt, parentId) => { const sendPromptOpenAI = async (model, userPrompt, parentId, _chatId) => {
if ($settings.OPENAI_API_KEY) { if ($settings.OPENAI_API_KEY) {
if (models) { if (models) {
let responseMessageId = uuidv4(); let responseMessageId = uuidv4();
@ -361,7 +363,7 @@
window.scrollTo({ top: document.body.scrollHeight }); window.scrollTo({ top: document.body.scrollHeight });
} }
await $db.updateChatById($chatId, { await $db.updateChatById(_chatId, {
title: title === '' ? 'New Chat' : title, title: title === '' ? 'New Chat' : title,
models: selectedModels, models: selectedModels,
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
@ -387,15 +389,16 @@
} }
if (messages.length == 2) { if (messages.length == 2) {
window.history.replaceState(history.state, '', `/c/${$chatId}`); window.history.replaceState(history.state, '', `/c/${_chatId}`);
await setChatTitle($chatId, userPrompt); await setChatTitle(_chatId, userPrompt);
} }
} }
} }
}; };
const submitPrompt = async (userPrompt) => { const submitPrompt = async (userPrompt) => {
console.log('submitPrompt'); const _chatId = JSON.parse(JSON.stringify($chatId));
console.log('submitPrompt', _chatId);
if (selectedModels.includes('')) { if (selectedModels.includes('')) {
toast.error('Model not selected'); toast.error('Model not selected');
@ -421,12 +424,10 @@
history.messages[userMessageId] = userMessage; history.messages[userMessageId] = userMessage;
history.currentId = userMessageId; history.currentId = userMessageId;
prompt = ''; await tick();
files = []; if (messages.length == 1) {
if (messages.length == 0) {
await $db.createNewChat({ await $db.createNewChat({
id: $chatId, id: _chatId,
title: 'New Chat', title: 'New Chat',
models: selectedModels, models: selectedModels,
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
@ -444,11 +445,14 @@
}); });
} }
prompt = '';
files = [];
setTimeout(() => { setTimeout(() => {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' }); window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
}, 50); }, 50);
await sendPrompt(userPrompt, userMessageId); await sendPrompt(userPrompt, userMessageId, _chatId);
} }
}; };
@ -458,7 +462,9 @@
}; };
const regenerateResponse = async () => { const regenerateResponse = async () => {
console.log('regenerateResponse'); const _chatId = JSON.parse(JSON.stringify($chatId));
console.log('regenerateResponse', _chatId);
if (messages.length != 0 && messages.at(-1).done == true) { if (messages.length != 0 && messages.at(-1).done == true) {
messages.splice(messages.length - 1, 1); messages.splice(messages.length - 1, 1);
messages = messages; messages = messages;
@ -466,7 +472,7 @@
let userMessage = messages.at(-1); let userMessage = messages.at(-1);
let userPrompt = userMessage.content; let userPrompt = userMessage.content;
await sendPrompt(userPrompt, userMessage.id); await sendPrompt(userPrompt, userMessage.id, _chatId);
} }
}; };

View File

@ -46,6 +46,8 @@
currentMessage.parentId !== null ? history.messages[currentMessage.parentId] : null; currentMessage.parentId !== null ? history.messages[currentMessage.parentId] : null;
} }
messages = _messages; messages = _messages;
} else {
messages = [];
} }
// onMount(async () => { // onMount(async () => {
@ -113,13 +115,13 @@
// Ollama functions // Ollama functions
////////////////////////// //////////////////////////
const sendPrompt = async (userPrompt, parentId) => { const sendPrompt = async (userPrompt, parentId, _chatId) => {
await Promise.all( await Promise.all(
selectedModels.map(async (model) => { selectedModels.map(async (model) => {
if (model.includes('gpt-')) { if (model.includes('gpt-')) {
await sendPromptOpenAI(model, userPrompt, parentId); await sendPromptOpenAI(model, userPrompt, parentId, _chatId);
} else { } else {
await sendPromptOllama(model, userPrompt, parentId); await sendPromptOllama(model, userPrompt, parentId, _chatId);
} }
}) })
); );
@ -127,7 +129,8 @@
await chats.set(await $db.getChats()); await chats.set(await $db.getChats());
}; };
const sendPromptOllama = async (model, userPrompt, parentId) => { const sendPromptOllama = async (model, userPrompt, parentId, _chatId) => {
console.log('sendPromptOllama');
let responseMessageId = uuidv4(); let responseMessageId = uuidv4();
let responseMessage = { let responseMessage = {
@ -263,7 +266,7 @@
window.scrollTo({ top: document.body.scrollHeight }); window.scrollTo({ top: document.body.scrollHeight });
} }
await $db.updateChatById($chatId, { await $db.updateChatById(_chatId, {
title: title === '' ? 'New Chat' : title, title: title === '' ? 'New Chat' : title,
models: selectedModels, models: selectedModels,
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
@ -288,12 +291,12 @@
} }
if (messages.length == 2 && messages.at(1).content !== '') { if (messages.length == 2 && messages.at(1).content !== '') {
window.history.replaceState(history.state, '', `/c/${$chatId}`); window.history.replaceState(history.state, '', `/c/${_chatId}`);
await generateChatTitle($chatId, userPrompt); await generateChatTitle(_chatId, userPrompt);
} }
}; };
const sendPromptOpenAI = async (model, userPrompt, parentId) => { const sendPromptOpenAI = async (model, userPrompt, parentId, _chatId) => {
if ($settings.OPENAI_API_KEY) { if ($settings.OPENAI_API_KEY) {
if (models) { if (models) {
let responseMessageId = uuidv4(); let responseMessageId = uuidv4();
@ -393,7 +396,7 @@
window.scrollTo({ top: document.body.scrollHeight }); window.scrollTo({ top: document.body.scrollHeight });
} }
await $db.updateChatById($chatId, { await $db.updateChatById(_chatId, {
title: title === '' ? 'New Chat' : title, title: title === '' ? 'New Chat' : title,
models: selectedModels, models: selectedModels,
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
@ -419,15 +422,16 @@
} }
if (messages.length == 2) { if (messages.length == 2) {
window.history.replaceState(history.state, '', `/c/${$chatId}`); window.history.replaceState(history.state, '', `/c/${_chatId}`);
await setChatTitle($chatId, userPrompt); await setChatTitle(_chatId, userPrompt);
} }
} }
} }
}; };
const submitPrompt = async (userPrompt) => { const submitPrompt = async (userPrompt) => {
console.log('submitPrompt'); const _chatId = JSON.parse(JSON.stringify($chatId));
console.log('submitPrompt', _chatId);
if (selectedModels.includes('')) { if (selectedModels.includes('')) {
toast.error('Model not selected'); toast.error('Model not selected');
@ -442,7 +446,8 @@
parentId: messages.length !== 0 ? messages.at(-1).id : null, parentId: messages.length !== 0 ? messages.at(-1).id : null,
childrenIds: [], childrenIds: [],
role: 'user', role: 'user',
content: userPrompt content: userPrompt,
files: files.length > 0 ? files : undefined
}; };
if (messages.length !== 0) { if (messages.length !== 0) {
@ -452,11 +457,10 @@
history.messages[userMessageId] = userMessage; history.messages[userMessageId] = userMessage;
history.currentId = userMessageId; history.currentId = userMessageId;
prompt = ''; await tick();
if (messages.length == 1) {
if (messages.length == 0) {
await $db.createNewChat({ await $db.createNewChat({
id: $chatId, id: _chatId,
title: 'New Chat', title: 'New Chat',
models: selectedModels, models: selectedModels,
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
@ -474,11 +478,14 @@
}); });
} }
prompt = '';
files = [];
setTimeout(() => { setTimeout(() => {
window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' }); window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
}, 50); }, 50);
await sendPrompt(userPrompt, userMessageId); await sendPrompt(userPrompt, userMessageId, _chatId);
} }
}; };
@ -488,7 +495,9 @@
}; };
const regenerateResponse = async () => { const regenerateResponse = async () => {
console.log('regenerateResponse'); const _chatId = JSON.parse(JSON.stringify($chatId));
console.log('regenerateResponse', _chatId);
if (messages.length != 0 && messages.at(-1).done == true) { if (messages.length != 0 && messages.at(-1).done == true) {
messages.splice(messages.length - 1, 1); messages.splice(messages.length - 1, 1);
messages = messages; messages = messages;
@ -496,7 +505,7 @@
let userMessage = messages.at(-1); let userMessage = messages.at(-1);
let userPrompt = userMessage.content; let userPrompt = userMessage.content;
await sendPrompt(userPrompt, userMessage.id); await sendPrompt(userPrompt, userMessage.id, _chatId);
} }
}; };