From 6a21a77ee998981a455ab6725cde4b8e8c8d2358 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 27 Aug 2024 17:05:24 +0200 Subject: [PATCH] refac --- backend/apps/rag/main.py | 12 ++- backend/main.py | 4 + src/lib/components/admin/Settings.svelte | 5 +- .../admin/Settings/Documents.svelte | 86 ++++++++++++------- src/lib/components/chat/Chat.svelte | 21 +++++ src/lib/components/chat/MessageInput.svelte | 15 +++- 6 files changed, 107 insertions(+), 36 deletions(-) diff --git a/backend/apps/rag/main.py b/backend/apps/rag/main.py index 3634481bd..870721d38 100644 --- a/backend/apps/rag/main.py +++ b/backend/apps/rag/main.py @@ -434,6 +434,11 @@ async def get_rag_config(user=Depends(get_admin_user)): } +class FileConfig(BaseModel): + max_size: Optional[int] = None + max_count: Optional[int] = None + + class ContentExtractionConfig(BaseModel): engine: str = "" tika_server_url: Optional[str] = None @@ -472,6 +477,7 @@ class WebConfig(BaseModel): class ConfigUpdateForm(BaseModel): pdf_extract_images: Optional[bool] = None + file: Optional[FileConfig] = None content_extraction: Optional[ContentExtractionConfig] = None chunk: Optional[ChunkParamUpdateForm] = None youtube: Optional[YoutubeLoaderConfig] = None @@ -486,6 +492,10 @@ async def update_rag_config(form_data: ConfigUpdateForm, user=Depends(get_admin_ else app.state.config.PDF_EXTRACT_IMAGES ) + if form_data.file is not None: + app.state.config.FILE_MAX_SIZE = form_data.file.max_size + app.state.config.FILE_MAX_COUNT = form_data.file.max_count + if form_data.content_extraction is not None: log.info(f"Updating text settings: {form_data.content_extraction}") app.state.config.CONTENT_EXTRACTION_ENGINE = form_data.content_extraction.engine @@ -526,11 +536,11 @@ async def update_rag_config(form_data: ConfigUpdateForm, user=Depends(get_admin_ return { "status": True, + "pdf_extract_images": app.state.config.PDF_EXTRACT_IMAGES, "file": { "max_size": app.state.config.FILE_MAX_SIZE, "max_count": app.state.config.FILE_MAX_COUNT, }, - "pdf_extract_images": app.state.config.PDF_EXTRACT_IMAGES, "content_extraction": { "engine": app.state.config.CONTENT_EXTRACTION_ENGINE, "tika_server_url": app.state.config.TIKA_SERVER_URL, diff --git a/backend/main.py b/backend/main.py index c4a9802a4..4b91cdc84 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1939,6 +1939,10 @@ async def get_app_config(request: Request): "engine": audio_app.state.config.STT_ENGINE, }, }, + "file": { + "max_size": rag_app.state.config.FILE_MAX_SIZE, + "max_count": rag_app.state.config.FILE_MAX_COUNT, + }, "permissions": {**webui_app.state.config.USER_PERMISSIONS}, } if user is not None diff --git a/src/lib/components/admin/Settings.svelte b/src/lib/components/admin/Settings.svelte index e242ab632..e02b79419 100644 --- a/src/lib/components/admin/Settings.svelte +++ b/src/lib/components/admin/Settings.svelte @@ -359,8 +359,11 @@ {:else if selectedTab === 'documents'} { + on:save={async () => { toast.success($i18n.t('Settings saved successfully!')); + + await tick(); + await config.set(await getBackendConfig()); }} /> {:else if selectedTab === 'web'} diff --git a/src/lib/components/admin/Settings/Documents.svelte b/src/lib/components/admin/Settings/Documents.svelte index 3d42032fd..7f935a08a 100644 --- a/src/lib/components/admin/Settings/Documents.svelte +++ b/src/lib/components/admin/Settings/Documents.svelte @@ -1,4 +1,8 @@ @@ -271,7 +278,6 @@ class="flex flex-col h-full justify-between space-y-3 text-sm" on:submit|preventDefault={() => { submitHandler(); - saveHandler(); }} >
@@ -622,36 +628,50 @@
{$i18n.t('Files')}
-
-
- {$i18n.t('Max Upload Count')} -
-
- -
-
-
{$i18n.t('Max Upload Size')}
- + + + +
+
+ +
+
+ {$i18n.t('Max Upload Count')} +
+
+ + +
diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 081415bc8..1de56c585 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -542,6 +542,27 @@ `Oops! Hold tight! Your files are still in the processing oven. We're cooking them up to perfection. Please be patient and we'll let you know once they're ready.` ) ); + } else if ( + ($config?.file?.max_count ?? null) !== null && + files.length + chatFiles.length > $config?.file?.max_count + ) { + console.log(chatFiles.length, files.length); + toast.error( + $i18n.t(`You can only chat with a maximum of {{maxCount}} file(s) at a time.`, { + maxCount: $config?.file?.max_count + }) + ); + } else if ( + ($config?.file?.max_size ?? null) !== null && + [...files, ...chatFiles].some( + (file) => file.size > ($config?.file?.max_size ?? 0) * 1024 * 1024 + ) + ) { + toast.error( + $i18n.t(`File size should not exceed {{maxSize}} MB.`, { + maxSize: $config?.file?.max_size + }) + ); } else { // Reset chat input textarea const chatTextAreaElement = document.getElementById('chat-textarea'); diff --git a/src/lib/components/chat/MessageInput.svelte b/src/lib/components/chat/MessageInput.svelte index 69c02d793..76f80f364 100644 --- a/src/lib/components/chat/MessageInput.svelte +++ b/src/lib/components/chat/MessageInput.svelte @@ -173,6 +173,20 @@ const inputFilesHandler = async (inputFiles) => { inputFiles.forEach((file) => { + console.log(file, file.name.split('.').at(-1)); + + if ( + ($config?.file?.max_size ?? null) !== null && + file.size > ($config?.file?.max_size ?? 0) * 1024 * 1024 + ) { + toast.error( + $i18n.t(`File size should not exceed {{maxSize}} MB.`, { + maxSize: $config?.file?.max_size + }) + ); + return; + } + if (['image/gif', 'image/webp', 'image/jpeg', 'image/png'].includes(file['type'])) { if (visionCapableModels.length === 0) { toast.error($i18n.t('Selected model(s) do not support image inputs')); @@ -222,7 +236,6 @@ if (e.dataTransfer?.files) { const inputFiles = Array.from(e.dataTransfer?.files); - console.log(file, file.name.split('.').at(-1)); if (inputFiles && inputFiles.length > 0) { console.log(inputFiles); inputFilesHandler(inputFiles);