mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-04-25 00:36:56 +02:00
node:sqlite sample
This commit is contained in:
parent
d1d1ed7a57
commit
dbc5098114
4 changed files with 81 additions and 0 deletions
2
examples/simple-node2/.gitignore
vendored
Normal file
2
examples/simple-node2/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules/
|
||||||
|
package-lock.json
|
||||||
56
examples/simple-node2/demo.mjs
Normal file
56
examples/simple-node2/demo.mjs
Normal file
|
|
@ -0,0 +1,56 @@
|
||||||
|
/**
|
||||||
|
* This demo Node.js script shows how you can use sqlite-vec with
|
||||||
|
* the new builtin node:sqlite module.
|
||||||
|
* Note that this requires Node v23.5.0 or above.
|
||||||
|
*/
|
||||||
|
import { DatabaseSync } from "node:sqlite";
|
||||||
|
import * as sqliteVec from "sqlite-vec";
|
||||||
|
|
||||||
|
// allExtension is required to enable extension support
|
||||||
|
const db = new DatabaseSync(":memory:", { allowExtension: true });
|
||||||
|
sqliteVec.load(db);
|
||||||
|
|
||||||
|
const { sqlite_version, vec_version } = db
|
||||||
|
.prepare(
|
||||||
|
"select sqlite_version() as sqlite_version, vec_version() as vec_version;",
|
||||||
|
)
|
||||||
|
.get();
|
||||||
|
|
||||||
|
console.log(`sqlite_version=${sqlite_version}, vec_version=${vec_version}`);
|
||||||
|
|
||||||
|
const items = [
|
||||||
|
[1, [0.1, 0.1, 0.1, 0.1]],
|
||||||
|
[2, [0.2, 0.2, 0.2, 0.2]],
|
||||||
|
[3, [0.3, 0.3, 0.3, 0.3]],
|
||||||
|
[4, [0.4, 0.4, 0.4, 0.4]],
|
||||||
|
[5, [0.5, 0.5, 0.5, 0.5]],
|
||||||
|
];
|
||||||
|
const query = [0.3, 0.3, 0.3, 0.3];
|
||||||
|
|
||||||
|
db.exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[4])");
|
||||||
|
|
||||||
|
const insertStmt = db.prepare(
|
||||||
|
"INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)",
|
||||||
|
);
|
||||||
|
|
||||||
|
// TODO node:sqlite doesn't have `.transaction()` support yet
|
||||||
|
for (const [id, vector] of items) {
|
||||||
|
// node:sqlite requires Uint8Array for BLOB values, so a bit awkward
|
||||||
|
insertStmt.run(BigInt(id), new Uint8Array(new Float32Array(vector).buffer));
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = db
|
||||||
|
.prepare(
|
||||||
|
`
|
||||||
|
SELECT
|
||||||
|
rowid,
|
||||||
|
distance
|
||||||
|
FROM vec_items
|
||||||
|
WHERE embedding MATCH ?
|
||||||
|
ORDER BY distance
|
||||||
|
LIMIT 3
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
.all(new Uint8Array(new Float32Array(query).buffer));
|
||||||
|
|
||||||
|
console.log(rows);
|
||||||
11
examples/simple-node2/package.json
Normal file
11
examples/simple-node2/package.json
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"name": "simple-node2",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"main": "demo.mjs",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=23.5.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"sqlite-vec": "latest"
|
||||||
|
}
|
||||||
|
}
|
||||||
12
examples/simple-node2/tmp.mjs
Normal file
12
examples/simple-node2/tmp.mjs
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue