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.
86 lines
2.6 KiB
JavaScript
86 lines
2.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { after, before, describe, it } from 'node:test';
|
|
|
|
import { api, registerUser, startServer } from './helpers.js';
|
|
|
|
/**
|
|
* Warming the model: the client asks for the weights to be resident before the
|
|
* question that needs them, because a cold load costs seconds.
|
|
*/
|
|
describe('model warm-up', () => {
|
|
let srv;
|
|
let user;
|
|
|
|
before(async () => {
|
|
srv = await startServer();
|
|
user = await registerUser(srv.base, 'warm@x.io');
|
|
});
|
|
|
|
after(async () => {
|
|
await srv.close();
|
|
});
|
|
|
|
it('loads the requested model through /api/generate', async () => {
|
|
const seen = srv.mock.requests.length;
|
|
|
|
const res = await api(srv.base, 'POST', '/api/chat/warm', {
|
|
token: user.token,
|
|
body: { model: 'qwen2.5:3b' },
|
|
});
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.deepEqual(res.json, { model: 'qwen2.5:3b', warmed: true });
|
|
|
|
const warm = srv.mock.requests.slice(seen).find((r) => r.url === '/api/generate');
|
|
assert.ok(warm, 'Ollama should have received a /api/generate call');
|
|
assert.equal(warm.payload.model, 'qwen2.5:3b');
|
|
assert.equal(warm.payload.keep_alive, '30m');
|
|
assert.equal(warm.payload.prompt, undefined, 'nothing should be generated');
|
|
});
|
|
|
|
it('falls back to the configured model when none is named', async () => {
|
|
const res = await api(srv.base, 'POST', '/api/chat/warm', {
|
|
token: user.token,
|
|
body: {},
|
|
});
|
|
|
|
assert.equal(res.status, 200);
|
|
assert.equal(res.json.warmed, true);
|
|
assert.equal(res.json.model, 'mock:1');
|
|
});
|
|
|
|
it('stays best effort when Ollama refuses to load the model', async () => {
|
|
const res = await api(srv.base, 'POST', '/api/chat/warm', {
|
|
token: user.token,
|
|
body: { model: 'boom' },
|
|
});
|
|
|
|
// A warm-up that fails must not look like an error to the client: the real
|
|
// chat request reports the problem.
|
|
assert.equal(res.status, 200);
|
|
assert.deepEqual(res.json, { model: 'boom', warmed: false });
|
|
});
|
|
|
|
it('keeps the model resident on every chat request', async () => {
|
|
const seen = srv.mock.requests.length;
|
|
|
|
const res = await api(srv.base, 'POST', '/api/chat', {
|
|
token: user.token,
|
|
body: { messages: [{ role: 'user', content: 'Bonjour' }] },
|
|
});
|
|
assert.equal(res.status, 200);
|
|
|
|
const chat = srv.mock.requests.slice(seen).find((r) => r.url === '/api/chat');
|
|
assert.ok(chat, 'Ollama should have received a /api/chat call');
|
|
assert.equal(chat.payload.keep_alive, '30m');
|
|
});
|
|
|
|
it('requires a token', async () => {
|
|
const res = await api(srv.base, 'POST', '/api/chat/warm', {
|
|
body: { model: 'qwen2.5:3b' },
|
|
});
|
|
|
|
assert.equal(res.status, 401);
|
|
});
|
|
});
|