Files
exercises/w14_olama/stream.js
Andrea Bianchi f80186aacd ollama
2026-05-26 08:43:10 +09:00

32 lines
840 B
JavaScript

import delay from 'delay';
fetch('http://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
model: 'deepseek-r1:latest',
prompt: 'Hello! Tell me a long long joke.',
stream: true,
think: false,
}),
})
.then((res) => {
const reader = res.body.getReader();
const decoder = new TextDecoder();
reader.read().then(async ({ done, value }) => {
while (!done) {
// Convert chunk to string
const chunk = decoder.decode(value, { stream: true });
// Optionally, print each chunk as it arrives
const line = JSON.parse(chunk);
process.stdout.write(line.response ?? '');
// Read the next chunk
({ done, value } = await reader.read());
}
});
})
.catch(console.error);