akhaliq HF Staff commited on
Commit
683f700
·
verified ·
1 Parent(s): 74e17ad

Upload index.js with huggingface_hub

Browse files
Files changed (1) hide show
  1. index.js +17 -4
index.js CHANGED
@@ -141,25 +141,38 @@ class Chatbot {
141
  const responseContainer = this.addMessage('', 'assistant', true);
142
  const messageContent = responseContainer.querySelector('.message-content');
143
 
 
 
 
 
 
 
 
 
 
 
 
144
  // Custom streamer to update UI
 
145
  const streamer = new TextStreamer(this.generator.tokenizer, {
146
  skip_prompt: true,
147
  skip_special_tokens: true,
148
  callback_function: (text) => {
149
- messageContent.textContent += text;
 
150
  this.scrollToBottom();
151
  }
152
  });
153
 
154
  // Generate response
155
- const output = await this.generator(this.messages, {
156
  max_new_tokens: 500,
157
  do_sample: false,
158
  streamer: streamer,
159
  });
160
 
161
- const assistantMessage = output[0].generated_text.at(-1).content;
162
- this.messages.push({ role: "assistant", content: assistantMessage });
163
 
164
  } catch (error) {
165
  console.error('Error generating response:', error);
 
141
  const responseContainer = this.addMessage('', 'assistant', true);
142
  const messageContent = responseContainer.querySelector('.message-content');
143
 
144
+ // Build the conversation context
145
+ let conversationText = "";
146
+ for (const msg of this.messages) {
147
+ if (msg.role === "user") {
148
+ conversationText += `User: ${msg.content}\n`;
149
+ } else if (msg.role === "assistant") {
150
+ conversationText += `Assistant: ${msg.content}\n`;
151
+ }
152
+ }
153
+ conversationText += "Assistant: ";
154
+
155
  // Custom streamer to update UI
156
+ let fullResponse = "";
157
  const streamer = new TextStreamer(this.generator.tokenizer, {
158
  skip_prompt: true,
159
  skip_special_tokens: true,
160
  callback_function: (text) => {
161
+ fullResponse += text;
162
+ messageContent.textContent = fullResponse;
163
  this.scrollToBottom();
164
  }
165
  });
166
 
167
  // Generate response
168
+ await this.generator(conversationText, {
169
  max_new_tokens: 500,
170
  do_sample: false,
171
  streamer: streamer,
172
  });
173
 
174
+ // Add the complete response to messages
175
+ this.messages.push({ role: "assistant", content: fullResponse });
176
 
177
  } catch (error) {
178
  console.error('Error generating response:', error);