|
| 1 | +/** |
| 2 | + * @fileoverview LLM-as-Judge Reranker — two-phase hybrid reranking using LLM calls. |
| 3 | + * |
| 4 | + * Phase 1: Batch pointwise scoring with a cheap model (gpt-4o-mini, haiku). |
| 5 | + * Groups documents into batches of 10, asks LLM to score 0-10. |
| 6 | + * Phase 2: Listwise final ranking with a synthesis model. |
| 7 | + * Takes top-K from phase 1, asks LLM to rank by relevance. |
| 8 | + * |
| 9 | + * Cognitive science: Combines absolute judgment (pointwise) with comparative |
| 10 | + * judgment (listwise) — mirrors how human expert reviewers evaluate documents. |
| 11 | + * |
| 12 | + * References: |
| 13 | + * - Sun, W., et al. (2023). "Is ChatGPT Good at Search? Investigating Large |
| 14 | + * Language Models as Re-Ranking Agents." arXiv:2304.09542 |
| 15 | + * - Qin, Z., et al. (2023). "Large Language Models are Effective Text Rankers |
| 16 | + * with Pairwise Ranking Prompting." arXiv:2306.17563 |
| 17 | + * |
| 18 | + * @module agentos/rag/reranking/providers/LlmJudgeReranker |
| 19 | + */ |
| 20 | + |
| 21 | +import type { |
| 22 | + IRerankerProvider, |
| 23 | + RerankerInput, |
| 24 | + RerankerOutput, |
| 25 | + RerankerRequestConfig, |
| 26 | + RerankedDocument, |
| 27 | +} from '../IRerankerService.js'; |
| 28 | + |
| 29 | +/** Configuration for the LLM judge reranker. */ |
| 30 | +export interface LlmJudgeRerankerConfig { |
| 31 | + /** LLM call function: (systemPrompt, userPrompt, model?) → response text. */ |
| 32 | + llmCallFn: (system: string, user: string, model?: string) => Promise<string>; |
| 33 | + /** Model for batch pointwise scoring (cheap). Auto-detected if not set. */ |
| 34 | + scoringModel?: string; |
| 35 | + /** Model for listwise final ranking (better). Agent's primary if not set. */ |
| 36 | + rankingModel?: string; |
| 37 | + /** Max documents to process in phase 1. */ |
| 38 | + maxPointwiseDocuments?: number; |
| 39 | + /** How many survive phase 1 into phase 2. */ |
| 40 | + pointwiseTopK?: number; |
| 41 | + /** Timeout per LLM call in ms. */ |
| 42 | + timeoutMs?: number; |
| 43 | + /** Batch size for pointwise scoring. */ |
| 44 | + batchSize?: number; |
| 45 | +} |
| 46 | + |
| 47 | +const POINTWISE_SYSTEM = `You are a relevance scorer. Rate each document's relevance to the query on a scale of 0-10. 10 = perfectly relevant, 0 = completely irrelevant. Return ONLY a JSON array of integer scores, one per document, in the same order. Example: [8, 3, 7, 2, 9]`; |
| 48 | + |
| 49 | +const LISTWISE_SYSTEM = `You are a relevance ranker. Rank the documents by relevance to the query, most relevant first. Return ONLY a JSON array of document IDs in ranked order. Example: ["doc-3", "doc-1", "doc-5"]`; |
| 50 | + |
| 51 | +/** Two-phase LLM-based reranker: batch pointwise + listwise top-K. */ |
| 52 | +export class LlmJudgeReranker implements IRerankerProvider { |
| 53 | + public readonly providerId = 'llm-judge' as const; |
| 54 | + |
| 55 | + private readonly llmCallFn: LlmJudgeRerankerConfig['llmCallFn']; |
| 56 | + private readonly scoringModel?: string; |
| 57 | + private readonly rankingModel?: string; |
| 58 | + private readonly maxPointwiseDocuments: number; |
| 59 | + private readonly pointwiseTopK: number; |
| 60 | + private readonly batchSize: number; |
| 61 | + |
| 62 | + constructor(config: LlmJudgeRerankerConfig) { |
| 63 | + this.llmCallFn = config.llmCallFn; |
| 64 | + this.scoringModel = config.scoringModel; |
| 65 | + this.rankingModel = config.rankingModel; |
| 66 | + this.maxPointwiseDocuments = config.maxPointwiseDocuments ?? 100; |
| 67 | + this.pointwiseTopK = config.pointwiseTopK ?? 20; |
| 68 | + this.batchSize = config.batchSize ?? 10; |
| 69 | + } |
| 70 | + |
| 71 | + async isAvailable(): Promise<boolean> { |
| 72 | + return typeof this.llmCallFn === 'function'; |
| 73 | + } |
| 74 | + |
| 75 | + async rerank(input: RerankerInput, config: RerankerRequestConfig): Promise<RerankerOutput> { |
| 76 | + const topN = config.topN ?? this.pointwiseTopK; |
| 77 | + let documents = input.documents; |
| 78 | + |
| 79 | + if (documents.length > this.maxPointwiseDocuments) { |
| 80 | + documents = documents.slice(0, this.maxPointwiseDocuments); |
| 81 | + } |
| 82 | + |
| 83 | + // Phase 1: Batch pointwise scoring |
| 84 | + const scored = await this.batchPointwiseScore(input.query, documents); |
| 85 | + |
| 86 | + // Sort by score descending, take top-K for phase 2 |
| 87 | + scored.sort((a, b) => b.score - a.score); |
| 88 | + const candidates = scored.slice(0, this.pointwiseTopK); |
| 89 | + |
| 90 | + // Phase 2: Listwise ranking |
| 91 | + let finalRanking: RerankedDocument[]; |
| 92 | + try { |
| 93 | + finalRanking = await this.listwiseRank(input.query, candidates, topN); |
| 94 | + } catch { |
| 95 | + // Fallback: use pointwise scores |
| 96 | + finalRanking = candidates.slice(0, topN).map((c, i) => ({ |
| 97 | + id: c.id, |
| 98 | + content: c.content, |
| 99 | + relevanceScore: 1 - (i / Math.max(topN, 1)), |
| 100 | + originalScore: c.originalScore, |
| 101 | + metadata: c.metadata, |
| 102 | + })); |
| 103 | + } |
| 104 | + |
| 105 | + return { results: finalRanking }; |
| 106 | + } |
| 107 | + |
| 108 | + /** Phase 1: Score documents in batches. */ |
| 109 | + private async batchPointwiseScore( |
| 110 | + query: string, |
| 111 | + documents: RerankerInput['documents'], |
| 112 | + ): Promise<Array<RerankerInput['documents'][number] & { score: number }>> { |
| 113 | + const batches: RerankerInput['documents'][] = []; |
| 114 | + for (let i = 0; i < documents.length; i += this.batchSize) { |
| 115 | + batches.push(documents.slice(i, i + this.batchSize)); |
| 116 | + } |
| 117 | + |
| 118 | + const results: Array<RerankerInput['documents'][number] & { score: number }> = []; |
| 119 | + |
| 120 | + for (const batch of batches) { |
| 121 | + const docList = batch |
| 122 | + .map((d, i) => `[${i + 1}] ${d.content.slice(0, 200)}`) |
| 123 | + .join('\n'); |
| 124 | + const userPrompt = `Query: "${query}"\n\nDocuments:\n${docList}`; |
| 125 | + |
| 126 | + try { |
| 127 | + const raw = await this.llmCallFn(POINTWISE_SYSTEM, userPrompt, this.scoringModel); |
| 128 | + const cleaned = raw.replace(/```json?\n?/g, '').replace(/```/g, '').trim(); |
| 129 | + const scores = JSON.parse(cleaned) as number[]; |
| 130 | + |
| 131 | + for (let i = 0; i < batch.length; i++) { |
| 132 | + results.push({ |
| 133 | + ...batch[i], |
| 134 | + score: typeof scores[i] === 'number' ? scores[i] : 0, |
| 135 | + }); |
| 136 | + } |
| 137 | + } catch { |
| 138 | + for (const doc of batch) { |
| 139 | + results.push({ ...doc, score: 0 }); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return results; |
| 145 | + } |
| 146 | + |
| 147 | + /** Phase 2: Listwise ranking of top candidates. */ |
| 148 | + private async listwiseRank( |
| 149 | + query: string, |
| 150 | + candidates: Array<RerankerInput['documents'][number] & { score: number }>, |
| 151 | + topN: number, |
| 152 | + ): Promise<RerankedDocument[]> { |
| 153 | + const docList = candidates |
| 154 | + .map((d) => `[${d.id}] ${d.content.slice(0, 200)}`) |
| 155 | + .join('\n'); |
| 156 | + const userPrompt = `Query: "${query}"\n\nDocuments:\n${docList}`; |
| 157 | + |
| 158 | + const raw = await this.llmCallFn(LISTWISE_SYSTEM, userPrompt, this.rankingModel); |
| 159 | + const cleaned = raw.replace(/```json?\n?/g, '').replace(/```/g, '').trim(); |
| 160 | + const ranking = JSON.parse(cleaned) as string[]; |
| 161 | + |
| 162 | + const candidateMap = new Map(candidates.map((c) => [c.id, c])); |
| 163 | + const results: RerankedDocument[] = []; |
| 164 | + |
| 165 | + for (let i = 0; i < Math.min(ranking.length, topN); i++) { |
| 166 | + const doc = candidateMap.get(ranking[i]); |
| 167 | + if (!doc) continue; |
| 168 | + results.push({ |
| 169 | + id: doc.id, |
| 170 | + content: doc.content, |
| 171 | + relevanceScore: 1 - (i / Math.max(ranking.length, 1)), |
| 172 | + originalScore: doc.originalScore, |
| 173 | + metadata: doc.metadata, |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + return results; |
| 178 | + } |
| 179 | +} |
0 commit comments