mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-04-25 00:36:56 +02:00
start simple examples
This commit is contained in:
parent
2572aa1413
commit
e6067e2711
28 changed files with 1354 additions and 2 deletions
48
examples/simple-deno/demo.ts
Normal file
48
examples/simple-deno/demo.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { Database } from "jsr:@db/sqlite@0.11";
|
||||
//import { loadablePath } from "npm:sqlite-vec";
|
||||
|
||||
const db = new Database(":memory:");
|
||||
db.enableLoadExtension = true;
|
||||
db.loadExtension("../../dist/vec0");
|
||||
db.enableLoadExtension = false;
|
||||
|
||||
const [sqlite_version, vec_version] = db
|
||||
.prepare("select sqlite_version(), vec_version()")
|
||||
.value<[string]>()!;
|
||||
console.log(`sqlite_version=${sqlite_version}, vec_version=${vec_version}`);
|
||||
|
||||
db.exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])");
|
||||
|
||||
const insertStmt = db.prepare(
|
||||
"INSERT INTO vec_items(rowid, embedding) VALUES (?1, vec_f32(?2))"
|
||||
);
|
||||
|
||||
const insertVectors = db.transaction((items) => {
|
||||
for (const [id, vector] of items) {
|
||||
insertStmt.run(BigInt(id), new Uint8Array(vector.buffer));
|
||||
}
|
||||
});
|
||||
|
||||
insertVectors([
|
||||
[1, new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])],
|
||||
[2, new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])],
|
||||
]);
|
||||
|
||||
const query = new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]);
|
||||
const rows = db
|
||||
.prepare(
|
||||
`
|
||||
SELECT
|
||||
rowid,
|
||||
distance
|
||||
FROM vec_items
|
||||
WHERE embedding MATCH ?
|
||||
ORDER BY distance
|
||||
LIMIT 5
|
||||
`
|
||||
)
|
||||
.all([new Uint8Array(query.buffer)]);
|
||||
|
||||
console.log(rows);
|
||||
|
||||
db.close();
|
||||
Loading…
Add table
Add a link
Reference in a new issue