diff --git a/chatv2.js b/chatv2.js index 32e223c..363b5d4 100644 --- a/chatv2.js +++ b/chatv2.js @@ -15,11 +15,29 @@ const BACKENDS = { name: 'Ollama (192.168.1.159:8081)' } } - const IS_PRODUCTION = window.location.hostname === 'jarvis-lan.appmodel.nl' const API_KEY = 'not-needed' const MAX_CHAT_HISTORY = 50 +const welcomeMessage = `# Welcome to LM Studio Chat! + +I now support **full conversation history**, **real-time streaming responses** and **dark, readable text formatting**. + +## Features: +1. **Full Chat History** - Complete conversation context is now sent to the AI +2. **Smart History Management** - Automatically keeps the last ${ MAX_CHAT_HISTORY } messages +3. **Streaming Mode** (enabled by default) - Watch responses appear word-by-word +4. **Markdown Rendering** - Proper formatting for code, lists, tables, and more +5. **Readable Dark Text** - No more eye strain from light gray text + +## Try it out: +- Ask follow-up questions that reference earlier messages +- Have a multi-turn conversation with full context +- Ask "what did I just ask?" to test history retention +- Watch the streaming response in real-time! + +> *Tip: You can toggle streaming and markdown using the checkboxes below.*` + let currentBackend = 'plato' let currentModel = null let availableModels = [] @@ -454,51 +472,53 @@ async function handleStreamingResponse(userMessage) { } async function handleNonStreamingResponse(userMessage) { - const typingIndicator = showTypingIndicator() + try { + const typingIndicator = showTypingIndicator() - if (!currentModel && availableModels.length > 0) { - currentModel = availableModels[0].id - document.getElementById('modelSelector').value = currentModel + if (!currentModel && availableModels.length > 0) { + currentModel = availableModels[0].id + document.getElementById('modelSelector').value = currentModel + } + + const requestBody = { + model : currentModel || 'local-model', + messages : getApiMessages(), + stream : false, + temperature: 0.7, + max_tokens : 2000 + } + + const response = await fetch(`${ getApiUrl() }/chat/completions`, { + method : 'POST', + headers: { + 'Content-Type' : 'application/json', + 'Authorization': `Bearer ${ API_KEY }` + }, + body : JSON.stringify(requestBody) + }) + + if (!response.ok) { + throw new Error(`API Error: ${ response.status } ${ response.statusText }`) + } + + const data = await response.json() + removeTypingIndicator(typingIndicator) + + if (data.choices && data.choices.length > 0) { + const assistantReply = data.choices[0].message.content + addMessage('assistant', assistantReply, markdownToggle.checked) + } else { + addMessage('assistant', 'No response generated.', false) + } + + } catch (error) { + console.error('Error:', error) + removeTypingIndicator(typingIndicator) + addMessage('assistant', `Error: ${ error.message }`, false) + } finally { + sendBtn.disabled = false + userInput.focus() } - - const requestBody = { - model : currentModel || 'local-model', - messages : getApiMessages(), - stream : false, - temperature: 0.7, - max_tokens : 2000 - } - - const response = await fetch(`${ getApiUrl() }/chat/completions`, { - method : 'POST', - headers: { - 'Content-Type' : 'application/json', - 'Authorization': `Bearer ${ API_KEY }` - }, - body : JSON.stringify(requestBody) - }) - - if (!response.ok) { - throw new Error(`API Error: ${ response.status } ${ response.statusText }`) - } - - const data = await response.json() - removeTypingIndicator(typingIndicator) - - if (data.choices && data.choices.length > 0) { - const assistantReply = data.choices[0].message.content - addMessage('assistant', assistantReply, markdownToggle.checked) - } else { - addMessage('assistant', 'No response generated.', false) - } - -} catch (error) { - console.error('Error:', error) - removeTypingIndicator(typingIndicator) - addMessage('assistant', `Error: ${ error.message }`, false) -} finally { - sendBtn.disabled = false - userInput.focus() } function stopStream() { @@ -559,7 +579,7 @@ window.onload = () => { function resetChat() { chatHistory = [] - chatLog.innerHTML = '' + chatLog.innerHTML = welcomeMessage localStorage.setItem('chatHistory', JSON.stringify(chatHistory)) }