From fef4725d569efd753bc0b8d216f124bc64c42f3d Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sun, 7 Jan 2024 00:57:10 -0800 Subject: [PATCH] feat: frontend file upload support --- backend/apps/rag/main.py | 4 +- src/lib/apis/rag/index.ts | 3 - src/lib/components/chat/MessageInput.svelte | 77 +++++++-- src/lib/utils/index.ts | 35 ++++ src/lib/utils/rag/index.ts | 20 +++ src/routes/(app)/+page.svelte | 176 ++++++++++++-------- 6 files changed, 223 insertions(+), 92 deletions(-) create mode 100644 src/lib/utils/rag/index.ts diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index 67b118cd..0e7f9b07 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -91,7 +91,7 @@ def store_web(form_data: StoreWebForm): loader = WebBaseLoader(form_data.url) data = loader.load() store_data_in_vector_db(data, form_data.collection_name) - return {"status": True} + return {"status": True, "collection_name": form_data.collection_name} except Exception as e: print(e) raise HTTPException( @@ -129,7 +129,7 @@ def store_doc(collection_name: str = Form(...), file: UploadFile = File(...)): data = loader.load() store_data_in_vector_db(data, collection_name) - return {"status": True} + return {"status": True, "collection_name": collection_name} except Exception as e: print(e) raise HTTPException( diff --git a/src/lib/apis/rag/index.ts b/src/lib/apis/rag/index.ts index 44ac0430..bafd0360 100644 --- a/src/lib/apis/rag/index.ts +++ b/src/lib/apis/rag/index.ts @@ -11,7 +11,6 @@ export const uploadDocToVectorDB = async (token: string, collection_name: string method: 'POST', headers: { Accept: 'application/json', - 'Content-Type': 'application/json', authorization: `Bearer ${token}` }, body: data @@ -85,7 +84,6 @@ export const queryVectorDB = async ( method: 'GET', headers: { Accept: 'application/json', - 'Content-Type': 'application/json', authorization: `Bearer ${token}` } } @@ -96,7 +94,6 @@ export const queryVectorDB = async ( }) .catch((err) => { error = err.detail; - console.log(err); return null; }); diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 1468310d..4cce1fcb 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -2,10 +2,11 @@ import toast from 'svelte-french-toast'; import { onMount, tick } from 'svelte'; import { settings } from '$lib/stores'; - import { findWordIndices } from '$lib/utils'; + import { calculateSHA256, findWordIndices } from '$lib/utils'; import Prompts from './MessageInput/PromptCommands.svelte'; import Suggestions from './MessageInput/Suggestions.svelte'; + import { uploadDocToVectorDB } from '$lib/apis/rag'; export let submitPrompt: Function; export let stopResponse: Function; @@ -98,7 +99,7 @@ dragged = true; }); - dropZone.addEventListener('drop', (e) => { + dropZone.addEventListener('drop', async (e) => { e.preventDefault(); console.log(e); @@ -115,14 +116,30 @@ ]; }; - if ( - e.dataTransfer?.files && - e.dataTransfer?.files.length > 0 && - ['image/gif', 'image/jpeg', 'image/png'].includes(e.dataTransfer?.files[0]['type']) - ) { - reader.readAsDataURL(e.dataTransfer?.files[0]); + if (e.dataTransfer?.files && e.dataTransfer?.files.length > 0) { + const file = e.dataTransfer?.files[0]; + if (['image/gif', 'image/jpeg', 'image/png'].includes(file['type'])) { + reader.readAsDataURL(file); + } else if (['application/pdf', 'text/plain'].includes(file['type'])) { + console.log(file); + const hash = await calculateSHA256(file); + // const res = uploadDocToVectorDB(localStorage.token, hash,file); + + if (true) { + files = [ + ...files, + { + type: 'doc', + name: file.name, + collection_name: hash + } + ]; + } + } else { + toast.error(`Unsupported File Type '${file['type']}'.`); + } } else { - toast.error(`Unsupported File Type '${e.dataTransfer?.files[0]['type']}'.`); + toast.error(`File not found.`); } } @@ -145,11 +162,11 @@
-
🏞️
-
Add Images
+
🗂️
+
Add Files
- Drop any images here to add to the conversation + Drop any files/images here to add to the conversation
@@ -237,10 +254,42 @@ }} > {#if files.length > 0} -
+
{#each files as file, fileIdx}
- input + {#if file.type === 'image'} + input + {:else if file.type === 'doc'} +
+
+ + + + +
+ +
+
+ {file.name} +
+ +
Document
+
+
+ {/if}