You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and dots ('.'), can be up to 35 characters long. Letters must be lowercase.
 
 

110 lines
3.8 KiB

<!-- chat/templates/chat/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat App</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/15.0.7/marked.min.js" integrity="sha512-rPuOZPx/WHMHNx2RoALKwiCDiDrCo4ekUctyTYKzBo8NGA79NcTW2gfrbcCL2RYL7RdjX2v9zR0fKyI4U4kPew==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
#chat-container {
border: 1px solid #ccc;
border-radius: 5px;
padding: 20px;
margin-bottom: 20px;
}
#prompt-input {
width: 100%;
}
#response-container {
min-height: 100px;
border: 1px solid #eee;
color: black;
padding: 10px;
margin-top: 10px;
border-radius: 5px;
}
button {
padding: 8px 16px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<h1>Simple Chat App</h1>
<input id="conversation-id" type="number" value="1">
<div id="chat-container">
<h3>Enter your message:</h3>
<textarea type="text" id="prompt-input" placeholder="Type your message..." value="hello">
</textarea>
<button onclick="sendPrompt()">Send</button>
<div id="response-container">
<div id="response-text">Response will appear here...</div>
</div>
</div>
<script>
const responseText = document.querySelector('#response-text');
const convId = document.querySelector('#conversation-id');
// Function to send the prompt and handle streaming response
responseText.innerHTML = marked.parse("");
async function sendPrompt() {
const promptInput = document.getElementById('prompt-input');
const content = promptInput.value;
// Clear previous response
responseText.textContent = '';
function base64ToUtf8(bytes) {
// Decode UTF-8 bytes to a JavaScript string
return (new TextDecoder()).decode(bytes, { stream: true });
}
try {
// Make the fetch request
const response = await fetch('/api/conversations/' + convId.value + '/prompt/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ content }),
});
// Handle the event stream
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
function readChunk() {
reader.read().then(({ done, value }) => {
if (done) return;
console.log(value);
buffer += (new TextDecoder()).decode(value, { stream: true })
console.log(buffer)
responseText.innerHTML = marked.parse(buffer);
readChunk();
});
}
readChunk();
} catch (error) {
console.error('Error:', error);
responseText.textContent = `Error: ${error.message}`;
}
}
</script>
</body>
</html>