@framers/agentos 0.1.156 → 0.1.158
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +554 -1914
- package/dist/api/agent.d.ts.map +1 -1
- package/dist/api/agent.js +11 -0
- package/dist/api/agent.js.map +1 -1
- package/dist/api/types.d.ts +15 -0
- package/dist/api/types.d.ts.map +1 -1
- package/dist/api/types.js.map +1 -1
- package/dist/query-router/QueryRouter.d.ts.map +1 -1
- package/dist/query-router/QueryRouter.js.map +1 -1
- package/dist/query-router/types.d.ts +12 -0
- package/dist/query-router/types.d.ts.map +1 -1
- package/dist/query-router/types.js +1 -0
- package/dist/query-router/types.js.map +1 -1
- package/dist/rag/citation/CitationVerifier.d.ts +29 -0
- package/dist/rag/citation/CitationVerifier.d.ts.map +1 -0
- package/dist/rag/citation/CitationVerifier.js +116 -0
- package/dist/rag/citation/CitationVerifier.js.map +1 -0
- package/dist/rag/citation/cosine.d.ts +10 -0
- package/dist/rag/citation/cosine.d.ts.map +1 -0
- package/dist/rag/citation/cosine.js +21 -0
- package/dist/rag/citation/cosine.js.map +1 -0
- package/dist/rag/citation/index.d.ts +8 -0
- package/dist/rag/citation/index.d.ts.map +1 -0
- package/dist/rag/citation/index.js +7 -0
- package/dist/rag/citation/index.js.map +1 -0
- package/dist/rag/citation/types.d.ts +68 -0
- package/dist/rag/citation/types.d.ts.map +1 -0
- package/dist/rag/citation/types.js +9 -0
- package/dist/rag/citation/types.js.map +1 -0
- package/dist/rag/index.d.ts +2 -0
- package/dist/rag/index.d.ts.map +1 -1
- package/dist/rag/index.js +2 -0
- package/dist/rag/index.js.map +1 -1
- package/dist/rag/reranking/IRerankerService.d.ts +9 -0
- package/dist/rag/reranking/IRerankerService.d.ts.map +1 -1
- package/dist/rag/reranking/RerankerService.d.ts +12 -1
- package/dist/rag/reranking/RerankerService.d.ts.map +1 -1
- package/dist/rag/reranking/RerankerService.js +46 -0
- package/dist/rag/reranking/RerankerService.js.map +1 -1
- package/dist/rag/reranking/index.d.ts +2 -0
- package/dist/rag/reranking/index.d.ts.map +1 -1
- package/dist/rag/reranking/index.js +1 -0
- package/dist/rag/reranking/index.js.map +1 -1
- package/dist/rag/reranking/providers/LlmJudgeReranker.d.ts +55 -0
- package/dist/rag/reranking/providers/LlmJudgeReranker.d.ts.map +1 -0
- package/dist/rag/reranking/providers/LlmJudgeReranker.js +121 -0
- package/dist/rag/reranking/providers/LlmJudgeReranker.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
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]`;
|
|
21
|
+
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"]`;
|
|
22
|
+
/** Two-phase LLM-based reranker: batch pointwise + listwise top-K. */
|
|
23
|
+
export class LlmJudgeReranker {
|
|
24
|
+
constructor(config) {
|
|
25
|
+
this.providerId = 'llm-judge';
|
|
26
|
+
this.llmCallFn = config.llmCallFn;
|
|
27
|
+
this.scoringModel = config.scoringModel;
|
|
28
|
+
this.rankingModel = config.rankingModel;
|
|
29
|
+
this.maxPointwiseDocuments = config.maxPointwiseDocuments ?? 100;
|
|
30
|
+
this.pointwiseTopK = config.pointwiseTopK ?? 20;
|
|
31
|
+
this.batchSize = config.batchSize ?? 10;
|
|
32
|
+
}
|
|
33
|
+
async isAvailable() {
|
|
34
|
+
return typeof this.llmCallFn === 'function';
|
|
35
|
+
}
|
|
36
|
+
async rerank(input, config) {
|
|
37
|
+
const topN = config.topN ?? this.pointwiseTopK;
|
|
38
|
+
let documents = input.documents;
|
|
39
|
+
if (documents.length > this.maxPointwiseDocuments) {
|
|
40
|
+
documents = documents.slice(0, this.maxPointwiseDocuments);
|
|
41
|
+
}
|
|
42
|
+
// Phase 1: Batch pointwise scoring
|
|
43
|
+
const scored = await this.batchPointwiseScore(input.query, documents);
|
|
44
|
+
// Sort by score descending, take top-K for phase 2
|
|
45
|
+
scored.sort((a, b) => b.score - a.score);
|
|
46
|
+
const candidates = scored.slice(0, this.pointwiseTopK);
|
|
47
|
+
// Phase 2: Listwise ranking
|
|
48
|
+
let finalRanking;
|
|
49
|
+
try {
|
|
50
|
+
finalRanking = await this.listwiseRank(input.query, candidates, topN);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
// Fallback: use pointwise scores
|
|
54
|
+
finalRanking = candidates.slice(0, topN).map((c, i) => ({
|
|
55
|
+
id: c.id,
|
|
56
|
+
content: c.content,
|
|
57
|
+
relevanceScore: 1 - (i / Math.max(topN, 1)),
|
|
58
|
+
originalScore: c.originalScore,
|
|
59
|
+
metadata: c.metadata,
|
|
60
|
+
}));
|
|
61
|
+
}
|
|
62
|
+
return { results: finalRanking };
|
|
63
|
+
}
|
|
64
|
+
/** Phase 1: Score documents in batches. */
|
|
65
|
+
async batchPointwiseScore(query, documents) {
|
|
66
|
+
const batches = [];
|
|
67
|
+
for (let i = 0; i < documents.length; i += this.batchSize) {
|
|
68
|
+
batches.push(documents.slice(i, i + this.batchSize));
|
|
69
|
+
}
|
|
70
|
+
const results = [];
|
|
71
|
+
for (const batch of batches) {
|
|
72
|
+
const docList = batch
|
|
73
|
+
.map((d, i) => `[${i + 1}] ${d.content.slice(0, 200)}`)
|
|
74
|
+
.join('\n');
|
|
75
|
+
const userPrompt = `Query: "${query}"\n\nDocuments:\n${docList}`;
|
|
76
|
+
try {
|
|
77
|
+
const raw = await this.llmCallFn(POINTWISE_SYSTEM, userPrompt, this.scoringModel);
|
|
78
|
+
const cleaned = raw.replace(/```json?\n?/g, '').replace(/```/g, '').trim();
|
|
79
|
+
const scores = JSON.parse(cleaned);
|
|
80
|
+
for (let i = 0; i < batch.length; i++) {
|
|
81
|
+
results.push({
|
|
82
|
+
...batch[i],
|
|
83
|
+
score: typeof scores[i] === 'number' ? scores[i] : 0,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
for (const doc of batch) {
|
|
89
|
+
results.push({ ...doc, score: 0 });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return results;
|
|
94
|
+
}
|
|
95
|
+
/** Phase 2: Listwise ranking of top candidates. */
|
|
96
|
+
async listwiseRank(query, candidates, topN) {
|
|
97
|
+
const docList = candidates
|
|
98
|
+
.map((d) => `[${d.id}] ${d.content.slice(0, 200)}`)
|
|
99
|
+
.join('\n');
|
|
100
|
+
const userPrompt = `Query: "${query}"\n\nDocuments:\n${docList}`;
|
|
101
|
+
const raw = await this.llmCallFn(LISTWISE_SYSTEM, userPrompt, this.rankingModel);
|
|
102
|
+
const cleaned = raw.replace(/```json?\n?/g, '').replace(/```/g, '').trim();
|
|
103
|
+
const ranking = JSON.parse(cleaned);
|
|
104
|
+
const candidateMap = new Map(candidates.map((c) => [c.id, c]));
|
|
105
|
+
const results = [];
|
|
106
|
+
for (let i = 0; i < Math.min(ranking.length, topN); i++) {
|
|
107
|
+
const doc = candidateMap.get(ranking[i]);
|
|
108
|
+
if (!doc)
|
|
109
|
+
continue;
|
|
110
|
+
results.push({
|
|
111
|
+
id: doc.id,
|
|
112
|
+
content: doc.content,
|
|
113
|
+
relevanceScore: 1 - (i / Math.max(ranking.length, 1)),
|
|
114
|
+
originalScore: doc.originalScore,
|
|
115
|
+
metadata: doc.metadata,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return results;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=LlmJudgeReranker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LlmJudgeReranker.js","sourceRoot":"","sources":["../../../../src/rag/reranking/providers/LlmJudgeReranker.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AA4BH,MAAM,gBAAgB,GAAG,2PAA2P,CAAC;AAErR,MAAM,eAAe,GAAG,+LAA+L,CAAC;AAExN,sEAAsE;AACtE,MAAM,OAAO,gBAAgB;IAU3B,YAAY,MAA8B;QAT1B,eAAU,GAAG,WAAoB,CAAC;QAUhD,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,qBAAqB,GAAG,MAAM,CAAC,qBAAqB,IAAI,GAAG,CAAC;QACjE,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC,aAAa,IAAI,EAAE,CAAC;QAChD,IAAI,CAA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