forked from open-webui/open-webui
		
	Update +page.svelte
This commit is contained in:
		
							parent
							
								
									34e0f64fb3
								
							
						
					
					
						commit
						9b2f39ba20
					
				
					 1 changed files with 118 additions and 76 deletions
				
			
		|  | @ -132,8 +132,108 @@ | ||||||
| 	// Ollama functions | 	// Ollama functions | ||||||
| 	////////////////////////// | 	////////////////////////// | ||||||
| 
 | 
 | ||||||
|  | 	const submitPrompt = async (userPrompt) => { | ||||||
|  | 		console.log('submitPrompt', $chatId); | ||||||
|  | 
 | ||||||
|  | 		if (selectedModels.includes('')) { | ||||||
|  | 			toast.error('Model not selected'); | ||||||
|  | 		} else if (messages.length != 0 && messages.at(-1).done != true) { | ||||||
|  | 			// Response not done | ||||||
|  | 			console.log('wait'); | ||||||
|  | 		} else { | ||||||
|  | 			// Reset chat message textarea height | ||||||
|  | 			document.getElementById('chat-textarea').style.height = ''; | ||||||
|  | 
 | ||||||
|  | 			// Create user message | ||||||
|  | 			let userMessageId = uuidv4(); | ||||||
|  | 			let userMessage = { | ||||||
|  | 				id: userMessageId, | ||||||
|  | 				parentId: messages.length !== 0 ? messages.at(-1).id : null, | ||||||
|  | 				childrenIds: [], | ||||||
|  | 				role: 'user', | ||||||
|  | 				content: userPrompt, | ||||||
|  | 				files: files.length > 0 ? files : undefined | ||||||
|  | 			}; | ||||||
|  | 
 | ||||||
|  | 			// Add message to history and Set currentId to messageId | ||||||
|  | 			history.messages[userMessageId] = userMessage; | ||||||
|  | 			history.currentId = userMessageId; | ||||||
|  | 
 | ||||||
|  | 			// Append messageId to childrenIds of parent message | ||||||
|  | 			if (messages.length !== 0) { | ||||||
|  | 				history.messages[messages.at(-1).id].childrenIds.push(userMessageId); | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			// 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: 'New Chat', | ||||||
|  | 						models: selectedModels, | ||||||
|  | 						system: $settings.system ?? undefined, | ||||||
|  | 						options: { | ||||||
|  | 							...($settings.options ?? {}) | ||||||
|  | 						}, | ||||||
|  | 						messages: messages, | ||||||
|  | 						history: history, | ||||||
|  | 						timestamp: Date.now() | ||||||
|  | 					}); | ||||||
|  | 					await chats.set(await getChatList(localStorage.token)); | ||||||
|  | 					await chatId.set(chat.id); | ||||||
|  | 				} else { | ||||||
|  | 					await chatId.set('local'); | ||||||
|  | 				} | ||||||
|  | 				await tick(); | ||||||
|  | 			} | ||||||
|  | 
 | ||||||
|  | 			// Reset chat input textarea | ||||||
|  | 			prompt = ''; | ||||||
|  | 			files = []; | ||||||
|  | 
 | ||||||
|  | 			// Send prompt | ||||||
|  | 			await sendPrompt(userPrompt, userMessageId); | ||||||
|  | 		} | ||||||
|  | 	}; | ||||||
|  | 
 | ||||||
