@zukhruf/vectors 0.0.0-stage → 0.8.33
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 +21 -0
- package/README.md +245 -2
- package/dist/dimension-mismatch-error.d.ts +8 -0
- package/dist/dimension-mismatch-error.d.ts.map +1 -0
- package/dist/dimension-mismatch-error.js +12 -0
- package/dist/dimension-mismatch-error.js.map +1 -0
- package/dist/distance.d.ts +16 -0
- package/dist/distance.d.ts.map +1 -0
- package/dist/distance.js +56 -0
- package/dist/distance.js.map +1 -0
- package/dist/elements.d.ts +3 -0
- package/dist/elements.d.ts.map +1 -0
- package/dist/elements.js +8 -0
- package/dist/elements.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/sqlite-vec-loaded-error.d.ts +6 -0
- package/dist/sqlite-vec-loaded-error.d.ts.map +1 -0
- package/dist/sqlite-vec-loaded-error.js +8 -0
- package/dist/sqlite-vec-loaded-error.js.map +1 -0
- package/dist/sqlite.d.ts +21 -0
- package/dist/sqlite.d.ts.map +1 -0
- package/dist/sqlite.js +129 -0
- package/dist/sqlite.js.map +1 -0
- package/dist/sqlserver.d.ts +17 -0
- package/dist/sqlserver.d.ts.map +1 -0
- package/dist/sqlserver.js +27 -0
- package/dist/sqlserver.js.map +1 -0
- package/dist/vector.d.ts +57 -0
- package/dist/vector.d.ts.map +1 -0
- package/dist/vector.js +135 -0
- package/dist/vector.js.map +1 -0
- package/dist/zero-vector-error.d.ts +9 -0
- package/dist/zero-vector-error.d.ts.map +1 -0
- package/dist/zero-vector-error.js +11 -0
- package/dist/zero-vector-error.js.map +1 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ezzabuzaid
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,246 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zukhruf/vectors
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Ezz accepted each ADR on 2026-11-18. The first release is built and tested, and not yet on npm.
|
|
4
|
+
|
|
5
|
+
A vector is a list of numbers that a model makes from a text, so that a program can find texts by meaning. Each database keeps and compares vectors in its own way. SQLite has no vector type, sqlite-vec reads float32 bytes, and SQL Server has its own `VECTOR` type. This package gives the vector types, their math, and the form of each vector in each database.
|
|
6
|
+
|
|
7
|
+
The package hides no database. It gives values and functions that a program puts into its own SQL, so the program keeps every feature of its database: its indexes, its filters, its joins and its approximate search ([ADR 0002](./docs/adr/0002-the-package-composes-into-the-hosts-sql-and-never-runs-it.md)). The package runs no SQL and opens no connection.
|
|
8
|
+
|
|
9
|
+
The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
|
|
10
|
+
|
|
11
|
+
## Use it
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @zukhruf/vectors
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The package has no dependencies.
|
|
18
|
+
|
|
19
|
+
### Compare vectors in memory
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { UnitVector, Vector, cosineDistance } from '@zukhruf/vectors';
|
|
23
|
+
|
|
24
|
+
const a = Vector.from(embeddingA); // number[] from the AI SDK, or a Float32Array
|
|
25
|
+
const b = Vector.from(embeddingB);
|
|
26
|
+
cosineDistance(a, b); // 1 − the cosine similarity: 0 for one direction, 2 for opposite
|
|
27
|
+
UnitVector.normalize(a); // length 1; a zero vector is a ZeroVectorError
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Search in plain SQLite
|
|
31
|
+
|
|
32
|
+
`install` adds the distance functions of sqlite-vec to a database that does not load the extension. The same SQL then runs with or without sqlite-vec ([ADR 0005](./docs/adr/0005-plain-sqlite-gets-the-distance-functions-of-sqlite-vec.md)).
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
36
|
+
|
|
37
|
+
import { Vector } from '@zukhruf/vectors';
|
|
38
|
+
import { install, toSql } from '@zukhruf/vectors/sqlite';
|
|
39
|
+
|
|
40
|
+
using database = new DatabaseSync('chunks.sqlite');
|
|
41
|
+
install(database);
|
|
42
|
+
|
|
43
|
+
database.exec(
|
|
44
|
+
`CREATE TABLE chunks (id TEXT PRIMARY KEY, source TEXT, embedding BLOB)`,
|
|
45
|
+
);
|
|
46
|
+
database
|
|
47
|
+
.prepare(`INSERT INTO chunks VALUES (?, ?, ?)`)
|
|
48
|
+
.run('guide#0', 'docs', toSql(Vector.from(embedding)));
|
|
49
|
+
|
|
50
|
+
const nearest = database
|
|
51
|
+
.prepare(
|
|
52
|
+
`SELECT id, vec_distance_cosine(embedding, ?) AS distance
|
|
53
|
+
FROM chunks
|
|
54
|
+
WHERE source = ? OR source LIKE 'shared/%'
|
|
55
|
+
ORDER BY distance
|
|
56
|
+
LIMIT 10`,
|
|
57
|
+
)
|
|
58
|
+
.all(toSql(Vector.from(query)), 'docs');
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Search with sqlite-vec
|
|
62
|
+
|
|
63
|
+
The program writes its own `vec0` table, with each feature of sqlite-vec: partition keys, metadata columns, auxiliary columns, int8 and bit vectors. The package gives the bytes.
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
database.loadExtension(sqliteVec.getLoadablePath());
|
|
67
|
+
database.exec(`CREATE VIRTUAL TABLE vec_chunks USING vec0(
|
|
68
|
+
source TEXT PARTITION KEY,
|
|
69
|
+
embedding FLOAT[384] DISTANCE_METRIC=cosine,
|
|
70
|
+
+content TEXT
|
|
71
|
+
)`);
|
|
72
|
+
database
|
|
73
|
+
.prepare(
|
|
74
|
+
`SELECT rowid, distance FROM vec_chunks WHERE embedding MATCH ? AND k = 10 AND source = ?`,
|
|
75
|
+
)
|
|
76
|
+
.all(toSql(Vector.from(query)), 'docs');
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Search in SQL Server
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { Vector } from '@zukhruf/vectors';
|
|
83
|
+
import { sqlType, toSql } from '@zukhruf/vectors/sqlserver';
|
|
84
|
+
|
|
85
|
+
const query = Vector.from(embedding);
|
|
86
|
+
await pool.request().input('q', mssql.NVarChar(mssql.MAX), toSql(query)).query(`
|
|
87
|
+
SELECT TOP (10) id, VECTOR_DISTANCE('cosine', embedding, CAST(@q AS ${sqlType(query)})) AS distance
|
|
88
|
+
FROM chunks
|
|
89
|
+
WHERE source = 'docs' OR JSON_VALUE(metadata, '$.shared') = 'true'
|
|
90
|
+
ORDER BY distance`);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
The approximate search of Azure SQL is the program's own SQL too:
|
|
94
|
+
|
|
95
|
+
```sql
|
|
96
|
+
CREATE VECTOR INDEX ix_chunks ON chunks (embedding) WITH (METRIC = 'cosine', TYPE = 'DiskANN');
|
|
97
|
+
|
|
98
|
+
DECLARE @qv VECTOR(384) = CAST(@q AS VECTOR(384));
|
|
99
|
+
SELECT TOP (10) WITH APPROXIMATE t.id, r.distance
|
|
100
|
+
FROM VECTOR_SEARCH(TABLE = chunks AS t, COLUMN = embedding, SIMILAR_TO = @qv, METRIC = 'cosine') AS r
|
|
101
|
+
WHERE t.source = 'docs'
|
|
102
|
+
ORDER BY r.distance;
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Search in Postgres
|
|
106
|
+
|
|
107
|
+
Postgres has its own client package, [pgvector](https://github.com/pgvector/pgvector-node), whose `toSql` and `fromSql` gave this package its names. Use it for the SQL form, and this package for the vector and its math ([ADR 0001](./docs/adr/0001-the-scope-and-the-name-come-from-the-vector-types-of-pgvector-and-the-math-of-dotnet.md), amendment):
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import pgvector from 'pgvector';
|
|
111
|
+
|
|
112
|
+
import { UnitVector, Vector } from '@zukhruf/vectors';
|
|
113
|
+
|
|
114
|
+
const unit = UnitVector.normalize(Vector.from(embedding));
|
|
115
|
+
await client.query('INSERT INTO items (embedding) VALUES ($1)', [
|
|
116
|
+
pgvector.toSql(Array.from(unit.values)),
|
|
117
|
+
]);
|
|
118
|
+
await client.query('SELECT id FROM items ORDER BY embedding <=> $1 LIMIT 10', [
|
|
119
|
+
pgvector.toSql(Array.from(Vector.from(query).values)),
|
|
120
|
+
]);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## API
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
// @zukhruf/vectors
|
|
127
|
+
class Vector {
|
|
128
|
+
static from(values: ArrayLike<number>): Vector;
|
|
129
|
+
readonly values: Float32Array; // a copy: a change to it does not change the vector
|
|
130
|
+
readonly dimensions: number;
|
|
131
|
+
}
|
|
132
|
+
class HalfVector {
|
|
133
|
+
static from(values: ArrayLike<number>): HalfVector;
|
|
134
|
+
readonly values: Float16Array;
|
|
135
|
+
readonly dimensions: number;
|
|
136
|
+
}
|
|
137
|
+
class Int8Vector {
|
|
138
|
+
static from(values: ArrayLike<number>): Int8Vector;
|
|
139
|
+
readonly values: Int8Array;
|
|
140
|
+
readonly dimensions: number;
|
|
141
|
+
}
|
|
142
|
+
class BitVector {
|
|
143
|
+
static from(bits: ArrayLike<boolean>): BitVector; // a multiple of 8 bits
|
|
144
|
+
readonly bits: boolean[]; // a copy
|
|
145
|
+
readonly dimensions: number;
|
|
146
|
+
}
|
|
147
|
+
class UnitVector extends Vector {
|
|
148
|
+
static normalize(vector: Vector | HalfVector): UnitVector;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function dot(a: Vector, b: Vector): number;
|
|
152
|
+
function norm(a: Vector): number;
|
|
153
|
+
function cosineSimilarity(a: Vector, b: Vector): number;
|
|
154
|
+
function cosineDistance(a: Vector, b: Vector): number; // 1 − cosineSimilarity
|
|
155
|
+
function euclideanDistance(a: Vector, b: Vector): number; // L2
|
|
156
|
+
function hammingDistance(a: BitVector, b: BitVector): number;
|
|
157
|
+
// Each function also takes HalfVector and Int8Vector where the engines do; both sides have one type.
|
|
158
|
+
|
|
159
|
+
// @zukhruf/vectors/sqlite (plain SQLite and sqlite-vec read the same bytes)
|
|
160
|
+
function install(database: DatabaseSync): void; // vec_distance_cosine, vec_distance_L2
|
|
161
|
+
class SqliteVecLoadedError extends Error {}
|
|
162
|
+
function toSql(vector: Vector | Int8Vector | BitVector): Uint8Array;
|
|
163
|
+
function fromSql<
|
|
164
|
+
T extends typeof Vector | typeof Int8Vector | typeof BitVector,
|
|
165
|
+
>(type: T, blob: Uint8Array): InstanceType<T>;
|
|
166
|
+
|
|
167
|
+
// @zukhruf/vectors/sqlserver
|
|
168
|
+
function toSql(vector: Vector | HalfVector): string; // JSON text with 9 or 5 significant digits
|
|
169
|
+
function sqlType(vector: Vector | HalfVector): string; // 'VECTOR(384)' or 'VECTOR(384, float16)'
|
|
170
|
+
function fromSql<T extends typeof Vector | typeof HalfVector>(
|
|
171
|
+
type: T,
|
|
172
|
+
text: string,
|
|
173
|
+
): InstanceType<T>;
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
`toSql` and `fromSql` take their names from [pgvector-node](https://github.com/pgvector/pgvector-node), the Postgres client with about one million downloads a week ([ADR 0006](./docs/adr/0006-each-engine-has-tosql-and-fromsql-after-pgvector.md)).
|
|
177
|
+
|
|
178
|
+
## What each call does underneath
|
|
179
|
+
|
|
180
|
+
| Call | What happens |
|
|
181
|
+
| ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
182
|
+
| `Vector.from(values)` | Copies the values into a new `Float32Array`. A value that is not finite is a `RangeError` |
|
|
183
|
+
| `UnitVector.normalize(v)` | Divides each value by the length, in float64. A length of 0 is a `ZeroVectorError` |
|
|
184
|
+
| `cosineDistance(a, b)` | 1 − dot(a, b) / (norm(a) · norm(b)), in float64. Other dimensions are a `DimensionMismatchError` |
|
|
185
|
+
| `sqlite.toSql(v)` | float32 values, little-endian; int8 values as bytes; bits packed least significant bit first, as sqlite-vec packs them |
|
|
186
|
+
| `sqlite.install(db)` | `db.function('vec_distance_cosine', …)` and `vec_distance_L2`, deterministic, with the results (to float32 precision), the errors and the `NULL` of sqlite-vec ([ported from sqlite-vec](./docs/ported-from-sqlite-vec.md)). A database that already has sqlite-vec is a `SqliteVecLoadedError` |
|
|
187
|
+
| `sqlserver.toSql(v)` | `JSON.stringify` of each value with 9 significant digits (float32) or 5 (float16) ([ADR 0007](./docs/adr/0007-sql-server-gets-json-text-with-the-digits-that-keep-each-value.md)) |
|
|
188
|
+
| `sqlserver.fromSql(type, text)` | `JSON.parse` of the text that SQL Server gives, into the type |
|
|
189
|
+
|
|
190
|
+
## Errors
|
|
191
|
+
|
|
192
|
+
| Error | When | The caller's next step |
|
|
193
|
+
| ---------------------------------- | ---------------------------------------------------------------------- | -------------------------------------------------------- |
|
|
194
|
+
| `DimensionMismatchError` | Two vectors of one call have other dimensions. `expected` and `actual` | Use one model for both vectors |
|
|
195
|
+
| `ZeroVectorError` (a `RangeError`) | A vector with no length is normalized | Skip the vector, or fail its record |
|
|
196
|
+
| `SqliteVecLoadedError` | `install` on a database that loaded sqlite-vec | Use the functions of sqlite-vec, which are already there |
|
|
197
|
+
|
|
198
|
+
## SQL Server: what the probes found
|
|
199
|
+
|
|
200
|
+
On Azure SQL Database (General Purpose, 2026-11-18), with throwaway tables:
|
|
201
|
+
|
|
202
|
+
- `VECTOR(n)` (float32, at most 1,998 dimensions) and `VECTOR(n, float16)` (at most 3,996) work. `VECTOR_DISTANCE('cosine', …)` gives 1 − the cosine similarity.
|
|
203
|
+
- A DiskANN index is version 3. The old `TOP_N =` parameter fails with Msg 42274. An index made on an empty table starts in the state `KNN` (exact), and a background task builds its graph about 16 seconds after the rows come.
|
|
204
|
+
- `INSERT`, `UPDATE`, `DELETE`, `MERGE` and a cascade from a foreign key show in the next search. `TRUNCATE` works.
|
|
205
|
+
- The recall@10 of the approximate search against the exact search was 78/80 to 80/80, also for a source of 3% of the rows.
|
|
206
|
+
- SQL Server 2025 RTM in a container has no `VECTOR_SEARCH` and no DiskANN index, so a test can run only the exact search there.
|
|
207
|
+
|
|
208
|
+
## The same concepts in other systems
|
|
209
|
+
|
|
210
|
+
| This package | pgvector-node | Drizzle ORM | .NET | sqlite-vec |
|
|
211
|
+
| ----------------------------------- | ------------------------------------------------------------ | ------------------------------------------------- | ------------------------------------------------------------------------------- | -------------------------------- |
|
|
212
|
+
| `Vector`, `HalfVector`, `BitVector` | `toSql(array)`, `SparseVector` | `vector({ dimensions })` column | `SqlVector<float>` (EF Core) | `vec_f32`, `vec_int8`, `vec_bit` |
|
|
213
|
+
| `cosineDistance(a, b)` | `cosineDistance(column, vector)` for Knex, Kysely, Sequelize | `cosineDistance(column, vector)` inside `orderBy` | `TensorPrimitives.CosineSimilarity`; `EF.Functions.VectorDistance('cosine', …)` | `vec_distance_cosine(a, b)` |
|
|
214
|
+
| `sqlite.install(db)` | none | none | none | the extension itself |
|
|
215
|
+
| the program's own SQL | the query builder's query | `db.select().orderBy(…)` | LINQ | `vec0` and `MATCH` |
|
|
216
|
+
|
|
217
|
+
## Why not an npm package
|
|
218
|
+
|
|
219
|
+
Checked on 2026-11-18:
|
|
220
|
+
|
|
221
|
+
| Package | Why it does not do the job |
|
|
222
|
+
| --------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------- |
|
|
223
|
+
| `pgvector` 0.3.1 (MIT, no dependencies, 1 M a week) | Postgres only. It is the model for this package's names, and the package for Postgres (Search in Postgres) |
|
|
224
|
+
| `sqlite-vec` 0.1.9 | It loads the extension (`getLoadablePath`, `load`) and gives no bytes and no functions for a database without it |
|
|
225
|
+
| `@mastra/core`, `@langchain/core` | One store interface that hides each engine; no SQL Server vectors |
|
|
226
|
+
| `@memberjunction/ai-vectors-sqlserver` | BUSL-1.1, and it works only inside MemberJunction |
|
|
227
|
+
| `typeorm` | A `vector` column of an ORM |
|
|
228
|
+
|
|
229
|
+
## Who moves onto it
|
|
230
|
+
|
|
231
|
+
The package is designed for its domain. Each program below moves onto it and drops its own copy.
|
|
232
|
+
|
|
233
|
+
| Program | Its copy today | After |
|
|
234
|
+
| ----------------------- | ------------------------------------------------------------------ | ---------------------------------------------- |
|
|
235
|
+
| `@deepagents/retrieval` | `vectorToBlob` in `stores/sqlite/sqlite.ts` | `sqlite.toSql` |
|
|
236
|
+
| `@zukhruf/embeddings` | `UnitVector` and `toBlob` in `src/vector.ts`, cosine in JavaScript | `UnitVector`, `sqlite.toSql`, `cosineDistance` |
|
|
237
|
+
| `@eltjs/embedding` | `unitVector` in `src/embedding.ts` (#4118) | `UnitVector`, `sqlite.toSql` |
|
|
238
|
+
| Context Compiler | through `@zukhruf/embeddings` | the same |
|
|
239
|
+
| datahub | none | `sqlserver.toSql`, `sqlType` |
|
|
240
|
+
|
|
241
|
+
The cache that embeds only changed text is not in this package ([ADR 0008](./docs/adr/0008-the-embedding-cache-decorates-the-model-and-lives-outside-this-package.md)).
|
|
242
|
+
|
|
243
|
+
## Later
|
|
244
|
+
|
|
245
|
+
- Hamming in plain SQLite, which needs the `vec_bit` subtype of sqlite-vec.
|
|
246
|
+
- Native vectors over TDS, when tedious supports them (pull request #1841).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Two vectors of one call have other dimensions, for example the vectors of two models. */
|
|
2
|
+
export declare class DimensionMismatchError extends Error {
|
|
3
|
+
readonly name = "DimensionMismatchError";
|
|
4
|
+
readonly expected: number;
|
|
5
|
+
readonly actual: number;
|
|
6
|
+
constructor(expected: number, actual: number);
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=dimension-mismatch-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dimension-mismatch-error.d.ts","sourceRoot":"","sources":["../src/dimension-mismatch-error.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C,SAAkB,IAAI,4BAA4B;IAClD,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;gBAEZ,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAK7C"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Two vectors of one call have other dimensions, for example the vectors of two models. */
|
|
2
|
+
export class DimensionMismatchError extends Error {
|
|
3
|
+
name = 'DimensionMismatchError';
|
|
4
|
+
expected;
|
|
5
|
+
actual;
|
|
6
|
+
constructor(expected, actual) {
|
|
7
|
+
super(`The vectors have other dimensions: ${expected} and ${actual}`);
|
|
8
|
+
this.expected = expected;
|
|
9
|
+
this.actual = actual;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=dimension-mismatch-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dimension-mismatch-error.js","sourceRoot":"","sources":["../src/dimension-mismatch-error.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,MAAM,OAAO,sBAAuB,SAAQ,KAAK;IAC7B,IAAI,GAAG,wBAAwB,CAAC;IACzC,QAAQ,CAAS;IACjB,MAAM,CAAS;IAExB,YAAY,QAAgB,EAAE,MAAc;QAC1C,KAAK,CAAC,sCAAsC,QAAQ,QAAQ,MAAM,EAAE,CAAC,CAAC;QACtE,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BitVector, HalfVector, Int8Vector, Vector } from './vector.ts';
|
|
2
|
+
type NumericVector = Vector | HalfVector | Int8Vector;
|
|
3
|
+
/** The dot product, in float64. */
|
|
4
|
+
export declare function dot<T extends NumericVector>(a: T, b: T): number;
|
|
5
|
+
/** The length of the vector, in float64. */
|
|
6
|
+
export declare function norm(a: NumericVector): number;
|
|
7
|
+
/** The cosine of the angle between the vectors. `NaN` when one of them has length 0. */
|
|
8
|
+
export declare function cosineSimilarity<T extends NumericVector>(a: T, b: T): number;
|
|
9
|
+
/** 1 − the cosine similarity: 0 for one direction, 1 for orthogonal, 2 for opposite. */
|
|
10
|
+
export declare function cosineDistance<T extends NumericVector>(a: T, b: T): number;
|
|
11
|
+
/** The L2 distance, in float64. */
|
|
12
|
+
export declare function euclideanDistance<T extends NumericVector>(a: T, b: T): number;
|
|
13
|
+
/** The number of bits that differ. */
|
|
14
|
+
export declare function hammingDistance(a: BitVector, b: BitVector): number;
|
|
15
|
+
export {};
|
|
16
|
+
//# sourceMappingURL=distance.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distance.d.ts","sourceRoot":"","sources":["../src/distance.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE7E,KAAK,aAAa,GAAG,MAAM,GAAG,UAAU,GAAG,UAAU,CAAC;AAEtD,mCAAmC;AACnC,wBAAgB,GAAG,CAAC,CAAC,SAAS,aAAa,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAO/D;AAED,4CAA4C;AAC5C,wBAAgB,IAAI,CAAC,CAAC,EAAE,aAAa,GAAG,MAAM,CAI7C;AAED,wFAAwF;AACxF,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,aAAa,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAE5E;AAED,wFAAwF;AACxF,wBAAgB,cAAc,CAAC,CAAC,SAAS,aAAa,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAE1E;AAED,mCAAmC;AACnC,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,aAAa,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,MAAM,CAQ7E;AAED,sCAAsC;AACtC,wBAAgB,eAAe,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAWlE"}
|
package/dist/distance.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { DimensionMismatchError } from "./dimension-mismatch-error.js";
|
|
2
|
+
import { elements } from "./elements.js";
|
|
3
|
+
/** The dot product, in float64. */
|
|
4
|
+
export function dot(a, b) {
|
|
5
|
+
const [x, y] = pair(a, b);
|
|
6
|
+
let sum = 0;
|
|
7
|
+
for (let index = 0; index < x.length; index++) {
|
|
8
|
+
sum += (x[index] ?? 0) * (y[index] ?? 0);
|
|
9
|
+
}
|
|
10
|
+
return sum;
|
|
11
|
+
}
|
|
12
|
+
/** The length of the vector, in float64. */
|
|
13
|
+
export function norm(a) {
|
|
14
|
+
let squares = 0;
|
|
15
|
+
for (const value of a[elements])
|
|
16
|
+
squares += value * value;
|
|
17
|
+
return Math.sqrt(squares);
|
|
18
|
+
}
|
|
19
|
+
/** The cosine of the angle between the vectors. `NaN` when one of them has length 0. */
|
|
20
|
+
export function cosineSimilarity(a, b) {
|
|
21
|
+
return dot(a, b) / (norm(a) * norm(b));
|
|
22
|
+
}
|
|
23
|
+
/** 1 − the cosine similarity: 0 for one direction, 1 for orthogonal, 2 for opposite. */
|
|
24
|
+
export function cosineDistance(a, b) {
|
|
25
|
+
return 1 - cosineSimilarity(a, b);
|
|
26
|
+
}
|
|
27
|
+
/** The L2 distance, in float64. */
|
|
28
|
+
export function euclideanDistance(a, b) {
|
|
29
|
+
const [x, y] = pair(a, b);
|
|
30
|
+
let squares = 0;
|
|
31
|
+
for (let index = 0; index < x.length; index++) {
|
|
32
|
+
const difference = (x[index] ?? 0) - (y[index] ?? 0);
|
|
33
|
+
squares += difference * difference;
|
|
34
|
+
}
|
|
35
|
+
return Math.sqrt(squares);
|
|
36
|
+
}
|
|
37
|
+
/** The number of bits that differ. */
|
|
38
|
+
export function hammingDistance(a, b) {
|
|
39
|
+
const [x, y] = pair(a, b);
|
|
40
|
+
let differ = 0;
|
|
41
|
+
for (let index = 0; index < x.length; index++) {
|
|
42
|
+
let bits = (x[index] ?? 0) ^ (y[index] ?? 0);
|
|
43
|
+
while (bits !== 0) {
|
|
44
|
+
differ += bits & 1;
|
|
45
|
+
bits >>= 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return differ;
|
|
49
|
+
}
|
|
50
|
+
function pair(a, b) {
|
|
51
|
+
if (a.dimensions !== b.dimensions) {
|
|
52
|
+
throw new DimensionMismatchError(a.dimensions, b.dimensions);
|
|
53
|
+
}
|
|
54
|
+
return [a[elements], b[elements]];
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=distance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"distance.js","sourceRoot":"","sources":["../src/distance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAKzC,mCAAmC;AACnC,MAAM,UAAU,GAAG,CAA0B,CAAI,EAAE,CAAI;IACrD,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC1B,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,CAAC,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAC9C,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC3C,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,4CAA4C;AAC5C,MAAM,UAAU,IAAI,CAAC,CAAgB;IACnC,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,MAAM,KAAK,IAAI,CAAC,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,KAAK,GAAG,KAAK,CAAC;IAC1D,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,gBAAgB,CAA0B,CAAI,EAAE,CAAI;IAClE,OAAO,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AACzC,CAAC;AAED,wFAAwF;AACxF,MAAM,UAAU,cAAc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,CAAC;AAChB,CAAC;AAED,SAAS,IAAI,CACX,CAAI,EACJ,CAAI;IAEJ,IAAI,CAAC,CAAC,UAAU,KAAK,CAAC,CAAC,UAAU,EAAE,CAAC;QAClC,MAAM,IAAI,sBAAsB,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC;IAC/D,CAAC;IACD,OAAO,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AACpC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,QAAQ,EAAE,OAAO,MAA4C,CAAC;AAI3E,eAAO,MAAM,YAAY,EAAE,OAAO,MAEjC,CAAC"}
|
package/dist/elements.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The typed array of a vector, for the functions of this package only. The
|
|
2
|
+
// index does not export these symbols, so a caller reads `values`, a copy, and
|
|
3
|
+
// cannot change a vector, a unit vector included.
|
|
4
|
+
export const elements = Symbol('@zukhruf/vectors elements');
|
|
5
|
+
// Makes a vector of an array that the package already checked or read from a
|
|
6
|
+
// SQL form, with no copy.
|
|
7
|
+
export const fromElements = Symbol('@zukhruf/vectors fromElements');
|
|
8
|
+
//# sourceMappingURL=elements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements.js","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,+EAA+E;AAC/E,kDAAkD;AAClD,MAAM,CAAC,MAAM,QAAQ,GAAkB,MAAM,CAAC,2BAA2B,CAAC,CAAC;AAE3E,6EAA6E;AAC7E,0BAA0B;AAC1B,MAAM,CAAC,MAAM,YAAY,GAAkB,MAAM,CAC/C,+BAA+B,CAChC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { DimensionMismatchError } from './dimension-mismatch-error.ts';
|
|
2
|
+
export { cosineDistance, cosineSimilarity, dot, euclideanDistance, hammingDistance, norm, } from './distance.ts';
|
|
3
|
+
export { BitVector, HalfVector, Int8Vector, UnitVector, Vector, } from './vector.ts';
|
|
4
|
+
export { ZeroVectorError } from './zero-vector-error.ts';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,GAAG,EACH,iBAAiB,EACjB,eAAe,EACf,IAAI,GACL,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,EACV,MAAM,GACP,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { DimensionMismatchError } from "./dimension-mismatch-error.js";
|
|
2
|
+
export { cosineDistance, cosineSimilarity, dot, euclideanDistance, hammingDistance, norm, } from "./distance.js";
|
|
3
|
+
export { BitVector, HalfVector, Int8Vector, UnitVector, Vector, } from "./vector.js";
|
|
4
|
+
export { ZeroVectorError } from "./zero-vector-error.js";
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,GAAG,EACH,iBAAiB,EACjB,eAAe,EACf,IAAI,GACL,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,SAAS,EACT,UAAU,EACV,UAAU,EACV,UAAU,EACV,MAAM,GACP,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-vec-loaded-error.d.ts","sourceRoot":"","sources":["../src/sqlite-vec-loaded-error.ts"],"names":[],"mappings":"AAAA,yFAAyF;AACzF,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,SAAkB,IAAI,0BAA0B;;CAOjD"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** `install` on a database that loaded sqlite-vec, which already has these functions. */
|
|
2
|
+
export class SqliteVecLoadedError extends Error {
|
|
3
|
+
name = 'SqliteVecLoadedError';
|
|
4
|
+
constructor() {
|
|
5
|
+
super('The database loaded sqlite-vec, which already has vec_distance_cosine and vec_distance_L2');
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=sqlite-vec-loaded-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite-vec-loaded-error.js","sourceRoot":"","sources":["../src/sqlite-vec-loaded-error.ts"],"names":[],"mappings":"AAAA,yFAAyF;AACzF,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAC3B,IAAI,GAAG,sBAAsB,CAAC;IAEhD;QACE,KAAK,CACH,2FAA2F,CAC5F,CAAC;IACJ,CAAC;CACF"}
|
package/dist/sqlite.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { BitVector, Int8Vector, Vector } from './vector.ts';
|
|
3
|
+
export { SqliteVecLoadedError } from './sqlite-vec-loaded-error.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Registers `vec_distance_cosine` and `vec_distance_L2` of sqlite-vec 0.1.9 on
|
|
6
|
+
* a database that does not load the extension, with its results (to float32
|
|
7
|
+
* precision), its `NULL` and its error texts, so one SQL runs with or without
|
|
8
|
+
* sqlite-vec. See docs/ported-from-sqlite-vec.md.
|
|
9
|
+
*/
|
|
10
|
+
export declare function install(database: DatabaseSync): void;
|
|
11
|
+
/**
|
|
12
|
+
* The bytes that plain SQLite and sqlite-vec read: float32 values
|
|
13
|
+
* little-endian, int8 values as bytes, and bits packed least significant bit
|
|
14
|
+
* first, as sqlite-vec packs them.
|
|
15
|
+
*/
|
|
16
|
+
export declare function toSql(vector: Vector | Int8Vector | BitVector): Uint8Array;
|
|
17
|
+
/** Reads the bytes of `toSql` as a vector of the type. A BLOB whose length does not fit the type is a `RangeError`. */
|
|
18
|
+
export declare function fromSql(type: typeof Vector, blob: Uint8Array): Vector;
|
|
19
|
+
export declare function fromSql(type: typeof Int8Vector, blob: Uint8Array): Int8Vector;
|
|
20
|
+
export declare function fromSql(type: typeof BitVector, blob: Uint8Array): BitVector;
|
|
21
|
+
//# sourceMappingURL=sqlite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAiB,MAAM,aAAa,CAAC;AAI/D,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI,CA0BpD;AA6ED;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,UAAU,CAczE;AAED,uHAAuH;AACvH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,MAAM,CAAC;AACvE,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,UAAU,CAAC;AAC/E,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,SAAS,EAAE,IAAI,EAAE,UAAU,GAAG,SAAS,CAAC"}
|
package/dist/sqlite.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { elements, fromElements } from "./elements.js";
|
|
2
|
+
import { SqliteVecLoadedError } from "./sqlite-vec-loaded-error.js";
|
|
3
|
+
import { BitVector, Int8Vector, Vector } from "./vector.js";
|
|
4
|
+
export { SqliteVecLoadedError } from "./sqlite-vec-loaded-error.js";
|
|
5
|
+
/**
|
|
6
|
+
* Registers `vec_distance_cosine` and `vec_distance_L2` of sqlite-vec 0.1.9 on
|
|
7
|
+
* a database that does not load the extension, with its results (to float32
|
|
8
|
+
* precision), its `NULL` and its error texts, so one SQL runs with or without
|
|
9
|
+
* sqlite-vec. See docs/ported-from-sqlite-vec.md.
|
|
10
|
+
*/
|
|
11
|
+
export function install(database) {
|
|
12
|
+
if (hasSqliteVec(database))
|
|
13
|
+
throw new SqliteVecLoadedError();
|
|
14
|
+
const options = { deterministic: true, useBigIntArguments: true };
|
|
15
|
+
database.function('vec_distance_cosine', options, (a, b) => {
|
|
16
|
+
const [x, y] = vectors(a, b);
|
|
17
|
+
let dot = 0;
|
|
18
|
+
let squaresX = 0;
|
|
19
|
+
let squaresY = 0;
|
|
20
|
+
x.forEach((value, index) => {
|
|
21
|
+
const other = y[index] ?? 0;
|
|
22
|
+
dot += value * other;
|
|
23
|
+
squaresX += value * value;
|
|
24
|
+
squaresY += other * other;
|
|
25
|
+
});
|
|
26
|
+
// A zero vector gives NaN, which SQLite returns as NULL, as sqlite-vec does.
|
|
27
|
+
return 1 - dot / (Math.sqrt(squaresX) * Math.sqrt(squaresY));
|
|
28
|
+
});
|
|
29
|
+
database.function('vec_distance_L2', options, (a, b) => {
|
|
30
|
+
const [x, y] = vectors(a, b);
|
|
31
|
+
let squares = 0;
|
|
32
|
+
x.forEach((value, index) => {
|
|
33
|
+
const difference = value - (y[index] ?? 0);
|
|
34
|
+
squares += difference * difference;
|
|
35
|
+
});
|
|
36
|
+
return Math.sqrt(squares);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
function hasSqliteVec(database) {
|
|
40
|
+
try {
|
|
41
|
+
database.prepare('SELECT vec_version()');
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
if (Error.isError(error) &&
|
|
46
|
+
error.message === 'no such function: vec_version') {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function vectors(a, b) {
|
|
53
|
+
const x = vector(a, '1st');
|
|
54
|
+
const y = vector(b, '2nd');
|
|
55
|
+
if (x.length !== y.length) {
|
|
56
|
+
throw new Error(`Vector dimension mistmatch. First vector has ${x.length} dimensions, while the second has ${y.length} dimensions.`);
|
|
57
|
+
}
|
|
58
|
+
return [x, y];
|
|
59
|
+
}
|
|
60
|
+
function vector(value, ordinal) {
|
|
61
|
+
const reading = `Error reading ${ordinal} vector:`;
|
|
62
|
+
if (value instanceof Uint8Array) {
|
|
63
|
+
if (value.length === 0) {
|
|
64
|
+
throw new Error(`${reading} zero-length vectors are not supported.`);
|
|
65
|
+
}
|
|
66
|
+
if (value.length % 4 !== 0) {
|
|
67
|
+
throw new Error(`${reading} invalid float32 vector BLOB length. Must be divisible by 4, found ${value.length}`);
|
|
68
|
+
}
|
|
69
|
+
const view = new DataView(value.buffer, value.byteOffset, value.byteLength);
|
|
70
|
+
return Float32Array.from({ length: value.length / 4 }, (_, index) => view.getFloat32(index * 4, true));
|
|
71
|
+
}
|
|
72
|
+
if (typeof value === 'string') {
|
|
73
|
+
const parsed = json(value);
|
|
74
|
+
if (!Array.isArray(parsed) ||
|
|
75
|
+
!parsed.every((item) => typeof item === 'number')) {
|
|
76
|
+
throw new Error(`${reading} the text is not a JSON array of numbers.`);
|
|
77
|
+
}
|
|
78
|
+
if (parsed.length === 0) {
|
|
79
|
+
throw new Error(`${reading} zero-length vectors are not supported.`);
|
|
80
|
+
}
|
|
81
|
+
return Float32Array.from(parsed);
|
|
82
|
+
}
|
|
83
|
+
const type = value === null ? 'NULL' : typeof value === 'bigint' ? 'INTEGER' : 'FLOAT';
|
|
84
|
+
throw new Error(`${reading} Input must have type BLOB (compact format) or TEXT (JSON), found ${type}`);
|
|
85
|
+
}
|
|
86
|
+
// sqlite-vec reads some malformed JSON (`[1,` as `[1]`); this port refuses it.
|
|
87
|
+
function json(text) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(text);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (error instanceof SyntaxError)
|
|
93
|
+
return undefined;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The bytes that plain SQLite and sqlite-vec read: float32 values
|
|
99
|
+
* little-endian, int8 values as bytes, and bits packed least significant bit
|
|
100
|
+
* first, as sqlite-vec packs them.
|
|
101
|
+
*/
|
|
102
|
+
export function toSql(vector) {
|
|
103
|
+
if (vector instanceof Vector) {
|
|
104
|
+
const values = vector[elements];
|
|
105
|
+
const bytes = new Uint8Array(values.length * 4);
|
|
106
|
+
const view = new DataView(bytes.buffer);
|
|
107
|
+
values.forEach((value, index) => view.setFloat32(index * 4, value, true));
|
|
108
|
+
return bytes;
|
|
109
|
+
}
|
|
110
|
+
const values = vector[elements];
|
|
111
|
+
return new Uint8Array(values.buffer, values.byteOffset, values.byteLength).slice();
|
|
112
|
+
}
|
|
113
|
+
export function fromSql(type, blob) {
|
|
114
|
+
if (blob.length === 0) {
|
|
115
|
+
throw new RangeError('A vector BLOB has at least one byte');
|
|
116
|
+
}
|
|
117
|
+
if (type === Int8Vector) {
|
|
118
|
+
return Int8Vector[fromElements](new Int8Array(blob.slice().buffer));
|
|
119
|
+
}
|
|
120
|
+
if (type === BitVector) {
|
|
121
|
+
return BitVector[fromElements](blob.slice());
|
|
122
|
+
}
|
|
123
|
+
if (blob.length % 4 !== 0) {
|
|
124
|
+
throw new RangeError(`A float32 vector BLOB has a multiple of 4 bytes, not ${blob.length}`);
|
|
125
|
+
}
|
|
126
|
+
const view = new DataView(blob.buffer, blob.byteOffset, blob.byteLength);
|
|
127
|
+
return Vector[fromElements](Float32Array.from({ length: blob.length / 4 }, (_, index) => view.getFloat32(index * 4, true)));
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=sqlite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AAEpE;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,QAAsB;IAC5C,IAAI,YAAY,CAAC,QAAQ,CAAC;QAAE,MAAM,IAAI,oBAAoB,EAAE,CAAC;IAC7D,MAAM,OAAO,GAAG,EAAE,aAAa,EAAE,IAAI,EAAE,kBAAkB,EAAE,IAAI,EAAE,CAAC;IAClE,QAAQ,CAAC,QAAQ,CAAC,qBAAqB,EAAE,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACzD,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QAC7B,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,IAAI,QAAQ,GAAG,CAAC,CAAC;QACjB,IAAI,QAAQ,GAAG,CAAC,CAAC;QACjB,CAAC,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAA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AU,CAAC,CAAC;IACzE,OAAO,MAAM,CAAC,YAAY,CAAC,CACzB,YAAY,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAC1D,IAAI,CAAC,UAAU,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,CAAC,CACjC,CACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { HalfVector, Vector } from './vector.ts';
|
|
2
|
+
/**
|
|
3
|
+
* The JSON text that SQL Server casts to a vector: each value with 9
|
|
4
|
+
* significant digits for float32 and 5 for float16, the fewest that keep each
|
|
5
|
+
* value. The host casts it: `CAST(@q AS ${sqlType(vector)})`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function toSql(vector: Vector | HalfVector): string;
|
|
8
|
+
/** The type of a `CAST`: `VECTOR(n)` for a `Vector`, `VECTOR(n, float16)` for a `HalfVector`. */
|
|
9
|
+
export declare function sqlType(vector: Vector | HalfVector): string;
|
|
10
|
+
/**
|
|
11
|
+
* Reads the JSON text that SQL Server gives for a vector. SQL Server prints
|
|
12
|
+
* float32 values with 8 digits, so a `Vector` can differ from the written one
|
|
13
|
+
* by one unit in the last place; a `HalfVector` gives the written values.
|
|
14
|
+
*/
|
|
15
|
+
export declare function fromSql(type: typeof Vector, text: string): Vector;
|
|
16
|
+
export declare function fromSql(type: typeof HalfVector, text: string): HalfVector;
|
|
17
|
+
//# sourceMappingURL=sqlserver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver.d.ts","sourceRoot":"","sources":["../src/sqlserver.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAEjD;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAKzD;AAED,iGAAiG;AACjG,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAI3D;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;AACnE,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { elements } from "./elements.js";
|
|
2
|
+
import { HalfVector, Vector } from "./vector.js";
|
|
3
|
+
/**
|
|
4
|
+
* The JSON text that SQL Server casts to a vector: each value with 9
|
|
5
|
+
* significant digits for float32 and 5 for float16, the fewest that keep each
|
|
6
|
+
* value. The host casts it: `CAST(@q AS ${sqlType(vector)})`.
|
|
7
|
+
*/
|
|
8
|
+
export function toSql(vector) {
|
|
9
|
+
const digits = vector instanceof HalfVector ? 5 : 9;
|
|
10
|
+
return JSON.stringify(Array.from(vector[elements], (value) => Number(value.toPrecision(digits))));
|
|
11
|
+
}
|
|
12
|
+
/** The type of a `CAST`: `VECTOR(n)` for a `Vector`, `VECTOR(n, float16)` for a `HalfVector`. */
|
|
13
|
+
export function sqlType(vector) {
|
|
14
|
+
return vector instanceof HalfVector
|
|
15
|
+
? `VECTOR(${vector.dimensions}, float16)`
|
|
16
|
+
: `VECTOR(${vector.dimensions})`;
|
|
17
|
+
}
|
|
18
|
+
export function fromSql(type, text) {
|
|
19
|
+
const values = JSON.parse(text);
|
|
20
|
+
if (!Array.isArray(values) ||
|
|
21
|
+
values.length === 0 ||
|
|
22
|
+
!values.every((value) => typeof value === 'number')) {
|
|
23
|
+
throw new RangeError('The text is not a JSON array of numbers');
|
|
24
|
+
}
|
|
25
|
+
return type === HalfVector ? HalfVector.from(values) : Vector.from(values);
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=sqlserver.js.map
|
|
@@ -0,0 +1 @@
|
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I,CAAC,MAAM,CAAC,CAAC;AAC7E,CAAC"}
|
package/dist/vector.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { elements, fromElements } from './elements.ts';
|
|
2
|
+
/** A vector of float32 numbers. */
|
|
3
|
+
export declare class Vector {
|
|
4
|
+
#private;
|
|
5
|
+
protected constructor(values: Float32Array);
|
|
6
|
+
/** Copies the values. A value that is not finite in float32 is a `RangeError`. */
|
|
7
|
+
static from(values: ArrayLike<number>): Vector;
|
|
8
|
+
static [fromElements](values: Float32Array): Vector;
|
|
9
|
+
get dimensions(): number;
|
|
10
|
+
/** A copy of the values. */
|
|
11
|
+
get values(): Float32Array;
|
|
12
|
+
get [elements](): Float32Array;
|
|
13
|
+
}
|
|
14
|
+
/** A float32 vector of length 1. The cosine similarity of two unit vectors is their dot product. */
|
|
15
|
+
export declare class UnitVector extends Vector {
|
|
16
|
+
/** Divides each value by the length, in float64. A length of 0 is a `ZeroVectorError`. */
|
|
17
|
+
static normalize(vector: Vector | HalfVector): UnitVector;
|
|
18
|
+
}
|
|
19
|
+
/** A vector of float16 numbers, which SQL Server keeps as `VECTOR(n, float16)`. */
|
|
20
|
+
export declare class HalfVector {
|
|
21
|
+
#private;
|
|
22
|
+
private constructor();
|
|
23
|
+
/** Copies the values. A value that is not finite in float16 is a `RangeError`. */
|
|
24
|
+
static from(values: ArrayLike<number>): HalfVector;
|
|
25
|
+
get dimensions(): number;
|
|
26
|
+
/** A copy of the values. */
|
|
27
|
+
get values(): Float16Array;
|
|
28
|
+
get [elements](): Float16Array;
|
|
29
|
+
}
|
|
30
|
+
/** A vector of int8 numbers, which sqlite-vec keeps as `INT8[n]`. */
|
|
31
|
+
export declare class Int8Vector {
|
|
32
|
+
#private;
|
|
33
|
+
private constructor();
|
|
34
|
+
/** Copies the values. A value that is not an integer from -128 to 127 is a `RangeError`. */
|
|
35
|
+
static from(values: ArrayLike<number>): Int8Vector;
|
|
36
|
+
static [fromElements](values: Int8Array): Int8Vector;
|
|
37
|
+
get dimensions(): number;
|
|
38
|
+
/** A copy of the values. */
|
|
39
|
+
get values(): Int8Array;
|
|
40
|
+
get [elements](): Int8Array;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A vector of bits, packed eight to a byte with the least significant bit
|
|
44
|
+
* first, as sqlite-vec packs them. Its dimensions are a multiple of 8.
|
|
45
|
+
*/
|
|
46
|
+
export declare class BitVector {
|
|
47
|
+
#private;
|
|
48
|
+
private constructor();
|
|
49
|
+
/** Packs the bits. A number of bits that is not a multiple of 8 is a `RangeError`. */
|
|
50
|
+
static from(bits: ArrayLike<boolean>): BitVector;
|
|
51
|
+
static [fromElements](bytes: Uint8Array): BitVector;
|
|
52
|
+
get dimensions(): number;
|
|
53
|
+
/** A copy of the bits, the first bit first. */
|
|
54
|
+
get bits(): boolean[];
|
|
55
|
+
get [elements](): Uint8Array;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=vector.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.d.ts","sourceRoot":"","sources":["../src/vector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAGvD,mCAAmC;AACnC,qBAAa,MAAM;;IAGjB,SAAS,aAAa,MAAM,EAAE,YAAY;IAI1C,kFAAkF;IAClF,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,MAAM;IAI9C,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM;IAInD,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,4BAA4B;IAC5B,IAAI,MAAM,IAAI,YAAY,CAEzB;IAED,IAAI,CAAC,QAAQ,CAAC,IAAI,YAAY,CAE7B;CACF;AAED,oGAAoG;AACpG,qBAAa,UAAW,SAAQ,MAAM;IACpC,0FAA0F;IAC1F,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,UAAU;CAQ1D;AAED,mFAAmF;AACnF,qBAAa,UAAU;;IAGrB,OAAO;IAIP,kFAAkF;IAClF,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,UAAU;IAIlD,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,4BAA4B;IAC5B,IAAI,MAAM,IAAI,YAAY,CAEzB;IAED,IAAI,CAAC,QAAQ,CAAC,IAAI,YAAY,CAE7B;CACF;AAED,qEAAqE;AACrE,qBAAa,UAAU;;IAGrB,OAAO;IAIP,4FAA4F;IAC5F,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,CAAC,MAAM,CAAC,GAAG,UAAU;IAalD,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,MAAM,EAAE,SAAS,GAAG,UAAU;IAIpD,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,4BAA4B;IAC5B,IAAI,MAAM,IAAI,SAAS,CAEtB;IAED,IAAI,CAAC,QAAQ,CAAC,IAAI,SAAS,CAE1B;CACF;AAED;;;GAGG;AACH,qBAAa,SAAS;;IAGpB,OAAO;IAIP,sFAAsF;IACtF,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,OAAO,CAAC,GAAG,SAAS;IAiBhD,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC,KAAK,EAAE,UAAU,GAAG,SAAS;IAInD,IAAI,UAAU,IAAI,MAAM,CAEvB;IAED,+CAA+C;IAC/C,IAAI,IAAI,IAAI,OAAO,EAAE,CAIpB;IAED,IAAI,CAAC,QAAQ,CAAC,IAAI,UAAU,CAE3B;CACF"}
|
package/dist/vector.js
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { elements, fromElements } from "./elements.js";
|
|
2
|
+
import { ZeroVectorError } from "./zero-vector-error.js";
|
|
3
|
+
/** A vector of float32 numbers. */
|
|
4
|
+
export class Vector {
|
|
5
|
+
#values;
|
|
6
|
+
constructor(values) {
|
|
7
|
+
this.#values = values;
|
|
8
|
+
}
|
|
9
|
+
/** Copies the values. A value that is not finite in float32 is a `RangeError`. */
|
|
10
|
+
static from(values) {
|
|
11
|
+
return new Vector(finite(Float32Array.from(values), 'float32'));
|
|
12
|
+
}
|
|
13
|
+
static [fromElements](values) {
|
|
14
|
+
return new Vector(finite(values, 'float32'));
|
|
15
|
+
}
|
|
16
|
+
get dimensions() {
|
|
17
|
+
return this.#values.length;
|
|
18
|
+
}
|
|
19
|
+
/** A copy of the values. */
|
|
20
|
+
get values() {
|
|
21
|
+
return this.#values.slice();
|
|
22
|
+
}
|
|
23
|
+
get [elements]() {
|
|
24
|
+
return this.#values;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** A float32 vector of length 1. The cosine similarity of two unit vectors is their dot product. */
|
|
28
|
+
export class UnitVector extends Vector {
|
|
29
|
+
/** Divides each value by the length, in float64. A length of 0 is a `ZeroVectorError`. */
|
|
30
|
+
static normalize(vector) {
|
|
31
|
+
const values = vector[elements];
|
|
32
|
+
let squares = 0;
|
|
33
|
+
for (const value of values)
|
|
34
|
+
squares += value * value;
|
|
35
|
+
const length = Math.sqrt(squares);
|
|
36
|
+
if (length === 0)
|
|
37
|
+
throw new ZeroVectorError();
|
|
38
|
+
return new UnitVector(Float32Array.from(values, (value) => value / length));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** A vector of float16 numbers, which SQL Server keeps as `VECTOR(n, float16)`. */
|
|
42
|
+
export class HalfVector {
|
|
43
|
+
#values;
|
|
44
|
+
constructor(values) {
|
|
45
|
+
this.#values = values;
|
|
46
|
+
}
|
|
47
|
+
/** Copies the values. A value that is not finite in float16 is a `RangeError`. */
|
|
48
|
+
static from(values) {
|
|
49
|
+
return new HalfVector(finite(Float16Array.from(values), 'float16'));
|
|
50
|
+
}
|
|
51
|
+
get dimensions() {
|
|
52
|
+
return this.#values.length;
|
|
53
|
+
}
|
|
54
|
+
/** A copy of the values. */
|
|
55
|
+
get values() {
|
|
56
|
+
return this.#values.slice();
|
|
57
|
+
}
|
|
58
|
+
get [elements]() {
|
|
59
|
+
return this.#values;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** A vector of int8 numbers, which sqlite-vec keeps as `INT8[n]`. */
|
|
63
|
+
export class Int8Vector {
|
|
64
|
+
#values;
|
|
65
|
+
constructor(values) {
|
|
66
|
+
this.#values = values;
|
|
67
|
+
}
|
|
68
|
+
/** Copies the values. A value that is not an integer from -128 to 127 is a `RangeError`. */
|
|
69
|
+
static from(values) {
|
|
70
|
+
const source = Array.from(values);
|
|
71
|
+
const index = source.findIndex((value) => !Number.isInteger(value) || value < -128 || value > 127);
|
|
72
|
+
if (index !== -1) {
|
|
73
|
+
throw new RangeError(`Element ${index} of the vector is not an integer from -128 to 127`);
|
|
74
|
+
}
|
|
75
|
+
return new Int8Vector(Int8Array.from(source));
|
|
76
|
+
}
|
|
77
|
+
static [fromElements](values) {
|
|
78
|
+
return new Int8Vector(values);
|
|
79
|
+
}
|
|
80
|
+
get dimensions() {
|
|
81
|
+
return this.#values.length;
|
|
82
|
+
}
|
|
83
|
+
/** A copy of the values. */
|
|
84
|
+
get values() {
|
|
85
|
+
return this.#values.slice();
|
|
86
|
+
}
|
|
87
|
+
get [elements]() {
|
|
88
|
+
return this.#values;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A vector of bits, packed eight to a byte with the least significant bit
|
|
93
|
+
* first, as sqlite-vec packs them. Its dimensions are a multiple of 8.
|
|
94
|
+
*/
|
|
95
|
+
export class BitVector {
|
|
96
|
+
#bytes;
|
|
97
|
+
constructor(bytes) {
|
|
98
|
+
this.#bytes = bytes;
|
|
99
|
+
}
|
|
100
|
+
/** Packs the bits. A number of bits that is not a multiple of 8 is a `RangeError`. */
|
|
101
|
+
static from(bits) {
|
|
102
|
+
if (bits.length % 8 !== 0) {
|
|
103
|
+
throw new RangeError(`A bit vector has a multiple of 8 bits, not ${bits.length}`);
|
|
104
|
+
}
|
|
105
|
+
return new BitVector(Uint8Array.from({ length: bits.length / 8 }, (_, byte) => {
|
|
106
|
+
let packed = 0;
|
|
107
|
+
for (let bit = 0; bit < 8; bit++) {
|
|
108
|
+
if (bits[byte * 8 + bit])
|
|
109
|
+
packed |= 1 << bit;
|
|
110
|
+
}
|
|
111
|
+
return packed;
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
static [fromElements](bytes) {
|
|
115
|
+
return new BitVector(bytes);
|
|
116
|
+
}
|
|
117
|
+
get dimensions() {
|
|
118
|
+
return this.#bytes.length * 8;
|
|
119
|
+
}
|
|
120
|
+
/** A copy of the bits, the first bit first. */
|
|
121
|
+
get bits() {
|
|
122
|
+
return Array.from(this.#bytes).flatMap((byte) => Array.from({ length: 8 }, (_, bit) => (byte & (1 << bit)) !== 0));
|
|
123
|
+
}
|
|
124
|
+
get [elements]() {
|
|
125
|
+
return this.#bytes;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function finite(values, type) {
|
|
129
|
+
const index = values.findIndex((value) => !Number.isFinite(value));
|
|
130
|
+
if (index !== -1) {
|
|
131
|
+
throw new RangeError(`Element ${index} of the vector is not a finite ${type} number`);
|
|
132
|
+
}
|
|
133
|
+
return values;
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=vector.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector.js","sourceRoot":"","sources":["../src/vector.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,mCAAmC;AACnC,MAAM,OAAO,MAAM;IACR,OAAO,CAAe;IAE/B,YAAsB,MAAoB;QACxC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,kFAAkF;I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C/C,IAAI,IAAI;QACN,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAC9C,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,CACjE,CAAC;IACJ,CAAC;IAED,IAAI,CAAC,QAAQ,CAAC;QACZ,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AAED,SAAS,MAAM,CACb,MAAS,EACT,IAAY;IAEZ,MAAM,KAAK,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC;IACnE,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;QACjB,MAAM,IAAI,UAAU,CAClB,WAAW,KAAK,kCAAkC,IAAI,SAAS,CAChE,CAAC;IACJ,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A vector of length 0 has no direction, so it has no unit vector. It is a
|
|
3
|
+
* RangeError, as the other value errors of the package are (ADR 0003).
|
|
4
|
+
*/
|
|
5
|
+
export declare class ZeroVectorError extends RangeError {
|
|
6
|
+
readonly name = "ZeroVectorError";
|
|
7
|
+
constructor();
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=zero-vector-error.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zero-vector-error.d.ts","sourceRoot":"","sources":["../src/zero-vector-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,qBAAa,eAAgB,SAAQ,UAAU;IAC7C,SAAkB,IAAI,qBAAqB;;CAK5C"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A vector of length 0 has no direction, so it has no unit vector. It is a
|
|
3
|
+
* RangeError, as the other value errors of the package are (ADR 0003).
|
|
4
|
+
*/
|
|
5
|
+
export class ZeroVectorError extends RangeError {
|
|
6
|
+
name = 'ZeroVectorError';
|
|
7
|
+
constructor() {
|
|
8
|
+
super('A vector of length 0 has no unit vector');
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=zero-vector-error.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zero-vector-error.js","sourceRoot":"","sources":["../src/zero-vector-error.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,OAAO,eAAgB,SAAQ,UAAU;IAC3B,IAAI,GAAG,iBAAiB,CAAC;IAE3C;QACE,KAAK,CAAC,yCAAyC,CAAC,CAAC;IACnD,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,63 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/vectors",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.8.33",
|
|
4
|
+
"description": "Vector types, their math, and the SQL form of each vector in SQLite, sqlite-vec and SQL Server, which a program composes into its own SQL. After the vector types of pgvector and the TensorPrimitives of .NET.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/JanuaryLabs/zukhruf",
|
|
9
|
+
"directory": "packages/vectors"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/JanuaryLabs/zukhruf/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/vectors#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"vector",
|
|
17
|
+
"embedding",
|
|
18
|
+
"vector-search",
|
|
19
|
+
"cosine-similarity",
|
|
20
|
+
"sqlite",
|
|
21
|
+
"sqlite-vec",
|
|
22
|
+
"sql-server",
|
|
23
|
+
"float16"
|
|
24
|
+
],
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
"./package.json": "./package.json",
|
|
28
|
+
".": {
|
|
29
|
+
"@zukhruf/source": "./src/index.ts",
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./sqlite": {
|
|
35
|
+
"@zukhruf/source": "./src/sqlite.ts",
|
|
36
|
+
"types": "./dist/sqlite.d.ts",
|
|
37
|
+
"import": "./dist/sqlite.js",
|
|
38
|
+
"default": "./dist/sqlite.js"
|
|
39
|
+
},
|
|
40
|
+
"./sqlserver": {
|
|
41
|
+
"@zukhruf/source": "./src/sqlserver.ts",
|
|
42
|
+
"types": "./dist/sqlserver.d.ts",
|
|
43
|
+
"import": "./dist/sqlserver.js",
|
|
44
|
+
"default": "./dist/sqlserver.js"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
"files": [
|
|
48
|
+
"dist",
|
|
49
|
+
"!dist/spec/**",
|
|
50
|
+
"!**/*.tsbuildinfo",
|
|
51
|
+
"!**/*.test.*"
|
|
52
|
+
],
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@types/mssql": "^12.3.0",
|
|
58
|
+
"@zukhruf/sqlite-open": "^0.8.33",
|
|
59
|
+
"@zukhruf/testing": "^0.8.33",
|
|
60
|
+
"mssql": "^12.7.14",
|
|
61
|
+
"sqlite-vec": "^0.1.9"
|
|
62
|
+
}
|
|
63
|
+
}
|