Create main.js
Browse files
main.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import initWebLLM from './Webllm/engine.js';
|
| 2 |
+
|
| 3 |
+
const chatBox = document.getElementById("chat-box");
|
| 4 |
+
const userInput = document.getElementById("user-input");
|
| 5 |
+
const sendButton = document.getElementById("send-button");
|
| 6 |
+
|
| 7 |
+
let engine = null;
|
| 8 |
+
|
| 9 |
+
(async () => {
|
| 10 |
+
engine = await initWebLLM({
|
| 11 |
+
modelUrl: "./distilgpt2-q4_k_s.bin",
|
| 12 |
+
tokenizerUrl: "./Webllm/tokenizer/"
|
| 13 |
+
});
|
| 14 |
+
})();
|
| 15 |
+
|
| 16 |
+
function addMessage(sender, text) {
|
| 17 |
+
const msg = document.createElement("div");
|
| 18 |
+
msg.className = `message ${sender}`;
|
| 19 |
+
msg.innerText = text;
|
| 20 |
+
chatBox.appendChild(msg);
|
| 21 |
+
chatBox.scrollTop = chatBox.scrollHeight;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
async function getBotResponse(input) {
|
| 25 |
+
if (!engine) return "Loading model...";
|
| 26 |
+
return await engine.chat(input);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
sendButton.onclick = async () => {
|
| 30 |
+
const text = userInput.value.trim();
|
| 31 |
+
if (!text) return;
|
| 32 |
+
|
| 33 |
+
addMessage("user", text);
|
| 34 |
+
userInput.value = "";
|
| 35 |
+
|
| 36 |
+
const response = await getBotResponse(text);
|
| 37 |
+
addMessage("bot", response);
|
| 38 |
+
};
|