| 	const sendPrompt = async (prompt, parentId) => { | 	const sendPrompt = async (prompt, parentId) => { | ||||||
| 		const _chatId = JSON.parse(JSON.stringify($chatId)); | 		const _chatId = JSON.parse(JSON.stringify($chatId)); | ||||||
|  | 
 | ||||||
|  | 		const docs = messages | ||||||
|  | 			.filter((message) => message?.files ?? null) | ||||||
|  | 			.map((message) => message.files.filter((item) => item.type === 'doc')) | ||||||
|  | 			.flat(1); | ||||||
|  | 
 | ||||||
|  | 		console.log(docs); | ||||||
|  | 		if (docs.length > 0) { | ||||||
|  | 			const query = history.messages[parentId].content; | ||||||
|  | 
 | ||||||
|  | 			let relevantContexts = await Promise.all( | ||||||
|  | 				docs.map(async (doc) => { | ||||||
|  | 					return await queryVectorDB(localStorage.token, doc.collection_name, query, 4).catch( | ||||||
|  | 						(error) => { | ||||||
|  | 							console.log(error); | ||||||
|  | 							return null; | ||||||
|  | 						} | ||||||
|  | 					); | ||||||
|  | 				}) | ||||||
|  | 			); | ||||||
|  | 			relevantContexts = relevantContexts.filter((context) => context); | ||||||
|  | 
 | ||||||
|  | 			const contextString = relevantContexts.reduce((a, context, i, arr) => { | ||||||
|  | 				return `${a}${context.documents.join(' ')}\n`; | ||||||
|  | 			}, ''); | ||||||
|  | 
 | ||||||
|  | 			console.log(contextString); | ||||||
|  | 
 | ||||||
|  | 			history.messages[parentId].raContent = RAGTemplate(contextString, query); | ||||||
|  | 			history.messages[parentId].contexts = relevantContexts; | ||||||
|  | 			await tick(); | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
| 		await Promise.all( | 		await Promise.all( | ||||||
| 			selectedModels.map(async (model) => { | 			selectedModels.map(async (model) => { | ||||||
| 				console.log(model); | 				console.log(model); | ||||||
|  | @ -151,6 +251,7 @@ | ||||||
| 
 | 
 | ||||||
| 		await chats.set(await getChatList(localStorage.token)); | 		await chats.set(await getChatList(localStorage.token)); | ||||||
| 	}; | 	}; | ||||||
|  | 
 | ||||||
| 	const sendPromptOllama = async (model, userPrompt, parentId, _chatId) => { | 	const sendPromptOllama = async (model, userPrompt, parentId, _chatId) => { | ||||||
| 		// Create response message | 		// Create response message | ||||||
| 		let responseMessageId = uuidv4(); | 		let responseMessageId = uuidv4(); | ||||||
|  | @ -195,7 +296,7 @@ | ||||||
| 				.filter((message) => message) | 				.filter((message) => message) | ||||||
| 				.map((message) => ({ | 				.map((message) => ({ | ||||||
| 					role: message.role, | 					role: message.role, | ||||||
| 					content: message.content, | 					content: message?.raContent ?? message.content, | ||||||
| 					...(message.files && { | 					...(message.files && { | ||||||
| 						images: message.files | 						images: message.files | ||||||
| 							.filter((file) => file.type === 'image') | 							.filter((file) => file.type === 'image') | ||||||
|  | @ -298,12 +399,14 @@ | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			if ($chatId == _chatId) { | 			if ($chatId == _chatId) { | ||||||
|  | 				if ($settings.saveChatHistory ?? true) { | ||||||
| 					chat = await updateChatById(localStorage.token, _chatId, { | 					chat = await updateChatById(localStorage.token, _chatId, { | ||||||
| 						messages: messages, | 						messages: messages, | ||||||
| 						history: history | 						history: history | ||||||
| 					}); | 					}); | ||||||
| 					await chats.set(await getChatList(localStorage.token)); | 					await chats.set(await getChatList(localStorage.token)); | ||||||
| 				} | 				} | ||||||
|  | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			if (res !== null) { | 			if (res !== null) { | ||||||
| 				const error = await res.json(); | 				const error = await res.json(); | ||||||
|  | @ -382,7 +485,7 @@ | ||||||
| 								content: [ | 								content: [ | ||||||
| 									{ | 									{ | ||||||
| 										type: 'text', | 										type: 'text', | ||||||
| 										text: message.content | 										text: message?.raContent ?? message.content | ||||||
| 									}, | 									}, | ||||||
| 									...message.files | 									...message.files | ||||||
| 										.filter((file) => file.type === 'image') | 										.filter((file) => file.type === 'image') | ||||||
|  | @ -394,7 +497,7 @@ | ||||||
| 										})) | 										})) | ||||||
| 								] | 								] | ||||||
| 						  } | 						  } | ||||||
| 						: { content: message.content }) | 						: { content: message?.raContent ?? message.content }) | ||||||
| 				})), | 				})), | ||||||
| 			seed: $settings?.options?.seed ?? undefined, | 			seed: $settings?.options?.seed ?? undefined, | ||||||
| 			stop: $settings?.options?.stop ?? undefined, | 			stop: $settings?.options?.stop ?? undefined, | ||||||
|  | @ -462,12 +565,14 @@ | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			if ($chatId == _chatId) { | 			if ($chatId == _chatId) { | ||||||
|  | 				if ($settings.saveChatHistory ?? true) { | ||||||
| 					chat = await updateChatById(localStorage.token, _chatId, { | 					chat = await updateChatById(localStorage.token, _chatId, { | ||||||
| 						messages: messages, | 						messages: messages, | ||||||
| 						history: history | 						history: history | ||||||
| 					}); | 					}); | ||||||
| 					await chats.set(await getChatList(localStorage.token)); | 					await chats.set(await getChatList(localStorage.token)); | ||||||
| 				} | 				} | ||||||
|  | 			} | ||||||
| 		} else { | 		} else { | ||||||
| 			if (res !== null) { | 			if (res !== null) { | ||||||
| 				const error = await res.json(); | 				const error = await res.json(); | ||||||
|  | @ -508,69 +613,6 @@ | ||||||
| 		} | 		} | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 	const submitPrompt = async (userPrompt) => { |  | ||||||
| 		console.log('submitPrompt', $chatId); |  | ||||||
| 
 |  | ||||||
| 		if (selectedModels.includes('')) { |  | ||||||
| 			toast.error('Model not selected'); |  | ||||||
| 		} else if (messages.length != 0 && messages.at(-1).done != true) { |  | ||||||
| 			// Response not done |  | ||||||
| 			console.log('wait'); |  | ||||||
| 		} else { |  | ||||||
| 			// Reset chat message textarea height |  | ||||||
| 			document.getElementById('chat-textarea').style.height = ''; |  | ||||||
| 
 |  | ||||||
| 			// Create user message |  | ||||||
| 			let userMessageId = uuidv4(); |  | ||||||
| 			let userMessage = { |  | ||||||
| 				id: userMessageId, |  | ||||||
| 				parentId: messages.length !== 0 ? messages.at(-1).id : null, |  | ||||||
| 				childrenIds: [], |  | ||||||
| 				role: 'user', |  | ||||||
| 				content: userPrompt, |  | ||||||
| 				files: files.length > 0 ? files : undefined |  | ||||||
| 			}; |  | ||||||
| 
 |  | ||||||
| 			// Add message to history and Set currentId to messageId |  | ||||||
| 			history.messages[userMessageId] = userMessage; |  | ||||||
| 			history.currentId = userMessageId; |  | ||||||
| 
 |  | ||||||
| 			// Append messageId to childrenIds of parent message |  | ||||||
| 			if (messages.length !== 0) { |  | ||||||
| 				history.messages[messages.at(-1).id].childrenIds.push(userMessageId); |  | ||||||
| 			} |  | ||||||
| 
 |  | ||||||
| 			// Wait until history/message have been updated |  | ||||||
| 			await tick(); |  | ||||||
| 
 |  | ||||||
| 			// Create new chat if only one message in messages |  | ||||||
| 			if (messages.length == 1) { |  | ||||||
| 				chat = await createNewChat(localStorage.token, { |  | ||||||
| 					id: $chatId, |  | ||||||
| 					title: 'New Chat', |  | ||||||
| 					models: selectedModels, |  | ||||||
| 					system: $settings.system ?? undefined, |  | ||||||
| 					options: { |  | ||||||
| 						...($settings.options ?? {}) |  | ||||||
| 					}, |  | ||||||
| 					messages: messages, |  | ||||||
| 					history: history, |  | ||||||
| 					timestamp: Date.now() |  | ||||||
| 				}); |  | ||||||
| 				await chats.set(await getChatList(localStorage.token)); |  | ||||||
| 				await chatId.set(chat.id); |  | ||||||
| 				await tick(); |  | ||||||
| 			} |  | ||||||
| 
 |  | ||||||
| 			// Reset chat input textarea |  | ||||||
| 			prompt = ''; |  | ||||||
| 			files = []; |  | ||||||
| 
 |  | ||||||
| 			// Send prompt |  | ||||||
| 			await sendPrompt(userPrompt, userMessageId); |  | ||||||
| 		} |  | ||||||
| 	}; |  | ||||||
| 
 |  | ||||||
| 	const stopResponse = () => { | 	const stopResponse = () => { | ||||||
| 		stopResponseFlag = true; | 		stopResponseFlag = true; | ||||||
| 		console.log('stopResponse'); | 		console.log('stopResponse'); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Timothy J. Baek
						Timothy J. Baek