forked from open-webui/open-webui
		
	feat: web rag support
This commit is contained in:
		
							parent
							
								
									5e672d9f79
								
							
						
					
					
						commit
						28226a6f97
					
				
					 5 changed files with 131 additions and 33 deletions
				
			
		| 
						 | 
				
			
			@ -6,7 +6,7 @@
 | 
			
		|||
 | 
			
		||||
	import Prompts from './MessageInput/PromptCommands.svelte';
 | 
			
		||||
	import Suggestions from './MessageInput/Suggestions.svelte';
 | 
			
		||||
	import { uploadDocToVectorDB } from '$lib/apis/rag';
 | 
			
		||||
	import { uploadDocToVectorDB, uploadWebToVectorDB } from '$lib/apis/rag';
 | 
			
		||||
	import AddFilesPlaceholder from '../AddFilesPlaceholder.svelte';
 | 
			
		||||
	import { SUPPORTED_FILE_TYPE, SUPPORTED_FILE_EXTENSIONS } from '$lib/constants';
 | 
			
		||||
	import Documents from './MessageInput/Documents.svelte';
 | 
			
		||||
| 
						 | 
				
			
			@ -137,6 +137,33 @@
 | 
			
		|||
		}
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const uploadWeb = async (url) => {
 | 
			
		||||
		console.log(url);
 | 
			
		||||
 | 
			
		||||
		const doc = {
 | 
			
		||||
			type: 'doc',
 | 
			
		||||
			name: url,
 | 
			
		||||
			collection_name: '',
 | 
			
		||||
			upload_status: false,
 | 
			
		||||
			error: ''
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
		try {
 | 
			
		||||
			files = [...files, doc];
 | 
			
		||||
			const res = await uploadWebToVectorDB(localStorage.token, '', url);
 | 
			
		||||
 | 
			
		||||
			if (res) {
 | 
			
		||||
				doc.upload_status = true;
 | 
			
		||||
				doc.collection_name = res.collection_name;
 | 
			
		||||
				files = files;
 | 
			
		||||
			}
 | 
			
		||||
		} catch (e) {
 | 
			
		||||
			// Remove the failed doc from the files array
 | 
			
		||||
			files = files.filter((f) => f.name !== url);
 | 
			
		||||
			toast.error(e);
 | 
			
		||||
		}
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	onMount(() => {
 | 
			
		||||
		const dropZone = document.querySelector('body');
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -258,6 +285,10 @@
 | 
			
		|||
					<Documents
 | 
			
		||||
						bind:this={documentsElement}
 | 
			
		||||
						bind:prompt
 | 
			
		||||
						on:url={(e) => {
 | 
			
		||||
							console.log(e);
 | 
			
		||||
							uploadWeb(e.detail);
 | 
			
		||||
						}}
 | 
			
		||||
						on:select={(e) => {
 | 
			
		||||
							console.log(e);
 | 
			
		||||
							files = [
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
	import { createEventDispatcher } from 'svelte';
 | 
			
		||||
 | 
			
		||||
	import { documents } from '$lib/stores';
 | 
			
		||||
	import { removeFirstHashWord } from '$lib/utils';
 | 
			
		||||
	import { removeFirstHashWord, isValidHttpUrl } from '$lib/utils';
 | 
			
		||||
	import { tick } from 'svelte';
 | 
			
		||||
 | 
			
		||||
	export let prompt = '';
 | 
			
		||||
| 
						 | 
				
			
			@ -37,9 +37,20 @@
 | 
			
		|||
		chatInputElement?.focus();
 | 
			
		||||
		await tick();
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const confirmSelectWeb = async (url) => {
 | 
			
		||||
		dispatch('url', url);
 | 
			
		||||
 | 
			
		||||
		prompt = removeFirstHashWord(prompt);
 | 
			
		||||
		const chatInputElement = document.getElementById('chat-textarea');
 | 
			
		||||
 | 
			
		||||
		await tick();
 | 
			
		||||
		chatInputElement?.focus();
 | 
			
		||||
		await tick();
 | 
			
		||||
	};
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
{#if filteredDocs.length > 0}
 | 
			
		||||
{#if filteredDocs.length > 0 || prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
 | 
			
		||||
	<div class="md:px-2 mb-3 text-left w-full">
 | 
			
		||||
		<div class="flex w-full rounded-lg border border-gray-100 dark:border-gray-700">
 | 
			
		||||
			<div class=" bg-gray-100 dark:bg-gray-700 w-10 rounded-l-lg text-center">
 | 
			
		||||
| 
						 | 
				
			
			@ -55,6 +66,7 @@
 | 
			
		|||
								: ''}"
 | 
			
		||||
							type="button"
 | 
			
		||||
							on:click={() => {
 | 
			
		||||
								console.log(doc);
 | 
			
		||||
								confirmSelect(doc);
 | 
			
		||||
							}}
 | 
			
		||||
							on:mousemove={() => {
 | 
			
		||||
| 
						 | 
				
			
			@ -71,6 +83,25 @@
 | 
			
		|||
							</div>
 | 
			
		||||
						</button>
 | 
			
		||||
					{/each}
 | 
			
		||||
 | 
			
		||||
					{#if prompt.split(' ')?.at(0)?.substring(1).startsWith('http')}
 | 
			
		||||
						<button
 | 
			
		||||
							class="px-3 py-1.5 rounded-lg w-full text-left bg-gray-100 selected-command-option-button"
 | 
			
		||||
							type="button"
 | 
			
		||||
							on:click={() => {
 | 
			
		||||
								const url = prompt.split(' ')?.at(0)?.substring(1);
 | 
			
		||||
								if (isValidHttpUrl(url)) {
 | 
			
		||||
									confirmSelectWeb(url);
 | 
			
		||||
								}
 | 
			
		||||
							}}
 | 
			
		||||
						>
 | 
			
		||||
							<div class=" font-medium text-black line-clamp-1">
 | 
			
		||||
								{prompt.split(' ')?.at(0)?.substring(1)}
 | 
			
		||||
							</div>
 | 
			
		||||
 | 
			
		||||
							<div class=" text-xs text-gray-600 line-clamp-1">Web</div>
 | 
			
		||||
						</button>
 | 
			
		||||
					{/if}
 | 
			
		||||
				</div>
 | 
			
		||||
			</div>
 | 
			
		||||
		</div>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue