Chatbot / main.js
Anshuman9600000's picture
Create main.js
99f2bbf verified
raw
history blame contribute delete
942 Bytes
import initWebLLM from './Webllm/engine.js';
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");
const sendButton = document.getElementById("send-button");
let engine = null;
(async () => {
engine = await initWebLLM({
modelUrl: "./distilgpt2-q4_k_s.bin",
tokenizerUrl: "./Webllm/tokenizer/"
});
})();
function addMessage(sender, text) {
const msg = document.createElement("div");
msg.className = `message ${sender}`;
msg.innerText = text;
chatBox.appendChild(msg);
chatBox.scrollTop = chatBox.scrollHeight;
}
async function getBotResponse(input) {
if (!engine) return "Loading model...";
return await engine.chat(input);
}
sendButton.onclick = async () => {
const text = userInput.value.trim();
if (!text) return;
addMessage("user", text);
userInput.value = "";
const response = await getBotResponse(text);
addMessage("bot", response);
};