Spaces:
Running
Running
Update playground.html
Browse files- playground.html +164 -95
playground.html
CHANGED
@@ -366,6 +366,7 @@
|
|
366 |
<option value="llama-3.1-405b">Llama 3.1 405b</option>
|
367 |
<option value="gemini-1.5-flash">Gemini 1.5 Flash</option>
|
368 |
<option value="gemini-pro">Gemini Pro</option>
|
|
|
369 |
<option value="mixtral-8x7b">Mixtral 8x7b</option>
|
370 |
<option value="command-r">Command-R</option>
|
371 |
<option value="command">Command</option>
|
@@ -428,34 +429,55 @@
|
|
428 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
429 |
}
|
430 |
function appendMessage(content, type = 'bot', isStreaming = false) {
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
459 |
$(document).ready(function () {
|
460 |
$('#modelSelect').select2({
|
461 |
placeholder: 'Select a model', // Placeholder text
|
@@ -507,74 +529,124 @@
|
|
507 |
}
|
508 |
|
509 |
async function callApi(userMessage, model) {
|
510 |
-
|
511 |
-
|
512 |
-
|
513 |
-
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
const cleanedChunk = chunk.trim().replace(/^data:\s*/, '');
|
545 |
-
const jsonChunks = cleanedChunk.split("data:").filter(Boolean);
|
546 |
-
|
547 |
-
jsonChunks.forEach(jsonString => {
|
548 |
-
try {
|
549 |
-
const jsonData = JSON.parse(jsonString);
|
550 |
-
const delta = jsonData.choices?.[0]?.delta || {};
|
551 |
-
let content = delta.content || "";
|
552 |
-
|
553 |
-
if (content) {
|
554 |
-
// Convert \n to <br>
|
555 |
-
// Properly handle newline characters
|
556 |
-
content = content.replace(/\\n/g, '<br>'); // Replace escaped newlines (\n) with <br>
|
557 |
-
|
558 |
-
// Call appendMessage with the processed content
|
559 |
-
appendMessage(content, 'bot', true);
|
560 |
-
|
561 |
-
}
|
562 |
-
} catch (err) {
|
563 |
-
console.warn("Parsing error:", err);
|
564 |
}
|
565 |
-
})
|
566 |
-
|
|
|
|
|
567 |
}
|
568 |
-
} else {
|
569 |
-
throw new Error(`API responded with status ${response.status}`);
|
570 |
}
|
571 |
-
}
|
572 |
-
|
573 |
-
throw error;
|
574 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
575 |
|
576 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
577 |
}
|
|
|
|
|
|
|
|
|
578 |
// Event Listeners
|
579 |
initialSendIcon.addEventListener('click', sendInitialMessage);
|
580 |
initialChatInput.addEventListener('keypress', (event) => {
|
@@ -592,9 +664,6 @@
|
|
592 |
</body>
|
593 |
<!-- random
|
594 |
comments
|
595 |
-
to
|
596 |
-
|
597 |
-
to
|
598 |
-
600 lines
|
599 |
-
coz why not -->
|
600 |
</html>
|
|
|
366 |
<option value="llama-3.1-405b">Llama 3.1 405b</option>
|
367 |
<option value="gemini-1.5-flash">Gemini 1.5 Flash</option>
|
368 |
<option value="gemini-pro">Gemini Pro</option>
|
369 |
+
<option value="searchgpt">SearchGPT(Realtime Stuff)</option>
|
370 |
<option value="mixtral-8x7b">Mixtral 8x7b</option>
|
371 |
<option value="command-r">Command-R</option>
|
372 |
<option value="command">Command</option>
|
|
|
429 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
430 |
}
|
431 |
function appendMessage(content, type = 'bot', isStreaming = false) {
|
432 |
+
if (type === 'bot' && isStreaming) {
|
433 |
+
if (!currentStreamingMessage) {
|
434 |
+
currentStreamingMessage = document.createElement('div');
|
435 |
+
currentStreamingMessage.className = `message ${type}`;
|
436 |
+
chatMessages.appendChild(currentStreamingMessage);
|
437 |
+
}
|
438 |
+
|
439 |
+
// Replace consecutive numbers followed by spaces with numbered list format
|
440 |
+
// Convert text between ** to bold, handling multiple occurrences
|
441 |
+
const formattedContent = content
|
442 |
+
.replace(/(\d+)\.\s*/g, '<br>$1. ') // Ensure proper spacing and line breaks
|
443 |
+
.replace(/\n/g, '<br>') // Convert remaining newlines
|
444 |
+
.replace(/\*\*(.*?)\*\*/g, function(match, p1) {
|
445 |
+
return '<strong>' + p1 + '</strong>';
|
446 |
+
});
|
447 |
+
|
448 |
+
// Safely set innerHTML
|
449 |
+
currentStreamingMessage.innerHTML += formattedContent;
|
450 |
+
|
451 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
452 |
+
} else {
|
453 |
+
// Similar logic for non-streaming messages
|
454 |
+
if (currentStreamingMessage) {
|
455 |
+
currentStreamingMessage = null;
|
456 |
+
}
|
457 |
+
|
458 |
+
const messageBox = document.createElement('div');
|
459 |
+
messageBox.className = `message ${type}`;
|
460 |
+
|
461 |
+
// Format content similarly
|
462 |
+
const formattedContent = content
|
463 |
+
.replace(/(\d+)\.\s*/g, '<br>$1. ')
|
464 |
+
.replace(/\n/g, '<br>')
|
465 |
+
.replace(/\*\*(.*?)\*\*/g, function(match, p1) {
|
466 |
+
return '<strong>' + p1 + '</strong>';
|
467 |
+
});
|
468 |
+
|
469 |
+
messageBox.innerHTML = formattedContent;
|
470 |
+
|
471 |
+
chatMessages.appendChild(messageBox);
|
472 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
473 |
+
|
474 |
+
// Update conversation history
|
475 |
+
conversationHistory.push({
|
476 |
+
role: type === 'user' ? 'user' : 'assistant',
|
477 |
+
content: content
|
478 |
+
});
|
479 |
+
}
|
480 |
+
}
|
481 |
$(document).ready(function () {
|
482 |
$('#modelSelect').select2({
|
483 |
placeholder: 'Select a model', // Placeholder text
|
|
|
529 |
}
|
530 |
|
531 |
async function callApi(userMessage, model) {
|
532 |
+
let fullResponse = "";
|
533 |
+
|
534 |
+
if (model === "searchgpt") {
|
535 |
+
const url = `https://parthsadaria-lokiai.hf.space/searchgpt?q=${encodeURIComponent(userMessage)}&stream=true&systemprompt=You are **SearchGPT**, an AI with internet access. Reply directly and accurately to user requests.`;
|
536 |
+
try {
|
537 |
+
const response = await fetch(url);
|
538 |
+
|
539 |
+
if (response.ok) {
|
540 |
+
const reader = response.body.getReader();
|
541 |
+
const decoder = new TextDecoder("utf-8");
|
542 |
+
let done = false;
|
543 |
+
|
544 |
+
while (!done) {
|
545 |
+
const { value, done: streamDone } = await reader.read();
|
546 |
+
done = streamDone;
|
547 |
+
|
548 |
+
if (value) {
|
549 |
+
const chunk = decoder.decode(value);
|
550 |
+
const cleanedChunk = chunk.trim().replace(/^data:\s*/, '');
|
551 |
+
const jsonChunks = cleanedChunk.split("data:").filter(Boolean);
|
552 |
+
|
553 |
+
jsonChunks.forEach(jsonString => {
|
554 |
+
try {
|
555 |
+
const jsonData = JSON.parse(jsonString);
|
556 |
+
const content = jsonData.choices?.[0]?.message?.content || "";
|
557 |
+
|
558 |
+
if (content) {
|
559 |
+
// Comprehensive newline conversion
|
560 |
+
const formattedContent = content
|
561 |
+
.replace(/\r\n/g, '<br>') // Windows-style newlines
|
562 |
+
.replace(/\n/g, '<br>') // Unix/Linux-style newlines
|
563 |
+
.replace(/\r/g, '<br>'); // Old Mac-style newlines
|
564 |
+
|
565 |
+
appendMessage(formattedContent, 'bot', true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
566 |
}
|
567 |
+
} catch (err) {
|
568 |
+
console.warn("Parsing error:", err);
|
569 |
+
}
|
570 |
+
});
|
571 |
}
|
|
|
|
|
572 |
}
|
573 |
+
} else {
|
574 |
+
throw new Error(`API responded with status ${response.status}`);
|
|
|
575 |
}
|
576 |
+
} catch (error) {
|
577 |
+
console.error("API call error:", error);
|
578 |
+
throw error;
|
579 |
+
}
|
580 |
+
|
581 |
+
} else {
|
582 |
+
// Similar changes for other models
|
583 |
+
const url = "https://parthsadaria-lokiai.hf.space/chat/completions";
|
584 |
+
const payload = {
|
585 |
+
model: model,
|
586 |
+
messages: [
|
587 |
+
...conversationHistory,
|
588 |
+
{ role: "user", content: userMessage }
|
589 |
+
],
|
590 |
+
stream: true
|
591 |
+
};
|
592 |
+
|
593 |
+
const headers = {
|
594 |
+
"Content-Type": "application/json"
|
595 |
+
};
|
596 |
+
|
597 |
+
try {
|
598 |
+
const response = await fetch(url, {
|
599 |
+
method: "POST",
|
600 |
+
headers: headers,
|
601 |
+
body: JSON.stringify(payload)
|
602 |
+
});
|
603 |
|
604 |
+
if (response.ok) {
|
605 |
+
const reader = response.body.getReader();
|
606 |
+
const decoder = new TextDecoder("utf-8");
|
607 |
+
let done = false;
|
608 |
+
|
609 |
+
while (!done) {
|
610 |
+
const { value, done: streamDone } = await reader.read();
|
611 |
+
done = streamDone;
|
612 |
+
|
613 |
+
if (value) {
|
614 |
+
const chunk = decoder.decode(value);
|
615 |
+
const cleanedChunk = chunk.trim().replace(/^data:\s*/, '');
|
616 |
+
const jsonChunks = cleanedChunk.split("data:").filter(Boolean);
|
617 |
+
|
618 |
+
jsonChunks.forEach(jsonString => {
|
619 |
+
try {
|
620 |
+
const jsonData = JSON.parse(jsonString);
|
621 |
+
const delta = jsonData.choices?.[0]?.delta || {};
|
622 |
+
let content = delta.content || "";
|
623 |
+
|
624 |
+
// Comprehensive newline conversion
|
625 |
+
content = content
|
626 |
+
.replace(/\r\n/g, '<br>') // Windows-style newlines
|
627 |
+
.replace(/\n/g, '<br>') // Unix/Linux-style newlines
|
628 |
+
.replace(/\r/g, '<br>'); // Old Mac-style newlines
|
629 |
+
|
630 |
+
if (content) {
|
631 |
+
appendMessage(content, 'bot', true);
|
632 |
+
}
|
633 |
+
} catch (err) {
|
634 |
+
console.warn("Parsing error:", err);
|
635 |
+
}
|
636 |
+
});
|
637 |
+
}
|
638 |
+
}
|
639 |
+
} else {
|
640 |
+
throw new Error(`API responded with status ${response.status}`);
|
641 |
+
}
|
642 |
+
} catch (error) {
|
643 |
+
console.error("API call error:", error);
|
644 |
+
throw error;
|
645 |
}
|
646 |
+
}
|
647 |
+
|
648 |
+
return fullResponse.trim();
|
649 |
+
}
|
650 |
// Event Listeners
|
651 |
initialSendIcon.addEventListener('click', sendInitialMessage);
|
652 |
initialChatInput.addEventListener('keypress', (event) => {
|
|
|
664 |
</body>
|
665 |
<!-- random
|
666 |
comments
|
667 |
+
to get
|
668 |
+
669 lines -->
|
|
|
|
|
|
|
669 |
</html>
|