@zukhruf/retrieval 0.8.77 → 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/README.md +8 -7
- 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/model-identity.d.ts +26 -0
- package/dist/model-identity.d.ts.map +1 -0
- package/dist/model-identity.js +31 -0
- package/dist/model-identity.js.map +1 -0
- package/dist/model-mismatch-error.d.ts +9 -12
- package/dist/model-mismatch-error.d.ts.map +1 -1
- package/dist/model-mismatch-error.js +6 -6
- package/dist/model-mismatch-error.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/sqlite/identifier.d.ts +3 -0
- package/dist/sqlite/identifier.d.ts.map +1 -0
- package/dist/sqlite/identifier.js +3 -0
- package/dist/sqlite/identifier.js.map +1 -0
- package/dist/sqlite/passage-rows.d.ts +13 -0
- package/dist/sqlite/passage-rows.d.ts.map +1 -0
- package/dist/sqlite/passage-rows.js +44 -0
- package/dist/sqlite/passage-rows.js.map +1 -0
- package/dist/sqlite/sqlite-bm25-retriever.d.ts +21 -0
- package/dist/sqlite/sqlite-bm25-retriever.d.ts.map +1 -0
- package/dist/sqlite/sqlite-bm25-retriever.js +48 -0
- package/dist/sqlite/sqlite-bm25-retriever.js.map +1 -0
- package/dist/sqlite/sqlite-embedding-retriever.d.ts +23 -0
- package/dist/sqlite/sqlite-embedding-retriever.d.ts.map +1 -0
- package/dist/sqlite/sqlite-embedding-retriever.js +125 -0
- package/dist/sqlite/sqlite-embedding-retriever.js.map +1 -0
- package/dist/sqlite/sqlite-passage-store.d.ts +23 -0
- package/dist/sqlite/sqlite-passage-store.d.ts.map +1 -0
- package/dist/sqlite/sqlite-passage-store.js +32 -0
- package/dist/sqlite/sqlite-passage-store.js.map +1 -0
- package/dist/sqlite/where.d.ts +13 -0
- package/dist/sqlite/where.d.ts.map +1 -0
- package/dist/sqlite/where.js +62 -0
- package/dist/sqlite/where.js.map +1 -0
- package/dist/sqlite.d.ts +4 -0
- package/dist/sqlite.d.ts.map +1 -0
- package/dist/sqlite.js +4 -0
- package/dist/sqlite.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 +15 -1
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ const results = await retriever.retrieve('rotate a key', {
|
|
|
60
60
|
});
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
The retriever reads the model identity of the stored vectors first. When its query model has another
|
|
63
|
+
The retriever reads the model identity of the stored vectors first. When its query model has another provider or model id, the call fails with `ModelMismatchError` and sends no model call and no search. When the query vector has other dimensions than the stored vectors, the call fails with `DimensionMismatchError` of `@zukhruf/vectors` after the model call and sends no search ([ADR 0010](./docs/adr/0010-an-embedding-retriever-refuses-a-query-model-whose-identity-differs-from-the-stored-vectors.md)).
|
|
64
64
|
|
|
65
65
|
## Search by keywords
|
|
66
66
|
|
|
@@ -185,10 +185,11 @@ On Postgres, pgvector filters after the approximate index scan, so a filtered ca
|
|
|
185
185
|
|
|
186
186
|
## Errors
|
|
187
187
|
|
|
188
|
-
| Error
|
|
189
|
-
|
|
|
190
|
-
| `ModelMismatchError`
|
|
191
|
-
| `
|
|
188
|
+
| Error | When |
|
|
189
|
+
| --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
190
|
+
| `ModelMismatchError` | The query model of an embedding retriever has another provider or model id than the stored vectors, before the model call |
|
|
191
|
+
| `DimensionMismatchError` (`@zukhruf/vectors`) | The query vector has other dimensions than the stored vectors, after the model call. `expected` and `actual` |
|
|
192
|
+
| `VectorMissingError` | `MaximalMarginalRelevance` gets a passage without a vector: the call did not ask for vectors |
|
|
192
193
|
|
|
193
194
|
An error of the model, of the engine or of the reranking model reaches the caller as it is. An aborted call rejects with the reason of its `abortSignal`.
|
|
194
195
|
|
|
@@ -367,8 +368,8 @@ interface PassageStore {
|
|
|
367
368
|
}
|
|
368
369
|
|
|
369
370
|
class ModelMismatchError extends Error {
|
|
370
|
-
readonly stored: { provider: string; modelId: string
|
|
371
|
-
readonly query: { provider: string; modelId: string; dimensions
|
|
371
|
+
readonly stored: { provider: string; modelId: string };
|
|
372
|
+
readonly query: { provider: string; modelId: string }; // before the model call; other dimensions are DimensionMismatchError of @zukhruf/vectors (ADR 0010)
|
|
372
373
|
}
|
|
373
374
|
class VectorMissingError extends Error {
|
|
374
375
|
readonly passageId: string;
|
|
@@ -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,26 @@
|
|
|
1
|
+
import type { EmbeddingModel } from 'ai';
|
|
2
|
+
/** The provider, the model id and the dimensions of the model that made a vector (ADR 0010). */
|
|
3
|
+
export type ModelIdentity = {
|
|
4
|
+
readonly provider: string;
|
|
5
|
+
readonly modelId: string;
|
|
6
|
+
readonly dimensions: number;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* The provider and the model id of a model, which a retriever compares
|
|
10
|
+
* before the model call. An AI SDK embedding model does not state its
|
|
11
|
+
* dimensions, so only its vector shows them.
|
|
12
|
+
*/
|
|
13
|
+
export type ModelName = Pick<ModelIdentity, 'provider' | 'modelId'>;
|
|
14
|
+
/**
|
|
15
|
+
* The form of elt's Embedding rule, `openai/text-embedding-3-small (1536
|
|
16
|
+
* dimensions)`, which the `model` column of the passage vectors holds.
|
|
17
|
+
*/
|
|
18
|
+
export declare const describe: ({ provider, modelId, dimensions, }: ModelIdentity) => string;
|
|
19
|
+
/** Reads the identity that the `model` column of the passage vectors holds. */
|
|
20
|
+
export declare function parseModelIdentity(text: string): ModelIdentity;
|
|
21
|
+
/**
|
|
22
|
+
* The provider and the model id of a query model, as elt names them: a
|
|
23
|
+
* string model id is `provider/model`, as the AI SDK gateway reads it.
|
|
24
|
+
*/
|
|
25
|
+
export declare function modelName(model: EmbeddingModel): ModelName;
|
|
26
|
+
//# sourceMappingURL=model-identity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-identity.d.ts","sourceRoot":"","sources":["../src/model-identity.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,IAAI,CAAC;AAEzC,gGAAgG;AAChG,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,EAAE,UAAU,GAAG,SAAS,CAAC,CAAC;AAEpE;;;GAGG;AACH,eAAO,MAAM,QAAQ,uCAIlB,aAAa,KAAG,MACkC,CAAC;AAItD,+EAA+E;AAC/E,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,CAY9D;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,cAAc,GAAG,SAAS,CAW1D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The form of elt's Embedding rule, `openai/text-embedding-3-small (1536
|
|
3
|
+
* dimensions)`, which the `model` column of the passage vectors holds.
|
|
4
|
+
*/
|
|
5
|
+
export const describe = ({ provider, modelId, dimensions, }) => `${provider}/${modelId} (${dimensions} dimensions)`;
|
|
6
|
+
const storedForm = /^([^/]+)\/(.+) \((\d+) dimensions\)$/;
|
|
7
|
+
/** Reads the identity that the `model` column of the passage vectors holds. */
|
|
8
|
+
export function parseModelIdentity(text) {
|
|
9
|
+
const [, provider, modelId, dimensions] = storedForm.exec(text) ?? [];
|
|
10
|
+
if (provider === undefined ||
|
|
11
|
+
modelId === undefined ||
|
|
12
|
+
dimensions === undefined) {
|
|
13
|
+
throw new TypeError(`The stored model ${JSON.stringify(text)} is not a model identity of the form "provider/model (n dimensions)"`);
|
|
14
|
+
}
|
|
15
|
+
return { provider, modelId, dimensions: Number(dimensions) };
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The provider and the model id of a query model, as elt names them: a
|
|
19
|
+
* string model id is `provider/model`, as the AI SDK gateway reads it.
|
|
20
|
+
*/
|
|
21
|
+
export function modelName(model) {
|
|
22
|
+
if (typeof model !== 'string') {
|
|
23
|
+
return { provider: model.provider, modelId: model.modelId };
|
|
24
|
+
}
|
|
25
|
+
const [, provider, modelId] = /^([^/]+)\/(.+)$/.exec(model) ?? [];
|
|
26
|
+
if (provider === undefined || modelId === undefined) {
|
|
27
|
+
throw new TypeError(`The model id ${JSON.stringify(model)} names no provider: write "provider/model"`);
|
|
28
|
+
}
|
|
29
|
+
return { provider, modelId };
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=model-identity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-identity.js","sourceRoot":"","sources":["../src/model-identity.ts"],"names":[],"mappings":"AAgBA;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,CAAC,EACvB,QAAQ,EACR,OAAO,EACP,UAAU,GACI,EAAU,EAAE,CAC1B,GAAG,QAAQ,IAAI,OAAO,KAAK,UAAU,cAAc,CAAC;AAEtD,MAAM,UAAU,GAAG,sCAAsC,CAAC;AAE1D,+EAA+E;AAC/E,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC7C,MAAM,CAAC,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,CAAC,GAAG,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACtE,IACE,QAAQ,KAAK,SAAS;QACtB,OAAO,KAAK,SAAS;QACrB,UAAU,KAAK,SAAS,EACxB,CAAC;QACD,MAAM,IAAI,SAAS,CACjB,oBAAoB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,sEAAsE,CAC/G,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;AAC/D,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,KAAqB;IAC7C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC;IAC9D,CAAC;IACD,MAAM,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,GAAG,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;IAClE,IAAI,QAAQ,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QACpD,MAAM,IAAI,SAAS,CACjB,gBAAgB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,4CAA4C,CAClF,CAAC;IACJ,CAAC;IACD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,CAAC;AAC/B,CAAC"}
|
|
@@ -1,18 +1,15 @@
|
|
|
1
|
-
type
|
|
2
|
-
readonly provider: string;
|
|
3
|
-
readonly modelId: string;
|
|
4
|
-
readonly dimensions: number;
|
|
5
|
-
};
|
|
1
|
+
import type { ModelName } from './model-identity.ts';
|
|
6
2
|
/**
|
|
7
|
-
* The query model of an embedding retriever has another model
|
|
8
|
-
* the model that made the stored vectors, so their vectors do not
|
|
9
|
-
* (ADR 0010).
|
|
3
|
+
* The query model of an embedding retriever has another provider or model id
|
|
4
|
+
* than the model that made the stored vectors, so their vectors do not
|
|
5
|
+
* compare (ADR 0010). The retriever finds it before the model call. A query
|
|
6
|
+
* vector of other dimensions is a `DimensionMismatchError` of
|
|
7
|
+
* `@zukhruf/vectors`, after the model call.
|
|
10
8
|
*/
|
|
11
9
|
export declare class ModelMismatchError extends Error {
|
|
12
10
|
readonly name = "ModelMismatchError";
|
|
13
|
-
readonly stored:
|
|
14
|
-
readonly query:
|
|
15
|
-
constructor(stored:
|
|
11
|
+
readonly stored: ModelName;
|
|
12
|
+
readonly query: ModelName;
|
|
13
|
+
constructor(stored: ModelName, query: ModelName);
|
|
16
14
|
}
|
|
17
|
-
export {};
|
|
18
15
|
//# sourceMappingURL=model-mismatch-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-mismatch-error.d.ts","sourceRoot":"","sources":["../src/model-mismatch-error.ts"],"names":[],"mappings":"AAAA,KAAK,
|
|
1
|
+
{"version":3,"file":"model-mismatch-error.d.ts","sourceRoot":"","sources":["../src/model-mismatch-error.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;;;;GAMG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,SAAkB,IAAI,wBAAwB;IAC9C,QAAQ,CAAC,MAAM,EAAE,SAAS,CAAC;IAC3B,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;IAE1B,YAAY,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAM9C;CACF"}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The query model of an embedding retriever has another model
|
|
3
|
-
* the model that made the stored vectors, so their vectors do not
|
|
4
|
-
* (ADR 0010).
|
|
2
|
+
* The query model of an embedding retriever has another provider or model id
|
|
3
|
+
* than the model that made the stored vectors, so their vectors do not
|
|
4
|
+
* compare (ADR 0010). The retriever finds it before the model call. A query
|
|
5
|
+
* vector of other dimensions is a `DimensionMismatchError` of
|
|
6
|
+
* `@zukhruf/vectors`, after the model call.
|
|
5
7
|
*/
|
|
6
8
|
export class ModelMismatchError extends Error {
|
|
7
9
|
name = 'ModelMismatchError';
|
|
8
10
|
stored;
|
|
9
11
|
query;
|
|
10
12
|
constructor(stored, query) {
|
|
11
|
-
super(`The query model ${
|
|
13
|
+
super(`The query model ${query.provider}/${query.modelId} is not the model of the stored vectors, ${stored.provider}/${stored.modelId}`);
|
|
12
14
|
this.stored = stored;
|
|
13
15
|
this.query = query;
|
|
14
16
|
}
|
|
15
17
|
}
|
|
16
|
-
/** The form of elt's Embedding rule: `openai/text-embedding-3-small (1536 dimensions)`. */
|
|
17
|
-
const describe = ({ provider, modelId, dimensions }) => `${provider}/${modelId} (${dimensions} dimensions)`;
|
|
18
18
|
//# sourceMappingURL=model-mismatch-error.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-mismatch-error.js","sourceRoot":"","sources":["../src/model-mismatch-error.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"model-mismatch-error.js","sourceRoot":"","sources":["../src/model-mismatch-error.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IACzB,IAAI,GAAG,oBAAoB,CAAC;IACrC,MAAM,CAAY;IAClB,KAAK,CAAY;IAE1B,YAAY,MAAiB,EAAE,KAAgB;QAC7C,KAAK,CACH,mBAAmB,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,OAAO,4CAA4C,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,OAAO,EAAE,CAClI,CAAC;QACF,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.d.ts","sourceRoot":"","sources":["../../src/sqlite/identifier.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MACP,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../src/sqlite/identifier.ts"],"names":[],"mappings":"AAAA,0HAA0H;AAC1H,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAU,EAAE,CACjD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC"}
|