doc updates

This commit is contained in:
Alex Garcia 2024-06-24 23:26:29 -07:00
parent 3a8ab9b489
commit dada170dc5
4 changed files with 54 additions and 13 deletions

View file

@ -8,8 +8,8 @@ functions:
vec_version: vec_version:
params: [] params: []
section: meta section: meta
desc: x desc: Returns a version string of the current `sqlite-vec` version.
example: x example: select vec_version();
vec_debug: vec_debug:
params: [] params: []
section: meta section: meta

View file

View file

@ -1,5 +1,7 @@
# Using `sqlite-vec` in Node.js, Deno, and Bun # Using `sqlite-vec` in Node.js, Deno, and Bun
[![npm](https://img.shields.io/npm/v/sqlite-vec.svg?color=green&logo=nodedotjs&logoColor=white)](https://www.npmjs.com/package/sqlite-vec)
To use `sqlite-vec` in Node.js, Deno or Bun, install the To use `sqlite-vec` in Node.js, Deno or Bun, install the
[`sqlite-vec` NPM package](https://npmjs.com/package/sqlite-vec) using your [`sqlite-vec` NPM package](https://npmjs.com/package/sqlite-vec) using your
favorite package manager: favorite package manager:
@ -40,26 +42,55 @@ console.log(`vec_version=${vec_version}`);
The `load()` function is compatable with The `load()` function is compatable with
[`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3), [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3),
[`node-sqlite3`](https://github.com/TryGhost/node-sqlite3), [`node-sqlite3`](https://github.com/TryGhost/node-sqlite3),
[`js:@db/sqlite`](https://jsr.io/@db/sqlite) (Deno), and [`jsr:@db/sqlite`](https://jsr.io/@db/sqlite) (Deno), and
[`bun:sqlite`](https://bun.sh/docs/api/sqlite). [`bun:sqlite`](https://bun.sh/docs/api/sqlite).
## Working with vectors in JavaScript ## Working with vectors in JavaScript
if your vectors are represented as an array of numbers, use 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),
use the and use
[`.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 insert the underlying ArrayBuffer. accessor to bind as a parameter to `sqlite-vec` SQL functions.
```js ```js
const embedding = new Float32Array([0.1, 0.2, 0.3]); // TODO
const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]);
const stmt = db.prepare("INSERT INTO vss_demo VALUES (?)"); const stmt = db.prepare("INSERT INTO vss_demo VALUES (?)");
stmt.run(embedding.buffer); stmt.run(embedding.buffer);
```
## Node.js ## Node.js
Here's a quick recipe of using `sqlite-vec` with [`better-sqlite3`](https://github.com/WiseLibs/better-sqlite3) in Node.js.
```js
```
See [`simple-node/demo.mjs`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-node/demo.mjs)
for a more complete Node.js demo.
## Deno ## Deno
## Bun 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 version `1.44` or greater, because of a bug in previous Deno version.
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
``` ```
See [`simple-deno/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-deno/demo.ts)
for a more complete Deno demo.
## Bun
Here's a quick recipe of using `sqlite-vec` with [`bun:sqlite`](https://bun.sh/docs/api/sqlite) in Bun. The `better-sqlite3` example above also works with Bun.
```ts
```
See [`simple-bun/demo.ts`](https://github.com/asg017/sqlite-vec/blob/main/examples/simple-bun/demo.ts)
for a more complete Bun demo.

View file

@ -57,10 +57,20 @@ If your vectors are from `numpy` arrays, the Python SQLite package allows you to
```python ```python
import numpy as np import numpy as np
import sqlite3
import sqlite_vec
db = sqlite3.connect(":memory:")
db.enable_load_extension(True)
sqlite_vec.load(db)
db.enable_load_extension(False)
db.execute("CREATE VIRTUAL TABLE vec_demo(sample_embedding float[4])")
embedding = np.array([0.1, 0.2, 0.3, 0.4]) embedding = np.array([0.1, 0.2, 0.3, 0.4])
result = db.execute('select vec_length(?)', [embedding.astype(np.float32)]).fetchone()[0] db.execute(
print(result) # 4 "INSERT INTO vec_demo(sample_embedding) VALUES (?)", [embedding.astype(np.float32)]
)
``` ```
## Recipes ## Recipes