Files
AiThingy/templates/chattest.html

71 lines
2.4 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Streaming Response</title>
</head>
<body>
<div class="container">
<h1>AiThingy</h1>
<p>Enter a message, chatId, and auth token below:</p>
<input placeholder="Message" id="message"></input>
<input placeholder="ChatId" id="chatid"></input>
<input placeholder="Token" id="token"></input>
<button onclick="startStreaming()">Send Message</button>
<br>
<textarea id="response-output" readonly placeholder="Waiting for streaming data..."></textarea>
</div>
<script>
async function startStreaming() {
const outputArea = document.getElementById('response-output');
outputArea.value = 'Connecting to endpoint...\n';
const chatId = document.getElementById("chatid").value;
const endpointUrl = 'http://127.0.0.1:5000/api/chat/' + chatId + '/generate';
const postData = {
"message": document.getElementById("message").value
};
try {
const response = await fetch(endpointUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'token': document.getElementById("token").value
},
body: JSON.stringify(postData)
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
let receivedChunks = '';
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
const chunk = decoder.decode(value, { stream: true });
const responseString = chunk.substring(6);
outputArea.value = responseString;
}
} catch (error) {
console.error('An error has occurred:', error);
outputArea.value += `\n\n--- An error has occurred: ${error.message} ---`;
}
}
</script>
</body>
</html>