fix: add user context to search reranking

This commit is contained in:
Manoj 2025-10-06 12:56:45 +05:30
parent 4de39a5871
commit 95786073ab
2 changed files with 21 additions and 2 deletions

View File

@ -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<SearchOptions>,
): Promise<StatementNode[]> {
// 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(

View File

@ -454,6 +454,7 @@ export async function applyLLMReranking(
bfs: StatementNode[];
},
limit: number = 10,
userContext?: { name?: string; userId: string },
): Promise<StatementNode[]> {
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