fix(backend): give the model its history back and its identity
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.
This commit is contained in:
5 files changed
+86
-2
No files matched your search
@@ -20,6 +20,7 @@ export async function startServer(options = {}) {
|
||||
ollamaUrl: mock.url,
|
||||
ollamaModel: 'mock:1',
|
||||
maxMessagesPerProject: options.maxMessagesPerProject,
|
||||
systemPrompt: options.systemPrompt,
|
||||
});
|
||||
|
||||
const db = createDatabase(config.dbPath, {
|
||||
|
||||
@@ -143,6 +143,25 @@ describe('streaming over SSE', () => {
|
||||
);
|
||||
});
|
||||
|
||||
// A weak model answers « je n'ai pas de mémoire » without being told what the
|
||||
// replayed history is and who it is.
|
||||
it('tells the model who it is and that the history is its memory', async () => {
|
||||
const seen = srv.mock.requests.length;
|
||||
const res = await fetch(`${srv.base}/api/chat/stream`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
|
||||
body: JSON.stringify({ message: 'Qui es-tu ?' }),
|
||||
});
|
||||
await readSse(res);
|
||||
|
||||
const system = lastMessagesSent(seen)[0];
|
||||
assert.equal(system.role, 'system');
|
||||
assert.match(system.content, /BlackAngelTV/, 'the identity must be stated');
|
||||
assert.match(system.content, /historique .* mémoire/i, 'the history is the memory');
|
||||
assert.match(system.content, /messages les plus anciens/, 'old turns count too');
|
||||
assert.match(system.content, /jamais que tu n'as pas de mémoire/, 'no false denial');
|
||||
});
|
||||
|
||||
it('stateless stream: works', async () => {
|
||||
const res = await fetch(`${srv.base}/api/chat/stream`, {
|
||||
method: 'POST',
|
||||
@@ -157,6 +176,33 @@ describe('streaming over SSE', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('system prompt override', () => {
|
||||
let srv;
|
||||
let user;
|
||||
|
||||
before(async () => {
|
||||
srv = await startServer({ systemPrompt: 'Tu es un modèle de test.' });
|
||||
user = await registerUser(srv.base, 'prompt@x.io');
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await srv.close();
|
||||
});
|
||||
|
||||
it('JARVIS_SYSTEM_PROMPT replaces the built-in identity', async () => {
|
||||
const res = await fetch(`${srv.base}/api/chat/stream`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
|
||||
body: JSON.stringify({ message: 'Bonjour' }),
|
||||
});
|
||||
await readSse(res);
|
||||
|
||||
const system = srv.mock.requests.at(-1).payload.messages[0];
|
||||
assert.equal(system.content, 'Tu es un modèle de test.');
|
||||
assert.doesNotMatch(system.content, /BlackAngelTV/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('POST /api/chat (spec SSE contract)', () => {
|
||||
let srv;
|
||||
let user;
|
||||
|
||||
@@ -36,6 +36,9 @@ describe('model warm-up', () => {
|
||||
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 () => {
|
||||
@@ -73,6 +76,9 @@ describe('model warm-up', () => {
|
||||
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 () => {
|
||||
|
||||
Reference in new issue
Block a user