pvanand commited on
Commit
1ddf5a9
·
verified ·
1 Parent(s): a0fc312

up-date <json>

Browse files
Files changed (1) hide show
  1. static/followup-agent.html +94 -100
static/followup-agent.html CHANGED
@@ -236,112 +236,106 @@
236
  sanitize: false
237
  });
238
 
239
- const app = new Vue({
240
- el: '#app',
241
- data: {
242
- messages: [],
243
- userInput: '',
244
- selectedOptions: {},
245
- conversationId: '',
246
- currentAudio: null,
247
- },
248
- methods: {
249
- async sendMessage() {
250
- if (!this.userInput.trim()) return;
251
-
252
- this.messages.push({ type: 'user', content: marked.parse(this.userInput) });
253
- const message = this.userInput;
254
- this.userInput = '';
255
- this.selectedOptions = {};
256
-
257
- try {
258
- const response = await fetch('https://pvanand-general-chat.hf.space/followup-agent', {
259
- method: 'POST',
260
- headers: {
261
- 'Content-Type': 'application/json',
262
- 'X-API-Key': '44d5c2ac18ced6fc25c1e57dcd06fc0b31fb4ad97bf56e67540671a647465df4'
263
- },
264
- body: JSON.stringify({
265
- query: message,
266
- model_id: 'openai/gpt-4o-mini',
267
- conversation_id: this.conversationId,
268
- user_id: 'string'
269
- })
270
- });
271
-
272
- const reader = response.body.getReader();
273
- let rawResponse = '';
274
- let streamingIndex = this.messages.push({ type: 'bot', content: 'Thinking...' }) - 1;
275
-
276
- while (true) {
277
- const { done, value } = await reader.read();
278
- if (done) break;
279
-
280
- const chunk = new TextDecoder().decode(value);
 
 
 
 
281
  rawResponse += chunk;
282
  this.$set(this.messages, streamingIndex, { type: 'bot', content: marked.parse(rawResponse) });
283
  }
284
-
285
- this.messages.splice(streamingIndex, 1);
286
-
287
- const jsonStart = rawResponse.lastIndexOf('\n\n');
288
- if (jsonStart !== -1) {
289
- const jsonStr = rawResponse.slice(jsonStart + 2);
290
- const parsedResponse = JSON.parse(jsonStr);
291
- this.renderParsedResponse(parsedResponse);
292
- }
293
- } catch (error) {
294
- console.error('Error:', error);
295
- this.messages.push({ type: 'bot', content: 'An error occurred while processing your request.' });
296
  }
297
- this.$nextTick(() => this.scrollToBottom());
298
- },
299
- async renderParsedResponse(parsedResponse) {
300
- let botResponse = '';
301
- let responseForAudio = '';
302
-
303
- if (parsedResponse.response) {
304
- botResponse += parsedResponse.response + '\n\n';
305
- responseForAudio = parsedResponse.response.replace(/#/g, '');
306
- }
307
-
308
- if (parsedResponse.clarification) {
309
- parsedResponse.clarification.forEach((item, questionIndex) => {
310
- botResponse += `**${item.question}**\n\n`;
311
- botResponse += '<div class="option-buttons">';
312
- item.options.forEach((option, optionIndex) => {
313
- const escapedOption = option.replace(/'/g, "\\'");
314
- botResponse += `<button class="option-button" onclick="app.toggleOption('${escapedOption}', ${questionIndex}, ${optionIndex})">${option}</button>`;
315
- });
316
- botResponse += '</div>\n\n';
317
  });
318
- }
319
-
320
- if (botResponse) {
321
- const markedContent = marked.parse(botResponse);
322
- const messageIndex = this.messages.push({
323
- type: 'bot',
324
- content: markedContent,
325
- audio: null,
326
- isPlaying: false
327
- }) - 1;
328
- this.$nextTick(async () => {
329
- document.querySelectorAll('pre code').forEach((block) => {
330
- hljs.highlightBlock(block);
331
- });
332
- this.scrollToBottom();
333
- this.updateButtonStates();
334
-
335
- if (responseForAudio) {
336
- const audioUrl = await this.convertToSpeech(responseForAudio);
337
- if (audioUrl) {
338
- this.$set(this.messages[messageIndex], 'audio', audioUrl);
339
- this.$forceUpdate();
340
- }
341
- }
342
  });
343
- }
344
- },
 
 
 
 
 
 
 
 
 
 
 
345
  toggleOption(option, questionIndex, optionIndex) {
346
  if (!this.selectedOptions[questionIndex]) {
347
  this.$set(this.selectedOptions, questionIndex, []);
 
236
  sanitize: false
237
  });
238
 
239
+ const app = new Vue({
240
+ el: '#app',
241
+ data: {
242
+ messages: [],
243
+ userInput: '',
244
+ selectedOptions: {},
245
+ conversationId: '',
246
+ currentAudio: null,
247
+ },
248
+ methods: {
249
+ async sendMessage() {
250
+ if (!this.userInput.trim()) return;
251
+
252
+ this.messages.push({ type: 'user', content: marked.parse(this.userInput) });
253
+ const message = this.userInput;
254
+ this.userInput = '';
255
+ this.selectedOptions = {};
256
+
257
+ try {
258
+ const response = await fetch('https://pvanand-general-chat.hf.space/v2/followup-agent', {
259
+ method: 'POST',
260
+ headers: {
261
+ 'Content-Type': 'application/json',
262
+ 'X-API-Key': '44d5c2ac18ced6fc25c1e57dcd06fc0b31fb4ad97bf56e67540671a647465df4'
263
+ },
264
+ body: JSON.stringify({
265
+ query: message,
266
+ model_id: 'openai/gpt-4o-mini',
267
+ conversation_id: this.conversationId,
268
+ user_id: 'string'
269
+ })
270
+ });
271
+
272
+ const reader = response.body.getReader();
273
+ let rawResponse = '';
274
+ let jsonResponse = null;
275
+ let streamingIndex = this.messages.push({ type: 'bot', content: 'Thinking...' }) - 1;
276
+
277
+ while (true) {
278
+ const { done, value } = await reader.read();
279
+ if (done) break;
280
+
281
+ const chunk = new TextDecoder().decode(value);
282
+ if (chunk.startsWith('<json>')) {
283
+ jsonResponse = JSON.parse(chunk.slice(6));
284
+ } else {
285
  rawResponse += chunk;
286
  this.$set(this.messages, streamingIndex, { type: 'bot', content: marked.parse(rawResponse) });
287
  }
 
 
 
 
 
 
 
 
 
 
 
 
288
  }
289
+
290
+ this.messages.splice(streamingIndex, 1);
291
+ this.renderParsedResponse(rawResponse, jsonResponse);
292
+ } catch (error) {
293
+ console.error('Error:', error);
294
+ this.messages.push({ type: 'bot', content: 'An error occurred while processing your request.' });
295
+ }
296
+ this.$nextTick(() => this.scrollToBottom());
297
+ },
298
+ renderParsedResponse(rawResponse, jsonResponse) {
299
+ let botResponse = rawResponse + '\n\n';
300
+ let responseForAudio = rawResponse.replace(/#/g, '');
301
+
302
+ if (jsonResponse && jsonResponse.clarification) {
303
+ jsonResponse.clarification.forEach((item, questionIndex) => {
304
+ botResponse += `**${item.question}**\n\n`;
305
+ botResponse += '<div class="option-buttons">';
306
+ item.options.forEach((option, optionIndex) => {
307
+ const escapedOption = option.replace(/'/g, "\\'");
308
+ botResponse += `<button class="option-button" onclick="app.toggleOption('${escapedOption}', ${questionIndex}, ${optionIndex})">${option}</button>`;
309
  });
310
+ botResponse += '</div>\n\n';
311
+ });
312
+ }
313
+
314
+ if (botResponse) {
315
+ const markedContent = marked.parse(botResponse);
316
+ const messageIndex = this.messages.push({
317
+ type: 'bot',
318
+ content: markedContent,
319
+ audio: null,
320
+ isPlaying: false
321
+ }) - 1;
322
+ this.$nextTick(async () => {
323
+ document.querySelectorAll('pre code').forEach((block) => {
324
+ hljs.highlightBlock(block);
 
 
 
 
 
 
 
 
 
325
  });
326
+ this.scrollToBottom();
327
+ this.updateButtonStates();
328
+
329
+ if (responseForAudio) {
330
+ const audioUrl = await this.convertToSpeech(responseForAudio);
331
+ if (audioUrl) {
332
+ this.$set(this.messages[messageIndex], 'audio', audioUrl);
333
+ this.$forceUpdate();
334
+ }
335
+ }
336
+ });
337
+ }
338
+ },
339
  toggleOption(option, questionIndex, optionIndex) {
340
  if (!this.selectedOptions[questionIndex]) {
341
  this.$set(this.selectedOptions, questionIndex, []);