Updated stream
This commit is contained in:
@@ -1,3 +1,17 @@
|
|||||||
import delay from 'delay';
|
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',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
model: 'deepseek-r1:latest',
|
model: 'deepseek-r1:latest',
|
||||||
prompt: 'Hello! Tell me a long long joke.',
|
prompt: 'Tell me a very long joke!',
|
||||||
stream: true,
|
stream: true,
|
||||||
think: false,
|
think: false,
|
||||||
}),
|
}),
|
||||||
})
|
};
|
||||||
|
|
||||||
|
fetch(endpoint, options)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
const reader = res.body.getReader();
|
const reader = res.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
|
let buffer = '';
|
||||||
|
|
||||||
reader.read().then(async ({ done, value }) => {
|
reader.read().then(async ({ done, value }) => {
|
||||||
while (!done) {
|
while (!done) {
|
||||||
// Convert chunk to string
|
buffer += decoder.decode(value, { stream: true });
|
||||||
const chunk = decoder.decode(value, { stream: true });
|
|
||||||
|
|
||||||
// Optionally, print each chunk as it arrives
|
const lines = buffer.split(/\r?\n/);
|
||||||
const line = JSON.parse(chunk);
|
buffer = lines.pop() ?? ''; // keep partial line for next chunk
|
||||||
process.stdout.write(line.response ?? '');
|
|
||||||
|
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());
|
({ 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);
|
.catch(console.error);
|
||||||
|
|||||||
Reference in New Issue
Block a user