From 95786073ab2cbaf943edb6b569300127aed695ad Mon Sep 17 00:00:00 2001 From: Manoj Date: Mon, 6 Oct 2025 12:56:45 +0530 Subject: [PATCH] fix: add user context to search reranking --- apps/webapp/app/services/search.server.ts | 11 ++++++++++- apps/webapp/app/services/search/rerank.ts | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/apps/webapp/app/services/search.server.ts b/apps/webapp/app/services/search.server.ts index 6e14c8b..05444f1 100644 --- a/apps/webapp/app/services/search.server.ts +++ b/apps/webapp/app/services/search.server.ts @@ -68,6 +68,7 @@ export class SearchService { // 2. Apply reranking strategy const rankedStatements = await this.rerankResults( query, + userId, { bm25: bm25Results, vector: vectorResults, bfs: bfsResults }, opts, ); @@ -223,6 +224,7 @@ export class SearchService { */ private async rerankResults( query: string, + userId: string, results: { bm25: StatementNode[]; vector: StatementNode[]; @@ -230,8 +232,15 @@ export class SearchService { }, options: Required, ): Promise { + // Fetch user profile for context + const user = await prisma.user.findUnique({ + where: { id: userId }, + select: { name: true, id: true }, + }); - return applyLLMReranking(query, results,); + const userContext = user ? { name: user.name ?? undefined, userId: user.id } : undefined; + + return applyLLMReranking(query, results, 10, userContext); } private async logRecallAsync( diff --git a/apps/webapp/app/services/search/rerank.ts b/apps/webapp/app/services/search/rerank.ts index b29f41e..0566a5d 100644 --- a/apps/webapp/app/services/search/rerank.ts +++ b/apps/webapp/app/services/search/rerank.ts @@ -454,6 +454,7 @@ export async function applyLLMReranking( bfs: StatementNode[]; }, limit: number = 10, + userContext?: { name?: string; userId: string }, ): Promise { const allResults = [ ...results.bm25.slice(0, 100), @@ -469,8 +470,16 @@ export async function applyLLMReranking( } - const prompt = `You are a relevance filter. Given a user query and a list of facts, identify ONLY the facts that are truly relevant to answering the query. + // Build user context section if provided + const userContextSection = userContext?.name + ? `\nUser Identity Context: +- The user's name is "${userContext.name}" +- References to "user", "${userContext.name}", or pronouns like "my/their" refer to the same person +- When matching queries about "user's X" or "${userContext.name}'s X", these are equivalent\n` + : ''; + const prompt = `You are a relevance filter. Given a user query and a list of facts, identify ONLY the facts that are truly relevant to answering the query. +${userContextSection} Query: "${query}" Facts: @@ -480,6 +489,7 @@ Instructions: - A fact is RELEVANT if it directly answers or provides information needed to answer the query - A fact is NOT RELEVANT if it's tangentially related but doesn't answer the query - Consider semantic meaning, not just keyword matching +${userContext?.name ? `- Remember: "user", "${userContext.name}", and possessive references ("my", "their") all refer to the same person` : ''} - Only return facts with HIGH relevance (≥80% confidence) - If you are not sure, return an empty array