feat(backend): keep the model resident and load it before it is asked
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.
This commit is contained in:
8 files changed
+285
-3
No files matched your search
@@ -98,6 +98,51 @@ describe('streaming over SSE', () => {
|
||||
assert.equal((await fetch(`${srv.base}/api/chat/stream`, { method: 'POST' })).status, 401);
|
||||
});
|
||||
|
||||
/** The last payload Ollama received, to check what the model was told. */
|
||||
function lastMessagesSent(seen) {
|
||||
const chat = srv.mock.requests
|
||||
.slice(seen)
|
||||
.filter((r) => r.url === '/api/chat')
|
||||
.at(-1);
|
||||
assert.ok(chat, 'Ollama should have received a chat request');
|
||||
return chat.payload.messages;
|
||||
}
|
||||
|
||||
// The whole point of a project: the model answers with its stored memory.
|
||||
it('replays the stored context to the model, in order', async () => {
|
||||
const p = await newProject();
|
||||
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
|
||||
token: user.token,
|
||||
body: { role: 'user', content: 'Mon code secret est BANANE42.' },
|
||||
});
|
||||
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
|
||||
token: user.token,
|
||||
body: { role: 'assistant', content: "C'est noté." },
|
||||
});
|
||||
|
||||
const seen = srv.mock.requests.length;
|
||||
const res = await fetch(`${srv.base}/api/projects/${p.id}/chat/stream`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
|
||||
body: JSON.stringify({ message: 'Quel est le code secret ?' }),
|
||||
});
|
||||
await readSse(res);
|
||||
|
||||
const messages = lastMessagesSent(seen);
|
||||
const system = messages.shift();
|
||||
assert.equal(system.role, 'system', 'the system prompt comes first');
|
||||
assert.ok(system.content.length > 0);
|
||||
|
||||
assert.deepEqual(
|
||||
messages.map((m) => [m.role, m.content]),
|
||||
[
|
||||
['user', 'Mon code secret est BANANE42.'],
|
||||
['assistant', "C'est noté."],
|
||||
['user', 'Quel est le code secret ?'],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
it('stateless stream: works', async () => {
|
||||
const res = await fetch(`${srv.base}/api/chat/stream`, {
|
||||
method: 'POST',
|
||||
@@ -168,6 +213,36 @@ describe('POST /api/chat (spec SSE contract)', () => {
|
||||
assert.ok(payloads.length - 1 >= 5, `chunks received: ${payloads.length - 1}`);
|
||||
});
|
||||
|
||||
// `POST /api/chat` has no database: the client's own history is what the
|
||||
// model sees, so it has to arrive whole and in order.
|
||||
it('forwards the whole conversation to the model', async () => {
|
||||
const seen = srv.mock.requests.length;
|
||||
const res = await postChat({
|
||||
messages: [
|
||||
{ role: 'user', content: 'Mon prénom est Damien.' },
|
||||
{ role: 'assistant', content: 'Enchanté Damien.' },
|
||||
{ role: 'user', content: 'Quel est mon prénom ?' },
|
||||
],
|
||||
});
|
||||
await readSsePayloads(res);
|
||||
|
||||
const chat = srv.mock.requests
|
||||
.slice(seen)
|
||||
.filter((r) => r.url === '/api/chat')
|
||||
.at(-1);
|
||||
const messages = chat.payload.messages;
|
||||
|
||||
assert.equal(messages[0].role, 'system');
|
||||
assert.deepEqual(
|
||||
messages.slice(1).map((m) => [m.role, m.content]),
|
||||
[
|
||||
['user', 'Mon prénom est Damien.'],
|
||||
['assistant', 'Enchanté Damien.'],
|
||||
['user', 'Quel est mon prénom ?'],
|
||||
],
|
||||
);
|
||||
});
|
||||
|
||||
it('answers with a real HTTP error when Ollama fails before the stream', async () => {
|
||||
const res = await postChat({ model: 'boom', messages: [{ role: 'user', content: 'boom' }] });
|
||||
|
||||
|
||||
Reference in new issue
Block a user