document.addEventListener('DOMContentLoaded', () => { const chatContainer = document.getElementById('chat-container'); const userInput = document.getElementById('user-input'); const sendButton = document.getElementById('send-button'); // Function to add a message to the chat function addMessage(content, isUser = false) { const messageDiv = document.createElement('div'); messageDiv.className = `chat-message ${isUser ? 'user-message' : 'bot-message'} p-4 rounded-lg ${isUser ? 'bg-blue-500 text-white ml-8' : 'bg-blue-50 text-gray-700 mr-8'}`; const messageContent = `

${isUser ? 'You' : 'RAG Bot'}

${content}

`; messageDiv.innerHTML = messageContent; chatContainer.appendChild(messageDiv); chatContainer.scrollTop = chatContainer.scrollHeight; feather.replace(); } // Function to show typing indicator function showTypingIndicator() { const typingDiv = document.createElement('div'); typingDiv.className = 'chat-message bot-message p-4 rounded-lg bg-blue-50 text-gray-700 mr-8'; typingDiv.innerHTML = `

RAG Bot

`; chatContainer.appendChild(typingDiv); chatContainer.scrollTop = chatContainer.scrollHeight; return typingDiv; } // Function to simulate RAG response (in a real app, this would call an API) function getRAGResponse(userMessage) { return new Promise((resolve) => { setTimeout(() => { // Simulate different responses based on input const responses = [ "I found some information about that. According to my knowledge...", "That's an interesting question. Here's what I know...", "I've retrieved some relevant documents that mention...", "Based on the data I have access to...", "I'm not entirely sure about that, but here's what I found..." ]; const randomResponse = responses[Math.floor(Math.random() * responses.length)]; resolve(randomResponse); }, 1500 + Math.random() * 2000); // Random delay between 1.5-3.5 seconds }); } // Handle send button click sendButton.addEventListener('click', async () => { const message = userInput.value.trim(); if (message) { addMessage(message, true); userInput.value = ''; const typingIndicator = showTypingIndicator(); const response = await getRAGResponse(message); // Remove typing indicator and show actual response chatContainer.removeChild(typingIndicator); addMessage(response); } }); // Handle Enter key press userInput.addEventListener('keypress', (e) => { if (e.key === 'Enter') { sendButton.click(); } }); });