Adicionar web/chat.js
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
// All comments are in English.
|
||||
|
||||
let conversationId = null;
|
||||
|
||||
function login() {
|
||||
window.location = "/login";
|
||||
}
|
||||
|
||||
async function newConversation() {
|
||||
const r = await fetch("/api/new_conversation");
|
||||
const data = await r.json();
|
||||
conversationId = data.conversation_id;
|
||||
document.getElementById("chat").innerHTML = "";
|
||||
}
|
||||
|
||||
async function sendMessage() {
|
||||
const prompt = document.getElementById("prompt").value;
|
||||
const model = "llama3.2";
|
||||
|
||||
const chatBox = document.getElementById("chat");
|
||||
chatBox.innerHTML += `<div class="user">${prompt}</div>`;
|
||||
|
||||
const url = `/api/stream?conversation_id=${conversationId}&model=${model}&prompt=${encodeURIComponent(prompt)}`;
|
||||
const evtSource = new EventSource(url);
|
||||
|
||||
let buffer = "";
|
||||
|
||||
evtSource.onmessage = function(event) {
|
||||
const data = JSON.parse(event.data);
|
||||
buffer += data.token;
|
||||
|
||||
document.getElementById("chat").innerHTML +=
|
||||
`<div class="assistant">${buffer}</div>`;
|
||||
};
|
||||
|
||||
evtSource.onerror = function() {
|
||||
evtSource.close();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user