@zukhruf/retrieval 0.8.80 → 0.8.81

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 CHANGED
@@ -134,7 +134,11 @@ const search = new PostprocessingRetriever({
134
134
  postprocessors: [
135
135
  new Rerank({ model: rerankingModel, topN: 20 }), // AI SDK rerank
136
136
  new SimilarityCutoff({ threshold: 0.3 }),
137
- new MaximalMarginalRelevance({ lambda: 0.5, topK: 8 }), // lambda 1 = relevance only
137
+ new MaximalMarginalRelevance({
138
+ model: embeddingModel,
139
+ lambda: 0.5,
140
+ topK: 8,
141
+ }), // lambda 1 = relevance only
138
142
  new Collapse({ field: 'source', size: 1 }), // one passage per source
139
143
  new ContextWindow({
140
144
  store: new SqlitePassageStore({ database, passages: 'docs_passages' }),
@@ -150,7 +154,7 @@ const results = await search.retrieve('rotate a key', {
150
154
  });
151
155
  ```
152
156
 
153
- `MaximalMarginalRelevance` compares the vectors of the passages, so its retriever runs with `includeVectors: true`.
157
+ `MaximalMarginalRelevance` compares the vectors of the passages, so its retriever runs with `includeVectors: true`. Its `model` embeds the query, and the relevance of a passage is the cosine similarity of the two vectors (ADR 0008, the proposed amendment of 2026-11-23).
154
158
 
155
159
  ## Search many queries at once
156
160
 
@@ -348,10 +352,17 @@ class SimilarityCutoff implements Postprocessor {
348
352
  constructor(options: { threshold: number }); // keeps score >= threshold
349
353
  }
350
354
  class MaximalMarginalRelevance implements Postprocessor {
351
- constructor(options: { lambda?: number; topK: number }); // 0.5; 1 = relevance only
355
+ constructor(options: {
356
+ model: EmbeddingModel; // embeds the query (proposed amendment of ADR 0008)
357
+ lambda?: number; // 0.5; 1 = relevance only
358
+ topK: number;
359
+ });
352
360
  }
353
361
  class Collapse implements Postprocessor {
354
- constructor(options?: { field?: string; size?: number }); // 'source', 1
362
+ constructor(options?: {
363
+ field?: 'source' | 'seq' | 'start' | 'end' | `metadata.${string}`; // 'source'
364
+ size?: number; // 1
365
+ });
355
366
  }
356
367
  class LongContextReorder implements Postprocessor {}
357
368
  class ContextWindow implements Postprocessor {
@@ -0,0 +1,27 @@
1
+ import type { Filter } from './filter.ts';
2
+ import type { Postprocessor } from './postprocessor.ts';
3
+ import type { ScoredPassage } from './scored-passage.ts';
4
+ type Field = Extract<Filter, {
5
+ readonly field: string;
6
+ }>['field'];
7
+ /**
8
+ * Keeps the best `size` passages of each value of a field, best first, as
9
+ * Elasticsearch's `collapse` keeps the top hit of each key and Vespa's
10
+ * `collapsesize` keeps that many (ADR 0008). The field is a passage field or
11
+ * `metadata.<column>`, as in a filter. Equal scores keep the order of the
12
+ * list. The passages without a value for the field (absent or null) are one
13
+ * group, as Elasticsearch groups a missing value under the key null (ADR
14
+ * 0008, the proposed amendment of 2026-11-23).
15
+ */
16
+ export declare class Collapse implements Postprocessor {
17
+ #private;
18
+ constructor(options?: {
19
+ /** `'source'` when absent: the passages of one document. */
20
+ readonly field?: Field;
21
+ /** How many passages each value keeps: 1 when absent. */
22
+ readonly size?: number;
23
+ });
24
+ postprocess(passages: readonly ScoredPassage[]): Promise<ScoredPassage[]>;
25
+ }
26
+ export {};
27
+ //# sourceMappingURL=collapse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collapse.d.ts","sourceRoot":"","sources":["../src/collapse.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,KAAK,KAAK,GAAG,OAAO,CAAC,MAAM,EAAE;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC,OAAO,CAAC,CAAC;AAElE;;;;;;;;GAQG;AACH,qBAAa,QAAS,YAAW,aAAa;;IAI5C,YACE,OAAO,GAAE;QACP,4DAA4D;QAC5D,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC;QACvB,yDAAyD;QACzD,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;KACnB,EAIP;IAEK,WAAW,CACf,QAAQ,EAAE,SAAS,aAAa,EAAE,GACjC,OAAO,CAAC,aAAa,EAAE,CAAC,CAU1B;CAWF"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Keeps the best `size` passages of each value of a field, best first, as
3
+ * Elasticsearch's `collapse` keeps the top hit of each key and Vespa's
4
+ * `collapsesize` keeps that many (ADR 0008). The field is a passage field or
5
+ * `metadata.<column>`, as in a filter. Equal scores keep the order of the
6
+ * list. The passages without a value for the field (absent or null) are one
7
+ * group, as Elasticsearch groups a missing value under the key null (ADR
8
+ * 0008, the proposed amendment of 2026-11-23).
9
+ */
10
+ export class Collapse {
11
+ #field;
12
+ #size;
13
+ constructor(options = {}) {
14
+ this.#field = options.field ?? 'source';
15
+ this.#size = options.size ?? 1;
16
+ }
17
+ async postprocess(passages) {
18
+ const kept = new Map();
19
+ return passages
20
+ .toSorted((a, b) => b.score - a.score)
21
+ .filter(({ passage }) => {
22
+ const key = this.#value(passage) ?? null;
23
+ const count = kept.get(key) ?? 0;
24
+ kept.set(key, count + 1);
25
+ return count < this.#size;
26
+ });
27
+ }
28
+ #value(passage) {
29
+ const field = this.#field;
30
+ return field === 'source' ||
31
+ field === 'seq' ||
32
+ field === 'start' ||
33
+ field === 'end'
34
+ ? passage[field]
35
+ : passage.metadata[field.slice('metadata.'.length)];
36
+ }
37
+ }
38
+ //# sourceMappingURL=collapse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collapse.js","sourceRoot":"","sources":["../src/collapse.ts"],"names":[],"mappings":"AAMA;;;;;;;;GAQG;AACH,MAAM,OAAO,QAAQ;IACV,MAAM,CAAQ;IACd,KAAK,CAAS;IAEvB,YACE,OAAO,GAKH,EAAE;QAEN,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,IAAI,QAAQ,CAAC;QACxC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,IAAI,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,KAAK,CAAC,WAAW,CACf,QAAkC;QAElC,MAAM,IAAI,GAAG,IAAI,GAAG,EAAmB,CAAC;QACxC,OAAO,QAAQ;aACZ,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC;aACrC,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE;YACtB,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC;YACzC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACjC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,GAAG,CAAC,CAAC,CAAC;YACzB,OAAO,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QAC5B,CAAC,CAAC,CAAC;IACP,CAAC;IAED,MAAM,CAAC,OAAiC;QACtC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,OAAO,KAAK,KAAK,QAAQ;YACvB,KAAK,KAAK,KAAK;YACf,KAAK,KAAK,OAAO;YACjB,KAAK,KAAK,KAAK;YACf,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC;YAChB,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;IACxD,CAAC;CACF"}
@@ -0,0 +1,26 @@
1
+ import type { PassageStore } from './passage-store.ts';
2
+ import type { Postprocessor } from './postprocessor.ts';
3
+ import type { ScoredPassage } from './scored-passage.ts';
4
+ /**
5
+ * Widens each passage to its window: the passages of its source from
6
+ * `seq - windowSize` to `seq + windowSize`, which the store gives (ADR 0008).
7
+ * The text of the passage becomes the text of its window, merged as
8
+ * Haystack's `SentenceWindowRetriever.merge_documents_text` merges it: in the
9
+ * order of `start`, each passage adds the part of its text past the end of
10
+ * the text before, so an overlap is not repeated. `start` and `end` become
11
+ * those of the window; the id, source, seq, metadata, score and vector stay.
12
+ * A passage that the store gives no window for stays as it is (ADR 0008, the
13
+ * proposed amendment of 2026-11-23).
14
+ */
15
+ export declare class ContextWindow implements Postprocessor {
16
+ #private;
17
+ constructor(options: {
18
+ readonly store: PassageStore;
19
+ /** How many passages before and after: 3 when absent, as Haystack's `window_size`. */
20
+ readonly windowSize?: number;
21
+ });
22
+ postprocess(passages: readonly ScoredPassage[], _query: string, options?: {
23
+ readonly abortSignal?: AbortSignal;
24
+ }): Promise<ScoredPassage[]>;
25
+ }
26
+ //# sourceMappingURL=context-window.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-window.d.ts","sourceRoot":"","sources":["../src/context-window.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;;;GAUG;AACH,qBAAa,aAAc,YAAW,aAAa;;IAIjD,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;QAC7B,sFAAsF;QACtF,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;KAC9B,EAQA;IAEK,WAAW,CACf,QAAQ,EAAE,SAAS,aAAa,EAAE,EAClC,MAAM,EAAE,MAAM,EACd,OAAO,GAAE;QAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAA;KAAO,GACnD,OAAO,CAAC,aAAa,EAAE,CAAC,CAc1B;CACF"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Widens each passage to its window: the passages of its source from
3
+ * `seq - windowSize` to `seq + windowSize`, which the store gives (ADR 0008).
4
+ * The text of the passage becomes the text of its window, merged as
5
+ * Haystack's `SentenceWindowRetriever.merge_documents_text` merges it: in the
6
+ * order of `start`, each passage adds the part of its text past the end of
7
+ * the text before, so an overlap is not repeated. `start` and `end` become
8
+ * those of the window; the id, source, seq, metadata, score and vector stay.
9
+ * A passage that the store gives no window for stays as it is (ADR 0008, the
10
+ * proposed amendment of 2026-11-23).
11
+ */
12
+ export class ContextWindow {
13
+ #store;
14
+ #windowSize;
15
+ constructor(options) {
16
+ this.#store = options.store;
17
+ this.#windowSize = options.windowSize ?? 3;
18
+ if (!Number.isInteger(this.#windowSize) || this.#windowSize < 1) {
19
+ throw new RangeError(`The windowSize is a positive integer, not ${this.#windowSize}`);
20
+ }
21
+ }
22
+ async postprocess(passages, _query, options = {}) {
23
+ options.abortSignal?.throwIfAborted();
24
+ return Promise.all(passages.map(async (scored) => {
25
+ const window = await this.#store.neighbours({
26
+ source: scored.passage.source,
27
+ seq: scored.passage.seq,
28
+ windowSize: this.#windowSize,
29
+ });
30
+ return window.length === 0
31
+ ? scored
32
+ : { ...scored, passage: widen(scored.passage, window) };
33
+ }));
34
+ }
35
+ }
36
+ function widen(passage, window) {
37
+ let text = '';
38
+ let end = 0;
39
+ const ordered = window.toSorted((a, b) => a.start - b.start);
40
+ for (const part of ordered) {
41
+ text += part.text.slice(Math.max(0, end - part.start));
42
+ end = Math.max(end, part.end);
43
+ }
44
+ return {
45
+ ...passage,
46
+ text,
47
+ start: Math.min(...ordered.map(({ start }) => start)),
48
+ end,
49
+ };
50
+ }
51
+ //# sourceMappingURL=context-window.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-window.js","sourceRoot":"","sources":["../src/context-window.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;GAUG;AACH,MAAM,OAAO,aAAa;IACf,MAAM,CAAe;IACrB,WAAW,CAAS;IAE7B,YAAY,OAIX;QACC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;QAC5B,IA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EAAE,CAAC;QAC3B,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QACvD,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;IAChC,CAAC;IACD,OAAO;QACL,GAAG,OAAO;QACV,IAAI;QACJ,KAAK,EAAE,IAAI,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;QACrD,GAAG;KACJ,CAAC;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,18 +1,25 @@
1
1
  export type { BatchRetriever } from './batch-retriever.ts';
2
+ export { Collapse } from './collapse.ts';
3
+ export { ContextWindow } from './context-window.ts';
2
4
  export { distributionBasedScoreFusion } from './distribution-based-score-fusion.ts';
3
5
  export type { Filter } from './filter.ts';
4
6
  export { FusionRetriever } from './fusion-retriever.ts';
5
7
  export type { Fusion } from './fusion.ts';
6
8
  export { HydeRetriever } from './hyde-retriever.ts';
9
+ export { LongContextReorder } from './long-context-reorder.ts';
10
+ export { MaximalMarginalRelevance } from './maximal-marginal-relevance.ts';
7
11
  export { ModelMismatchError } from './model-mismatch-error.ts';
8
12
  export { MultiQueryRetriever } from './multi-query-retriever.ts';
9
13
  export type { PassageStore } from './passage-store.ts';
10
14
  export type { Passage } from './passage.ts';
15
+ export { PostprocessingRetriever } from './postprocessing-retriever.ts';
11
16
  export type { Postprocessor } from './postprocessor.ts';
12
17
  export { reciprocalRankFusion } from './reciprocal-rank-fusion.ts';
13
18
  export { relativeScoreFusion } from './relative-score-fusion.ts';
19
+ export { Rerank } from './rerank.ts';
14
20
  export type { RetrieveOptions } from './retrieve-options.ts';
15
21
  export type { Retriever } from './retriever.ts';
16
22
  export type { ScoredPassage } from './scored-passage.ts';
23
+ export { SimilarityCutoff } from './similarity-cutoff.ts';
17
24
  export { VectorMissingError } from './vector-missing-error.ts';
18
25
  //# sourceMappingURL=index.d.ts.map
@@ -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,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"}
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,QAAQ,EAAE,MAAM,eAAe,CAAC;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{ distributionBasedScoreFusion } from './distribution-based-score-fusion.js';
2
4
  export { FusionRetriever } from './fusion-retriever.js';
3
5
  export { HydeRetriever } from './hyde-retriever.js';
6
+ export { LongContextReorder } from './long-context-reorder.js';
7
+ export { MaximalMarginalRelevance } from './maximal-marginal-relevance.js';
4
8
  export { ModelMismatchError } from './model-mismatch-error.js';
5
9
  export { MultiQueryRetriever } from './multi-query-retriever.js';
10
+ export { PostprocessingRetriever } from './postprocessing-retriever.js';
6
11
  export { reciprocalRankFusion } from './reciprocal-rank-fusion.js';
7
12
  export { relativeScoreFusion } from './relative-score-fusion.js';
13
+ export { Rerank } from './rerank.js';
14
+ export { SimilarityCutoff } from './similarity-cutoff.js';
8
15
  export { VectorMissingError } from './vector-missing-error.js';
9
16
  //# 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":"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"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,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,wBAAwB,EAAE,MAAM,iCAAiC,CAAC;AAC3E,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGjE,OAAO,EAAE,uBAAuB,EAAE,MAAM,+BAA+B,CAAC;AAExE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC"}
@@ -0,0 +1,18 @@
1
+ import type { Postprocessor } from './postprocessor.ts';
2
+ import type { ScoredPassage } from './scored-passage.ts';
3
+ /**
4
+ * Puts the best passages at the two ends of the list and the worst in the
5
+ * middle, where a language model reads a long context worst (Liu et al.,
6
+ * "Lost in the Middle", 2023, arXiv:2307.03172). The algorithm is copied from
7
+ * LangChain's `_litm_reordering`
8
+ * (langchain_community/document_transformers/long_context_reorder.py): the
9
+ * list, best first, is reversed, and each passage in turn goes to the front
10
+ * at an even position and to the back at an odd one. LlamaIndex's
11
+ * `LongContextReorder` (llama_index/core/postprocessor/node.py) gives the
12
+ * same order for a list sorted best first. The scores stay; the list is no
13
+ * longer sorted by them.
14
+ */
15
+ export declare class LongContextReorder implements Postprocessor {
16
+ postprocess(passages: readonly ScoredPassage[]): Promise<ScoredPassage[]>;
17
+ }
18
+ //# sourceMappingURL=long-context-reorder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"long-context-reorder.d.ts","sourceRoot":"","sources":["../src/long-context-reorder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;;;;;GAWG;AACH,qBAAa,kBAAmB,YAAW,aAAa;IAChD,WAAW,CACf,QAAQ,EAAE,SAAS,aAAa,EAAE,GACjC,OAAO,CAAC,aAAa,EAAE,CAAC,CAO1B;CACF"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Puts the best passages at the two ends of the list and the worst in the
3
+ * middle, where a language model reads a long context worst (Liu et al.,
4
+ * "Lost in the Middle", 2023, arXiv:2307.03172). The algorithm is copied from
5
+ * LangChain's `_litm_reordering`
6
+ * (langchain_community/document_transformers/long_context_reorder.py): the
7
+ * list, best first, is reversed, and each passage in turn goes to the front
8
+ * at an even position and to the back at an odd one. LlamaIndex's
9
+ * `LongContextReorder` (llama_index/core/postprocessor/node.py) gives the
10
+ * same order for a list sorted best first. The scores stay; the list is no
11
+ * longer sorted by them.
12
+ */
13
+ export class LongContextReorder {
14
+ async postprocess(passages) {
15
+ const reordered = [];
16
+ for (const [index, passage] of passages.toReversed().entries()) {
17
+ if (index % 2 === 1)
18
+ reordered.push(passage);
19
+ else
20
+ reordered.unshift(passage);
21
+ }
22
+ return reordered;
23
+ }
24
+ }
25
+ //# sourceMappingURL=long-context-reorder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"long-context-reorder.js","sourceRoot":"","sources":["../src/long-context-reorder.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;GAWG;AACH,MAAM,OAAO,kBAAkB;IAC7B,KAAK,CAAC,WAAW,CACf,QAAkC;QAElC,MAAM,SAAS,GAAoB,EAAE,CAAC;QACtC,KAAK,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE,EAAE,CAAC;YAC/D,IAAI,KAAK,GAAG,CAAC,KAAK,CAAC;gBAAE,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;;gBACxC,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,SAAS,CAAC;IACnB,CAAC;CACF"}
@@ -0,0 +1,32 @@
1
+ import { type EmbeddingModel } from 'ai';
2
+ import type { Postprocessor } from './postprocessor.ts';
3
+ import type { ScoredPassage } from './scored-passage.ts';
4
+ /**
5
+ * Maximal marginal relevance (Carbonell and Goldstein, SIGIR 1998): picks
6
+ * `topK` passages one at a time, each with the best
7
+ * `lambda * relevance - (1 - lambda) * redundancy`. The relevance is the
8
+ * cosine similarity of the passage's vector to the query's vector, and the
9
+ * redundancy is the passage's highest cosine similarity to a passage picked
10
+ * before. The first pick is the most relevant passage. A tie goes to the
11
+ * passage that comes first in the list. This is LangChain's
12
+ * `maximalMarginalRelevance` (libs/langchain-core/src/utils/math.ts). The
13
+ * model embeds the query, as Haystack's `SentenceTransformersDiversityRanker`
14
+ * embeds it (ADR 0008, the proposed amendment of 2026-11-23).
15
+ *
16
+ * The passages come back in the order of the picks, with the scores they had.
17
+ * They need their vectors: a retriever gives them with `includeVectors: true`.
18
+ */
19
+ export declare class MaximalMarginalRelevance implements Postprocessor {
20
+ #private;
21
+ constructor(options: {
22
+ /** The model that made the vectors of the passages; it embeds the query. */
23
+ readonly model: EmbeddingModel;
24
+ /** From 0 to 1: 1 is relevance only, 0 is diversity only. 0.5 when absent. */
25
+ readonly lambda?: number;
26
+ readonly topK: number;
27
+ });
28
+ postprocess(passages: readonly ScoredPassage[], query: string, options?: {
29
+ readonly abortSignal?: AbortSignal;
30
+ }): Promise<ScoredPassage[]>;
31
+ }
32
+ //# sourceMappingURL=maximal-marginal-relevance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"maximal-marginal-relevance.d.ts","sourceRoot":"","sources":["../src/maximal-marginal-relevance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAS,MAAM,IAAI,CAAC;AAIhD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGzD;;;;;;;;;;;;;;GAcG;AACH,qBAAa,wBAAyB,YAAW,aAAa;;IAK5D,YAAY,OAAO,EAAE;QACnB,4EAA4E;QAC5E,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;QAC/B,8EAA8E;QAC9E,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;KACvB,EAUA;IAEK,WAAW,CACf,QAAQ,EAAE,SAAS,aAAa,EAAE,EAClC,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;QAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAA;KAAO,GACnD,OAAO,CAAC,aAAa,EAAE,CAAC,CA6C1B;CACF"}
@@ -0,0 +1,76 @@
1
+ import { embed } from 'ai';
2
+ import { Vector, cosineSimilarity } from '@zukhruf/vectors';
3
+ import { VectorMissingError } from './vector-missing-error.js';
4
+ /**
5
+ * Maximal marginal relevance (Carbonell and Goldstein, SIGIR 1998): picks
6
+ * `topK` passages one at a time, each with the best
7
+ * `lambda * relevance - (1 - lambda) * redundancy`. The relevance is the
8
+ * cosine similarity of the passage's vector to the query's vector, and the
9
+ * redundancy is the passage's highest cosine similarity to a passage picked
10
+ * before. The first pick is the most relevant passage. A tie goes to the
11
+ * passage that comes first in the list. This is LangChain's
12
+ * `maximalMarginalRelevance` (libs/langchain-core/src/utils/math.ts). The
13
+ * model embeds the query, as Haystack's `SentenceTransformersDiversityRanker`
14
+ * embeds it (ADR 0008, the proposed amendment of 2026-11-23).
15
+ *
16
+ * The passages come back in the order of the picks, with the scores they had.
17
+ * They need their vectors: a retriever gives them with `includeVectors: true`.
18
+ */
19
+ export class MaximalMarginalRelevance {
20
+ #model;
21
+ #lambda;
22
+ #topK;
23
+ constructor(options) {
24
+ this.#model = options.model;
25
+ this.#lambda = options.lambda ?? 0.5;
26
+ this.#topK = options.topK;
27
+ if (!(this.#lambda >= 0 && this.#lambda <= 1)) {
28
+ throw new RangeError(`The lambda is from 0 to 1, not ${this.#lambda}`);
29
+ }
30
+ if (!Number.isInteger(this.#topK) || this.#topK < 1) {
31
+ throw new RangeError(`The topK is a positive integer, not ${this.#topK}`);
32
+ }
33
+ }
34
+ async postprocess(passages, query, options = {}) {
35
+ const candidates = passages.map((scored) => {
36
+ if (scored.vector === undefined) {
37
+ throw new VectorMissingError(scored.passage.id);
38
+ }
39
+ return { scored, vector: Vector.from(scored.vector) };
40
+ });
41
+ if (candidates.length === 0)
42
+ return [];
43
+ const { embedding } = await embed({
44
+ model: this.#model,
45
+ value: query,
46
+ ...(options.abortSignal === undefined
47
+ ? {}
48
+ : { abortSignal: options.abortSignal }),
49
+ });
50
+ const queryVector = Vector.from(embedding);
51
+ const remaining = candidates.map(({ scored, vector }) => ({
52
+ scored,
53
+ vector,
54
+ relevance: cosineSimilarity(vector, queryVector),
55
+ redundancy: Number.NEGATIVE_INFINITY,
56
+ }));
57
+ const picked = [];
58
+ while (picked.length < this.#topK && remaining.length > 0) {
59
+ const last = picked.at(-1);
60
+ const value = (candidate) => last === undefined
61
+ ? candidate.relevance
62
+ : this.#lambda * candidate.relevance -
63
+ (1 - this.#lambda) * candidate.redundancy;
64
+ if (last !== undefined) {
65
+ for (const candidate of remaining) {
66
+ candidate.redundancy = Math.max(candidate.redundancy, cosineSimilarity(candidate.vector, last.vector));
67
+ }
68
+ }
69
+ const best = remaining.reduce((first, next) => value(next) > value(first) ? next : first);
70
+ picked.push(best);
71
+ remaining.splice(remaining.indexOf(best), 1);
72
+ }
73
+ return picked.map(({ scored }) => scored);
74
+ }
75
+ }
76
+ //# sourceMappingURL=maximal-marginal-relevance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"maximal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CAAC,UAAU,CAAC;YAChD,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;gBACvB,KAAK,MAAM,SAAS,IAAI,SAAS,EAAE,CAAC;oBAClC,SAAS,CAAC,UAAU,GAAG,IAAI,CAAC,GAAG,CAC7B,SAAS,CAAC,UAAU,EACpB,gBAAgB,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,CAChD,CAAC;gBACJ,CAAC;YACH,CAAC;YACD,MAAM,IAAI,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAC5C,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAC1C,CAAC;YACF,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAClB,SAAS,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC;QAC/C,CAAC;QACD,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;CACF"}
@@ -0,0 +1,20 @@
1
+ import type { Postprocessor } from './postprocessor.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
+ * Runs its postprocessors, in order, on the result of its retriever, as
7
+ * LangChain's `ContextualCompressionRetriever` runs its compressor and
8
+ * LlamaIndex runs its `node_postprocessors` (ADR 0008). Each postprocessor
9
+ * gets the list of the one before it, the query and the abort signal of the
10
+ * call.
11
+ */
12
+ export declare class PostprocessingRetriever implements Retriever {
13
+ #private;
14
+ constructor(options: {
15
+ readonly retriever: Retriever;
16
+ readonly postprocessors: readonly Postprocessor[];
17
+ });
18
+ retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
19
+ }
20
+ //# sourceMappingURL=postprocessing-retriever.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postprocessing-retriever.d.ts","sourceRoot":"","sources":["../src/postprocessing-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,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,uBAAwB,YAAW,SAAS;;IAIvD,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;QAC9B,QAAQ,CAAC,cAAc,EAAE,SAAS,aAAa,EAAE,CAAC;KACnD,EAGA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAU1B;CACF"}
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Runs its postprocessors, in order, on the result of its retriever, as
3
+ * LangChain's `ContextualCompressionRetriever` runs its compressor and
4
+ * LlamaIndex runs its `node_postprocessors` (ADR 0008). Each postprocessor
5
+ * gets the list of the one before it, the query and the abort signal of the
6
+ * call.
7
+ */
8
+ export class PostprocessingRetriever {
9
+ #retriever;
10
+ #postprocessors;
11
+ constructor(options) {
12
+ this.#retriever = options.retriever;
13
+ this.#postprocessors = options.postprocessors;
14
+ }
15
+ async retrieve(query, options = {}) {
16
+ const signal = options.abortSignal === undefined
17
+ ? {}
18
+ : { abortSignal: options.abortSignal };
19
+ let passages = await this.#retriever.retrieve(query, options);
20
+ for (const postprocessor of this.#postprocessors) {
21
+ passages = await postprocessor.postprocess(passages, query, signal);
22
+ }
23
+ return passages;
24
+ }
25
+ }
26
+ //# sourceMappingURL=postprocessing-retriever.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"postprocessing-retriever.js","sourceRoot":"","sources":["../src/postprocessing-retriever.ts"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,MAAM,OAAO,uBAAuB;IACzB,UAAU,CAAY;IACtB,eAAe,CAA2B;IAEnD,YAAY,OAGX;QACC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;QACpC,IAAI,CAAC,eAAe,GAAG,OAAO,CAAC,cAAc,CAAC;IAChD,CAAC;IAED,KAAK,CAAC,QAAQ,CACZ,KAAa,EACb,OAAO,GAAoB,EAAE;QAE7B,MAAM,MAAM,GACV,OAAO,CAAC,WAAW,KAAK,SAAS;YAC/B,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,CAAC,WAAW,EAAE,CAAC;QAC3C,IAAI,QAAQ,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAC9D,KAAK,MAAM,aAAa,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YACjD,QAAQ,GAAG,MAAM,aAAa,CAAC,WAAW,CAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QACtE,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF"}
@@ -0,0 +1,23 @@
1
+ import { type RerankingModel } from 'ai';
2
+ import type { Postprocessor } from './postprocessor.ts';
3
+ import type { ScoredPassage } from './scored-passage.ts';
4
+ /**
5
+ * Reranks the passages with a reranking model through the AI SDK's `rerank`
6
+ * (ADR 0008). The model gets the text of each passage and `topN`; without
7
+ * `topN` it ranks every passage. Each passage that the model ranks gets the
8
+ * relevance that the model gives it as its score, higher is better, best
9
+ * first; a passage that the model leaves out is dropped. An empty list makes
10
+ * no model call, as the AI SDK's `rerank` sends none.
11
+ */
12
+ export declare class Rerank implements Postprocessor {
13
+ #private;
14
+ constructor(options: {
15
+ readonly model: RerankingModel;
16
+ /** How many passages the model keeps: every passage when absent. */
17
+ readonly topN?: number;
18
+ });
19
+ postprocess(passages: readonly ScoredPassage[], query: string, options?: {
20
+ readonly abortSignal?: AbortSignal;
21
+ }): Promise<ScoredPassage[]>;
22
+ }
23
+ //# sourceMappingURL=rerank.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rerank.d.ts","sourceRoot":"","sources":["../src/rerank.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAU,MAAM,IAAI,CAAC;AAEjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;;;;GAOG;AACH,qBAAa,MAAO,YAAW,aAAa;;IAI1C,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;QAC/B,oEAAoE;QACpE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;KACxB,EAGA;IAEK,WAAW,CACf,QAAQ,EAAE,SAAS,aAAa,EAAE,EAClC,KAAK,EAAE,MAAM,EACb,OAAO,GAAE;QAAE,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAA;KAAO,GACnD,OAAO,CAAC,aAAa,EAAE,CAAC,CAwB1B;CACF"}
package/dist/rerank.js ADDED
@@ -0,0 +1,42 @@
1
+ import { rerank } from 'ai';
2
+ /**
3
+ * Reranks the passages with a reranking model through the AI SDK's `rerank`
4
+ * (ADR 0008). The model gets the text of each passage and `topN`; without
5
+ * `topN` it ranks every passage. Each passage that the model ranks gets the
6
+ * relevance that the model gives it as its score, higher is better, best
7
+ * first; a passage that the model leaves out is dropped. An empty list makes
8
+ * no model call, as the AI SDK's `rerank` sends none.
9
+ */
10
+ export class Rerank {
11
+ #model;
12
+ #topN;
13
+ constructor(options) {
14
+ this.#model = options.model;
15
+ this.#topN = options.topN;
16
+ }
17
+ async postprocess(passages, query, options = {}) {
18
+ const { ranking } = await rerank({
19
+ model: this.#model,
20
+ documents: passages.map(({ passage }) => passage.text),
21
+ query,
22
+ ...(this.#topN === undefined ? {} : { topN: this.#topN }),
23
+ ...(options.abortSignal === undefined
24
+ ? {}
25
+ : { abortSignal: options.abortSignal }),
26
+ });
27
+ const relevance = new Map(ranking.map(({ originalIndex, score }) => [originalIndex, score]));
28
+ return passages
29
+ .flatMap(({ passage, vector }, index) => {
30
+ const score = relevance.get(index);
31
+ if (score === undefined)
32
+ return [];
33
+ return [
34
+ vector === undefined
35
+ ? { passage, score }
36
+ : { passage, score, vector },
37
+ ];
38
+ })
39
+ .toSorted((a, b) => b.score - a.score);
40
+ }
41
+ }
42
+ //# sourceMappingURL=rerank.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rerank.js","sourceRoot":"","sources":["../src/rerank.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,MAAM,EAAE,MAAM,IAAI,CAAC;AAKjD;;;;;;;GAOG;AACH,MAAM,OAAO,MAAM;IACR,MAAM,CAAiB;IACvB,KAAK,CAAqB;IAEnC,YAAY,OAIX;QACC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,WAAW,CACf,QAAkC,EAClC,KAAa,EACb,OAAO,GAA2C,EAAE;QAEpD,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,MAAM,CAAC;YAC/B,KAAK,EAAE,IAAI,CAAC,MAAM;YAClB,SAAS,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;YACtD,KAAK;YACL,GAAG,CAAC,IAAI,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC;YACzD,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,MAAM,SAAS,GAAG,IAAI,GAAG,CACvB,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,aAAa,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC,CAClE,CAAC;QACF,OAAO,QAAQ;aACZ,OAAO,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,EAAE;YACtC,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACnC,IAAI,KAAK,KAAK,SAAS;gBAAE,OAAO,EAAE,CAAC;YACnC,OAAO;gBACL,MAAM,KAAK,SAAS;oBAClB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE;oBACpB,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE;aAC/B,CAAC;QACJ,CAAC,CAAC;aACD,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;CACF"}
@@ -0,0 +1,15 @@
1
+ import type { Postprocessor } from './postprocessor.ts';
2
+ import type { ScoredPassage } from './scored-passage.ts';
3
+ /**
4
+ * Keeps the passages whose score is at least the threshold, in their order,
5
+ * as LlamaIndex's `SimilarityPostprocessor` drops a node whose score is below
6
+ * its `similarity_cutoff` (ADR 0005, ADR 0008).
7
+ */
8
+ export declare class SimilarityCutoff implements Postprocessor {
9
+ #private;
10
+ constructor(options: {
11
+ readonly threshold: number;
12
+ });
13
+ postprocess(passages: readonly ScoredPassage[]): Promise<ScoredPassage[]>;
14
+ }
15
+ //# sourceMappingURL=similarity-cutoff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"similarity-cutoff.d.ts","sourceRoot":"","sources":["../src/similarity-cutoff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD;;;;GAIG;AACH,qBAAa,gBAAiB,YAAW,aAAa;;IAGpD,YAAY,OAAO,EAAE;QAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;KAAE,EAElD;IAEK,WAAW,CACf,QAAQ,EAAE,SAAS,aAAa,EAAE,GACjC,OAAO,CAAC,aAAa,EAAE,CAAC,CAE1B;CACF"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Keeps the passages whose score is at least the threshold, in their order,
3
+ * as LlamaIndex's `SimilarityPostprocessor` drops a node whose score is below
4
+ * its `similarity_cutoff` (ADR 0005, ADR 0008).
5
+ */
6
+ export class SimilarityCutoff {
7
+ #threshold;
8
+ constructor(options) {
9
+ this.#threshold = options.threshold;
10
+ }
11
+ async postprocess(passages) {
12
+ return passages.filter(({ score }) => score >= this.#threshold);
13
+ }
14
+ }
15
+ //# sourceMappingURL=similarity-cutoff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"similarity-cutoff.js","sourceRoot":"","sources":["../src/similarity-cutoff.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,OAAO,gBAAgB;IAClB,UAAU,CAAS;IAE5B,YAAY,OAAuC;QACjD,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;IACtC,CAAC;IAED,KAAK,CAAC,WAAW,CACf,QAAkC;QAElC,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,IAAI,IAAI,CAAC,UAAU,CAAC,CAAC;IAClE,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/retrieval",
3
- "version": "0.8.80",
3
+ "version": "0.8.81",
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,11 +51,11 @@
51
51
  "access": "public"
52
52
  },
53
53
  "dependencies": {
54
- "@zukhruf/statistics": "^0.8.80",
55
- "@zukhruf/vectors": "^0.8.80",
54
+ "@zukhruf/statistics": "^0.8.81",
55
+ "@zukhruf/vectors": "^0.8.81",
56
56
  "ai": "^7.0.179"
57
57
  },
58
58
  "devDependencies": {
59
- "@zukhruf/sqlite-open": "^0.8.80"
59
+ "@zukhruf/sqlite-open": "^0.8.81"
60
60
  }
61
61
  }