From 0260c714d91954423a9b9cda760a211249642400 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 13:11:56 -0700 Subject: [PATCH 1/9] refac --- .../chat/Messages/CompareMessages.svelte | 76 ++++++++++--------- src/routes/(app)/+page.svelte | 48 +++++++----- src/routes/(app)/c/[id]/+page.svelte | 48 +++++++----- 3 files changed, 96 insertions(+), 76 deletions(-) diff --git a/src/lib/components/chat/Messages/CompareMessages.svelte b/src/lib/components/chat/Messages/CompareMessages.svelte index 87c3c88d9..60efdb2ab 100644 --- a/src/lib/components/chat/Messages/CompareMessages.svelte +++ b/src/lib/components/chat/Messages/CompareMessages.svelte @@ -41,6 +41,44 @@ }; }, {}); + const showPreviousMessage = (model) => { + groupedMessagesIdx[model] = Math.max(0, groupedMessagesIdx[model] - 1); + let messageId = groupedMessages[model].messages[groupedMessagesIdx[model]].id; + + console.log(messageId); + let messageChildrenIds = history.messages[messageId].childrenIds; + + while (messageChildrenIds.length !== 0) { + messageId = messageChildrenIds.at(-1); + messageChildrenIds = history.messages[messageId].childrenIds; + } + + history.currentId = messageId; + + dispatch('change'); + }; + + const showNextMessage = (model) => { + groupedMessagesIdx[model] = Math.min( + groupedMessages[model].messages.length - 1, + groupedMessagesIdx[model] + 1 + ); + + let messageId = groupedMessages[model].messages[groupedMessagesIdx[model]].id; + console.log(messageId); + + let messageChildrenIds = history.messages[messageId].childrenIds; + + while (messageChildrenIds.length !== 0) { + messageId = messageChildrenIds.at(-1); + messageChildrenIds = history.messages[messageId].childrenIds; + } + + history.currentId = messageId; + + dispatch('change'); + }; + onMount(async () => { await tick(); currentMessageId = messages[messageIdx].id; @@ -97,42 +135,8 @@ isLastMessage={true} {updateChatMessages} {confirmEditResponseMessage} - showPreviousMessage={() => { - groupedMessagesIdx[model] = Math.max(0, groupedMessagesIdx[model] - 1); - let messageId = groupedMessages[model].messages[groupedMessagesIdx[model]].id; - - console.log(messageId); - let messageChildrenIds = history.messages[messageId].childrenIds; - - while (messageChildrenIds.length !== 0) { - messageId = messageChildrenIds.at(-1); - messageChildrenIds = history.messages[messageId].childrenIds; - } - - history.currentId = messageId; - - dispatch('change'); - }} - showNextMessage={() => { - groupedMessagesIdx[model] = Math.min( - groupedMessages[model].messages.length - 1, - groupedMessagesIdx[model] + 1 - ); - - let messageId = groupedMessages[model].messages[groupedMessagesIdx[model]].id; - console.log(messageId); - - let messageChildrenIds = history.messages[messageId].childrenIds; - - while (messageChildrenIds.length !== 0) { - messageId = messageChildrenIds.at(-1); - messageChildrenIds = history.messages[messageId].childrenIds; - } - - history.currentId = messageId; - - dispatch('change'); - }} + showPreviousMessage={() => showPreviousMessage(model)} + showNextMessage={() => showNextMessage(model)} {rateMessage} {copyToClipboard} {continueGeneration} diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index cd40d7610..d5642c926 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -257,26 +257,6 @@ let userContext = null; - if ($settings?.memory ?? false) { - const res = await queryMemory(localStorage.token, prompt).catch((error) => { - toast.error(error); - return null; - }); - - if (res) { - if (res.documents[0].length > 0) { - userContext = res.documents.reduce((acc, doc, index) => { - const createdAtTimestamp = res.metadatas[index][0].created_at; - const createdAtDate = new Date(createdAtTimestamp * 1000).toISOString().split('T')[0]; - acc.push(`${index + 1}. [${createdAtDate}]. ${doc[0]}`); - return acc; - }, []); - } - - console.log(userContext); - } - } - await Promise.all( (modelId ? [modelId] : atSelectedModel !== '' ? [atSelectedModel.id] : selectedModels).map( async (modelId) => { @@ -309,6 +289,34 @@ ]; } + await tick(); + + if ($settings?.memory ?? false) { + if (userContext === null) { + const res = await queryMemory(localStorage.token, prompt).catch((error) => { + toast.error(error); + return null; + }); + + if (res) { + if (res.documents[0].length > 0) { + userContext = res.documents.reduce((acc, doc, index) => { + const createdAtTimestamp = res.metadatas[index][0].created_at; + const createdAtDate = new Date(createdAtTimestamp * 1000) + .toISOString() + .split('T')[0]; + acc.push(`${index + 1}. [${createdAtDate}]. ${doc[0]}`); + return acc; + }, []); + } + + console.log(userContext); + } + } + } + + responseMessage.userContext = userContext; + if (model?.external) { await sendPromptOpenAI(model, prompt, responseMessageId, _chatId); } else if (model) { diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index e60fd93cf..8088a4bb0 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -263,26 +263,6 @@ let userContext = null; - if ($settings?.memory ?? false) { - const res = await queryMemory(localStorage.token, prompt).catch((error) => { - toast.error(error); - return null; - }); - - if (res) { - if (res.documents[0].length > 0) { - userContext = res.documents.reduce((acc, doc, index) => { - const createdAtTimestamp = res.metadatas[index][0].created_at; - const createdAtDate = new Date(createdAtTimestamp * 1000).toISOString().split('T')[0]; - acc.push(`${index + 1}. [${createdAtDate}]. ${doc[0]}`); - return acc; - }, []); - } - - console.log(userContext); - } - } - await Promise.all( (modelId ? [modelId] : atSelectedModel !== '' ? [atSelectedModel.id] : selectedModels).map( async (modelId) => { @@ -315,6 +295,34 @@ ]; } + await tick(); + + if ($settings?.memory ?? false) { + if (userContext === null) { + const res = await queryMemory(localStorage.token, prompt).catch((error) => { + toast.error(error); + return null; + }); + + if (res) { + if (res.documents[0].length > 0) { + userContext = res.documents.reduce((acc, doc, index) => { + const createdAtTimestamp = res.metadatas[index][0].created_at; + const createdAtDate = new Date(createdAtTimestamp * 1000) + .toISOString() + .split('T')[0]; + acc.push(`${index + 1}. [${createdAtDate}]. ${doc[0]}`); + return acc; + }, []); + } + + console.log(userContext); + } + } + } + + responseMessage.userContext = userContext; + if (model?.external) { await sendPromptOpenAI(model, prompt, responseMessageId, _chatId); } else if (model) { From bcc2bab623496f03468fb2d9ce41fb0b0870363e Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 13:15:41 -0700 Subject: [PATCH 2/9] refac --- src/routes/(app)/+page.svelte | 6 ++---- src/routes/(app)/c/[id]/+page.svelte | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index d5642c926..6b929795f 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -255,8 +255,6 @@ const sendPrompt = async (prompt, parentId, modelId = null) => { const _chatId = JSON.parse(JSON.stringify($chatId)); - let userContext = null; - await Promise.all( (modelId ? [modelId] : atSelectedModel !== '' ? [atSelectedModel.id] : selectedModels).map( async (modelId) => { @@ -273,7 +271,7 @@ role: 'assistant', content: '', model: model.id, - userContext: userContext, + userContext: null, timestamp: Math.floor(Date.now() / 1000) // Unix epoch }; @@ -291,6 +289,7 @@ await tick(); + let userContext = null; if ($settings?.memory ?? false) { if (userContext === null) { const res = await queryMemory(localStorage.token, prompt).catch((error) => { @@ -314,7 +313,6 @@ } } } - responseMessage.userContext = userContext; if (model?.external) { diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 8088a4bb0..8bf3b6d54 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -261,8 +261,6 @@ const sendPrompt = async (prompt, parentId, modelId = null) => { const _chatId = JSON.parse(JSON.stringify($chatId)); - let userContext = null; - await Promise.all( (modelId ? [modelId] : atSelectedModel !== '' ? [atSelectedModel.id] : selectedModels).map( async (modelId) => { @@ -279,7 +277,7 @@ role: 'assistant', content: '', model: model.id, - userContext: userContext, + userContext: null, timestamp: Math.floor(Date.now() / 1000) // Unix epoch }; @@ -297,6 +295,7 @@ await tick(); + let userContext = null; if ($settings?.memory ?? false) { if (userContext === null) { const res = await queryMemory(localStorage.token, prompt).catch((error) => { @@ -320,7 +319,6 @@ } } } - responseMessage.userContext = userContext; if (model?.external) { From ab271c82eeef9e3a820611fad97435f9a0155d9d Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 13:24:46 -0700 Subject: [PATCH 3/9] Update ProfileImage.svelte --- src/lib/components/chat/Messages/ProfileImage.svelte | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages/ProfileImage.svelte b/src/lib/components/chat/Messages/ProfileImage.svelte index 80bdea2c7..44f3b5fce 100644 --- a/src/lib/components/chat/Messages/ProfileImage.svelte +++ b/src/lib/components/chat/Messages/ProfileImage.svelte @@ -10,7 +10,8 @@ crossorigin="anonymous" src={src.startsWith(WEBUI_BASE_URL) || src.startsWith('https://www.gravatar.com/avatar/') || - src.startsWith('data:') + src.startsWith('data:') || + src.startsWith('/') ? src : `/user.png`} class=" w-8 object-cover rounded-full" From b088b0657582d42ddda9b78d9c25868c9a4e8f60 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 15:45:57 -0700 Subject: [PATCH 4/9] refac --- src/lib/components/chat/Messages/CodeBlock.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages/CodeBlock.svelte b/src/lib/components/chat/Messages/CodeBlock.svelte index 4131fbd85..b9f6382ab 100644 --- a/src/lib/components/chat/Messages/CodeBlock.svelte +++ b/src/lib/components/chat/Messages/CodeBlock.svelte @@ -213,7 +213,7 @@ __builtins__.input = input`);
{@html lang}
- {#if ['', 'python'].includes(lang) && (lang === 'python' || checkPythonCode(code))} + {#if lang === 'python' || (lang === '' && checkPythonCode(code))} {#if executing}
Running
{:else} From a600a32f189be15916a3c43f7d43e5338d1d5f71 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 17:53:21 -0700 Subject: [PATCH 5/9] fix --- src/routes/(app)/+page.svelte | 6 +++--- src/routes/(app)/c/[id]/+page.svelte | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 6b929795f..6ef6b7d0e 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -341,12 +341,12 @@ scrollToBottom(); const messagesBody = [ - $settings.system || (responseMessage?.userContext ?? null) + $settings.system || Array.isArray(responseMessage?.userContext ?? null) ? { role: 'system', content: - $settings.system + (responseMessage?.userContext ?? null) - ? `\n\nUser Context:\n${responseMessage.userContext.join('\n')}` + $settings.system + Array.isArray(responseMessage?.userContext ?? null) + ? `\n\nUser Context:\n${userContext.join('\n')}` : '' } : undefined, diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 8bf3b6d54..2da659e72 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -347,12 +347,12 @@ scrollToBottom(); const messagesBody = [ - $settings.system || (responseMessage?.userContext ?? null) + $settings.system || Array.isArray(responseMessage?.userContext ?? null) ? { role: 'system', content: - $settings.system + (responseMessage?.userContext ?? null) - ? `\n\nUser Context:\n${responseMessage.userContext.join('\n')}` + $settings.system + Array.isArray(responseMessage?.userContext ?? null) + ? `\n\nUser Context:\n${userContext.join('\n')}` : '' } : undefined, From 3c55773bb2b4ec0700793edf51b12788c9972caf Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 18:17:18 -0700 Subject: [PATCH 6/9] fix --- src/routes/(app)/+page.svelte | 8 ++++---- src/routes/(app)/c/[id]/+page.svelte | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 6ef6b7d0e..04729b8ec 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -341,12 +341,12 @@ scrollToBottom(); const messagesBody = [ - $settings.system || Array.isArray(responseMessage?.userContext ?? null) + ($settings.system || responseMessage?.userContext) ?? null ? { role: 'system', content: - $settings.system + Array.isArray(responseMessage?.userContext ?? null) - ? `\n\nUser Context:\n${userContext.join('\n')}` + $settings.system + responseMessage?.userContext ?? null + ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' } : undefined, @@ -605,7 +605,7 @@ role: 'system', content: $settings.system + (responseMessage?.userContext ?? null) - ? `\n\nUser Context:\n${responseMessage.userContext.join('\n')}` + ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' } : undefined, diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 2da659e72..449f5c658 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -347,12 +347,12 @@ scrollToBottom(); const messagesBody = [ - $settings.system || Array.isArray(responseMessage?.userContext ?? null) + ($settings.system || responseMessage?.userContext) ?? null ? { role: 'system', content: - $settings.system + Array.isArray(responseMessage?.userContext ?? null) - ? `\n\nUser Context:\n${userContext.join('\n')}` + $settings.system + responseMessage?.userContext ?? null + ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' } : undefined, @@ -611,7 +611,7 @@ role: 'system', content: $settings.system + (responseMessage?.userContext ?? null) - ? `\n\nUser Context:\n${responseMessage.userContext.join('\n')}` + ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' } : undefined, From 056c413e23e1fc149c23231ac46ed86a7cbf3d0c Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 19 May 2024 18:23:22 -0700 Subject: [PATCH 7/9] fix --- src/routes/(app)/+page.svelte | 2 +- src/routes/(app)/c/[id]/+page.svelte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 04729b8ec..66cb37755 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -341,7 +341,7 @@ scrollToBottom(); const messagesBody = [ - ($settings.system || responseMessage?.userContext) ?? null + $settings.system || (responseMessage?.userContext ?? null) ? { role: 'system', content: diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 449f5c658..91d6098cd 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -347,7 +347,7 @@ scrollToBottom(); const messagesBody = [ - ($settings.system || responseMessage?.userContext) ?? null + $settings.system || (responseMessage?.userContext ?? null) ? { role: 'system', content: From 322db31dc9d40a54aeb9f98cd3b818a6ad08b8be Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 20 May 2024 07:22:43 -0700 Subject: [PATCH 8/9] fix: rag --- backend/apps/rag/main.py | 6 ++++++ backend/requirements.txt | 1 + src/lib/constants.ts | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index 9a1a0c13e..f08d81a3b 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -28,6 +28,7 @@ from langchain_community.document_loaders import ( UnstructuredXMLLoader, UnstructuredRSTLoader, UnstructuredExcelLoader, + UnstructuredPowerPointLoader, YoutubeLoader, ) from langchain.text_splitter import RecursiveCharacterTextSplitter @@ -768,6 +769,11 @@ def get_loader(filename: str, file_content_type: str, file_path: str): "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ] or file_ext in ["xls", "xlsx"]: loader = UnstructuredExcelLoader(file_path) + elif file_content_type in [ + "application/vnd.ms-powerpoint", + "application/vnd.openxmlformats-officedocument.presentationml.presentation", + ] or file_ext in ["ppt", "pptx"]: + loader = UnstructuredPowerPointLoader(file_path) elif file_ext in known_source_ext or ( file_content_type and file_content_type.find("text/") >= 0 ): diff --git a/backend/requirements.txt b/backend/requirements.txt index c8b699447..a82da1966 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -35,6 +35,7 @@ chromadb==0.4.24 sentence-transformers==2.7.0 pypdf==4.2.0 docx2txt==0.8 +python-pptx==0.6.23 unstructured==0.11.8 Markdown==3.6 pypandoc==1.13 diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 62ac2b8c3..a305bea7c 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -86,7 +86,9 @@ export const SUPPORTED_FILE_EXTENSIONS = [ 'csv', 'txt', 'xls', - 'xlsx' + 'xlsx', + 'pptx', + 'ppt' ]; // Source: https://kit.svelte.dev/docs/modules#$env-static-public From 54677a33747209e1e181e3ff228aae1d051a0760 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Mon, 20 May 2024 10:10:35 -0700 Subject: [PATCH 9/9] fix --- src/routes/(app)/+page.svelte | 10 ++++++---- src/routes/(app)/c/[id]/+page.svelte | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index 66cb37755..b54deab43 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -344,10 +344,11 @@ $settings.system || (responseMessage?.userContext ?? null) ? { role: 'system', - content: - $settings.system + responseMessage?.userContext ?? null + content: `${$settings?.system ?? ''}${ + responseMessage?.userContext ?? null ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' + }` } : undefined, ...messages @@ -603,10 +604,11 @@ $settings.system || (responseMessage?.userContext ?? null) ? { role: 'system', - content: - $settings.system + (responseMessage?.userContext ?? null) + content: `${$settings?.system ?? ''}${ + responseMessage?.userContext ?? null ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' + }` } : undefined, ...messages diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 91d6098cd..668cbba3e 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -350,10 +350,11 @@ $settings.system || (responseMessage?.userContext ?? null) ? { role: 'system', - content: - $settings.system + responseMessage?.userContext ?? null + content: `${$settings?.system ?? ''}${ + responseMessage?.userContext ?? null ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' + }` } : undefined, ...messages @@ -609,10 +610,11 @@ $settings.system || (responseMessage?.userContext ?? null) ? { role: 'system', - content: - $settings.system + (responseMessage?.userContext ?? null) + content: `${$settings?.system ?? ''}${ + responseMessage?.userContext ?? null ? `\n\nUser Context:\n${(responseMessage?.userContext ?? []).join('\n')}` : '' + }` } : undefined, ...messages