From 7349e1d8c8f5125d7f567e520b5e923c380f41f9 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 10 Jun 2024 00:56:13 -0700 Subject: [PATCH] refac --- src/lib/components/chat/Chat.svelte | 129 +++++++++++++++------------- 1 file changed, 71 insertions(+), 58 deletions(-) diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 49ba8961d..861b985a5 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -358,29 +358,6 @@ // Wait until history/message have been updated await tick(); - // Create new chat if only one message in messages - if (messages.length == 1) { - if ($settings.saveChatHistory ?? true) { - chat = await createNewChat(localStorage.token, { - id: $chatId, - title: $i18n.t('New Chat'), - models: selectedModels, - system: $settings.system ?? undefined, - options: { - ...($settings.params ?? {}) - }, - messages: messages, - history: history, - tags: [], - timestamp: Date.now() - }); - await chats.set(await getChatList(localStorage.token)); - await chatId.set(chat.id); - } else { - await chatId.set('local'); - } - await tick(); - } // Send prompt _responses = await sendPrompt(userPrompt, userMessageId); } @@ -390,15 +367,78 @@ const sendPrompt = async (prompt, parentId, modelId = null) => { let _responses = []; + + // If modelId is provided, use it, else use selected model + let selectedModelIds = modelId + ? [modelId] + : atSelectedModel !== undefined + ? [atSelectedModel.id] + : selectedModels; + + // Create response messages for each selected model + const responseMessageIds = {}; + for (const modelId of selectedModelIds) { + const model = $models.filter((m) => m.id === modelId).at(0); + + if (model) { + let responseMessageId = uuidv4(); + let responseMessage = { + parentId: parentId, + id: responseMessageId, + childrenIds: [], + role: 'assistant', + content: '', + model: model.id, + modelName: model.name ?? model.id, + userContext: null, + timestamp: Math.floor(Date.now() / 1000) // Unix epoch + }; + + // Add message to history and Set currentId to messageId + history.messages[responseMessageId] = responseMessage; + history.currentId = responseMessageId; + + // Append messageId to childrenIds of parent message + if (parentId !== null) { + history.messages[parentId].childrenIds = [ + ...history.messages[parentId].childrenIds, + responseMessageId + ]; + } + + responseMessageIds[modelId] = responseMessageId; + } + } + await tick(); + + // Create new chat if only one message in messages + if (messages.length == 2) { + if ($settings.saveChatHistory ?? true) { + chat = await createNewChat(localStorage.token, { + id: $chatId, + title: $i18n.t('New Chat'), + models: selectedModels, + system: $settings.system ?? undefined, + options: { + ...($settings.params ?? {}) + }, + messages: messages, + history: history, + tags: [], + timestamp: Date.now() + }); + await chats.set(await getChatList(localStorage.token)); + await chatId.set(chat.id); + } else { + await chatId.set('local'); + } + await tick(); + } + const _chatId = JSON.parse(JSON.stringify($chatId)); await Promise.all( - (modelId - ? [modelId] - : atSelectedModel !== undefined - ? [atSelectedModel.id] - : selectedModels - ).map(async (modelId) => { + selectedModelIds.map(async (modelId) => { console.log('modelId', modelId); const model = $models.filter((m) => m.id === modelId).at(0); @@ -416,33 +456,8 @@ ); } - // Create response message - let responseMessageId = uuidv4(); - let responseMessage = { - parentId: parentId, - id: responseMessageId, - childrenIds: [], - role: 'assistant', - content: '', - model: model.id, - modelName: model.name ?? model.id, - userContext: null, - timestamp: Math.floor(Date.now() / 1000) // Unix epoch - }; - - // Add message to history and Set currentId to messageId - history.messages[responseMessageId] = responseMessage; - history.currentId = responseMessageId; - - // Append messageId to childrenIds of parent message - if (parentId !== null) { - history.messages[parentId].childrenIds = [ - ...history.messages[parentId].childrenIds, - responseMessageId - ]; - } - - await tick(); + let responseMessageId = responseMessageIds[modelId]; + let responseMessage = history.messages[responseMessageId]; let userContext = null; if ($settings?.memory ?? false) { @@ -451,7 +466,6 @@ toast.error(error); return null; }); - if (res) { if (res.documents[0].length > 0) { userContext = res.documents.reduce((acc, doc, index) => { @@ -477,7 +491,6 @@ } let _response = null; - if (model?.owned_by === 'openai') { _response = await sendPromptOpenAI(model, prompt, responseMessageId, _chatId); } else if (model) {