Spaces:
Running
Running
up-date <json>
Browse files- static/followup-agent.html +94 -100
static/followup-agent.html
CHANGED
@@ -236,112 +236,106 @@
|
|
236 |
sanitize: false
|
237 |
});
|
238 |
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
|
|
|
|
|
|
|
|
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 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
botResponse += '</
|
317 |
});
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
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, []);
|