Ollama drops an idle model after five minutes by default, so the first question after a pause pays the whole several-second load. Every chat request now carries keep_alive (30 minutes by default) and POST /api/chat/warm loads the weights on demand, letting a client wake the model while the user is still writing. A warm-up that fails answers warmed=false instead of an error: it is an optimisation, not a step the answer depends on.
100 lines
3.2 KiB
JavaScript
100 lines
3.2 KiB
JavaScript
import http from 'node:http';
|
|
|
|
const DEFAULT_PIECES = ['Bon', 'jour', ' ', 'Tony', ', ', 'je ', 'suis ', 'Jarvis', '.'];
|
|
|
|
/**
|
|
* Faux serveur Ollama : répond en NDJSON morceau par morceau quand `stream`
|
|
* est vrai, renvoie `done_reason: "load"` sur `/api/generate`, et renvoie une
|
|
* erreur si le modèle vaut "boom". Chaque requête reçue est conservée dans
|
|
* `requests` pour que les tests vérifient ce qui part réellement vers Ollama.
|
|
*/
|
|
export async function startMockOllama({ pieces = DEFAULT_PIECES, delay = 2 } = {}) {
|
|
// Every payload the backend sent, so a test can check what Ollama received
|
|
// (the model, `keep_alive`, the replayed context…).
|
|
const requests = [];
|
|
|
|
const server = http.createServer((req, res) => {
|
|
if (req.method === 'GET' && req.url === '/api/tags') {
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ models: [{ name: 'mock:1' }] }));
|
|
return;
|
|
}
|
|
|
|
// "Load the model": Ollama answers as soon as the weights are resident.
|
|
if (req.method === 'POST' && req.url === '/api/generate') {
|
|
let body = '';
|
|
req.on('data', (c) => (body += c));
|
|
req.on('end', () => {
|
|
const payload = JSON.parse(body || '{}');
|
|
requests.push({ url: req.url, payload });
|
|
|
|
if (payload.model === 'boom') {
|
|
res.writeHead(500, { 'Content-Type': 'application/json' }).end('{"error":"boom"}');
|
|
return;
|
|
}
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(
|
|
JSON.stringify({
|
|
model: payload.model,
|
|
response: '',
|
|
done: true,
|
|
done_reason: 'load',
|
|
}),
|
|
);
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && req.url === '/api/chat') {
|
|
let body = '';
|
|
req.on('data', (c) => (body += c));
|
|
req.on('end', () => {
|
|
const payload = JSON.parse(body || '{}');
|
|
requests.push({ url: req.url, payload });
|
|
|
|
if (payload.model === 'boom') {
|
|
res.writeHead(500, { 'Content-Type': 'application/json' }).end('{"error":"boom"}');
|
|
return;
|
|
}
|
|
|
|
const full = pieces.join('');
|
|
|
|
if (!payload.stream) {
|
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
res.end(JSON.stringify({ model: payload.model, message: { role: 'assistant', content: full }, done: true }));
|
|
return;
|
|
}
|
|
|
|
res.writeHead(200, { 'Content-Type': 'application/x-ndjson' });
|
|
let i = 0;
|
|
const timer = setInterval(() => {
|
|
if (i < pieces.length) {
|
|
res.write(
|
|
JSON.stringify({ model: payload.model, message: { role: 'assistant', content: pieces[i] }, done: false }) +
|
|
'\n',
|
|
);
|
|
i++;
|
|
} else {
|
|
clearInterval(timer);
|
|
res.write(JSON.stringify({ model: payload.model, done: true }) + '\n');
|
|
res.end();
|
|
}
|
|
}, delay);
|
|
});
|
|
return;
|
|
}
|
|
|
|
res.writeHead(404).end();
|
|
});
|
|
|
|
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
|
|
const { port } = server.address();
|
|
|
|
return {
|
|
url: `http://127.0.0.1:${port}`,
|
|
requests,
|
|
close: () => new Promise((resolve) => server.close(resolve)),
|
|
};
|
|
}
|