@zukhruf/retrieval 0.8.92 → 0.8.94
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 +50 -6
- package/dist/sqlserver/binary.d.ts +8 -0
- package/dist/sqlserver/binary.d.ts.map +1 -0
- package/dist/sqlserver/binary.js +8 -0
- package/dist/sqlserver/binary.js.map +1 -0
- package/dist/sqlserver/bind.d.ts +6 -0
- package/dist/sqlserver/bind.d.ts.map +1 -0
- package/dist/sqlserver/bind.js +10 -0
- package/dist/sqlserver/bind.js.map +1 -0
- package/dist/sqlserver/identifier.d.ts +3 -0
- package/dist/sqlserver/identifier.d.ts.map +1 -0
- package/dist/sqlserver/identifier.js +3 -0
- package/dist/sqlserver/identifier.js.map +1 -0
- package/dist/sqlserver/passage-row.d.ts +11 -0
- package/dist/sqlserver/passage-row.d.ts.map +1 -0
- package/dist/sqlserver/passage-row.js +22 -0
- package/dist/sqlserver/passage-row.js.map +1 -0
- package/dist/sqlserver/sqlserver-embedding-retriever.d.ts +23 -0
- package/dist/sqlserver/sqlserver-embedding-retriever.d.ts.map +1 -0
- package/dist/sqlserver/sqlserver-embedding-retriever.js +125 -0
- package/dist/sqlserver/sqlserver-embedding-retriever.js.map +1 -0
- package/dist/sqlserver/sqlserver-keyword-retriever.d.ts +27 -0
- package/dist/sqlserver/sqlserver-keyword-retriever.d.ts.map +1 -0
- package/dist/sqlserver/sqlserver-keyword-retriever.js +64 -0
- package/dist/sqlserver/sqlserver-keyword-retriever.js.map +1 -0
- package/dist/sqlserver/sqlserver-passage-store.d.ts +43 -0
- package/dist/sqlserver/sqlserver-passage-store.d.ts.map +1 -0
- package/dist/sqlserver/sqlserver-passage-store.js +67 -0
- package/dist/sqlserver/sqlserver-passage-store.js.map +1 -0
- package/dist/sqlserver/sqlserver-session.d.ts +11 -0
- package/dist/sqlserver/sqlserver-session.d.ts.map +1 -0
- package/dist/sqlserver/sqlserver-session.js +2 -0
- package/dist/sqlserver/sqlserver-session.js.map +1 -0
- package/dist/sqlserver/where.d.ts +11 -0
- package/dist/sqlserver/where.d.ts.map +1 -0
- package/dist/sqlserver/where.js +64 -0
- package/dist/sqlserver/where.js.map +1 -0
- package/dist/sqlserver.d.ts +5 -0
- package/dist/sqlserver.d.ts.map +1 -0
- package/dist/sqlserver.js +4 -0
- package/dist/sqlserver.js.map +1 -0
- package/package.json +13 -5
package/README.md
CHANGED
|
@@ -40,11 +40,11 @@ const results = await retriever.retrieve('how do I rotate a key?');
|
|
|
40
40
|
// 10 scored passages, best first: { passage: { id, text, source, seq, start, end, metadata }, score }
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
-
The passage store is the root of the composition. It holds the connection and the passages, and on Postgres the schema of the contract. Each retriever and `ContextWindow` takes the store and only its own part: the vectors and the model, or the keyword index. So two retrievers of one search read one connection and one contract, as Haystack's retrievers take their `document_store`.
|
|
43
|
+
The passage store is the root of the composition. It holds the connection and the passages, and on Postgres and SQL Server the schema of the contract. Each retriever and `ContextWindow` takes the store and only its own part: the vectors and the model, or the keyword index. So two retrievers of one search read one connection and one contract, as Haystack's retrievers take their `document_store`.
|
|
44
44
|
|
|
45
45
|
## Search passages by meaning
|
|
46
46
|
|
|
47
|
-
An embedding retriever embeds the query with its AI SDK model and sends one SQL query to the engine. The engine ranks the passages by cosine similarity: `vec_distance_cosine` on SQLite, pgvector's `<=>` on Postgres, `VECTOR_DISTANCE`
|
|
47
|
+
An embedding retriever embeds the query with its AI SDK model and sends one SQL query to the engine. The engine ranks the passages by cosine similarity: `vec_distance_cosine` on SQLite, pgvector's `<=>` on Postgres, `VECTOR_DISTANCE` on SQL Server ([ADR 0003](./docs/adr/0003-an-embedding-retriever-runs-the-vector-search-in-the-sql-of-its-engine-one-query-or-a-batch.md)). The score is `1 - cosine distance`.
|
|
48
48
|
|
|
49
49
|
```ts
|
|
50
50
|
// Planned API.
|
|
@@ -106,6 +106,50 @@ const keywords = new PgvectorKeywordRetriever({
|
|
|
106
106
|
});
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
## Search SQL Server
|
|
110
|
+
|
|
111
|
+
`@zukhruf/retrieval/sqlserver` needs SQL Server 2025 or Azure SQL for `VECTOR` and `VECTOR_DISTANCE`, and Full-Text Search for the keyword retriever. The session takes named parameters, `@p0` for the key `p0`, as the SQL Server store of `@zukhruf/embedding-cache` does, so the package imports no driver.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
// Planned API.
|
|
115
|
+
import mssql from 'mssql';
|
|
116
|
+
|
|
117
|
+
import {
|
|
118
|
+
SqlServerEmbeddingRetriever,
|
|
119
|
+
SqlServerKeywordRetriever,
|
|
120
|
+
SqlServerPassageStore,
|
|
121
|
+
} from '@zukhruf/retrieval/sqlserver';
|
|
122
|
+
|
|
123
|
+
const pool = await new mssql.ConnectionPool(connectionString).connect();
|
|
124
|
+
const store = new SqlServerPassageStore({
|
|
125
|
+
session: {
|
|
126
|
+
async query(sql, parameters) {
|
|
127
|
+
const request = pool.request();
|
|
128
|
+
for (const [name, value] of Object.entries(parameters))
|
|
129
|
+
request.input(name, value);
|
|
130
|
+
return (await request.query(sql)).recordset ?? [];
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
schema: 'rag', // the default schema of the user when absent
|
|
134
|
+
passages: 'docs_passages',
|
|
135
|
+
});
|
|
136
|
+
const meaning = new SqlServerEmbeddingRetriever({
|
|
137
|
+
store,
|
|
138
|
+
vectors: 'docs_passage_vectors', // [rag].[docs_passage_vectors]
|
|
139
|
+
model,
|
|
140
|
+
});
|
|
141
|
+
const keywords = new SqlServerKeywordRetriever({
|
|
142
|
+
store,
|
|
143
|
+
index: { table: 'docs_passages' }, // the table with the full-text index on [text]
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
- **The vector search is exact.** `VECTOR_DISTANCE` reads every vector of the identity of the query; it uses no DiskANN index. The approximate `VECTOR_SEARCH` waits: on SQL Server 2025 it is a preview, filters after the search and needs a read-only table with an INT key ([ADR 0003](./docs/adr/0003-an-embedding-retriever-runs-the-vector-search-in-the-sql-of-its-engine-one-query-or-a-batch.md)).
|
|
148
|
+
- **Ids, sources and model identities compare in binary**, `Latin1_General_100_BIN2`, so `A` is not `a` and a tie orders as on SQLite and Postgres, whatever the collation of the database is. A filter compares in the collation of its column, usually case-insensitive.
|
|
149
|
+
- **The keyword index is a table.** `CONTAINSTABLE` reads a full-text index of a base table, never of a view, so `index.table` names the table whose full-text key is the passage id; the passages can be a view over it ([ADR 0004](./docs/adr/0004-a-keyword-retriever-queries-the-keyword-index-that-elt-writes.md)). The score is `RANK`, from 0 to 1000.
|
|
150
|
+
- **Make the index with `STOPLIST = OFF`.** With the system stoplist, a stop word or a single letter in an `AND` query gives no passages. A word of punctuation only is dropped from the query, because SQL Server reads it as a stop word under every stoplist.
|
|
151
|
+
- **A query of more than 4,000 characters of condition fails** with SQL Server's error 4110: the driver binds the text as `NVARCHAR(MAX)`, which `CONTAINSTABLE` refuses.
|
|
152
|
+
|
|
109
153
|
## Combine searches
|
|
110
154
|
|
|
111
155
|
`FusionRetriever` asks each retriever for `rankWindowSize` passages, fuses the lists on `Passage.id` and gives the best `topK`. The default fusion is reciprocal rank fusion with k 60 and ranks from 1; each retriever has a weight of 1 ([ADR 0007](./docs/adr/0007-reciprocal-rank-fusion-is-the-default-fusion-with-k-60-and-ranks-from-1.md)).
|
|
@@ -180,7 +224,7 @@ const results = await search.retrieve('rotate a key', {
|
|
|
180
224
|
|
|
181
225
|
## Search many queries at once
|
|
182
226
|
|
|
183
|
-
Batch search is part of vector search. Each embedding retriever has `retrieveMany` beside `retrieve`. It embeds the queries in one model call and runs one SQL query: `CROSS JOIN LATERAL` on Postgres
|
|
227
|
+
Batch search is part of vector search. Each embedding retriever has `retrieveMany` beside `retrieve`. It embeds the queries in one model call and runs one SQL query: `CROSS JOIN LATERAL` on Postgres, `CROSS APPLY` over the query vectors of one `OPENJSON` parameter on SQL Server. The SQLite form waits on a cost probe (#5422) ([ADR 0003](./docs/adr/0003-an-embedding-retriever-runs-the-vector-search-in-the-sql-of-its-engine-one-query-or-a-batch.md)).
|
|
184
228
|
|
|
185
229
|
```ts
|
|
186
230
|
// Planned API.
|
|
@@ -474,14 +518,14 @@ class SqlServerEmbeddingRetriever implements BatchRetriever {
|
|
|
474
518
|
class SqlServerKeywordRetriever implements Retriever {
|
|
475
519
|
constructor(options: {
|
|
476
520
|
store: SqlServerPassageStore;
|
|
477
|
-
index: string;
|
|
521
|
+
index: { table: string }; // the base table with the full-text index on [text]
|
|
478
522
|
operator?: 'AND' | 'OR';
|
|
479
|
-
});
|
|
523
|
+
}); // 'AND'
|
|
480
524
|
}
|
|
481
525
|
class SqlServerPassageStore implements PassageStore {
|
|
482
526
|
constructor(options: {
|
|
483
527
|
session: SqlServerSession;
|
|
484
|
-
schema?: string;
|
|
528
|
+
schema?: string; // the default schema of the user when absent
|
|
485
529
|
passages: string;
|
|
486
530
|
});
|
|
487
531
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The collation of every id, model identity and source that the retrievers
|
|
3
|
+
* compare or order: UTF-16 code units, the order of JavaScript's `sort()`,
|
|
4
|
+
* whatever the collation of the database is. A case-insensitive default
|
|
5
|
+
* would make `a` and `A` one id.
|
|
6
|
+
*/
|
|
7
|
+
export declare const binary = "COLLATE Latin1_General_100_BIN2";
|
|
8
|
+
//# sourceMappingURL=binary.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../../src/sqlserver/binary.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,MAAM,oCAAoC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The collation of every id, model identity and source that the retrievers
|
|
3
|
+
* compare or order: UTF-16 code units, the order of JavaScript's `sort()`,
|
|
4
|
+
* whatever the collation of the database is. A case-insensitive default
|
|
5
|
+
* would make `a` and `A` one id.
|
|
6
|
+
*/
|
|
7
|
+
export const binary = 'COLLATE Latin1_General_100_BIN2';
|
|
8
|
+
//# sourceMappingURL=binary.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.js","sourceRoot":"","sources":["../../src/sqlserver/binary.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,iCAAiC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bind.d.ts","sourceRoot":"","sources":["../../src/sqlserver/bind.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,wBAAgB,IAAI,CAClB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,KAAK,EAAE,OAAO,GACb,MAAM,CAIR"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adds a value to the named parameters of a statement and gives its
|
|
3
|
+
* placeholder: `@p0` for the first, `@p1` for the next.
|
|
4
|
+
*/
|
|
5
|
+
export function bind(parameters, value) {
|
|
6
|
+
const name = `p${Object.keys(parameters).length}`;
|
|
7
|
+
parameters[name] = value;
|
|
8
|
+
return `@${name}`;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=bind.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bind.js","sourceRoot":"","sources":["../../src/sqlserver/bind.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,IAAI,CAClB,UAAmC,EACnC,KAAc;IAEd,MAAM,IAAI,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,MAAM,EAAE,CAAC;IAClD,UAAU,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC;IACzB,OAAO,IAAI,IAAI,EAAE,CAAC;AACpB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.d.ts","sourceRoot":"","sources":["../../src/sqlserver/identifier.ts"],"names":[],"mappings":"AAAA,sIAAsI;AACtI,eAAO,MAAM,UAAU,SAAU,MAAM,KAAG,MACP,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"identifier.js","sourceRoot":"","sources":["../../src/sqlserver/identifier.ts"],"names":[],"mappings":"AAAA,sIAAsI;AACtI,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAY,EAAU,EAAE,CACjD,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Passage } from '../passage.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The SQL of one cell that holds the row of the passages `p` as JSON text.
|
|
4
|
+
* One cell keeps every column of the passages under its own name, whatever
|
|
5
|
+
* cells the search adds beside it. A `BIT` is `true` or `false`, a
|
|
6
|
+
* `DATETIME2` ISO text, and NULL `null`.
|
|
7
|
+
*/
|
|
8
|
+
export declare const passageJson = "(SELECT p.* FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES)";
|
|
9
|
+
/** The passage of a cell that `passageJson` gave. A metadata column is any other column of the passages, under its own name. */
|
|
10
|
+
export declare function passage(cell: unknown): Passage;
|
|
11
|
+
//# sourceMappingURL=passage-row.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"passage-row.d.ts","sourceRoot":"","sources":["../../src/sqlserver/passage-row.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAI7C;;;;;GAKG;AACH,eAAO,MAAM,WAAW,2EACkD,CAAC;AAE3E,gIAAgI;AAChI,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,OAAO,CAa9C"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const passageColumns = new Set(['id', 'source', 'seq', 'start', 'end', 'text']);
|
|
2
|
+
/**
|
|
3
|
+
* The SQL of one cell that holds the row of the passages `p` as JSON text.
|
|
4
|
+
* One cell keeps every column of the passages under its own name, whatever
|
|
5
|
+
* cells the search adds beside it. A `BIT` is `true` or `false`, a
|
|
6
|
+
* `DATETIME2` ISO text, and NULL `null`.
|
|
7
|
+
*/
|
|
8
|
+
export const passageJson = '(SELECT p.* FOR JSON PATH, WITHOUT_ARRAY_WRAPPER, INCLUDE_NULL_VALUES)';
|
|
9
|
+
/** The passage of a cell that `passageJson` gave. A metadata column is any other column of the passages, under its own name. */
|
|
10
|
+
export function passage(cell) {
|
|
11
|
+
const row = JSON.parse(String(cell));
|
|
12
|
+
return {
|
|
13
|
+
id: String(row['id']),
|
|
14
|
+
text: String(row['text']),
|
|
15
|
+
source: String(row['source']),
|
|
16
|
+
seq: Number(row['seq']),
|
|
17
|
+
start: Number(row['start']),
|
|
18
|
+
end: Number(row['end']),
|
|
19
|
+
metadata: Object.fromEntries(Object.entries(row).filter(([name]) => !passageColumns.has(name))),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=passage-row.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"passage-row.js","sourceRoot":"","sources":["../../src/sqlserver/passage-row.ts"],"names":[],"mappings":"AAEA,MAAM,cAAc,GAAG,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC;AAEhF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,WAAW,GACtB,wEAAwE,CAAC;AAE3E,gIAAgI;AAChI,MAAM,UAAU,OAAO,CAAC,IAAa;IACnC,MAAM,GAAG,GAA4B,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;IAC9D,OAAO;QACL,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACrB,IAAI,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACzB,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC7B,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACvB,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC3B,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACvB,QAAQ,EAAE,MAAM,CAAC,WAAW,CAC1B,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAClE;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type EmbeddingModel } from 'ai';
|
|
2
|
+
import type { BatchRetriever } from '../batch-retriever.ts';
|
|
3
|
+
import type { RetrieveOptions } from '../retrieve-options.ts';
|
|
4
|
+
import type { ScoredPassage } from '../scored-passage.ts';
|
|
5
|
+
import { type SqlServerPassageStore } from './sqlserver-passage-store.ts';
|
|
6
|
+
/**
|
|
7
|
+
* Ranks the passages by the cosine similarity of their vectors to the query,
|
|
8
|
+
* with `VECTOR_DISTANCE` in the SQL of SQL Server 2025 (ADR 0003). The search
|
|
9
|
+
* is exact: `VECTOR_DISTANCE` uses no vector index.
|
|
10
|
+
*/
|
|
11
|
+
export declare class SqlServerEmbeddingRetriever implements BatchRetriever {
|
|
12
|
+
#private;
|
|
13
|
+
constructor(options: {
|
|
14
|
+
/** The store of the passages, whose session and schema the search uses. */
|
|
15
|
+
readonly store: SqlServerPassageStore;
|
|
16
|
+
/** The passage vectors, in the schema of the store. */
|
|
17
|
+
readonly vectors: string;
|
|
18
|
+
readonly model: EmbeddingModel;
|
|
19
|
+
});
|
|
20
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
|
|
21
|
+
retrieveMany(queries: readonly string[], options?: RetrieveOptions): Promise<ScoredPassage[][]>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=sqlserver-embedding-retriever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-embedding-retriever.d.ts","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-embedding-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,cAAc,EAAoB,MAAM,IAAI,CAAC;AAK3D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAQ5D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAI1D,OAAO,EACL,KAAK,qBAAqB,EAE3B,MAAM,8BAA8B,CAAC;AAItC;;;;GAIG;AACH,qBAAa,2BAA4B,YAAW,cAAc;;IAMhE,YAAY,OAAO,EAAE;QACnB,2EAA2E;QAC3E,QAAQ,CAAC,KAAK,EAAE,qBAAqB,CAAC;QACtC,uDAAuD;QACvD,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,KAAK,EAAE,cAAc,CAAC;KAChC,EAMA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAU1B;IAEK,YAAY,CAChB,OAAO,EAAE,SAAS,MAAM,EAAE,EAC1B,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAU5B;CAsGF"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { embed, embedMany } from 'ai';
|
|
2
|
+
import { DimensionMismatchError, Vector } from '@zukhruf/vectors';
|
|
3
|
+
import { fromSql, sqlType, toSql } from '@zukhruf/vectors/sqlserver';
|
|
4
|
+
import { describe, modelName, parseModelIdentity, } from '../model-identity.js';
|
|
5
|
+
import { ModelMismatchError } from '../model-mismatch-error.js';
|
|
6
|
+
import { binary } from './binary.js';
|
|
7
|
+
import { bind } from './bind.js';
|
|
8
|
+
import { passage, passageJson } from './passage-row.js';
|
|
9
|
+
import { contractOf, } from './sqlserver-passage-store.js';
|
|
10
|
+
import { where } from './where.js';
|
|
11
|
+
/**
|
|
12
|
+
* Ranks the passages by the cosine similarity of their vectors to the query,
|
|
13
|
+
* with `VECTOR_DISTANCE` in the SQL of SQL Server 2025 (ADR 0003). The search
|
|
14
|
+
* is exact: `VECTOR_DISTANCE` uses no vector index.
|
|
15
|
+
*/
|
|
16
|
+
export class SqlServerEmbeddingRetriever {
|
|
17
|
+
#session;
|
|
18
|
+
#passages;
|
|
19
|
+
#vectors;
|
|
20
|
+
#model;
|
|
21
|
+
constructor(options) {
|
|
22
|
+
const contract = contractOf(options.store);
|
|
23
|
+
this.#session = contract.session;
|
|
24
|
+
this.#passages = contract.passages;
|
|
25
|
+
this.#vectors = contract.relation(options.vectors);
|
|
26
|
+
this.#model = options.model;
|
|
27
|
+
}
|
|
28
|
+
async retrieve(query, options = {}) {
|
|
29
|
+
const [passages] = await this.#retrieve(1, options, async (abortSignal) => {
|
|
30
|
+
const { embedding } = await embed({
|
|
31
|
+
model: this.#model,
|
|
32
|
+
value: query,
|
|
33
|
+
...(abortSignal === undefined ? {} : { abortSignal }),
|
|
34
|
+
});
|
|
35
|
+
return [embedding];
|
|
36
|
+
});
|
|
37
|
+
return passages ?? [];
|
|
38
|
+
}
|
|
39
|
+
async retrieveMany(queries, options = {}) {
|
|
40
|
+
if (queries.length === 0)
|
|
41
|
+
return [];
|
|
42
|
+
return this.#retrieve(queries.length, options, async (abortSignal) => {
|
|
43
|
+
const { embeddings } = await embedMany({
|
|
44
|
+
model: this.#model,
|
|
45
|
+
values: [...queries],
|
|
46
|
+
...(abortSignal === undefined ? {} : { abortSignal }),
|
|
47
|
+
});
|
|
48
|
+
return embeddings;
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
async #retrieve(count, options, embedQueries) {
|
|
52
|
+
options.abortSignal?.throwIfAborted();
|
|
53
|
+
const query = modelName(this.#model);
|
|
54
|
+
const stored = await this.#storedIdentities();
|
|
55
|
+
const [first] = stored;
|
|
56
|
+
if (first === undefined)
|
|
57
|
+
return Array.from({ length: count }, () => []);
|
|
58
|
+
// A vector column has its dimensions in its type, `VECTOR(n)`, so the
|
|
59
|
+
// table holds one identity of a model, unlike SQLite and Postgres.
|
|
60
|
+
const identity = stored.find(({ provider, modelId }) => provider === query.provider && modelId === query.modelId);
|
|
61
|
+
if (identity === undefined) {
|
|
62
|
+
throw new ModelMismatchError({ provider: first.provider, modelId: first.modelId }, query);
|
|
63
|
+
}
|
|
64
|
+
const vectors = await embedQueries(options.abortSignal);
|
|
65
|
+
options.abortSignal?.throwIfAborted();
|
|
66
|
+
const dimensions = vectors[0]?.length ?? 0;
|
|
67
|
+
if (dimensions !== identity.dimensions) {
|
|
68
|
+
throw new DimensionMismatchError(identity.dimensions, dimensions);
|
|
69
|
+
}
|
|
70
|
+
return this.#search(vectors, identity, options);
|
|
71
|
+
}
|
|
72
|
+
/** The identities of the stored vectors, in binary order of their text, as SQLite orders text. */
|
|
73
|
+
async #storedIdentities() {
|
|
74
|
+
const rows = await this.#session.query(`SELECT DISTINCT v.[model] ${binary} AS [model] FROM ${this.#vectors} v ORDER BY 1`, {});
|
|
75
|
+
return rows.map(({ model }) => parseModelIdentity(String(model)));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* One SQL statement for every query vector: the query vectors are one JSON
|
|
79
|
+
* parameter that `OPENJSON` reads, so a batch of any size stays under the
|
|
80
|
+
* 2,100 parameters of SQL Server, and each query runs its own ordered,
|
|
81
|
+
* limited scan through `CROSS APPLY`. It compares a query only with the
|
|
82
|
+
* vectors of its own model identity, skips a NULL vector, which would sort
|
|
83
|
+
* first, and breaks a tie of distance by the passage id in binary order.
|
|
84
|
+
*/
|
|
85
|
+
async #search(vectors, identity, options) {
|
|
86
|
+
const parameters = {};
|
|
87
|
+
const queries = bind(parameters, `[${vectors.map((vector) => toSql(Vector.from(vector))).join(',')}]`);
|
|
88
|
+
const model = bind(parameters, describe(identity));
|
|
89
|
+
const filter = where(options.filter, parameters);
|
|
90
|
+
const topK = bind(parameters, options.topK ?? 10);
|
|
91
|
+
// A CAST target cannot be a parameter; sqlType checks the dimensions.
|
|
92
|
+
const type = sqlType(Vector, identity.dimensions);
|
|
93
|
+
const rows = await this.#session.query(`
|
|
94
|
+
WITH [queries] AS (
|
|
95
|
+
SELECT CONVERT(INT, j.[key]) AS [query], CAST(j.[value] AS ${type}) AS [vector]
|
|
96
|
+
FROM OPENJSON(${queries}) j
|
|
97
|
+
)
|
|
98
|
+
SELECT q.[query], r.[distance], r.[vector], r.[passage]
|
|
99
|
+
FROM [queries] q CROSS APPLY (
|
|
100
|
+
SELECT TOP (${topK})
|
|
101
|
+
VECTOR_DISTANCE('cosine', v.[vector], q.[vector]) AS [distance],
|
|
102
|
+
CAST(v.[vector] AS NVARCHAR(MAX)) AS [vector],
|
|
103
|
+
${passageJson} AS [passage],
|
|
104
|
+
p.[id] ${binary} AS [id]
|
|
105
|
+
FROM ${this.#vectors} v JOIN ${this.#passages} p ON p.[id] = v.[id] ${binary}
|
|
106
|
+
WHERE v.[model] = ${model} ${binary} AND v.[vector] IS NOT NULL AND ${filter}
|
|
107
|
+
ORDER BY 1, 4
|
|
108
|
+
) r
|
|
109
|
+
ORDER BY q.[query], r.[distance], r.[id]`, parameters);
|
|
110
|
+
const lists = vectors.map(() => []);
|
|
111
|
+
for (const cells of rows) {
|
|
112
|
+
lists[Number(cells['query'])]?.push({
|
|
113
|
+
passage: passage(cells['passage']),
|
|
114
|
+
score: 1 - Number(cells['distance']),
|
|
115
|
+
...(options.includeVectors === true
|
|
116
|
+
? {
|
|
117
|
+
vector: Array.from(fromSql(Vector, String(cells['vector'])).values),
|
|
118
|
+
}
|
|
119
|
+
: {}),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return lists;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//# sourceMappingURL=sqlserver-embedding-retriever.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-embedding-retriever.js","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-embedding-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAE3D,OAAO,EAAE,sBAAsB,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AAGrE,OAAO,EAEL,QAAQ,EACR,SAAS,EACT,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,kBAAkB,EAAE,MAAM,4BAA4B,CAAC;AAGhE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAEL,UAAU,GACX,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;;;GAIG;AACH,MAAM,OAAO,2BAA2B;IAC7B,QAAQ,CAAmB;IAC3B,SAAS,CAAS;IAClB,QAAQ,CAAS;IACjB,MAAM,CAAiB;IAEhC,YAAY,OAMX;QACC,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,QAAQ,CAAC;QACnC,IAAI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xD,KAAK,IAAI,MAAM,mCAAmC,MAAM;;;+CAGrC,EACzC,UAAU,CACX,CAAC;QACF,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,GAAoB,EAAE,CAAC,EAAE,CAAC,CAAC;QACrD,KAAK,MAAM,KAAK,IAAI,IAAI,EAAE,CAAC;YACzB,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC;gBAClC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;gBAClC,KAAK,EAAE,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;gBACpC,GAAG,CAAC,OAAO,CAAC,cAAc,KAAK,IAAI;oBACjC,CAAC,CAAC;wBACE,MAAM,EAAE,KAAK,CAAC,IAAI,CAChB,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAChD;qBACF;oBACH,CAAC,CAAC,EAAE,CAAC;aACR,CAAC,CAAC;QACL,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;CACF"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { RetrieveOptions } from '../retrieve-options.ts';
|
|
2
|
+
import type { Retriever } from '../retriever.ts';
|
|
3
|
+
import type { ScoredPassage } from '../scored-passage.ts';
|
|
4
|
+
import { type SqlServerPassageStore } from './sqlserver-passage-store.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Ranks the passages by SQL Server full-text search over the keyword index
|
|
7
|
+
* that elt writes: a full-text index on the `[text]` column of a base table
|
|
8
|
+
* whose full-text key is the passage id (ADR 0004). The search is
|
|
9
|
+
* `CONTAINSTABLE`, and the score is its `RANK`, which is higher for a better
|
|
10
|
+
* match (ADR 0005).
|
|
11
|
+
*/
|
|
12
|
+
export declare class SqlServerKeywordRetriever implements Retriever {
|
|
13
|
+
#private;
|
|
14
|
+
constructor(options: {
|
|
15
|
+
/** The store of the passages, whose session and schema the search uses. */
|
|
16
|
+
readonly store: SqlServerPassageStore;
|
|
17
|
+
/** The keyword index as elt writes it. */
|
|
18
|
+
readonly index: {
|
|
19
|
+
/** The base table, in the schema of the store, that holds the full-text index on `[text]` with the passage id as its key. */
|
|
20
|
+
readonly table: string;
|
|
21
|
+
};
|
|
22
|
+
/** How the words of the query join: 'AND' when absent. */
|
|
23
|
+
readonly operator?: 'AND' | 'OR';
|
|
24
|
+
});
|
|
25
|
+
retrieve(query: string, options?: RetrieveOptions): Promise<ScoredPassage[]>;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=sqlserver-keyword-retriever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-keyword-retriever.d.ts","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-keyword-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACjD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAI1D,OAAO,EACL,KAAK,qBAAqB,EAE3B,MAAM,8BAA8B,CAAC;AAItC;;;;;;GAMG;AACH,qBAAa,yBAA0B,YAAW,SAAS;;IAMzD,YAAY,OAAO,EAAE;QACnB,2EAA2E;QAC3E,QAAQ,CAAC,KAAK,EAAE,qBAAqB,CAAC;QACtC,0CAA0C;QAC1C,QAAQ,CAAC,KAAK,EAAE;YACd,6HAA6H;YAC7H,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;SACxB,CAAC;QACF,0DAA0D;QAC1D,QAAQ,CAAC,QAAQ,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;KAClC,EAMA;IAEK,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,GAAE,eAAoB,GAC5B,OAAO,CAAC,aAAa,EAAE,CAAC,CAwB1B;CACF"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { binary } from './binary.js';
|
|
2
|
+
import { bind } from './bind.js';
|
|
3
|
+
import { passage, passageJson } from './passage-row.js';
|
|
4
|
+
import { contractOf, } from './sqlserver-passage-store.js';
|
|
5
|
+
import { where } from './where.js';
|
|
6
|
+
/**
|
|
7
|
+
* Ranks the passages by SQL Server full-text search over the keyword index
|
|
8
|
+
* that elt writes: a full-text index on the `[text]` column of a base table
|
|
9
|
+
* whose full-text key is the passage id (ADR 0004). The search is
|
|
10
|
+
* `CONTAINSTABLE`, and the score is its `RANK`, which is higher for a better
|
|
11
|
+
* match (ADR 0005).
|
|
12
|
+
*/
|
|
13
|
+
export class SqlServerKeywordRetriever {
|
|
14
|
+
#session;
|
|
15
|
+
#passages;
|
|
16
|
+
#index;
|
|
17
|
+
#operator;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
const contract = contractOf(options.store);
|
|
20
|
+
this.#session = contract.session;
|
|
21
|
+
this.#passages = contract.passages;
|
|
22
|
+
this.#index = contract.relation(options.index.table);
|
|
23
|
+
this.#operator = options.operator ?? 'AND';
|
|
24
|
+
}
|
|
25
|
+
async retrieve(query, options = {}) {
|
|
26
|
+
options.abortSignal?.throwIfAborted();
|
|
27
|
+
const words = phrases(query);
|
|
28
|
+
if (words.length === 0)
|
|
29
|
+
return [];
|
|
30
|
+
const parameters = {};
|
|
31
|
+
// A condition longer than 4,000 characters reaches the caller as SQL
|
|
32
|
+
// Server's error 4110: the driver binds it as NVARCHAR(MAX), which
|
|
33
|
+
// CONTAINSTABLE refuses.
|
|
34
|
+
const condition = bind(parameters, words.join(` ${this.#operator} `));
|
|
35
|
+
const filter = where(options.filter, parameters);
|
|
36
|
+
const topK = bind(parameters, options.topK ?? 10);
|
|
37
|
+
const rows = await this.#session.query(`
|
|
38
|
+
SELECT TOP (${topK}) k.[RANK] AS [score], ${passageJson} AS [passage]
|
|
39
|
+
FROM CONTAINSTABLE(${this.#index}, [text], ${condition}) k
|
|
40
|
+
JOIN ${this.#passages} p ON p.[id] = k.[KEY] ${binary}
|
|
41
|
+
WHERE ${filter}
|
|
42
|
+
ORDER BY k.[RANK] DESC, p.[id] ${binary}`, parameters);
|
|
43
|
+
return rows.map((cells) => ({
|
|
44
|
+
passage: passage(cells['passage']),
|
|
45
|
+
score: Number(cells['score']),
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Each word of the query as one CONTAINS phrase, `"…"`. A `"` would end the
|
|
51
|
+
* phrase and a `*` before its end makes a prefix term, so each becomes a
|
|
52
|
+
* space, which the word breaker reads as it reads the punctuation. A word
|
|
53
|
+
* with no letter or digit makes no token and acts as a noise word, which
|
|
54
|
+
* gives an AND no rows, so it is dropped. `AND`, `OR`, `NEAR`, `&`, `|`, `!`,
|
|
55
|
+
* `~` and parentheses inside a phrase are text.
|
|
56
|
+
*/
|
|
57
|
+
function phrases(query) {
|
|
58
|
+
return query
|
|
59
|
+
.split(/\s+/)
|
|
60
|
+
.map((word) => word.replaceAll(/["*]/g, ' ').trim())
|
|
61
|
+
.filter((word) => /[\p{L}\p{N}]/u.test(word))
|
|
62
|
+
.map((word) => `"${word}"`);
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=sqlserver-keyword-retriever.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-keyword-retriever.js","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-keyword-retriever.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACxD,OAAO,EAEL,UAAU,GACX,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC;;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AAI,CAAC,SAAS,0BAA0B,MAAM;cAC7C,MAAM;uCACmB,MAAM,EAAE,EACzC,UAAU,CACX,CAAC;QACF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;YAC1B,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC;YAClC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;SAC9B,CAAC,CAAC,CAAC;IACN,CAAC;CACF;AAED;;;;;;;GAOG;AACH,SAAS,OAAO,CAAC,KAAa;IAC5B,OAAO,KAAK;SACT,KAAK,CAAC,KAAK,CAAC;SACZ,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;SACnD,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SAC5C,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { PassageStore } from '../passage-store.ts';
|
|
2
|
+
import type { Passage } from '../passage.ts';
|
|
3
|
+
import type { SqlServerSession } from './sqlserver-session.ts';
|
|
4
|
+
/** The connection and the relations of a SqlServerPassageStore, which the SQL Server retrievers read through it. */
|
|
5
|
+
export type SqlServerContract = {
|
|
6
|
+
readonly session: SqlServerSession;
|
|
7
|
+
/** The passages, qualified by the schema of the store when it has one. */
|
|
8
|
+
readonly passages: string;
|
|
9
|
+
/** Another relation of the contract, such as the passage vectors or the table of the full-text index, qualified as the passages are. */
|
|
10
|
+
relation(name: string): string;
|
|
11
|
+
};
|
|
12
|
+
/** The connection and the relations of a store, so a retriever states neither again. Not exported from the package. */
|
|
13
|
+
export declare function contractOf(store: SqlServerPassageStore): SqlServerContract;
|
|
14
|
+
/**
|
|
15
|
+
* The passage contract in SQL Server: the connection, the schema and the
|
|
16
|
+
* passages, which the SQL Server retrievers and ContextWindow compose with.
|
|
17
|
+
* The schema qualifies every relation of the contract: the passages, the
|
|
18
|
+
* passage vectors and the table of the full-text index. It reads passages by
|
|
19
|
+
* id, and the neighbours of a passage in its source. Ids and sources compare
|
|
20
|
+
* in binary (`Latin1_General_100_BIN2`), so `A` is not `a` under a
|
|
21
|
+
* case-insensitive database.
|
|
22
|
+
*/
|
|
23
|
+
export declare class SqlServerPassageStore implements PassageStore {
|
|
24
|
+
#private;
|
|
25
|
+
constructor(options: {
|
|
26
|
+
readonly session: SqlServerSession;
|
|
27
|
+
/** The schema of the contract; the default schema of the user finds each relation when absent. */
|
|
28
|
+
readonly schema?: string;
|
|
29
|
+
readonly passages: string;
|
|
30
|
+
});
|
|
31
|
+
get(ids: readonly string[]): Promise<ReadonlyMap<string, Passage>>;
|
|
32
|
+
/**
|
|
33
|
+
* The passages of the source from `seq - windowSize` to `seq + windowSize`,
|
|
34
|
+
* the passage itself among them, in the order of `seq`, as Haystack's
|
|
35
|
+
* `SentenceWindowRetriever` reads its window.
|
|
36
|
+
*/
|
|
37
|
+
neighbours(options: {
|
|
38
|
+
readonly source: string;
|
|
39
|
+
readonly seq: number;
|
|
40
|
+
readonly windowSize: number;
|
|
41
|
+
}): Promise<Passage[]>;
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=sqlserver-passage-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-passage-store.d.ts","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-passage-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAK7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE/D,oHAAoH;AACpH,MAAM,MAAM,iBAAiB,GAAG;IAC9B,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC;IACnC,0EAA0E;IAC1E,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,wIAAwI;IACxI,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CAChC,CAAC;AAYF,uHAAuH;AACvH,wBAAgB,UAAU,CAAC,KAAK,EAAE,qBAAqB,GAAG,iBAAiB,CAE1E;AAED;;;;;;;;GAQG;AACH,qBAAa,qBAAsB,YAAW,YAAY;;IAaxD,YAAY,OAAO,EAAE;QACnB,QAAQ,CAAC,OAAO,EAAE,gBAAgB,CAAC;QACnC,kGAAkG;QAClG,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;KAC3B,EAIA;IAQK,GAAG,CAAC,GAAG,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAavE;IAED;;;;OAIG;IACG,UAAU,CAAC,OAAO,EAAE;QACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;KAC7B,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC,CAUrB;CACF"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { binary } from './binary.js';
|
|
2
|
+
import { bind } from './bind.js';
|
|
3
|
+
import { identifier } from './identifier.js';
|
|
4
|
+
import { passage, passageJson } from './passage-row.js';
|
|
5
|
+
const bridge = {
|
|
6
|
+
contractOf: () => {
|
|
7
|
+
throw new Error('retrieval: the SqlServerPassageStore class has not loaded');
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
/** The connection and the relations of a store, so a retriever states neither again. Not exported from the package. */
|
|
11
|
+
export function contractOf(store) {
|
|
12
|
+
return bridge.contractOf(store);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The passage contract in SQL Server: the connection, the schema and the
|
|
16
|
+
* passages, which the SQL Server retrievers and ContextWindow compose with.
|
|
17
|
+
* The schema qualifies every relation of the contract: the passages, the
|
|
18
|
+
* passage vectors and the table of the full-text index. It reads passages by
|
|
19
|
+
* id, and the neighbours of a passage in its source. Ids and sources compare
|
|
20
|
+
* in binary (`Latin1_General_100_BIN2`), so `A` is not `a` under a
|
|
21
|
+
* case-insensitive database.
|
|
22
|
+
*/
|
|
23
|
+
export class SqlServerPassageStore {
|
|
24
|
+
#session;
|
|
25
|
+
#schema;
|
|
26
|
+
#passages;
|
|
27
|
+
static {
|
|
28
|
+
bridge.contractOf = (store) => ({
|
|
29
|
+
session: store.#session,
|
|
30
|
+
passages: store.#passages,
|
|
31
|
+
relation: (name) => store.#relation(name),
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
constructor(options) {
|
|
35
|
+
this.#session = options.session;
|
|
36
|
+
this.#schema = options.schema;
|
|
37
|
+
this.#passages = this.#relation(options.passages);
|
|
38
|
+
}
|
|
39
|
+
#relation(name) {
|
|
40
|
+
return this.#schema === undefined
|
|
41
|
+
? identifier(name)
|
|
42
|
+
: `${identifier(this.#schema)}.${identifier(name)}`;
|
|
43
|
+
}
|
|
44
|
+
async get(ids) {
|
|
45
|
+
const parameters = {};
|
|
46
|
+
const list = bind(parameters, JSON.stringify(ids));
|
|
47
|
+
const rows = await this.#session.query(`SELECT ${passageJson} AS [passage] FROM ${this.#passages} p WHERE p.[id] ${binary} IN (SELECT j.[value] FROM OPENJSON(${list}) j)`, parameters);
|
|
48
|
+
return new Map(rows.map((cells) => {
|
|
49
|
+
const found = passage(cells['passage']);
|
|
50
|
+
return [found.id, found];
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* The passages of the source from `seq - windowSize` to `seq + windowSize`,
|
|
55
|
+
* the passage itself among them, in the order of `seq`, as Haystack's
|
|
56
|
+
* `SentenceWindowRetriever` reads its window.
|
|
57
|
+
*/
|
|
58
|
+
async neighbours(options) {
|
|
59
|
+
const parameters = {};
|
|
60
|
+
const source = bind(parameters, options.source);
|
|
61
|
+
const from = bind(parameters, options.seq - options.windowSize);
|
|
62
|
+
const to = bind(parameters, options.seq + options.windowSize);
|
|
63
|
+
const rows = await this.#session.query(`SELECT ${passageJson} AS [passage] FROM ${this.#passages} p WHERE p.[source] = ${source} ${binary} AND p.[seq] BETWEEN ${from} AND ${to} ORDER BY p.[seq], p.[id] ${binary}`, parameters);
|
|
64
|
+
return rows.map((cells) => passage(cells['passage']));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=sqlserver-passage-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-passage-store.js","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-passage-store.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAYxD,MAAM,MAAM,GAER;IACF,UAAU,EAAE,GAAG,EAAE;QACf,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,uHAAuH;AACvH,MAAM,UAAU,UAAU,CAAC,KAA4B;IACrD,OAAO,MAAM,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;AAClC,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,qBAAqB;IACvB,QAAQ,CAAmB;IAC3B,OAAO,CAAqB;IAC5B,SAAS,CAAS;IAE3B;QACE,MAAM,CAAC,UAAU,GAAG,CAAC,KAAK,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AAC;IACJ,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,UAAU,CAAC,OAIhB;QACC,MAAM,UAAU,GAA4B,EAAE,CAAC;QAC/C,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QAChD,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;QAChE,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,EAAE,OAAO,CAAC,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;QAC9D,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,KAAK,CACpC,UAAU,WAAW,sBAAsB,IAAI,CAAC,SAAS,yBAAyB,MAAM,IAAI,MAAM,wBAAwB,IAAI,QAAQ,EAAE,6BAA6B,MAAM,EAAE,EAC7K,UAAU,CACX,CAAC;QACF,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACxD,CAAC;CACF"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The port through which the SQL Server retrievers send their SQL: the host
|
|
3
|
+
* writes it over its own driver, as for the SQL Server store of
|
|
4
|
+
* `@zukhruf/embedding-cache`. The parameters are named, `@p0` for the key
|
|
5
|
+
* `p0`, and the promise gives the rows of the result. It rejects with the
|
|
6
|
+
* error of the driver as it is.
|
|
7
|
+
*/
|
|
8
|
+
export type SqlServerSession = {
|
|
9
|
+
query(sql: string, parameters: Readonly<Record<string, unknown>>): Promise<readonly Record<string, unknown>[]>;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=sqlserver-session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-session.d.ts","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-session.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,CACH,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,GAC5C,OAAO,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;CAChD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver-session.js","sourceRoot":"","sources":["../../src/sqlserver/sqlserver-session.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Filter } from '../filter.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The filter as a T-SQL condition on the passages, aliased `p`, with each
|
|
4
|
+
* value a named parameter (ADR 0009). `==` and `!=` compare NULL as a value
|
|
5
|
+
* and `not in` keeps a NULL, as Haystack's pgvector store writes them
|
|
6
|
+
* (`IS NOT DISTINCT FROM`, `IS DISTINCT FROM`, `IS NULL OR NOT IN`). T-SQL has
|
|
7
|
+
* no `TRUE` or `FALSE` condition, so all is `1 = 1` and none is `1 = 0`. A
|
|
8
|
+
* string compares in the collation of its column.
|
|
9
|
+
*/
|
|
10
|
+
export declare function where(filter: Filter | undefined, parameters: Record<string, unknown>): string;
|
|
11
|
+
//# sourceMappingURL=where.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"where.d.ts","sourceRoot":"","sources":["../../src/sqlserver/where.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAO3C;;;;;;;GAOG;AACH,wBAAgB,KAAK,CACnB,MAAM,EAAE,MAAM,GAAG,SAAS,EAC1B,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAClC,MAAM,CAER"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { bind } from './bind.js';
|
|
2
|
+
import { identifier } from './identifier.js';
|
|
3
|
+
const passageFields = new Set(['source', 'seq', 'start', 'end']);
|
|
4
|
+
const metadata = 'metadata.';
|
|
5
|
+
/**
|
|
6
|
+
* The filter as a T-SQL condition on the passages, aliased `p`, with each
|
|
7
|
+
* value a named parameter (ADR 0009). `==` and `!=` compare NULL as a value
|
|
8
|
+
* and `not in` keeps a NULL, as Haystack's pgvector store writes them
|
|
9
|
+
* (`IS NOT DISTINCT FROM`, `IS DISTINCT FROM`, `IS NULL OR NOT IN`). T-SQL has
|
|
10
|
+
* no `TRUE` or `FALSE` condition, so all is `1 = 1` and none is `1 = 0`. A
|
|
11
|
+
* string compares in the collation of its column.
|
|
12
|
+
*/
|
|
13
|
+
export function where(filter, parameters) {
|
|
14
|
+
return filter === undefined ? '1 = 1' : condition(filter, parameters);
|
|
15
|
+
}
|
|
16
|
+
function condition(filter, parameters) {
|
|
17
|
+
if ('conditions' in filter) {
|
|
18
|
+
const parts = filter.conditions.map((part) => condition(part, parameters));
|
|
19
|
+
if (filter.operator === 'OR')
|
|
20
|
+
return parts.length === 0 ? '1 = 0' : `(${parts.join(' OR ')})`;
|
|
21
|
+
const all = parts.length === 0 ? '1 = 1' : `(${parts.join(' AND ')})`;
|
|
22
|
+
return filter.operator === 'NOT' ? `(NOT ${all})` : all;
|
|
23
|
+
}
|
|
24
|
+
const field = column(filter.field);
|
|
25
|
+
const { operator, value } = filter;
|
|
26
|
+
if (operator === 'in' || operator === 'not in') {
|
|
27
|
+
if (!Array.isArray(value)) {
|
|
28
|
+
throw new TypeError(`The value of an ${JSON.stringify(operator)} filter on ${filter.field} is an array, not ${JSON.stringify(value)}`);
|
|
29
|
+
}
|
|
30
|
+
// T-SQL refuses an empty list, `IN ()`; no value is in it.
|
|
31
|
+
if (value.length === 0)
|
|
32
|
+
return operator === 'in' ? '1 = 0' : '1 = 1';
|
|
33
|
+
const list = `(${value.map((item) => bind(parameters, parameter(item))).join(', ')})`;
|
|
34
|
+
return operator === 'in'
|
|
35
|
+
? `${field} IN ${list}`
|
|
36
|
+
: `(${field} IS NULL OR ${field} NOT IN ${list})`;
|
|
37
|
+
}
|
|
38
|
+
const placeholder = bind(parameters, parameter(value));
|
|
39
|
+
if (operator === '==')
|
|
40
|
+
return `${field} IS NOT DISTINCT FROM ${placeholder}`;
|
|
41
|
+
if (operator === '!=')
|
|
42
|
+
return `${field} IS DISTINCT FROM ${placeholder}`;
|
|
43
|
+
return `${field} ${operator} ${placeholder}`;
|
|
44
|
+
}
|
|
45
|
+
function column(field) {
|
|
46
|
+
if (passageFields.has(field))
|
|
47
|
+
return `p.${identifier(field)}`;
|
|
48
|
+
if (field.startsWith(metadata))
|
|
49
|
+
return `p.${identifier(field.slice(metadata.length))}`;
|
|
50
|
+
throw new TypeError(`The filter field ${JSON.stringify(field)} is not source, seq, start, end or metadata.<column>`);
|
|
51
|
+
}
|
|
52
|
+
// A boolean stays a boolean: the driver binds it as a BIT, which compares
|
|
53
|
+
// with a BIT column.
|
|
54
|
+
function parameter(value) {
|
|
55
|
+
if (value === null ||
|
|
56
|
+
typeof value === 'string' ||
|
|
57
|
+
typeof value === 'number' ||
|
|
58
|
+
typeof value === 'bigint' ||
|
|
59
|
+
typeof value === 'boolean') {
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
throw new TypeError(`A filter value is a string, a number, a bigint, a boolean or null, not ${String(value)}`);
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=where.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"where.js","sourceRoot":"","sources":["../../src/sqlserver/where.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE7C,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,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,MAAM,IAAI,SAAS,CACjB,oBAAoB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,sDAAsD,CAChG,CAAC;AACJ,CAAC;AAED,0EAA0E;AAC1E,qBAAqB;AACrB,SAAS,SAAS,CAAC,KAAc;IAC/B,IACE,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,SAAS,EAC1B,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,MAAM,IAAI,SAAS,CACjB,0EAA0E,MAAM,CAAC,KAAK,CAAC,EAAE,CAC1F,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { SqlServerEmbeddingRetriever } from './sqlserver/sqlserver-embedding-retriever.ts';
|
|
2
|
+
export { SqlServerKeywordRetriever } from './sqlserver/sqlserver-keyword-retriever.ts';
|
|
3
|
+
export { SqlServerPassageStore } from './sqlserver/sqlserver-passage-store.ts';
|
|
4
|
+
export type { SqlServerSession } from './sqlserver/sqlserver-session.ts';
|
|
5
|
+
//# sourceMappingURL=sqlserver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver.d.ts","sourceRoot":"","sources":["../src/sqlserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,8CAA8C,CAAC;AAC3F,OAAO,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAC;AACvF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC;AAC/E,YAAY,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { SqlServerEmbeddingRetriever } from './sqlserver/sqlserver-embedding-retriever.js';
|
|
2
|
+
export { SqlServerKeywordRetriever } from './sqlserver/sqlserver-keyword-retriever.js';
|
|
3
|
+
export { SqlServerPassageStore } from './sqlserver/sqlserver-passage-store.js';
|
|
4
|
+
//# sourceMappingURL=sqlserver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver.js","sourceRoot":"","sources":["../src/sqlserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,8CAA8C,CAAC;AAC3F,OAAO,EAAE,yBAAyB,EAAE,MAAM,4CAA4C,CAAC;AACvF,OAAO,EAAE,qBAAqB,EAAE,MAAM,wCAAwC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/retrieval",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.94",
|
|
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": {
|
|
@@ -45,6 +45,12 @@
|
|
|
45
45
|
"types": "./dist/postgres.d.ts",
|
|
46
46
|
"import": "./dist/postgres.js",
|
|
47
47
|
"default": "./dist/postgres.js"
|
|
48
|
+
},
|
|
49
|
+
"./sqlserver": {
|
|
50
|
+
"@zukhruf/source": "./src/sqlserver.ts",
|
|
51
|
+
"types": "./dist/sqlserver.d.ts",
|
|
52
|
+
"import": "./dist/sqlserver.js",
|
|
53
|
+
"default": "./dist/sqlserver.js"
|
|
48
54
|
}
|
|
49
55
|
},
|
|
50
56
|
"files": [
|
|
@@ -57,15 +63,17 @@
|
|
|
57
63
|
"access": "public"
|
|
58
64
|
},
|
|
59
65
|
"dependencies": {
|
|
60
|
-
"@zukhruf/statistics": "^0.8.
|
|
61
|
-
"@zukhruf/vectors": "^0.8.
|
|
66
|
+
"@zukhruf/statistics": "^0.8.94",
|
|
67
|
+
"@zukhruf/vectors": "^0.8.94",
|
|
62
68
|
"ai": "^7.0.179",
|
|
63
69
|
"pgvector": "^0.3.1"
|
|
64
70
|
},
|
|
65
71
|
"devDependencies": {
|
|
72
|
+
"@types/mssql": "^12.3.0",
|
|
66
73
|
"@types/pg": "^8.23.1",
|
|
67
|
-
"@zukhruf/sqlite-open": "^0.8.
|
|
68
|
-
"@zukhruf/testing": "^0.8.
|
|
74
|
+
"@zukhruf/sqlite-open": "^0.8.94",
|
|
75
|
+
"@zukhruf/testing": "^0.8.94",
|
|
76
|
+
"mssql": "^12.8.0",
|
|
69
77
|
"pg": "^8.23.1"
|
|
70
78
|
}
|
|
71
79
|
}
|