fuseResults
in ContextfuseResults
is Calledcontext-mixer.ts
ContextMixer.getContext
const fusedResults = fuseResults(
results.map(r => r.snippets),
result => {
if (typeof result.startLine === 'undefined' || typeof result.endLine === 'undefined') {
return [result.uri.toString()];
}
const lineIds = [];
for (let i = result.startLine; i <= result.endLine; i++) {
lineIds.push(`${result.uri.toString()}:${i}`);
}
return lineIds;
}
);
fuseResults
is Definedexport function fuseResults<T>(
retrievedSets: Set<T>[],
rankingIdentities: (result: T) => string[]
): Set<T> {
const resultsByDocument = new Map<string, Map<number, T[]>>();
// Map results by their identifiers
retrievedSets.forEach((results, retrieverIndex) => {
for (const result of results) {
for (const id of rankingIdentities(result)) {
if (!resultsByDocument.has(id)) {
resultsByDocument.set(id, new Map());
}
const document = resultsByDocument.get(id)!;
if (!document.has(retrieverIndex)) {
document.set(retrieverIndex, []);
}
document.get(retrieverIndex)!.push(result);
}
}
});
// Calculate reciprocal ranks
const fusedDocumentScores: Map<string, number> = new Map();
retrievedSets.forEach((results, retrieverIndex) => {
results.forEach((result, rank) => {
for (const id of rankingIdentities(result)) {
const isBestRankForRetriever = resultsByDocument.get(id)?.get(retrieverIndex)?.[0] === result;
if (!isBestRankForRetriever) continue;
const reciprocalRank = 1 / (RRF_K + rank);
const score = fusedDocumentScores.get(id) ?? 0;
fusedDocumentScores.set(id, score + reciprocalRank);
}
});
});
// Sort and combine results
const topDocuments = [...fusedDocumentScores.entries()].sort((a, b) => b[1] - a[1]).map(e => e[0]);
const fusedResults: Set<T> = new Set();
for (const id of topDocuments) {
const resultByDocument = resultsByDocument.get(id);
if (!resultByDocument) continue;
const maxMatches = Math.max(...[...resultByDocument.values()].map(r => r.length));
for (let i = 0; i < maxMatches; i++) {
for (const snippets of resultByDocument.values()) {
if (i < snippets.length) {
fusedResults.add(snippets[i]);
}
}
}
}
return fusedResults;
}
High-Level Example:
["foo.ts:1-5", "foo.ts:20-25"]
["foo.ts:3-7"]
Identifiers:
"foo.ts:1-5"
: ["foo.ts:1", "foo.ts:2", "foo.ts:3", "foo.ts:4", "foo.ts:5"]
"foo.ts:20-25"
: ["foo.ts:20", "foo.ts:21", "foo.ts:22", "foo.ts:23", "foo.ts:24", "foo.ts:25"]
"foo.ts:3-7"
: ["foo.ts:3", "foo.ts:4", "foo.ts:5", "foo.ts:6", "foo.ts:7"]
Combining and Ranking: