From dada170dc59145123e3fe01289b8284637014d70 Mon Sep 17 00:00:00 2001 From: Alex Garcia Date: Mon, 24 Jun 2024 23:26:29 -0700 Subject: [PATCH] doc updates --- reference.yaml | 6 +++--- site/using/deno.md | 0 site/using/js.md | 47 ++++++++++++++++++++++++++++++++++++-------- site/using/python.md | 14 +++++++++++-- 4 files changed, 54 insertions(+), 13 deletions(-) delete mode 100644 site/using/deno.md diff --git a/reference.yaml b/reference.yaml index 6920b23..6dcc083 100644 --- a/reference.yaml +++ b/reference.yaml @@ -8,8 +8,8 @@ functions: vec_version: params: [] section: meta - desc: x - example: x + desc: Returns a version string of the current `sqlite-vec` version. + example: select vec_version(); vec_debug: params: [] section: meta @@ -118,4 +118,4 @@ entrypoints: compile_options: - SQLITE_VEC_ENABLE_AVX - SQLITE_VEC_ENABLE_NEON - + diff --git a/site/using/deno.md b/site/using/deno.md deleted file mode 100644 index e69de29..0000000 diff --git a/site/using/js.md b/site/using/js.md index a5fd6d5..7b979c2 100644 --- a/site/using/js.md +++ b/site/using/js.md @@ -1,5 +1,7 @@ # 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 [`sqlite-vec` NPM package](https://npmjs.com/package/sqlite-vec) using your favorite package manager: @@ -40,26 +42,55 @@ console.log(`vec_version=${vec_version}`); The `load()` function is compatable with [`better-sqlite3`](https://github.com/WiseLibs/better-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). ## Working with vectors in JavaScript -if your vectors are represented as an array of numbers, use -[Float32Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array), -use the +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), +and use [`.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 -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 (?)"); stmt.run(embedding.buffer); - +``` ## 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 -## 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. diff --git a/site/using/python.md b/site/using/python.md index b5ad220..ab68418 100644 --- a/site/using/python.md +++ b/site/using/python.md @@ -57,10 +57,20 @@ If your vectors are from `numpy` arrays, the Python SQLite package allows you to ```python 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]) -result = db.execute('select vec_length(?)', [embedding.astype(np.float32)]).fetchone()[0] -print(result) # 4 +db.execute( + "INSERT INTO vec_demo(sample_embedding) VALUES (?)", [embedding.astype(np.float32)] +) ``` ## Recipes