Create templates/index.html
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Model-Tiny Chat</title> <style> body { font-family: sans-serif; max-width: 800px; margin: 2em auto; padding: 0 1em; } #chatbox { border: 1px solid #ccc; height: 400px; overflow-y: scroll; padding: 1em; margin-bottom: 1em; } .user { color: blue; font-weight: bold; } .bot { color: green; font-weight: bold; } #input-area { display: flex; gap: 10px; } input { flex-grow: 1; padding: 10px; } </style></head><body> <h1>AshokGakr/model-tiny Agent</h1> <div id="chatbox"></div> <div id="input-area"> <input type="text" id="user-input" placeholder="Type your message here..."> <button onclick="sendMessage()">Send</button> </div>
<script> let messages = [ {"role": "system", "content": "You are a helpful assistant specialized in reasoning and tool use."} ];
async function sendMessage() { const input = document.getElementById('user-input'); const chatbox = document.getElementById('chatbox'); const text = input.value; if (!text) return;
// Update UI and Message History messages.push({"role": "user", "content": text}); chatbox.innerHTML += `<p><span class="user">User:</span> ${text}</p>`; input.value = '';
const botMessageDiv = document.createElement('p'); botMessageDiv.innerHTML = '<span class="bot">Bot:</span> <span class="content"></span>'; chatbox.appendChild(botMessageDiv); const contentSpan = botMessageDiv.querySelector('.content');
// 4. Handle Streaming Request // This replicates the "Observe -> Think -> Act" loop in real-time [9, 10]. const response = await fetch('/chat', { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({ messages }) });
const reader = response.body.getReader(); const decoder = new TextDecoder(); let fullBotResponse = "";
while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value, { stream: true }); fullBotResponse += chunk; contentSpan.innerText = fullBotResponse; chatbox.scrollTop = chatbox.scrollHeight; } messages.push({"role": "assistant", "content": fullBotResponse}); } </script></body></html>templates/index.htmlAdded+68