File size: 936 Bytes
38b4eff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<head><title>SSE Test</title></head>
<body>
<h1>SSE Test</h1>
<div id="output" style="border: 1px solid #333; padding: 20px; min-height: 100px; white-space: pre-wrap;"></div>
<script>
async function test() {
  const resp = await fetch('/api/chat', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({messages: [{role:'user', content:'hi'}], use_uplink: false})
  });
  const reader = resp.body.getReader();
  const decoder = new TextDecoder();
  const out = document.getElementById('output');
  let count = 0;
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    const chunk = decoder.decode(value, { stream: true });
    out.textContent += 'CHUNK ' + (++count) + ': ' + chunk.substring(0, 200) + '\n';
  }
  out.textContent += '\n=== DONE ===';
}
test().catch(e => out.textContent = 'ERROR: ' + e);
</script>
</body>
</html>