Asked what the first message of the conversation was, the assistant answered that it keeps no memory between interactions while the replayed context sat right in front of it. Two causes, both measured on the real model. The system prompt never said the history is its memory, nor who it is, so it fell back on its training habit of denying recall. And Ollama's default context window silently truncates from the beginning: on a 9194-token conversation only 4053 tokens were evaluated, so the oldest messages never reached the model at all. The window is now requested explicitly (16384 by default) and the warm-up asks for the same one, otherwise Ollama reloads the weights and throws the preheat away. The prompt states the identity and forbids the false denial, while still letting the assistant admit a message older than the replayed window.
92 lines
3.0 KiB
JavaScript
92 lines
3.0 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');
|
|
// Same window as a chat request, otherwise the model is reloaded and the
|
|
// warm-up is wasted.
|
|
assert.equal(warm.payload.options.num_ctx, 16_384);
|
|
});
|
|
|
|
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');
|
|
// The default window of many models (4096) would drop the oldest messages
|
|
// of a 40-message project before the model sees them.
|
|
assert.equal(chat.payload.options.num_ctx, 16_384);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|