This commit is contained in:
Andrea Bianchi
2026-05-26 08:43:10 +09:00
parent 70649dff69
commit f80186aacd
6 changed files with 121 additions and 0 deletions

31
w14_olama/stream.js Normal file
View File

@@ -0,0 +1,31 @@
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);