@eltjs/vectorizer 0.0.0-stage → 0.13.8
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/LICENSE +202 -0
- package/README.md +823 -2
- package/dist/copies.d.ts +2 -0
- package/dist/copies.js +8 -0
- package/dist/declared-columns.d.ts +18 -0
- package/dist/declared-columns.js +23 -0
- package/dist/options.d.ts +28 -0
- package/dist/options.js +20 -0
- package/dist/passage-rules.d.ts +90 -0
- package/dist/passage-rules.js +156 -0
- package/dist/postgresql/keyword-index.d.ts +18 -0
- package/dist/postgresql/keyword-index.js +106 -0
- package/dist/postgresql/vectorizer.d.ts +45 -0
- package/dist/postgresql/vectorizer.js +260 -0
- package/dist/postgresql.d.ts +2 -0
- package/dist/postgresql.js +1 -0
- package/dist/sqlite/keyword-index.d.ts +18 -0
- package/dist/sqlite/keyword-index.js +98 -0
- package/dist/sqlite/vectorizer.d.ts +35 -0
- package/dist/sqlite/vectorizer.js +236 -0
- package/dist/sqlite.d.ts +2 -0
- package/dist/sqlite.js +1 -0
- package/dist/sqlserver/keyword-index.d.ts +19 -0
- package/dist/sqlserver/keyword-index.js +169 -0
- package/dist/sqlserver/vectorizer.d.ts +44 -0
- package/dist/sqlserver/vectorizer.js +253 -0
- package/dist/sqlserver.d.ts +2 -0
- package/dist/sqlserver.js +1 -0
- package/package.json +74 -4
package/README.md
CHANGED
|
@@ -1,3 +1,824 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Vectorizer
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`Vectorizer` is one declaration that keeps the passage contract of [`@zukhruf/retrieval`](https://github.com/JanuaryLabs/zukhruf/tree/main/packages/retrieval) in step with what an elt pipeline loads: the passages of each source row with their metadata, the vector of each passage with the identity of its model, and a keyword index. The retrievers of `@zukhruf/retrieval` then search the rows by meaning and by words. pgai's `create_vectorizer` does the same job in Postgres. The words of this package are in its [glossary](./CONTEXT.md).
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @eltjs/vectorizer @eltjs/elt @eltjs/elt-sqlite @zukhruf/retrieval
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
`@eltjs/vectorizer/sqlite` writes the contract into a SQLite file, `@eltjs/vectorizer/postgresql` into a Postgres database ([Search in Postgres](#search-in-postgres)), and `@eltjs/vectorizer/sqlserver` into a SQL Server database ([Search in SQL Server](#search-in-sql-server)).
|
|
10
|
+
|
|
11
|
+
## Search the rows of a table
|
|
12
|
+
|
|
13
|
+
The program loads three tasks that it holds, through an `IterableSource`, and declares a `Vectorizer` over their table. Then it searches the passages with the retrievers of `@zukhruf/retrieval`, widens the best passage with `ContextWindow`, and reads the status view.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { join } from 'node:path';
|
|
17
|
+
|
|
18
|
+
import type {
|
|
19
|
+
EmbeddingModelV4,
|
|
20
|
+
EmbeddingModelV4CallOptions,
|
|
21
|
+
EmbeddingModelV4Result,
|
|
22
|
+
} from '@ai-sdk/provider';
|
|
23
|
+
import { TemporaryFiles } from '@zukhruf/fs';
|
|
24
|
+
import { ContextWindow, FusionRetriever } from '@zukhruf/retrieval';
|
|
25
|
+
import {
|
|
26
|
+
SqliteBM25Retriever,
|
|
27
|
+
SqliteEmbeddingRetriever,
|
|
28
|
+
SqlitePassageStore,
|
|
29
|
+
} from '@zukhruf/retrieval/sqlite';
|
|
30
|
+
import { open } from '@zukhruf/sqlite-open';
|
|
31
|
+
import { install } from '@zukhruf/vectors/sqlite';
|
|
32
|
+
import { countTokens } from 'gpt-tokenizer/encoding/cl100k_base';
|
|
33
|
+
|
|
34
|
+
import { Connection, Copy, IterableSource, Pipeline } from '@eltjs/elt';
|
|
35
|
+
import { SQLiteDestination, SQLiteSyncHistory } from '@eltjs/elt-sqlite';
|
|
36
|
+
import { Vectorizer } from '@eltjs/vectorizer/sqlite';
|
|
37
|
+
|
|
38
|
+
// A local embedding model with no network: the vector of a text counts each
|
|
39
|
+
// letter a to z. Give your own AI SDK embedding model in its place.
|
|
40
|
+
class LetterModel implements EmbeddingModelV4 {
|
|
41
|
+
readonly specificationVersion = 'v4';
|
|
42
|
+
readonly provider = 'readme';
|
|
43
|
+
readonly modelId = 'letters';
|
|
44
|
+
readonly maxEmbeddingsPerCall = undefined;
|
|
45
|
+
readonly supportsParallelCalls = true;
|
|
46
|
+
|
|
47
|
+
async doEmbed({
|
|
48
|
+
values,
|
|
49
|
+
}: EmbeddingModelV4CallOptions): Promise<EmbeddingModelV4Result> {
|
|
50
|
+
return {
|
|
51
|
+
embeddings: values.map((text) =>
|
|
52
|
+
Array.from(
|
|
53
|
+
{ length: 26 },
|
|
54
|
+
(_, letter) =>
|
|
55
|
+
[...text.toLowerCase()].filter(
|
|
56
|
+
(character) => character.charCodeAt(0) - 97 === letter,
|
|
57
|
+
).length,
|
|
58
|
+
),
|
|
59
|
+
),
|
|
60
|
+
warnings: [],
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const tasks = [
|
|
66
|
+
{
|
|
67
|
+
name: 'fix-git',
|
|
68
|
+
title: 'Fix a broken git history',
|
|
69
|
+
body: 'A rebase went wrong and two commits are lost. Find them in the reflog and put them back on main.',
|
|
70
|
+
category: 'software-engineering',
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: 'train-fasttext',
|
|
74
|
+
title: 'Train a fastText model',
|
|
75
|
+
body: 'Train a text classifier on the reviews in the data folder, and save a model of less than 150 MB.',
|
|
76
|
+
category: 'model-training',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: 'git-multibranch',
|
|
80
|
+
title: 'Deploy two git branches',
|
|
81
|
+
body: 'Set up a git server where a push to main or dev deploys that branch to its own web folder.',
|
|
82
|
+
category: 'system-administration',
|
|
83
|
+
},
|
|
84
|
+
];
|
|
85
|
+
|
|
86
|
+
await using directory = await new TemporaryFiles().directory('vectorizer-');
|
|
87
|
+
const model = new LetterModel();
|
|
88
|
+
const source = new IterableSource({
|
|
89
|
+
identity: 'terminal-bench',
|
|
90
|
+
streams: {
|
|
91
|
+
tasks: {
|
|
92
|
+
description: 'The tasks of Terminal-Bench 2.',
|
|
93
|
+
jsonSchema: {
|
|
94
|
+
type: 'object',
|
|
95
|
+
description: 'A task.',
|
|
96
|
+
properties: {
|
|
97
|
+
name: { type: 'string', description: 'The name of the task.' },
|
|
98
|
+
title: { type: 'string', description: 'Its title.' },
|
|
99
|
+
body: { type: 'string', description: 'What it asks.' },
|
|
100
|
+
category: { type: 'string', description: 'Its category.' },
|
|
101
|
+
},
|
|
102
|
+
required: ['name', 'title', 'body', 'category'],
|
|
103
|
+
},
|
|
104
|
+
primaryKey: ['name'],
|
|
105
|
+
snapshot: () => tasks,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
const destination = new SQLiteDestination({
|
|
110
|
+
path: join(directory.path, 'tasks.sqlite'),
|
|
111
|
+
});
|
|
112
|
+
const table = destination.table('tasks');
|
|
113
|
+
const vectorizer = new Vectorizer({
|
|
114
|
+
into: destination,
|
|
115
|
+
from: table,
|
|
116
|
+
name: 'docs',
|
|
117
|
+
description: 'The tasks of Terminal-Bench 2.',
|
|
118
|
+
key: 'name',
|
|
119
|
+
loading: { columns: ['title', 'body'] },
|
|
120
|
+
chunking: {
|
|
121
|
+
tokenizer: { name: 'cl100k_base', count: countTokens },
|
|
122
|
+
maxTokens: 16,
|
|
123
|
+
overlapTokens: 0,
|
|
124
|
+
},
|
|
125
|
+
embedding: { model, dimensions: 26, clock: globalThis },
|
|
126
|
+
metadata: ['category'],
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
await new Pipeline({
|
|
130
|
+
clock: globalThis,
|
|
131
|
+
history: new SQLiteSyncHistory(),
|
|
132
|
+
connections: [
|
|
133
|
+
new Connection({
|
|
134
|
+
name: 'terminal-bench',
|
|
135
|
+
source,
|
|
136
|
+
destination,
|
|
137
|
+
steps: [
|
|
138
|
+
new Copy(source.streams.tasks, table, {
|
|
139
|
+
syncMode: 'full_refresh',
|
|
140
|
+
destinationSyncMode: 'overwrite_dedup',
|
|
141
|
+
}),
|
|
142
|
+
],
|
|
143
|
+
}),
|
|
144
|
+
],
|
|
145
|
+
derived: [vectorizer],
|
|
146
|
+
}).run();
|
|
147
|
+
|
|
148
|
+
using database = open(destination.path, { readOnly: true });
|
|
149
|
+
install(database);
|
|
150
|
+
const store = new SqlitePassageStore({
|
|
151
|
+
database,
|
|
152
|
+
passages: vectorizer.contract.passages,
|
|
153
|
+
});
|
|
154
|
+
const meaning = new SqliteEmbeddingRetriever({
|
|
155
|
+
store,
|
|
156
|
+
vectors: vectorizer.contract.vectors,
|
|
157
|
+
model,
|
|
158
|
+
});
|
|
159
|
+
const words = new SqliteBM25Retriever({
|
|
160
|
+
store,
|
|
161
|
+
index: vectorizer.contract.index,
|
|
162
|
+
});
|
|
163
|
+
const search = new FusionRetriever({ retrievers: [meaning, words] });
|
|
164
|
+
|
|
165
|
+
const found = await search.retrieve('deploy git branches', { topK: 3 });
|
|
166
|
+
for (const { passage, score } of found)
|
|
167
|
+
console.log(passage.id, passage.metadata.category, score.toFixed(4));
|
|
168
|
+
|
|
169
|
+
const [widened] = await new ContextWindow({ store, windowSize: 5 }).postprocess(
|
|
170
|
+
found,
|
|
171
|
+
'deploy git branches',
|
|
172
|
+
);
|
|
173
|
+
console.log(JSON.stringify(widened?.passage.text));
|
|
174
|
+
|
|
175
|
+
console.log({ ...database.prepare('SELECT * FROM "docs_status"').get() });
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The output:
|
|
179
|
+
|
|
180
|
+
```text
|
|
181
|
+
git-multibranch:0 system-administration 0.0328
|
|
182
|
+
git-multibranch:1 system-administration 0.0161
|
|
183
|
+
fix-git:2 software-engineering 0.0159
|
|
184
|
+
"Deploy two git branches\nSet up a git server where a push to main or dev deploys that branch to its own web folder."
|
|
185
|
+
{
|
|
186
|
+
state: 'current',
|
|
187
|
+
as_of: '2026-11-24T17:30:59.962Z',
|
|
188
|
+
pending_sources: 0,
|
|
189
|
+
pending_vectors: 0
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
- `from` is the table of a copy, and `into` is its file. A `View` can be `from` too: it names its own file, so the vectorizer then takes no `into`.
|
|
194
|
+
- `key` is the column, or the columns, that identify a source row.
|
|
195
|
+
- `loading: { columns }` gives the text of these columns, with a line break between them.
|
|
196
|
+
- `chunking` gives the sizes of [`@eltjs/chunking`](../chunking/README.md): the tokenizer of the embedding model, `maxTokens` and `overlapTokens`.
|
|
197
|
+
- `embedding` gives the options of [`@eltjs/embedding`](../embedding/README.md) without `key` and `fields`: the AI SDK embedding model, its `dimensions` and the `clock` of its calls.
|
|
198
|
+
- `metadata` lists the columns of the source row that each passage gives beside its text. With no `metadata`, a passage has none.
|
|
199
|
+
- `vectorizer.contract` gives the names that the store and the retrievers take: `{ passages, vectors, index }`.
|
|
200
|
+
|
|
201
|
+
## Search the text of stored files
|
|
202
|
+
|
|
203
|
+
With `loading: { file }`, the passages cut the text of the file that a column names. The vectorizer reads each file once with a `DocumentParser`, as `DocumentText` of elt does, and keys the passages by the source key, so two rows that share one file each have their passages.
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
|
|
207
|
+
import { join } from 'node:path';
|
|
208
|
+
|
|
209
|
+
import type {
|
|
210
|
+
EmbeddingModelV4,
|
|
211
|
+
EmbeddingModelV4CallOptions,
|
|
212
|
+
EmbeddingModelV4Result,
|
|
213
|
+
} from '@ai-sdk/provider';
|
|
214
|
+
import { TemporaryFiles } from '@zukhruf/fs';
|
|
215
|
+
import {
|
|
216
|
+
SqliteBM25Retriever,
|
|
217
|
+
SqlitePassageStore,
|
|
218
|
+
} from '@zukhruf/retrieval/sqlite';
|
|
219
|
+
import { open } from '@zukhruf/sqlite-open';
|
|
220
|
+
|
|
221
|
+
import {
|
|
222
|
+
Catalog,
|
|
223
|
+
Connection,
|
|
224
|
+
Copy,
|
|
225
|
+
type CopyConfiguration,
|
|
226
|
+
DocumentParser,
|
|
227
|
+
Gauge,
|
|
228
|
+
LocalFiles,
|
|
229
|
+
Pipeline,
|
|
230
|
+
Source,
|
|
231
|
+
Stream,
|
|
232
|
+
} from '@eltjs/elt';
|
|
233
|
+
import { SQLiteDestination, SQLiteSyncHistory } from '@eltjs/elt-sqlite';
|
|
234
|
+
import { Vectorizer } from '@eltjs/vectorizer/sqlite';
|
|
235
|
+
|
|
236
|
+
// A local embedding model with no network, as in the first example.
|
|
237
|
+
class LetterModel implements EmbeddingModelV4 {
|
|
238
|
+
readonly specificationVersion = 'v4';
|
|
239
|
+
readonly provider = 'readme';
|
|
240
|
+
readonly modelId = 'letters';
|
|
241
|
+
readonly maxEmbeddingsPerCall = undefined;
|
|
242
|
+
readonly supportsParallelCalls = true;
|
|
243
|
+
|
|
244
|
+
async doEmbed({
|
|
245
|
+
values,
|
|
246
|
+
}: EmbeddingModelV4CallOptions): Promise<EmbeddingModelV4Result> {
|
|
247
|
+
return {
|
|
248
|
+
embeddings: values.map((text) =>
|
|
249
|
+
Array.from(
|
|
250
|
+
{ length: 26 },
|
|
251
|
+
(_, letter) =>
|
|
252
|
+
[...text.toLowerCase()].filter(
|
|
253
|
+
(character) => character.charCodeAt(0) - 97 === letter,
|
|
254
|
+
).length,
|
|
255
|
+
),
|
|
256
|
+
),
|
|
257
|
+
warnings: [],
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// The notes a user keeps in one folder. Each file is one record.
|
|
263
|
+
class NotesFolder extends Source {
|
|
264
|
+
readonly identity = 'notes-folder';
|
|
265
|
+
readonly notes = new Stream({
|
|
266
|
+
name: 'notes',
|
|
267
|
+
jsonSchema: {
|
|
268
|
+
type: 'object',
|
|
269
|
+
description: 'A file in the folder.',
|
|
270
|
+
properties: { name: { type: 'string', description: 'File name.' } },
|
|
271
|
+
required: ['name'],
|
|
272
|
+
},
|
|
273
|
+
primaryKey: ['name'],
|
|
274
|
+
supportedSyncModes: ['full_refresh'],
|
|
275
|
+
supportsFileTransfer: true,
|
|
276
|
+
});
|
|
277
|
+
protected readonly catalog = new Catalog([this.notes]);
|
|
278
|
+
readonly #folder: string;
|
|
279
|
+
|
|
280
|
+
constructor(folder: string) {
|
|
281
|
+
super();
|
|
282
|
+
this.#folder = folder;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
override coverage() {
|
|
286
|
+
return { description: 'Every file in the folder.', selection: {} };
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
override async currentAsOf() {
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
protected override async open() {
|
|
294
|
+
return new AsyncDisposableStack();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// This program only runs and never watches, so no watch reads this gauge.
|
|
298
|
+
override changeSignals(streams: readonly Stream[]) {
|
|
299
|
+
const gauge = new (class extends Gauge<number> {
|
|
300
|
+
protected async read() {
|
|
301
|
+
return 0;
|
|
302
|
+
}
|
|
303
|
+
})();
|
|
304
|
+
return [{ gauge, streams, interval: 1000, timeout: 1000 }];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
protected override async *extract(configuration: CopyConfiguration) {
|
|
308
|
+
for (const name of await readdir(this.#folder))
|
|
309
|
+
yield {
|
|
310
|
+
stream: configuration.stream.name,
|
|
311
|
+
data: { name },
|
|
312
|
+
file: join(this.#folder, name),
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// The text of a plain text file.
|
|
318
|
+
class TextParser extends DocumentParser {
|
|
319
|
+
constructor() {
|
|
320
|
+
super('text/plain');
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
override async parse(path: string) {
|
|
324
|
+
return readFile(path, 'utf8');
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
await using directory = await new TemporaryFiles().directory('vectorizer-');
|
|
329
|
+
const folder = join(directory.path, 'Notes');
|
|
330
|
+
await mkdir(folder);
|
|
331
|
+
await writeFile(
|
|
332
|
+
join(folder, 'monday.txt'),
|
|
333
|
+
'Call the cooperative about the cold store.',
|
|
334
|
+
);
|
|
335
|
+
await writeFile(join(folder, 'tuesday.txt'), 'Order panels for the barn roof.');
|
|
336
|
+
|
|
337
|
+
const source = new NotesFolder(folder);
|
|
338
|
+
const files = new LocalFiles({ directory: join(directory.path, 'files') });
|
|
339
|
+
const destination = new SQLiteDestination({
|
|
340
|
+
path: join(directory.path, 'notes.sqlite'),
|
|
341
|
+
});
|
|
342
|
+
const notes = destination.table('notes', (c) => [
|
|
343
|
+
c.text('name').primaryKey(),
|
|
344
|
+
c.text('noteRef').from(source.notes.file.store(files)),
|
|
345
|
+
]);
|
|
346
|
+
const vectorizer = new Vectorizer({
|
|
347
|
+
into: destination,
|
|
348
|
+
from: notes,
|
|
349
|
+
name: 'notes_search',
|
|
350
|
+
description: 'The notes in the folder.',
|
|
351
|
+
key: 'name',
|
|
352
|
+
loading: { file: 'noteRef', files, parser: () => new TextParser() },
|
|
353
|
+
chunking: 'none',
|
|
354
|
+
embedding: { model: new LetterModel(), dimensions: 26, clock: globalThis },
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
await new Pipeline({
|
|
358
|
+
clock: globalThis,
|
|
359
|
+
history: new SQLiteSyncHistory(),
|
|
360
|
+
connections: [
|
|
361
|
+
new Connection({
|
|
362
|
+
name: 'notes',
|
|
363
|
+
source,
|
|
364
|
+
destination,
|
|
365
|
+
steps: [new Copy(source.notes, notes)],
|
|
366
|
+
}),
|
|
367
|
+
],
|
|
368
|
+
derived: [vectorizer],
|
|
369
|
+
}).run();
|
|
370
|
+
|
|
371
|
+
using database = open(destination.path, { readOnly: true });
|
|
372
|
+
const store = new SqlitePassageStore({
|
|
373
|
+
database,
|
|
374
|
+
passages: vectorizer.contract.passages,
|
|
375
|
+
});
|
|
376
|
+
const words = new SqliteBM25Retriever({
|
|
377
|
+
store,
|
|
378
|
+
index: vectorizer.contract.index,
|
|
379
|
+
});
|
|
380
|
+
for (const { passage } of await words.retrieve('cold store'))
|
|
381
|
+
console.log(
|
|
382
|
+
passage.id,
|
|
383
|
+
JSON.stringify(passage.text),
|
|
384
|
+
passage.start,
|
|
385
|
+
passage.end,
|
|
386
|
+
);
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
The output:
|
|
390
|
+
|
|
391
|
+
```text
|
|
392
|
+
monday.txt:0 "Call the cooperative about the cold store." 0 42
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## Search in Postgres
|
|
396
|
+
|
|
397
|
+
`@eltjs/vectorizer/postgresql` writes the same contract into the database of a `PostgresDestination` of [elt-postgresql](../destinations/postgresql/README.md), and the Postgres retrievers of `@zukhruf/retrieval` read it. The server needs pgvector, for example the image `pgvector/pgvector:pg18`. The destination creates the extension `vector` when the database has none.
|
|
398
|
+
|
|
399
|
+
```sh
|
|
400
|
+
npm install @eltjs/vectorizer @eltjs/elt @eltjs/elt-postgresql @zukhruf/retrieval postgres
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
The program loads the tasks of "Search the rows of a table" into Postgres, and searches them as that program does. `DATABASE_URL` names the database. It gives the passages and scores of the SQLite program. postgres.js reads a `bigint` as text, so the counts of `docs_status` are text.
|
|
404
|
+
|
|
405
|
+
```ts
|
|
406
|
+
import type {
|
|
407
|
+
EmbeddingModelV4,
|
|
408
|
+
EmbeddingModelV4CallOptions,
|
|
409
|
+
EmbeddingModelV4Result,
|
|
410
|
+
} from '@ai-sdk/provider';
|
|
411
|
+
import { ContextWindow, FusionRetriever } from '@zukhruf/retrieval';
|
|
412
|
+
import {
|
|
413
|
+
PgvectorEmbeddingRetriever,
|
|
414
|
+
PgvectorKeywordRetriever,
|
|
415
|
+
PostgresPassageStore,
|
|
416
|
+
type PostgresSession,
|
|
417
|
+
} from '@zukhruf/retrieval/postgres';
|
|
418
|
+
import { countTokens } from 'gpt-tokenizer/encoding/cl100k_base';
|
|
419
|
+
import postgres from 'postgres';
|
|
420
|
+
|
|
421
|
+
import { Connection, Copy, IterableSource, Pipeline } from '@eltjs/elt';
|
|
422
|
+
import {
|
|
423
|
+
PostgresDestination,
|
|
424
|
+
PostgresSyncHistory,
|
|
425
|
+
} from '@eltjs/elt-postgresql';
|
|
426
|
+
import { Vectorizer } from '@eltjs/vectorizer/postgresql';
|
|
427
|
+
|
|
428
|
+
// A local embedding model with no network, as in the first example.
|
|
429
|
+
class LetterModel implements EmbeddingModelV4 {
|
|
430
|
+
readonly specificationVersion = 'v4';
|
|
431
|
+
readonly provider = 'readme';
|
|
432
|
+
readonly modelId = 'letters';
|
|
433
|
+
readonly maxEmbeddingsPerCall = undefined;
|
|
434
|
+
readonly supportsParallelCalls = true;
|
|
435
|
+
|
|
436
|
+
async doEmbed({
|
|
437
|
+
values,
|
|
438
|
+
}: EmbeddingModelV4CallOptions): Promise<EmbeddingModelV4Result> {
|
|
439
|
+
return {
|
|
440
|
+
embeddings: values.map((text) =>
|
|
441
|
+
Array.from(
|
|
442
|
+
{ length: 26 },
|
|
443
|
+
(_, letter) =>
|
|
444
|
+
[...text.toLowerCase()].filter(
|
|
445
|
+
(character) => character.charCodeAt(0) - 97 === letter,
|
|
446
|
+
).length,
|
|
447
|
+
),
|
|
448
|
+
),
|
|
449
|
+
warnings: [],
|
|
450
|
+
};
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const tasks = [
|
|
455
|
+
{
|
|
456
|
+
name: 'fix-git',
|
|
457
|
+
title: 'Fix a broken git history',
|
|
458
|
+
body: 'A rebase went wrong and two commits are lost. Find them in the reflog and put them back on main.',
|
|
459
|
+
category: 'software-engineering',
|
|
460
|
+
},
|
|
461
|
+
{
|
|
462
|
+
name: 'train-fasttext',
|
|
463
|
+
title: 'Train a fastText model',
|
|
464
|
+
body: 'Train a text classifier on the reviews in the data folder, and save a model of less than 150 MB.',
|
|
465
|
+
category: 'model-training',
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
name: 'git-multibranch',
|
|
469
|
+
title: 'Deploy two git branches',
|
|
470
|
+
body: 'Set up a git server where a push to main or dev deploys that branch to its own web folder.',
|
|
471
|
+
category: 'system-administration',
|
|
472
|
+
},
|
|
473
|
+
];
|
|
474
|
+
|
|
475
|
+
// A database whose server has pgvector, such as the image
|
|
476
|
+
// pgvector/pgvector:pg18.
|
|
477
|
+
const url = process.env['DATABASE_URL'] ?? 'postgres://localhost/tasks';
|
|
478
|
+
const model = new LetterModel();
|
|
479
|
+
const source = new IterableSource({
|
|
480
|
+
identity: 'terminal-bench',
|
|
481
|
+
streams: {
|
|
482
|
+
tasks: {
|
|
483
|
+
description: 'The tasks of Terminal-Bench 2.',
|
|
484
|
+
jsonSchema: {
|
|
485
|
+
type: 'object',
|
|
486
|
+
description: 'A task.',
|
|
487
|
+
properties: {
|
|
488
|
+
name: { type: 'string', description: 'The name of the task.' },
|
|
489
|
+
title: { type: 'string', description: 'Its title.' },
|
|
490
|
+
body: { type: 'string', description: 'What it asks.' },
|
|
491
|
+
category: { type: 'string', description: 'Its category.' },
|
|
492
|
+
},
|
|
493
|
+
required: ['name', 'title', 'body', 'category'],
|
|
494
|
+
},
|
|
495
|
+
primaryKey: ['name'],
|
|
496
|
+
snapshot: () => tasks,
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
});
|
|
500
|
+
const destination = new PostgresDestination({ url });
|
|
501
|
+
const table = destination.table('tasks');
|
|
502
|
+
const vectorizer = new Vectorizer({
|
|
503
|
+
into: destination,
|
|
504
|
+
from: table,
|
|
505
|
+
name: 'docs',
|
|
506
|
+
description: 'The tasks of Terminal-Bench 2.',
|
|
507
|
+
key: 'name',
|
|
508
|
+
loading: { columns: ['title', 'body'] },
|
|
509
|
+
chunking: {
|
|
510
|
+
tokenizer: { name: 'cl100k_base', count: countTokens },
|
|
511
|
+
maxTokens: 16,
|
|
512
|
+
overlapTokens: 0,
|
|
513
|
+
},
|
|
514
|
+
embedding: { model, dimensions: 26, clock: globalThis },
|
|
515
|
+
metadata: ['category'],
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
await new Pipeline({
|
|
519
|
+
clock: globalThis,
|
|
520
|
+
history: new PostgresSyncHistory({ url }),
|
|
521
|
+
connections: [
|
|
522
|
+
new Connection({
|
|
523
|
+
name: 'terminal-bench',
|
|
524
|
+
source,
|
|
525
|
+
destination,
|
|
526
|
+
steps: [
|
|
527
|
+
new Copy(source.streams.tasks, table, {
|
|
528
|
+
syncMode: 'full_refresh',
|
|
529
|
+
destinationSyncMode: 'overwrite_dedup',
|
|
530
|
+
}),
|
|
531
|
+
],
|
|
532
|
+
}),
|
|
533
|
+
],
|
|
534
|
+
derived: [vectorizer],
|
|
535
|
+
}).run();
|
|
536
|
+
|
|
537
|
+
// The retrievers send their SQL through a session that the host writes over
|
|
538
|
+
// its own driver, here postgres.js.
|
|
539
|
+
const sql = postgres(url, { max: 1 });
|
|
540
|
+
try {
|
|
541
|
+
const session: PostgresSession = {
|
|
542
|
+
query: async (text, parameters) => [
|
|
543
|
+
...(await sql.unsafe(
|
|
544
|
+
text,
|
|
545
|
+
parameters.map((value) =>
|
|
546
|
+
typeof value === 'number' ? value : String(value),
|
|
547
|
+
),
|
|
548
|
+
)),
|
|
549
|
+
],
|
|
550
|
+
};
|
|
551
|
+
const store = new PostgresPassageStore({
|
|
552
|
+
session,
|
|
553
|
+
schema: vectorizer.contract.schema,
|
|
554
|
+
passages: vectorizer.contract.passages,
|
|
555
|
+
});
|
|
556
|
+
const meaning = new PgvectorEmbeddingRetriever({
|
|
557
|
+
store,
|
|
558
|
+
vectors: vectorizer.contract.vectors,
|
|
559
|
+
model,
|
|
560
|
+
});
|
|
561
|
+
const words = new PgvectorKeywordRetriever({
|
|
562
|
+
store,
|
|
563
|
+
index: vectorizer.contract.index,
|
|
564
|
+
});
|
|
565
|
+
const search = new FusionRetriever({ retrievers: [meaning, words] });
|
|
566
|
+
|
|
567
|
+
const found = await search.retrieve('deploy git branches', { topK: 3 });
|
|
568
|
+
for (const { passage, score } of found)
|
|
569
|
+
console.log(passage.id, passage.metadata['category'], score.toFixed(4));
|
|
570
|
+
|
|
571
|
+
const [widened] = await new ContextWindow({
|
|
572
|
+
store,
|
|
573
|
+
windowSize: 5,
|
|
574
|
+
}).postprocess(found, 'deploy git branches');
|
|
575
|
+
console.log(JSON.stringify(widened?.passage.text));
|
|
576
|
+
|
|
577
|
+
console.log({ ...(await sql`SELECT * FROM marts.docs_status`)[0] });
|
|
578
|
+
} finally {
|
|
579
|
+
await sql.end();
|
|
580
|
+
}
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
The output:
|
|
584
|
+
|
|
585
|
+
```text
|
|
586
|
+
git-multibranch:0 system-administration 0.0328
|
|
587
|
+
git-multibranch:1 system-administration 0.0161
|
|
588
|
+
fix-git:2 software-engineering 0.0159
|
|
589
|
+
"Deploy two git branches\nSet up a git server where a push to main or dev deploys that branch to its own web folder."
|
|
590
|
+
{
|
|
591
|
+
state: 'current',
|
|
592
|
+
as_of: 2026-11-24T18:47:45.867Z,
|
|
593
|
+
pending_sources: '0',
|
|
594
|
+
pending_vectors: '0'
|
|
595
|
+
}
|
|
596
|
+
```
|
|
597
|
+
|
|
598
|
+
- `vectorizer.contract` is `{ schema: 'marts', passages, vectors, index: { column, configuration } }`, the options of `PostgresPassageStore`, `PgvectorEmbeddingRetriever` and `PgvectorKeywordRetriever`. The retrievers send their SQL through a `PostgresSession`, which the host writes over its own driver.
|
|
599
|
+
- Every relation of the contract is a view in `marts`, so a reader with `SELECT` on the views of `marts` searches. The views read the raw tables in `_warehouse` with the rights of their owner, and the reader may not read `_warehouse` itself.
|
|
600
|
+
- The keyword index is the table `_warehouse.raw_docs_keywords`: `id text COLLATE "C"` (its primary key), `source`, `seq`, `text`, and `keywords`, a `tsvector` that Postgres generates from the text with the text search configuration `keywords.configuration`, `'english'` when absent. A GIN index indexes `keywords`. The passages view gives `keywords`, and the retrievers leave it out of the metadata. A change of the configuration makes the table again at the next refresh. See [ADR 0002](./docs/adr/0002-the-keyword-index-is-a-member-that-keeps-a-keywords-table-and-its-full-text-index-in-step.md).
|
|
601
|
+
- `indexing: { vectors }` declares the index of the vectors: `hnsw({ m?, efConstruction? })` of elt-postgresql, an HNSW index with `vector_cosine_ops`, when absent, or `'none'`, which compares every vector.
|
|
602
|
+
- While another model embeds the vectors again in place, a search with the model of the few vectors not made again yet still gives its `topK`: the retriever sets `hnsw.iterative_scan`, so the scan of the HNSW index goes on until enough vectors of its model pass.
|
|
603
|
+
- The views find each row by an index: HNSW for the vectors, GIN for the keywords, the index on `part_hash` of the parts, and the key of the source row in the type of its key column. See [ADR 0001](./docs/adr/0001-the-vectorizer-writes-the-passage-contract-of-zukhruf-retrieval-one-subpath-per-engine.md).
|
|
604
|
+
- postgres.js reads a `bigint` as text, so the source of a key of the columns `site` and an integer `id` is `["s1","7"]`.
|
|
605
|
+
|
|
606
|
+
## Search in SQL Server
|
|
607
|
+
|
|
608
|
+
`@eltjs/vectorizer/sqlserver` writes the same contract into the database of an `SqlServerDestination` of [elt-sqlserver](../destinations/sqlserver/README.md), and the SQL Server retrievers of `@zukhruf/retrieval` read it.
|
|
609
|
+
|
|
610
|
+
```sh
|
|
611
|
+
npm install @eltjs/vectorizer @eltjs/elt @eltjs/elt-sqlserver @zukhruf/retrieval mssql
|
|
612
|
+
```
|
|
613
|
+
|
|
614
|
+
```ts
|
|
615
|
+
import { FusionRetriever } from '@zukhruf/retrieval';
|
|
616
|
+
import {
|
|
617
|
+
SqlServerEmbeddingRetriever,
|
|
618
|
+
SqlServerKeywordRetriever,
|
|
619
|
+
SqlServerPassageStore,
|
|
620
|
+
} from '@zukhruf/retrieval/sqlserver';
|
|
621
|
+
import { countTokens } from 'gpt-tokenizer/encoding/cl100k_base';
|
|
622
|
+
import mssql from 'mssql';
|
|
623
|
+
|
|
624
|
+
import { Connection, Copy, IterableSource, Pipeline } from '@eltjs/elt';
|
|
625
|
+
import {
|
|
626
|
+
SqlServerCheckpointStore,
|
|
627
|
+
SqlServerDestination,
|
|
628
|
+
SqlServerSyncHistory,
|
|
629
|
+
} from '@eltjs/elt-sqlserver';
|
|
630
|
+
import { Vectorizer } from '@eltjs/vectorizer/sqlserver';
|
|
631
|
+
|
|
632
|
+
// The LetterModel class and the tasks array of "Search the rows of a table".
|
|
633
|
+
const model = new LetterModel();
|
|
634
|
+
const login = {
|
|
635
|
+
server: 'localhost',
|
|
636
|
+
port: 1433,
|
|
637
|
+
database: 'tasks',
|
|
638
|
+
user: 'elt_writer',
|
|
639
|
+
password: '…',
|
|
640
|
+
trustServerCertificate: true,
|
|
641
|
+
};
|
|
642
|
+
const source = new IterableSource({
|
|
643
|
+
identity: 'terminal-bench',
|
|
644
|
+
streams: {
|
|
645
|
+
tasks: {
|
|
646
|
+
description: 'The tasks of Terminal-Bench 2.',
|
|
647
|
+
jsonSchema: {
|
|
648
|
+
type: 'object',
|
|
649
|
+
description: 'A task.',
|
|
650
|
+
properties: {
|
|
651
|
+
name: { type: 'string', description: 'The name of the task.' },
|
|
652
|
+
title: { type: 'string', description: 'Its title.' },
|
|
653
|
+
body: { type: 'string', description: 'What it asks.' },
|
|
654
|
+
category: { type: 'string', description: 'Its category.' },
|
|
655
|
+
},
|
|
656
|
+
required: ['name', 'title', 'body', 'category'],
|
|
657
|
+
},
|
|
658
|
+
primaryKey: ['name'],
|
|
659
|
+
snapshot: () => tasks,
|
|
660
|
+
},
|
|
661
|
+
},
|
|
662
|
+
});
|
|
663
|
+
const destination = new SqlServerDestination({ login });
|
|
664
|
+
const table = destination.table('tasks');
|
|
665
|
+
const vectorizer = new Vectorizer({
|
|
666
|
+
into: destination,
|
|
667
|
+
from: table,
|
|
668
|
+
name: 'docs',
|
|
669
|
+
description: 'The tasks of Terminal-Bench 2.',
|
|
670
|
+
key: 'name',
|
|
671
|
+
loading: { columns: ['title', 'body'] },
|
|
672
|
+
chunking: {
|
|
673
|
+
tokenizer: { name: 'cl100k_base', count: countTokens },
|
|
674
|
+
maxTokens: 16,
|
|
675
|
+
overlapTokens: 0,
|
|
676
|
+
},
|
|
677
|
+
embedding: { model, dimensions: 26, clock: globalThis },
|
|
678
|
+
metadata: ['category'],
|
|
679
|
+
keywords: { language: 1033 },
|
|
680
|
+
indexing: { vectors: 'none' },
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
await new Pipeline({
|
|
684
|
+
clock: globalThis,
|
|
685
|
+
history: new SqlServerSyncHistory({ login }),
|
|
686
|
+
connections: [
|
|
687
|
+
new Connection({
|
|
688
|
+
name: 'terminal-bench',
|
|
689
|
+
source,
|
|
690
|
+
destination,
|
|
691
|
+
checkpoints: new SqlServerCheckpointStore({ login }),
|
|
692
|
+
steps: [
|
|
693
|
+
new Copy(source.streams.tasks, table, {
|
|
694
|
+
syncMode: 'full_refresh',
|
|
695
|
+
destinationSyncMode: 'overwrite_dedup',
|
|
696
|
+
}),
|
|
697
|
+
],
|
|
698
|
+
}),
|
|
699
|
+
],
|
|
700
|
+
derived: [vectorizer],
|
|
701
|
+
}).run();
|
|
702
|
+
|
|
703
|
+
// A reader needs only SELECT on the schema marts.
|
|
704
|
+
const pool = await new mssql.ConnectionPool({
|
|
705
|
+
...login,
|
|
706
|
+
user: 'reader',
|
|
707
|
+
}).connect();
|
|
708
|
+
const session = {
|
|
709
|
+
query: async (sql: string, parameters: Readonly<Record<string, unknown>>) => {
|
|
710
|
+
const request = pool.request();
|
|
711
|
+
for (const [name, value] of Object.entries(parameters))
|
|
712
|
+
request.input(name, value);
|
|
713
|
+
return (await request.query(sql)).recordset ?? [];
|
|
714
|
+
},
|
|
715
|
+
};
|
|
716
|
+
const store = new SqlServerPassageStore({
|
|
717
|
+
session,
|
|
718
|
+
schema: vectorizer.contract.schema,
|
|
719
|
+
passages: vectorizer.contract.passages,
|
|
720
|
+
});
|
|
721
|
+
const search = new FusionRetriever({
|
|
722
|
+
retrievers: [
|
|
723
|
+
new SqlServerEmbeddingRetriever({
|
|
724
|
+
store,
|
|
725
|
+
vectors: vectorizer.contract.vectors,
|
|
726
|
+
model,
|
|
727
|
+
}),
|
|
728
|
+
new SqlServerKeywordRetriever({ store, index: vectorizer.contract.index }),
|
|
729
|
+
],
|
|
730
|
+
});
|
|
731
|
+
for (const { passage, score } of await search.retrieve('deploy git branches'))
|
|
732
|
+
console.log(passage.id, passage.metadata.category, score.toFixed(4));
|
|
733
|
+
await pool.close();
|
|
734
|
+
```
|
|
735
|
+
|
|
736
|
+
This program did not run when this section was written, so no output is pasted here. SQL Server 2025 runs only where Docker runs amd64 natively, and on the Mac of the build Docker emulates amd64, which Microsoft does not support (the tests skip there, #5469). `src/sqlserver.test.ts` makes the same calls with SQL Server 2025 and Full-Text Search, and CI runs it on amd64. This follows elt-sqlserver, whose recipe of a vector index ran on Azure SQL Database because the container could not run it.
|
|
737
|
+
|
|
738
|
+
- `vectorizer.contract` is `{ schema: 'marts', passages, vectors, index: { table } }`, the options of `SqlServerPassageStore`, `SqlServerEmbeddingRetriever` and `SqlServerKeywordRetriever`.
|
|
739
|
+
- Every relation of the contract is in `marts`, so a reader with `GRANT SELECT ON SCHEMA::marts` searches. The views read the raw tables in `_warehouse` through the ownership chain, and the reader may not read `_warehouse` itself.
|
|
740
|
+
- The keyword index is the base table `marts.docs_keywords`: `id NVARCHAR(450) COLLATE Latin1_General_100_BIN2` (its primary key, the full-text key), `source`, `seq` and `text NVARCHAR(MAX)`. A full-text index on `text` has `STOPLIST = OFF`, `CHANGE_TRACKING AUTO` and the language of `keywords.language`, an LCID: 1033 (English) when absent. `CONTAINSTABLE` reads this table, not a view. See [ADR 0002](./docs/adr/0002-the-keyword-index-is-a-member-that-keeps-a-keywords-table-and-its-full-text-index-in-step.md).
|
|
741
|
+
- A run ends only when the full-text index holds its passages: the keyword index is `current` only when SQL Server shows no population that runs and no change that waits. So a search after `run()` finds each passage.
|
|
742
|
+
- Ids and sources compare in binary, so the sources `a` and `A` are two source rows and give the passages `a:0` and `A:0`, also in a database whose collation ignores case.
|
|
743
|
+
- `indexing: { vectors: 'none' }` is the only declaration of the vectors index. A search compares every vector with `VECTOR_DISTANCE`. The DiskANN index of SQL Server 2025 waits for zukhruf #5475.
|
|
744
|
+
- A server with no Full-Text Search fails the keyword index with a `SqlServerSetupError`, of failure type `config`, before it makes the table. On Linux, install the package `mssql-server-fts`. Azure SQL Database has Full-Text Search.
|
|
745
|
+
- A passage id has at most 450 characters, the most that a full-text key of SQL Server holds. A source row whose passage ids would be longer is a rule failure of its own: its row of `docs_chunks` is `failed`, with an error that names the length and the limit, and `derived_status` counts it in `failed`. The other rows go on. Give the vectorizer a shorter source key.
|
|
746
|
+
|
|
747
|
+
## The relations
|
|
748
|
+
|
|
749
|
+
A vectorizer named `docs` refreshes these members, in this order, in the file of its source. Each is a derived dataset of elt-sqlite with its own row of `derived_status`.
|
|
750
|
+
|
|
751
|
+
| Relation | What it holds |
|
|
752
|
+
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
753
|
+
| `docs_text` | Only with `loading: { file }`: the text of each stored file, a computed dataset of `DocumentText` |
|
|
754
|
+
| `docs_documents` | Only with `loading: { file }`: the text of the file of each source row, by the source key |
|
|
755
|
+
| `docs_chunks` | The parts of the text of each source row, a computed dataset; its parts are in `docs_chunks_parts` |
|
|
756
|
+
| `docs_keywords` | The id, source, seq and text of each passage. The FTS5 table `docs_passages_fts` indexes the text |
|
|
757
|
+
| `docs_passages` | The passages: `rowid`, `id`, `source`, `seq`, `start`, `end`, `text` and the metadata columns |
|
|
758
|
+
| `docs_chunk_texts` | The text of each part, by its part hash |
|
|
759
|
+
| `docs_chunk_vectors` | The vector of each part text, a computed dataset of `Embedding` |
|
|
760
|
+
| `docs_passage_vectors` | The vectors: `id`, `vector` and `model`, the identity of the model, for example `openai/text-embedding-3-small (1536 dimensions)` |
|
|
761
|
+
| `docs_status` | One row: `state`, `as_of`, `pending_sources` and `pending_vectors` |
|
|
762
|
+
|
|
763
|
+
- The id of a passage is `<source>:<seq>`.
|
|
764
|
+
- The passages of a source row tile its text: `start` and `end` count code points, and `ContextWindow` merges passages by them with no gap.
|
|
765
|
+
- A part text that many passages share is embedded once.
|
|
766
|
+
- A vector that the model failed is not in `docs_passage_vectors`, so a vector search never gives its passage. A keyword search still gives it.
|
|
767
|
+
- The keywords table, the FTS5 table and its three triggers are one declaration. When one of them differs from the declaration, the next refresh makes all of them again. See [ADR 0002](./docs/adr/0002-the-keyword-index-is-a-member-that-keeps-a-keywords-table-and-its-full-text-index-in-step.md).
|
|
768
|
+
|
|
769
|
+
## One passage for each row
|
|
770
|
+
|
|
771
|
+
`chunking: 'none'` gives one passage for each source row: its whole text, with `seq` 0, `start` 0 and `end` the count of code points of the text. Use it for short rows, for example a title and a summary.
|
|
772
|
+
|
|
773
|
+
## A key of several columns
|
|
774
|
+
|
|
775
|
+
When `key` is a list of two or more columns, the source of a passage is the JSON array of their values, for example `["s1",7]`, and its id is `["s1",7]:0`. A key of one column gives its plain value, for example `7`.
|
|
776
|
+
|
|
777
|
+
## A new model
|
|
778
|
+
|
|
779
|
+
When the model or its `dimensions` change, the next refresh makes each vector again, in place. While it runs, `docs_status` has the state `refreshing` and counts the texts that wait in `pending_vectors`. A retriever with the new model finds only the passages that it already embedded, and a retriever with the old model finds only the others. When the refresh ends, each vector has the new model.
|
|
780
|
+
|
|
781
|
+
## Rules and failures
|
|
782
|
+
|
|
783
|
+
- The constructor refuses, with a `TypeError` that names it, a metadata column that has the name of a column of the passages (`rowid`, `id`, `source`, `seq`, `start`, `end` or `text` on SQLite, and `keywords` in place of `rowid` on Postgres, in any ASCII case), and a metadata column or a key column that is listed twice.
|
|
784
|
+
- Before it reads, the pipeline refuses a vectorizer whose table no copy loads into its file, and a key, metadata or loading column that the stream or the target does not declare. The run rejects with a `TypeError`.
|
|
785
|
+
- The name of the vectorizer and the name of each relation count in the check of distinct names of the pipeline.
|
|
786
|
+
- A vectorizer fails as one dataset of the pipeline ([elt ADR 0034](../elt/docs/adr/0034-a-composite-dataset-refreshes-its-members-in-order-as-one-dataset.md)): the first member that fails stops the members after it, the copies and the other datasets go on, and the refresh after the next pass tries it again. A run rejects with the `DerivedDatasetError` of the vectorizer, whose `cause` is the `DerivedDatasetError` of the member. `docs_status` then has the state `failed`.
|
|
787
|
+
|
|
788
|
+
## The same in other systems
|
|
789
|
+
|
|
790
|
+
| System | What it is |
|
|
791
|
+
| ------------------------------ | -------------------------------------------------------------------------------- |
|
|
792
|
+
| pgai `create_vectorizer` | One call that keeps the embeddings of a Postgres table in step with its rows |
|
|
793
|
+
| Airbyte vector destinations | `text_fields` and `metadata_fields` of each record, chunked and embedded on load |
|
|
794
|
+
| LlamaIndex `IngestionPipeline` | The steps that make nodes and their embeddings from documents |
|
|
795
|
+
|
|
796
|
+
## Decisions
|
|
797
|
+
|
|
798
|
+
- [ADR 0001](./docs/adr/0001-the-vectorizer-writes-the-passage-contract-of-zukhruf-retrieval-one-subpath-per-engine.md): the vectorizer writes the passage contract of `@zukhruf/retrieval`, one subpath per engine.
|
|
799
|
+
- [ADR 0002](./docs/adr/0002-the-keyword-index-is-a-member-that-keeps-a-keywords-table-and-its-full-text-index-in-step.md): the keyword index is a member that keeps a keywords table and its full-text index in step.
|
|
800
|
+
|
|
801
|
+
## Development
|
|
802
|
+
|
|
803
|
+
```sh
|
|
804
|
+
npx nx run vectorizer:test # builds, then runs the tests of each subpath; SQL Server 2025 runs only where Docker runs amd64 natively
|
|
805
|
+
npx nx run vectorizer:typecheck # formats, lints, then type checks
|
|
806
|
+
```
|
|
807
|
+
|
|
808
|
+
```
|
|
809
|
+
src/
|
|
810
|
+
sqlite.ts the subpath /sqlite
|
|
811
|
+
options.ts SourceKey, Loading, ChunkingOptions, EmbeddingOptions and the checks of the columns
|
|
812
|
+
passage-rules.ts the rules of the passages: the chunks of Chunking by source, or one part for each row
|
|
813
|
+
sqlite/vectorizer.ts Vectorizer: the members and the views of SQLite
|
|
814
|
+
sqlite/keyword-index.ts KeywordIndex: the keywords table, its FTS5 table and its triggers
|
|
815
|
+
postgresql.ts the subpath /postgresql
|
|
816
|
+
postgresql/vectorizer.ts Vectorizer: the members and the views of Postgres
|
|
817
|
+
postgresql/keyword-index.ts KeywordIndex: the keywords table with its generated tsvector and GIN index
|
|
818
|
+
sqlserver.ts the subpath /sqlserver
|
|
819
|
+
sqlserver/vectorizer.ts Vectorizer: the members and the views of SQL Server
|
|
820
|
+
sqlserver/keyword-index.ts KeywordIndex: the keywords table in marts and its full-text index
|
|
821
|
+
copies.ts the copies of the pipeline into the destination of a vectorizer
|
|
822
|
+
declared-columns.ts the check that the stream declares each column that the vectorizer reads
|
|
823
|
+
testing/letter-model.ts LetterModel, the embedding model of the tests
|
|
824
|
+
```
|