@zukhruf/retrieval 0.8.78 → 0.8.79
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/dist/distribution-based-score-fusion.d.ts +10 -0
- package/dist/distribution-based-score-fusion.d.ts.map +1 -0
- package/dist/distribution-based-score-fusion.js +21 -0
- package/dist/distribution-based-score-fusion.js.map +1 -0
- package/dist/fusion-retriever.d.ts +26 -0
- package/dist/fusion-retriever.d.ts.map +1 -0
- package/dist/fusion-retriever.js +30 -0
- package/dist/fusion-retriever.js.map +1 -0
- package/dist/hyde-retriever.d.ts +20 -0
- package/dist/hyde-retriever.d.ts.map +1 -0
- package/dist/hyde-retriever.js +29 -0
- package/dist/hyde-retriever.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/multi-query-retriever.d.ts +28 -0
- package/dist/multi-query-retriever.d.ts.map +1 -0
- package/dist/multi-query-retriever.js +95 -0
- package/dist/multi-query-retriever.js.map +1 -0
- package/dist/reciprocal-rank-fusion.d.ts +11 -0
- package/dist/reciprocal-rank-fusion.d.ts.map +1 -0
- package/dist/reciprocal-rank-fusion.js +13 -0
- package/dist/reciprocal-rank-fusion.js.map +1 -0
- package/dist/relative-score-fusion.d.ts +10 -0
- package/dist/relative-score-fusion.d.ts.map +1 -0
- package/dist/relative-score-fusion.js +18 -0
- package/dist/relative-score-fusion.js.map +1 -0
- package/dist/sum-on-id.d.ts +14 -0
- package/dist/sum-on-id.d.ts.map +1 -0
- package/dist/sum-on-id.js +36 -0
- package/dist/sum-on-id.js.map +1 -0
- package/package.json +4 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Fusion } from './fusion.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Distribution-based score fusion: each list's scores are scaled from
|
|
4
|
+
* mean - 3σ (0) to mean + 3σ (1), with the population standard deviation,
|
|
5
|
+
* and not clipped; then the weighted scaled scores of a passage are added
|
|
6
|
+
* (Qdrant `dbsf`, LlamaIndex `dist_based_score`). A list with σ 0 gives each
|
|
7
|
+
* passage 0.5, as Qdrant gives it (ADR 0007, amendment 2026-11-23).
|
|
8
|
+
*/
|
|
9
|
+
export declare function distributionBasedScoreFusion(): Fusion;
|
|
10
|
+
//# sourceMappingURL=distribution-based-score-fusion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distribution-based-score-fusion.d.ts","sourceRoot":"","sources":["../src/distribution-based-score-fusion.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C;;;;;;GAMG;AACH,wBAAgB,4BAA4B,IAAI,MAAM,CAarD"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { mean, std } from '@zukhruf/statistics';
|
|
2
|
+
import { sumOnId } from './sum-on-id.js';
|
|
3
|
+
/**
|
|
4
|
+
* Distribution-based score fusion: each list's scores are scaled from
|
|
5
|
+
* mean - 3σ (0) to mean + 3σ (1), with the population standard deviation,
|
|
6
|
+
* and not clipped; then the weighted scaled scores of a passage are added
|
|
7
|
+
* (Qdrant `dbsf`, LlamaIndex `dist_based_score`). A list with σ 0 gives each
|
|
8
|
+
* passage 0.5, as Qdrant gives it (ADR 0007, amendment 2026-11-23).
|
|
9
|
+
*/
|
|
10
|
+
export function distributionBasedScoreFusion() {
|
|
11
|
+
return {
|
|
12
|
+
fuse: (lists, weights) => sumOnId(lists, weights, (scores) => {
|
|
13
|
+
const average = mean(scores);
|
|
14
|
+
const deviation = std(scores);
|
|
15
|
+
const lowest = average - 3 * deviation;
|
|
16
|
+
const highest = average + 3 * deviation;
|
|
17
|
+
return scores.map((score) => deviation === 0 ? 0.5 : (score - lowest) / (highest - lowest));
|
|
18
|
+
}),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=distribution-based-score-fusion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distribution-based-score-fusion.js","sourceRoot":"","sources":["../src/distribution-based-score-fusion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAGhD,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC;;;;;;GAMG;AACH,MAAM,UAAU,4BAA4B;IAC1C,OAAO;QACL,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CACvB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE;YACjC,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;YAC7B,MAAM,SAAS,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC;YAC9B,MAAM,MAAM,GAAG,OAAO,GAAG,CAAC,GAAG,SAAS,CAAC;YACvC,MAAM,OAAO,GAAG,OAAO,GAAG,CAAC,GAAG,SAAS,CAAC;YACxC,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAC1B,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,CAC9D,CAAC;QACJ,CAAC,CAAC;KACL,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Fusion } from './fusion.ts';
|
|
2
|
+
import type { RetrieveOptions } from './retrieve-options.ts';
|
|
3
|
+
import type { Retriever } from './retriever.ts';
|
|
4
|
+
import type { ScoredPassage } from './scored-passage.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Asks each retriever for `rankWindowSize` passages, fuses the lists on
|
|
7
|
+
* `Passage.id` and gives the best `topK` (ADR 0007).
|
|
8
|
+
*/
|
|
9
|
+
export declare class FusionRetriever implements Retriever {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(options: {
|
|
12
|
+
readonly retrievers: readonly Retriever[];
|
|
13
|
+
/** `reciprocalRankFusion()` when absent: k 60, ranks from 1. */
|
|
14
|
+
readonly fusion?: Fusion;
|
|
15
|
+
/** One weight for each retriever, in their order: 1 each when absent. */
|
|
16
|
+
readonly weights?: readonly number[];
|
|
17
|
+
/**
|
|
18
|
+
* How many passages each retriever gives to the fusion: the `topK` of
|
|
19
|
+
* the call when absent. It is at least that `topK`, as Elasticsearch's
|
|
20
|
+
* `rank_window_size` is at least `size`.
|
|
21
|
+
*/
|
|
22
|
+
readonly rankWindowSize?: number;
|
|
23
|
+
});
|
|
24
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=fusion-retriever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fusion-retriever.d.ts","sourceRoot":"","sources":["../src/fusion-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;GAGG;AACH,qBAAa,eAAgB,YAAW,SAAS;;IAM/C,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,UAAU,EAAE,SAAS,SAAS,EAAE,CAAC;QAC1C,gEAAgE;QAChE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QACzB,yEAAyE;QACzE,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;QACrC;;;;WAIG;QACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;KAClC,EAUA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAc1B;CACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { reciprocalRankFusion } from './reciprocal-rank-fusion.js';
|
|
2
|
+
/**
|
|
3
|
+
* Asks each retriever for `rankWindowSize` passages, fuses the lists on
|
|
4
|
+
* `Passage.id` and gives the best `topK` (ADR 0007).
|
|
5
|
+
*/
|
|
6
|
+
export class FusionRetriever {
|
|
7
|
+
#retrievers;
|
|
8
|
+
#fusion;
|
|
9
|
+
#weights;
|
|
10
|
+
#rankWindowSize;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.#retrievers = options.retrievers;
|
|
13
|
+
this.#fusion = options.fusion ?? reciprocalRankFusion();
|
|
14
|
+
this.#weights = options.weights ?? options.retrievers.map(() => 1);
|
|
15
|
+
this.#rankWindowSize = options.rankWindowSize;
|
|
16
|
+
if (this.#weights.length !== this.#retrievers.length) {
|
|
17
|
+
throw new RangeError(`A FusionRetriever takes one weight for each retriever: got ${this.#weights.length} weights for ${this.#retrievers.length} retrievers`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
async retrieve(query, options = {}) {
|
|
21
|
+
const topK = options.topK ?? 10;
|
|
22
|
+
const rankWindowSize = this.#rankWindowSize ?? topK;
|
|
23
|
+
if (rankWindowSize < topK) {
|
|
24
|
+
throw new RangeError(`The rankWindowSize ${rankWindowSize} is smaller than the topK ${topK} of the call`);
|
|
25
|
+
}
|
|
26
|
+
const lists = await Promise.all(this.#retrievers.map((retriever) => retriever.retrieve(query, { ...options, topK: rankWindowSize })));
|
|
27
|
+
return this.#fusion.fuse(lists, this.#weights).slice(0, topK);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=fusion-retriever.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fusion-retriever.js","sourceRoot":"","sources":["../src/fusion-retriever.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAKnE;;;GAGG;AACH,MAAM,OAAO,eAAe;IACjB,WAAW,CAAuB;IAClC,OAAO,CAAS;IAChB,QAAQ,CAAoB;IAC5B,eAAe,CAAqB;IAE7C,YAAY,OAYX;QACC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,IAAI,oBAAoB,EAAE,CAAC;QACxD,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,OAAO,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;QACnE,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC;QAC9C,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,KAAK,IAAI,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC;YACrD,MAAM,IAAI,UAAU,CAClB,8DAA8D,IAAI,CAAC,QAAQ,CAAC,MAAM,gBAAgB,IAAI,CAAC,WAAW,CAAC,MAAM,aAAa,CACvI,CAAC;QACJ,CAAC;IACH,CAAC;IAED,KAAK,CAAC,QAAQ,CACZ,KAAa,EACb,OAAO,GAAoB,EAAE;QAE7B,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC;QAChC,MAAM,cAAc,GAAG,IAAI,CAAC,eAAe,IAAI,IAAI,CAAC;QACpD,IAAI,cAAc,GAAG,IAAI,EAAE,CAAC;YAC1B,MAAM,IAAI,UAAU,CAClB,sBAAsB,cAAc,6BAA6B,IAAI,cAAc,CACpF,CAAC;QACJ,CAAC;QACD,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAC7B,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CACjC,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,CAAC,CAChE,CACF,CAAC;QACF,OAAO,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;IAChE,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type LanguageModel } from 'ai';
|
|
2
|
+
import type { RetrieveOptions } from './retrieve-options.ts';
|
|
3
|
+
import type { Retriever } from './retriever.ts';
|
|
4
|
+
import type { ScoredPassage } from './scored-passage.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Hypothetical document embeddings (HyDE; Gao, Ma, Lin and Callan, 2022):
|
|
7
|
+
* asks the language model for a passage that answers the query, and searches
|
|
8
|
+
* with that passage as the query (ADR 0008). The prompt is the "websearch"
|
|
9
|
+
* prompt of LangChain's `HydeRetriever`
|
|
10
|
+
* (libs/langchain-classic/src/retrievers/hyde.ts, getPromptTemplateFromKey).
|
|
11
|
+
*/
|
|
12
|
+
export declare class HydeRetriever implements Retriever {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(options: {
|
|
15
|
+
readonly retriever: Retriever;
|
|
16
|
+
readonly model: LanguageModel;
|
|
17
|
+
});
|
|
18
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=hyde-retriever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hyde-retriever.d.ts","sourceRoot":"","sources":["../src/hyde-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAgB,MAAM,IAAI,CAAC;AAEtD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;GAMG;AACH,qBAAa,aAAc,YAAW,SAAS;;IAI7C,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;QAC9B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;KAC/B,EAGA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAW1B;CACF"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { generateText } from 'ai';
|
|
2
|
+
/**
|
|
3
|
+
* Hypothetical document embeddings (HyDE; Gao, Ma, Lin and Callan, 2022):
|
|
4
|
+
* asks the language model for a passage that answers the query, and searches
|
|
5
|
+
* with that passage as the query (ADR 0008). The prompt is the "websearch"
|
|
6
|
+
* prompt of LangChain's `HydeRetriever`
|
|
7
|
+
* (libs/langchain-classic/src/retrievers/hyde.ts, getPromptTemplateFromKey).
|
|
8
|
+
*/
|
|
9
|
+
export class HydeRetriever {
|
|
10
|
+
#retriever;
|
|
11
|
+
#model;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
this.#retriever = options.retriever;
|
|
14
|
+
this.#model = options.model;
|
|
15
|
+
}
|
|
16
|
+
async retrieve(query, options = {}) {
|
|
17
|
+
const { text } = await generateText({
|
|
18
|
+
model: this.#model,
|
|
19
|
+
prompt: `Please write a passage to answer the question
|
|
20
|
+
Question: ${query}
|
|
21
|
+
Passage:`,
|
|
22
|
+
...(options.abortSignal === undefined
|
|
23
|
+
? {}
|
|
24
|
+
: { abortSignal: options.abortSignal }),
|
|
25
|
+
});
|
|
26
|
+
return this.#retriever.retrieve(text, options);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=hyde-retriever.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hyde-retriever.js","sourceRoot":"","sources":["../src/hyde-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,YAAY,EAAE,MAAM,IAAI,CAAC;AAMtD;;;;;;GAMG;AACH,MAAM,OAAO,aAAa;IACf,UAAU,CAAY;IACtB,MAAM,CAAgB;IAE/B,YAAY,OAGX;QACC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,QAAQ,CACZ,KAAa,EACb,OAAO,GAAoB,EAAE;QAE7B,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,YAAY,CAAC;YAClC,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,MAAM,EAAE;YACF,KAAK;SACR;YACH,GAAG,CAAC,OAAO,CAAC,WAAW,KAAK,SAAS;gBACnC,CAAC,CAAC,EAAE;gBACJ,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;SAC1C,CAAC,CAAC;QACH,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;IACjD,CAAC;CACF"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
export type { BatchRetriever } from './batch-retriever.ts';
|
|
2
|
+
export { distributionBasedScoreFusion } from './distribution-based-score-fusion.ts';
|
|
2
3
|
export type { Filter } from './filter.ts';
|
|
4
|
+
export { FusionRetriever } from './fusion-retriever.ts';
|
|
3
5
|
export type { Fusion } from './fusion.ts';
|
|
6
|
+
export { HydeRetriever } from './hyde-retriever.ts';
|
|
4
7
|
export { ModelMismatchError } from './model-mismatch-error.ts';
|
|
8
|
+
export { MultiQueryRetriever } from './multi-query-retriever.ts';
|
|
5
9
|
export type { PassageStore } from './passage-store.ts';
|
|
6
10
|
export type { Passage } from './passage.ts';
|
|
7
11
|
export type { Postprocessor } from './postprocessor.ts';
|
|
12
|
+
export { reciprocalRankFusion } from './reciprocal-rank-fusion.ts';
|
|
13
|
+
export { relativeScoreFusion } from './relative-score-fusion.ts';
|
|
8
14
|
export type { RetrieveOptions } from './retrieve-options.ts';
|
|
9
15
|
export type { Retriever } from './retriever.ts';
|
|
10
16
|
export type { ScoredPassage } from './scored-passage.ts';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,EAAE,4BAA4B,EAAE,MAAM,sCAAsC,CAAC;AACpF,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,YAAY,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,YAAY,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,YAAY,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
export { distributionBasedScoreFusion } from './distribution-based-score-fusion.js';
|
|
2
|
+
export { FusionRetriever } from './fusion-retriever.js';
|
|
3
|
+
export { HydeRetriever } from './hyde-retriever.js';
|
|
1
4
|
export { ModelMismatchError } from './model-mismatch-error.js';
|
|
5
|
+
export { MultiQueryRetriever } from './multi-query-retriever.js';
|
|
6
|
+
export { reciprocalRankFusion } from './reciprocal-rank-fusion.js';
|
|
7
|
+
export { relativeScoreFusion } from './relative-score-fusion.js';
|
|
2
8
|
export { VectorMissingError } from './vector-missing-error.js';
|
|
3
9
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,4BAA4B,EAAE,MAAM,sCAAsC,CAAC;AAEpF,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAIjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAIjE,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type LanguageModel } from 'ai';
|
|
2
|
+
import type { Fusion } from './fusion.ts';
|
|
3
|
+
import type { RetrieveOptions } from './retrieve-options.ts';
|
|
4
|
+
import type { Retriever } from './retriever.ts';
|
|
5
|
+
import type { ScoredPassage } from './scored-passage.ts';
|
|
6
|
+
/**
|
|
7
|
+
* Asks the language model for `queryCount` other versions of the query,
|
|
8
|
+
* searches the query and each version, and fuses the lists (ADR 0008, the
|
|
9
|
+
* proposed amendment of 2026-11-23). The prompt, the JSON answer, the cut at
|
|
10
|
+
* `queryCount` and the query searched once beside the versions are those of
|
|
11
|
+
* Haystack's `QueryExpander` (haystack/components/query/query_expander.py:
|
|
12
|
+
* DEFAULT_PROMPT_TEMPLATE, `n_expansions` 4, `include_original_query` True).
|
|
13
|
+
* The list of the query comes first, as LlamaIndex's `QueryFusionRetriever`
|
|
14
|
+
* puts the query first, so a tie goes to a passage that the query found.
|
|
15
|
+
*/
|
|
16
|
+
export declare class MultiQueryRetriever implements Retriever {
|
|
17
|
+
#private;
|
|
18
|
+
constructor(options: {
|
|
19
|
+
readonly retriever: Retriever;
|
|
20
|
+
readonly model: LanguageModel;
|
|
21
|
+
/** How many versions the model writes, besides the query: 4 when absent. */
|
|
22
|
+
readonly queryCount?: number;
|
|
23
|
+
/** `reciprocalRankFusion()` when absent. */
|
|
24
|
+
readonly fusion?: Fusion;
|
|
25
|
+
});
|
|
26
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=multi-query-retriever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-query-retriever.d.ts","sourceRoot":"","sources":["../src/multi-query-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAoC,MAAM,IAAI,CAAC;AAE1E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;;GASG;AACH,qBAAa,mBAAoB,YAAW,SAAS;;IAMnD,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;QAC9B,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;QAC9B,4EAA4E;QAC5E,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAC7B,4CAA4C;QAC5C,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;KAC1B,EAUA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAwB1B;CACF"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Output, generateText, jsonSchema } from 'ai';
|
|
2
|
+
import { reciprocalRankFusion } from './reciprocal-rank-fusion.js';
|
|
3
|
+
/**
|
|
4
|
+
* Asks the language model for `queryCount` other versions of the query,
|
|
5
|
+
* searches the query and each version, and fuses the lists (ADR 0008, the
|
|
6
|
+
* proposed amendment of 2026-11-23). The prompt, the JSON answer, the cut at
|
|
7
|
+
* `queryCount` and the query searched once beside the versions are those of
|
|
8
|
+
* Haystack's `QueryExpander` (haystack/components/query/query_expander.py:
|
|
9
|
+
* DEFAULT_PROMPT_TEMPLATE, `n_expansions` 4, `include_original_query` True).
|
|
10
|
+
* The list of the query comes first, as LlamaIndex's `QueryFusionRetriever`
|
|
11
|
+
* puts the query first, so a tie goes to a passage that the query found.
|
|
12
|
+
*/
|
|
13
|
+
export class MultiQueryRetriever {
|
|
14
|
+
#retriever;
|
|
15
|
+
#model;
|
|
16
|
+
#queryCount;
|
|
17
|
+
#fusion;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.#retriever = options.retriever;
|
|
20
|
+
this.#model = options.model;
|
|
21
|
+
this.#queryCount = options.queryCount ?? 4;
|
|
22
|
+
this.#fusion = options.fusion ?? reciprocalRankFusion();
|
|
23
|
+
if (!Number.isInteger(this.#queryCount) || this.#queryCount < 1) {
|
|
24
|
+
throw new RangeError(`The queryCount is a positive integer, not ${this.#queryCount}`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
async retrieve(query, options = {}) {
|
|
28
|
+
const { output } = await generateText({
|
|
29
|
+
model: this.#model,
|
|
30
|
+
prompt: expansionPrompt(query.trim(), this.#queryCount),
|
|
31
|
+
output: Output.object({ schema: expansions }),
|
|
32
|
+
...(options.abortSignal === undefined
|
|
33
|
+
? {}
|
|
34
|
+
: { abortSignal: options.abortSignal }),
|
|
35
|
+
});
|
|
36
|
+
const queries = [
|
|
37
|
+
query,
|
|
38
|
+
...output.queries
|
|
39
|
+
.slice(0, this.#queryCount)
|
|
40
|
+
.filter((version) => version.toLowerCase() !== query.toLowerCase()),
|
|
41
|
+
];
|
|
42
|
+
const lists = await Promise.all(queries.map((version) => this.#retriever.retrieve(version, options)));
|
|
43
|
+
return this.#fusion
|
|
44
|
+
.fuse(lists, lists.map(() => 1))
|
|
45
|
+
.slice(0, options.topK ?? 10);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const expansions = jsonSchema({
|
|
49
|
+
type: 'object',
|
|
50
|
+
properties: { queries: { type: 'array', items: { type: 'string' } } },
|
|
51
|
+
required: ['queries'],
|
|
52
|
+
additionalProperties: false,
|
|
53
|
+
}, {
|
|
54
|
+
validate: (value) => typeof value === 'object' &&
|
|
55
|
+
value !== null &&
|
|
56
|
+
'queries' in value &&
|
|
57
|
+
Array.isArray(value.queries) &&
|
|
58
|
+
value.queries.every((item) => typeof item === 'string')
|
|
59
|
+
? { success: true, value: { queries: value.queries } }
|
|
60
|
+
: {
|
|
61
|
+
success: false,
|
|
62
|
+
error: new TypeError(`The model gave no "queries" array of strings: ${JSON.stringify(value)}`),
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const expansionPrompt = (query, count) => `You are part of an information system that processes user queries for retrieval.
|
|
66
|
+
You have to expand a given query into ${count} queries that are
|
|
67
|
+
semantically similar to improve retrieval recall.
|
|
68
|
+
|
|
69
|
+
Structure:
|
|
70
|
+
Follow the structure shown below in examples to generate expanded queries.
|
|
71
|
+
These examples show the output when 3 queries are requested.
|
|
72
|
+
|
|
73
|
+
Examples:
|
|
74
|
+
1. Query: "climate change effects"
|
|
75
|
+
{"queries": ["impact of climate change", "consequences of global warming", "effects of environmental changes"]}
|
|
76
|
+
|
|
77
|
+
2. Query: "machine learning algorithms"
|
|
78
|
+
{"queries": ["neural networks", "clustering techniques", "supervised learning methods"]}
|
|
79
|
+
|
|
80
|
+
3. Query: "open source NLP frameworks"
|
|
81
|
+
{"queries": ["natural language processing tools", "free nlp libraries", "open-source NLP platforms"]}
|
|
82
|
+
|
|
83
|
+
Guidelines:
|
|
84
|
+
- Generate queries that use different words and phrasings
|
|
85
|
+
- Include synonyms and related terms
|
|
86
|
+
- Maintain the same core meaning and intent
|
|
87
|
+
- Make queries that are likely to retrieve relevant information the original might miss
|
|
88
|
+
- Focus on variations that would work well with keyword-based search
|
|
89
|
+
- Respond in the same language as the input query
|
|
90
|
+
|
|
91
|
+
Your Task:
|
|
92
|
+
Query: "${query}"
|
|
93
|
+
|
|
94
|
+
You *must* respond with a JSON object containing a "queries" array with exactly ${count} expanded queries.`;
|
|
95
|
+
//# sourceMappingURL=multi-query-retriever.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-query-retriever.js","sourceRoot":"","sources":["../src/multi-query-retriever.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Fusion } from './fusion.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Gives each passage the sum of `weight / (k + rank)` over the lists that
|
|
4
|
+
* hold it, with ranks from 1 (Cormack, Clarke and Büttcher, SIGIR 2009;
|
|
5
|
+
* LangChain `EnsembleRetriever`, `weight / (rank + this.c)`). The scores of
|
|
6
|
+
* the lists are not read, only their order (ADR 0007).
|
|
7
|
+
*/
|
|
8
|
+
export declare function reciprocalRankFusion({ k, }?: {
|
|
9
|
+
readonly k?: number;
|
|
10
|
+
}): Fusion;
|
|
11
|
+
//# sourceMappingURL=reciprocal-rank-fusion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reciprocal-rank-fusion.d.ts","sourceRoot":"","sources":["../src/reciprocal-rank-fusion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,CAAM,GACP,GAAE;IAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,MAAM,CAOvC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { sumOnId } from './sum-on-id.js';
|
|
2
|
+
/**
|
|
3
|
+
* Gives each passage the sum of `weight / (k + rank)` over the lists that
|
|
4
|
+
* hold it, with ranks from 1 (Cormack, Clarke and Büttcher, SIGIR 2009;
|
|
5
|
+
* LangChain `EnsembleRetriever`, `weight / (rank + this.c)`). The scores of
|
|
6
|
+
* the lists are not read, only their order (ADR 0007).
|
|
7
|
+
*/
|
|
8
|
+
export function reciprocalRankFusion({ k = 60, } = {}) {
|
|
9
|
+
return {
|
|
10
|
+
fuse: (lists, weights) => sumOnId(lists, weights, (scores) => scores.map((_, index) => 1 / (k + index + 1))),
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=reciprocal-rank-fusion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reciprocal-rank-fusion.js","sourceRoot":"","sources":["../src/reciprocal-rank-fusion.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAAC,EACnC,CAAC,GAAG,EAAE,GACP,GAA4B,EAAE;IAC7B,OAAO;QACL,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CACvB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE,CACjC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAC9C;KACJ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Fusion } from './fusion.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Min-max scaling: the best score of each list becomes 1 and the worst 0,
|
|
4
|
+
* then the weighted scaled scores of a passage are added (Weaviate
|
|
5
|
+
* `relativeScoreFusion`, LlamaIndex `relative_score`). A list whose scores
|
|
6
|
+
* are all equal gives each passage 1, as Weaviate and OpenSearch give it
|
|
7
|
+
* (ADR 0007, amendment 2026-11-23).
|
|
8
|
+
*/
|
|
9
|
+
export declare function relativeScoreFusion(): Fusion;
|
|
10
|
+
//# sourceMappingURL=relative-score-fusion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative-score-fusion.d.ts","sourceRoot":"","sources":["../src/relative-score-fusion.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C;;;;;;GAMG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,CAW5C"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { sumOnId } from './sum-on-id.js';
|
|
2
|
+
/**
|
|
3
|
+
* Min-max scaling: the best score of each list becomes 1 and the worst 0,
|
|
4
|
+
* then the weighted scaled scores of a passage are added (Weaviate
|
|
5
|
+
* `relativeScoreFusion`, LlamaIndex `relative_score`). A list whose scores
|
|
6
|
+
* are all equal gives each passage 1, as Weaviate and OpenSearch give it
|
|
7
|
+
* (ADR 0007, amendment 2026-11-23).
|
|
8
|
+
*/
|
|
9
|
+
export function relativeScoreFusion() {
|
|
10
|
+
return {
|
|
11
|
+
fuse: (lists, weights) => sumOnId(lists, weights, (scores) => {
|
|
12
|
+
const lowest = Math.min(...scores);
|
|
13
|
+
const highest = Math.max(...scores);
|
|
14
|
+
return scores.map((score) => highest === lowest ? 1 : (score - lowest) / (highest - lowest));
|
|
15
|
+
}),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=relative-score-fusion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"relative-score-fusion.js","sourceRoot":"","sources":["../src/relative-score-fusion.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzC;;;;;;GAMG;AACH,MAAM,UAAU,mBAAmB;IACjC,OAAO;QACL,IAAI,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,CACvB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,MAAM,EAAE,EAAE;YACjC,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;YACnC,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC;YACpC,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAC1B,OAAO,KAAK,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,OAAO,GAAG,MAAM,CAAC,CAC/D,CAAC;QACJ,CAAC,CAAC;KACL,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ScoredPassage } from './scored-passage.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The part that the three fusions share: each list's scores become new
|
|
4
|
+
* scores by the fusion's own rule, each new score is multiplied by the weight
|
|
5
|
+
* of its list, and the products of one `Passage.id` are added, best first. A
|
|
6
|
+
* passage that a list does not hold adds nothing from that list (ADR 0007,
|
|
7
|
+
* amendment 2026-11-23).
|
|
8
|
+
*
|
|
9
|
+
* The passage is the one of the first list that holds the id, its vector the
|
|
10
|
+
* first that a list gives, and equal fused scores keep the order in which
|
|
11
|
+
* their ids first appear (`toSorted` is stable).
|
|
12
|
+
*/
|
|
13
|
+
export declare function sumOnId(lists: readonly (readonly ScoredPassage[])[], weights: readonly number[], rescale: (scores: readonly number[]) => readonly number[]): ScoredPassage[];
|
|
14
|
+
//# sourceMappingURL=sum-on-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sum-on-id.d.ts","sourceRoot":"","sources":["../src/sum-on-id.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CACrB,KAAK,EAAE,SAAS,CAAC,SAAS,aAAa,EAAE,CAAC,EAAE,EAC5C,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,EAAE,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,KAAK,SAAS,MAAM,EAAE,GACxD,aAAa,EAAE,CA0BjB"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The part that the three fusions share: each list's scores become new
|
|
3
|
+
* scores by the fusion's own rule, each new score is multiplied by the weight
|
|
4
|
+
* of its list, and the products of one `Passage.id` are added, best first. A
|
|
5
|
+
* passage that a list does not hold adds nothing from that list (ADR 0007,
|
|
6
|
+
* amendment 2026-11-23).
|
|
7
|
+
*
|
|
8
|
+
* The passage is the one of the first list that holds the id, its vector the
|
|
9
|
+
* first that a list gives, and equal fused scores keep the order in which
|
|
10
|
+
* their ids first appear (`toSorted` is stable).
|
|
11
|
+
*/
|
|
12
|
+
export function sumOnId(lists, weights, rescale) {
|
|
13
|
+
if (weights.length !== lists.length) {
|
|
14
|
+
throw new RangeError(`A fusion takes one weight for each list: got ${weights.length} weights for ${lists.length} lists`);
|
|
15
|
+
}
|
|
16
|
+
const fused = new Map();
|
|
17
|
+
for (const [index, list] of lists.entries()) {
|
|
18
|
+
const weight = weights[index] ?? 1;
|
|
19
|
+
const scores = rescale(list.map(({ score }) => score));
|
|
20
|
+
for (const [position, { passage, vector }] of list.entries()) {
|
|
21
|
+
const score = weight * (scores[position] ?? 0);
|
|
22
|
+
const entry = fused.get(passage.id);
|
|
23
|
+
if (entry === undefined) {
|
|
24
|
+
fused.set(passage.id, { passage, score, vector });
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
entry.score += score;
|
|
28
|
+
entry.vector ??= vector;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return [...fused.values()]
|
|
33
|
+
.map(({ passage, score, vector }) => vector === undefined ? { passage, score } : { passage, score, vector })
|
|
34
|
+
.toSorted((a, b) => b.score - a.score);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=sum-on-id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sum-on-id.js","sourceRoot":"","sources":["../src/sum-on-id.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,OAAO,CACrB,KAA4C,EAC5C,OAA0B,EAC1B,OAAyD;IAEzD,IAAI,OAAO,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,EAAE,CAAC;QACpC,MAAM,IAAI,UAAU,CAClB,gDAAgD,OAAO,CAAC,MAAM,gBAAgB,KAAK,CAAC,MAAM,QAAQ,CACnG,CAAC;IACJ,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,GAAG,EAAiB,CAAC;IACvC,KAAK,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;QAC5C,MAAM,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnC,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QACvD,KAAK,MAAM,CAAC,QAAQ,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,IAAI,IAAI,CAAC,OAAO,EAAE,EAAE,CAAC;YAC7D,MAAM,KAAK,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;YAC/C,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;YACpC,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;gBACxB,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE,EAAE,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;YACpD,CAAC;iBAAM,CAAC;gBACN,KAAK,CAAC,KAAK,IAAI,KAAK,CAAC;gBACrB,KAAK,CAAC,MAAM,KAAK,MAAM,CAAC;YAC1B,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;SACvB,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE,EAAE,CAClC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CACvE;SACA,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;AAC3C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/retrieval",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.79",
|
|
4
4
|
"description": "The query side of RAG: retrievers that search a passage table by meaning or keywords in SQLite, Postgres or SQL Server, fuse lists, rerank and post-process, and give the best passages first with a score that is higher for a better passage.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -51,10 +51,11 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@zukhruf/
|
|
54
|
+
"@zukhruf/statistics": "^0.8.79",
|
|
55
|
+
"@zukhruf/vectors": "^0.8.79",
|
|
55
56
|
"ai": "^7.0.179"
|
|
56
57
|
},
|
|
57
58
|
"devDependencies": {
|
|
58
|
-
"@zukhruf/sqlite-open": "^0.8.
|
|
59
|
+
"@zukhruf/sqlite-open": "^0.8.79"
|
|
59
60
|
}
|
|
60
61
|
}
|