Updated stream
This commit is contained in:
@@ -1,3 +1,17 @@
|
||||
import delay from 'delay';
|
||||
|
||||
fetch('http://...', {});
|
||||
fetch('http://127.0.0.1:11434/api/generate', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model: 'deepseek-r1:latest', // replace with your model name
|
||||
system: 'You are a helpful assistant.',
|
||||
prompt: 'Tell me a joke!',
|
||||
stream: false,
|
||||
think: false,
|
||||
}),
|
||||
})
|
||||
.then((res) => res.json())
|
||||
.then((data) => data.response)
|
||||
.then(console.log)
|
||||
.catch(console.error);
|
||||
|
||||
@@ -1,31 +1,60 @@
|
||||
import delay from 'delay';
|
||||
const endpoint = 'http://10.249.196.59:11434/api/generate';
|
||||
|
||||
fetch('http://127.0.0.1:11434/api/generate', {
|
||||
const options = {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model: 'deepseek-r1:latest',
|
||||
prompt: 'Hello! Tell me a long long joke.',
|
||||
prompt: 'Tell me a very long joke!',
|
||||
stream: true,
|
||||
think: false,
|
||||
}),
|
||||
})
|
||||
};
|
||||
|
||||
fetch(endpoint, options)
|
||||
.then((res) => {
|
||||
const reader = res.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
let buffer = '';
|
||||
|
||||
reader.read().then(async ({ done, value }) => {
|
||||
while (!done) {
|
||||
// Convert chunk to string
|
||||
const chunk = decoder.decode(value, { stream: true });
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
|
||||
// Optionally, print each chunk as it arrives
|
||||
const line = JSON.parse(chunk);
|
||||
process.stdout.write(line.response ?? '');
|
||||
const lines = buffer.split(/\r?\n/);
|
||||
buffer = lines.pop() ?? ''; // keep partial line for next chunk
|
||||
|
||||
for (const rawLine of lines) {
|
||||
const line = rawLine.trim();
|
||||
if (!line) continue;
|
||||
|
||||
let payload = line;
|
||||
if (payload.startsWith('data:')) {
|
||||
payload = payload.slice(5).trim();
|
||||
}
|
||||
if (!payload || payload === '[DONE]') continue;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(payload);
|
||||
process.stdout.write(parsed.response ?? parsed.text ?? '');
|
||||
} catch (err) {
|
||||
// If this chunk is not valid JSON, print it as raw text as a fallback.
|
||||
process.stdout.write(payload);
|
||||
}
|
||||
}
|
||||
|
||||
// Read the next chunk
|
||||
({ done, value } = await reader.read());
|
||||
}
|
||||
|
||||
if (buffer) {
|
||||
try {
|
||||
const parsed = JSON.parse(buffer);
|
||||
process.stdout.write(parsed.response ?? parsed.text ?? '');
|
||||
} catch (err) {
|
||||
process.stdout.write(buffer);
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(console.error);
|
||||
|
||||
Reference in New Issue
Block a user