example updates

This commit is contained in:
Alex Garcia 2024-05-11 00:21:44 -07:00
parent 12c96fc6fe
commit 85fac4c5ef
10 changed files with 112 additions and 76 deletions

View file

@ -12,24 +12,29 @@ const { sqlite_version, vec_version } = db
console.log(`sqlite_version=${sqlite_version}, vec_version=${vec_version}`);
db.exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])");
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 (?, vec_f32(?))"
"INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)"
);
const insertVectors = db.transaction((items) => {
for (const [id, vector] of items) {
insertStmt.run(BigInt(id), vector);
insertStmt.run(BigInt(id), new Float32Array(vector));
}
});
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])],
]);
insertVectors(items);
const query = new Float32Array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8]);
const rows = db
.prepare(
`
@ -39,9 +44,9 @@ const rows = db
FROM vec_items
WHERE embedding MATCH ?
ORDER BY distance
LIMIT 5
LIMIT 3
`
)
.all(query);
.all(new Float32Array(query));
console.log(rows);