feat(backend): recite the project context to verify what the model receives
Storing a context is not enough: the user needs to know it actually reaches Ollama, and that the messages replayed are the ones they expect. The read-only probe answers that by asking the model to recite its context and reporting how many messages were replayed, while persisting nothing.
This commit is contained in:
2 files changed
+94
-5
No files matched your search
@@ -57,9 +57,23 @@ const projectChatSchema = {
|
||||
},
|
||||
};
|
||||
|
||||
const recallSchema = {
|
||||
type: 'object',
|
||||
additionalProperties: false,
|
||||
properties: {
|
||||
message: { type: 'string', minLength: 1, maxLength: 2000 },
|
||||
model: { type: 'string', maxLength: 120 },
|
||||
},
|
||||
};
|
||||
|
||||
/** Max number of project messages fed back to Ollama as context. */
|
||||
const CONTEXT_LIMIT = 40;
|
||||
|
||||
/** Question asked by the client's « vérifier le contexte » button. */
|
||||
const RECALL_PROMPT =
|
||||
"Rappelle en deux ou trois phrases ce que tu retiens du contexte de ce projet. " +
|
||||
"Si le contexte est vide, dis-le clairement.";
|
||||
|
||||
/**
|
||||
* Project routes: each project belongs to the authenticated user and its
|
||||
* messages form the context sent to Jarvis.
|
||||
@@ -76,12 +90,20 @@ export async function projectRoutes(fastify) {
|
||||
return project;
|
||||
}
|
||||
|
||||
/** Context sent to Ollama: project history + the new prompt. */
|
||||
function buildMessages(projectId, message) {
|
||||
const history = db
|
||||
/**
|
||||
* The project's stored context, oldest first, as Ollama sees it.
|
||||
* Read on every request: the context is never cached on the client, so what
|
||||
* the model answers with is always what the database holds right now.
|
||||
*/
|
||||
function readContext(projectId) {
|
||||
return db
|
||||
.listMessages(projectId, { limit: CONTEXT_LIMIT })
|
||||
.map((m) => ({ role: m.role, content: m.content }));
|
||||
return [...history, { role: 'user', content: message }];
|
||||
}
|
||||
|
||||
/** Context sent to Ollama: project history + the new prompt. */
|
||||
function buildMessages(projectId, message) {
|
||||
return [...readContext(projectId), { role: 'user', content: message }];
|
||||
}
|
||||
|
||||
fastify.post('/api/projects', { ...auth, schema: { body: createSchema } }, async (request, reply) => {
|
||||
@@ -201,6 +223,34 @@ export async function projectRoutes(fastify) {
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Asks the model to recite the context it just received, to verify that the
|
||||
* stored history really reaches Ollama. Nothing is persisted: this is a
|
||||
* read-only probe. The `done` event carries `contextMessages`, the exact
|
||||
* number of stored messages replayed to the model.
|
||||
*/
|
||||
fastify.post(
|
||||
'/api/projects/:id/context/recall',
|
||||
{ ...auth, schema: { params: paramsSchema, body: recallSchema } },
|
||||
async (request, reply) => {
|
||||
const project = requireProject(request);
|
||||
const history = readContext(project.id);
|
||||
const messages = [
|
||||
...history,
|
||||
{ role: 'user', content: request.body.message ?? RECALL_PROMPT },
|
||||
];
|
||||
|
||||
return streamChatToClient({
|
||||
ollama: fastify.ollama,
|
||||
reply,
|
||||
request,
|
||||
messages,
|
||||
model: request.body.model,
|
||||
onComplete: () => ({ contextMessages: history.length }),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Same, but streamed: each chunk is sent as soon as Ollama produces it.
|
||||
fastify.post(
|
||||
'/api/projects/:id/chat/stream',
|
||||
|
||||
Reference in new issue
Block a user