diff --git a/backend/src/config.js b/backend/src/config.js index a55478b..e06790b 100644 --- a/backend/src/config.js +++ b/backend/src/config.js @@ -85,10 +85,32 @@ export function loadConfig(overrides = {}) { * model ready between two exchanges. */ keepAlive: overrides.ollamaKeepAlive ?? process.env.OLLAMA_KEEP_ALIVE ?? '30m', + /** + * Context window requested from Ollama. Its default (4096 tokens for many + * models) silently drops the **oldest** messages of a long conversation + * before the model ever sees them: a 40-message project easily exceeds it, + * and the assistant then answers as if it had no history. 16384 covers a + * full replayed conversation with room to spare. + */ + numCtx: overrides.ollamaNumCtx ?? Number(process.env.OLLAMA_NUM_CTX ?? 16_384), + /** + * Instructions sent ahead of every conversation. The identity and the + * memory clause are not decoration: without them a small model answers + * « je n'ai pas de mémoire entre les conversations » while the replayed + * history sits right in front of it. The last line also tells it what to + * say when the message asked for is older than the replayed window. + */ systemPrompt: overrides.systemPrompt ?? process.env.JARVIS_SYSTEM_PROMPT ?? - "Tu es Jarvis, l'assistant personnel de l'utilisateur. Réponds de façon claire, concise et utile, en français sauf si on te demande une autre langue.", + "Tu es Jarvis, l'assistant personnel de l'utilisateur, développé par BlackAngelTV. " + + 'Réponds de façon claire, concise et utile, en français sauf si on te demande une autre langue. ' + + "L'historique de la conversation ci-dessus est ta mémoire : relis-le et appuie-toi dessus, " + + 'y compris sur les messages les plus anciens, pour répondre. ' + + "Ne déclare jamais que tu n'as pas de mémoire ou que tu ne peux pas relier les messages " + + "entre eux : si l'information demandée ne figure pas dans l'historique, dis simplement que " + + "tu ne l'as pas reçue. " + + 'Si on te demande qui tu es, réponds que tu es Jarvis, développé par BlackAngelTV.', }, }; } diff --git a/backend/src/services/ollama.js b/backend/src/services/ollama.js index 8ba43f5..92802ff 100644 --- a/backend/src/services/ollama.js +++ b/backend/src/services/ollama.js @@ -21,6 +21,9 @@ export function createOllama(config) { // Keep the weights resident between two questions: reloading them costs // seconds and is what makes a cold answer feel slow. keep_alive: config.ollama.keepAlive, + // Without an explicit window, Ollama keeps its small default and drops the + // oldest messages of a long conversation on the floor. + options: { num_ctx: config.ollama.numCtx }, messages: [{ role: 'system', content: config.ollama.systemPrompt }, ...messages], }; } @@ -84,7 +87,13 @@ export function createOllama(config) { response = await fetch(`${baseUrl}/api/generate`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ model: target, keep_alive: config.ollama.keepAlive }), + // Same window as a chat request: Ollama reloads the model when the + // requested context size changes, which would throw the warm-up away. + body: JSON.stringify({ + model: target, + keep_alive: config.ollama.keepAlive, + options: { num_ctx: config.ollama.numCtx }, + }), }); } catch (cause) { throw new OllamaError(`Cannot reach Ollama (${baseUrl}).`, 502); diff --git a/backend/test/helpers.js b/backend/test/helpers.js index 4f3dce1..d8ac951 100644 --- a/backend/test/helpers.js +++ b/backend/test/helpers.js @@ -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, { diff --git a/backend/test/stream.test.js b/backend/test/stream.test.js index 5ed0388..b2154de 100644 --- a/backend/test/stream.test.js +++ b/backend/test/stream.test.js @@ -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; diff --git a/backend/test/warm.test.js b/backend/test/warm.test.js index 330f5d8..04e974a 100644 --- a/backend/test/warm.test.js +++ b/backend/test/warm.test.js @@ -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 () => {