This commit is contained in:
Alex Garcia 2025-01-24 13:30:18 -08:00
parent dbc5098114
commit a2dd24f27e
2 changed files with 43 additions and 12 deletions

View file

@ -50,7 +50,7 @@ The `load()` function is compatible with
if your vectors are represented as an array of numbers, wrap it in a if your vectors are represented as an array of numbers, wrap it in a
[`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array), [`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array),
and use and use the
[`.buffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/buffer) [`.buffer`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/buffer)
accessor to bind as a parameter to `sqlite-vec` SQL functions. accessor to bind as a parameter to `sqlite-vec` SQL functions.
@ -62,8 +62,32 @@ console.log(stmt.run(embedding.buffer)); // 4
## Node.js ## Node.js
Here's a quick recipe of using `sqlite-vec` with If you're on Node.js `23.5.0` or above, you can use [the builtin `node:sqlite` module](https://nodejs.org/api/sqlite.html) with `sqlite-vec` like so:
[`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) in Node.js.
```js
import { DatabaseSync } from "node:sqlite";
import * as sqliteVec from "sqlite-vec";
const db = new DatabaseSync(":memory:", { allowExtension: true });
sqliteVec.load(db);
const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]);
const { result } = db
.prepare("select vec_length(?) as result")
.get(new Uint8Array(embedding.buffer));
console.log(result); // 4
```
See
[`simple-node2/demo.mjs`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-node2/demo.mjs)
for a complete `node:sqlite` + `sqlite-vec` demo.
Alternatively, you can use the
[`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3)
NPM package with `sqlite-vec` in Node as well.
```js ```js
import * as sqliteVec from "sqlite-vec"; import * as sqliteVec from "sqlite-vec";
@ -83,7 +107,7 @@ console.log(result); // 4
See See
[`simple-node/demo.mjs`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-node/demo.mjs) [`simple-node/demo.mjs`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-node/demo.mjs)
for a more complete Node.js demo. for a more complete demo.
## Deno ## Deno
@ -91,13 +115,11 @@ Here's a quick recipe of using `sqlite-vec` with
[`jsr:@db/sqlite`](https://jsr.io/@db/sqlite) in Deno. It will only work on Deno [`jsr:@db/sqlite`](https://jsr.io/@db/sqlite) in Deno. It will only work on Deno
version `1.44` or greater, because of a bug in previous Deno versions. version `1.44` or greater, because of a bug in previous Deno versions.
Keep in mind, the `better-sqlite3` example above also works in Deno, you just
need to prefix the `better-sqlite3` import with `npm:`, like
`import * from "npm:better-sqlite3"`.
```ts ```ts
import { Database } from "jsr:@db/sqlite@0.11"; import { Database } from "jsr:@db/sqlite";
import * as sqliteVec from "npm:sqlite-vec@0.0.1-alpha.9"; import * as sqliteVec from "npm:sqlite-vec";
const db = new Database(":memory:"); const db = new Database(":memory:");
db.enableLoadExtension = true; db.enableLoadExtension = true;
@ -115,11 +137,17 @@ See
[`simple-deno/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-deno/demo.ts) [`simple-deno/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-deno/demo.ts)
for a more complete Deno demo. for a more complete Deno demo.
The `better-sqlite3` example above also works in Deno, when the `better-sqlite3` import is prefixed with `npm:`:
```js
import * from "better-sqlite3"; // [!code --]
import * from "npm:better-sqlite3"; // [!code ++]
```
## Bun ## Bun
Here's a quick recipe of using `sqlite-vec` with Here's a quick recipe of using `sqlite-vec` with
[`bun:sqlite`](https://bun.sh/docs/api/sqlite) in Bun. The `better-sqlite3` [`bun:sqlite`](https://bun.sh/docs/api/sqlite) in Bun.
example above also works with Bun.
```ts ```ts
import { Database } from "bun:sqlite"; import { Database } from "bun:sqlite";
@ -143,3 +171,6 @@ console.log(result); // 4
See See
[`simple-bun/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-bun/demo.ts) [`simple-bun/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-bun/demo.ts)
for a more complete Bun demo. for a more complete Bun demo.
The `better-sqlite3`
example above also works with Bun.

View file

@ -40,7 +40,7 @@ for a more complete Python demo.
If your vectors in Python are provided as a list of floats, you can If your vectors in Python are provided as a list of floats, you can
convert them into the compact BLOB format that `sqlite-vec` uses with convert them into the compact BLOB format that `sqlite-vec` uses with
`serialize_float32()`. This will internally call [`struct.pack()`](https://docs.python.org/3/library/struct.html#struct.pack). `serialize_float32()`. This internally calls [`struct.pack()`](https://docs.python.org/3/library/struct.html#struct.pack).
```python ```python
from sqlite_vec import serialize_float32 from sqlite_vec import serialize_float32