37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
fetch('http://localhost:8000', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
messages: [{ role: 'user', content: 'Hello, tell me a joke' }]
|
|
})
|
|
})
|
|
.then(response => {
|
|
const reader = response.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let fullText = '';
|
|
|
|
function read() {
|
|
reader.read().then(({ done, value }) => {
|
|
if (done) {
|
|
console.log('\n--- Stream complete ---');
|
|
console.log('Full response:', fullText);
|
|
return;
|
|
}
|
|
|
|
const chunk = decoder.decode(value);
|
|
const lines = chunk.split('\n').filter(line => line.startsWith('data: '));
|
|
|
|
lines.forEach(line => {
|
|
const json = JSON.parse(line.replace('data: ', ''));
|
|
if (json.content) {
|
|
fullText += json.content;
|
|
console.clear();
|
|
console.log(fullText);
|
|
}
|
|
});
|
|
|
|
read();
|
|
});
|
|
}
|
|
read();
|
|
}); |