neetnestor commited on
Commit
dff0f00
1 Parent(s): 5ca5f25

feat: enter key to send

Browse files
Files changed (1) hide show
  1. index.js +16 -2
index.js CHANGED
@@ -9,7 +9,7 @@ const messages = [
9
  ];
10
 
11
  const availableModels = webllm.prebuiltAppConfig.model_list.map(
12
- (m) => m.model_id,
13
  );
14
  let selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-1k";
15
 
@@ -20,6 +20,7 @@ function updateEngineInitProgressCallback(report) {
20
  }
21
 
22
  // Create engine instance
 
23
  const engine = new webllm.MLCEngine();
24
  engine.setInitProgressCallback(updateEngineInitProgressCallback);
25
 
@@ -31,6 +32,7 @@ async function initializeWebLLMEngine() {
31
  top_p: 1,
32
  };
33
  await engine.reload(selectedModel, config);
 
34
  }
35
 
36
  async function streamingGenerating(messages, onUpdate, onFinish, onError) {
@@ -61,6 +63,9 @@ async function streamingGenerating(messages, onUpdate, onFinish, onError) {
61
 
62
  /*************** UI logic ***************/
63
  function onMessageSend() {
 
 
 
64
  const input = document.getElementById("user-input").value.trim();
65
  const message = {
66
  content: input,
@@ -95,13 +100,17 @@ function onMessageSend() {
95
  `decoding: ${usage.extra.decode_tokens_per_s.toFixed(4)} tokens/sec`;
96
  document.getElementById("chat-stats").classList.remove("hidden");
97
  document.getElementById("chat-stats").textContent = usageText;
 
 
 
 
98
  };
99
 
100
  streamingGenerating(
101
  messages,
102
  updateLastMessage,
103
  onFinishGenerating,
104
- console.error,
105
  );
106
  }
107
 
@@ -148,3 +157,8 @@ document.getElementById("download").addEventListener("click", function () {
148
  document.getElementById("send").addEventListener("click", function () {
149
  onMessageSend();
150
  });
 
 
 
 
 
 
9
  ];
10
 
11
  const availableModels = webllm.prebuiltAppConfig.model_list.map(
12
+ (m) => m.model_id
13
  );
14
  let selectedModel = "Llama-3.1-8B-Instruct-q4f32_1-1k";
15
 
 
20
  }
21
 
22
  // Create engine instance
23
+ let modelLoaded = false;
24
  const engine = new webllm.MLCEngine();
25
  engine.setInitProgressCallback(updateEngineInitProgressCallback);
26
 
 
32
  top_p: 1,
33
  };
34
  await engine.reload(selectedModel, config);
35
+ modelLoaded = true;
36
  }
37
 
38
  async function streamingGenerating(messages, onUpdate, onFinish, onError) {
 
63
 
64
  /*************** UI logic ***************/
65
  function onMessageSend() {
66
+ if (!modelLoaded) {
67
+ return;
68
+ }
69
  const input = document.getElementById("user-input").value.trim();
70
  const message = {
71
  content: input,
 
100
  `decoding: ${usage.extra.decode_tokens_per_s.toFixed(4)} tokens/sec`;
101
  document.getElementById("chat-stats").classList.remove("hidden");
102
  document.getElementById("chat-stats").textContent = usageText;
103
+
104
+ document
105
+ .getElementById("user-input")
106
+ .setAttribute("placeholder", "Type a message...");
107
  };
108
 
109
  streamingGenerating(
110
  messages,
111
  updateLastMessage,
112
  onFinishGenerating,
113
+ console.error
114
  );
115
  }
116
 
 
157
  document.getElementById("send").addEventListener("click", function () {
158
  onMessageSend();
159
  });
160
+ document.getElementById("user-input").addEventListener("keydown", (event) => {
161
+ if (event.key === "Enter") {
162
+ onMessageSend();
163
+ }
164
+ });