Skip to content

Commit

Permalink
start simple examples
Browse files Browse the repository at this point in the history
  • Loading branch information
asg017 committed May 11, 2024
1 parent 2572aa1 commit e6067e2
Show file tree
Hide file tree
Showing 28 changed files with 1,354 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,17 @@ jobs:
with:
name: sqlite-vec-windows-x86_64-extension
path: dist/*
build-wasm32-emscripten:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: mymindstorm/setup-emsdk@v12
with:
version: "latest"
- run: ./scripts/vendor.sh
- run: make sqlite-vec.h
- run: make wasm
- uses: actions/upload-artifact@v4
with:
name: sqlite-vec-wasm32-emscripten
path: dist/*
49 changes: 49 additions & 0 deletions examples/simple-bun/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Database } from "bun:sqlite";

Database.setCustomSQLite("/usr/local/opt/sqlite3/lib/libsqlite3.dylib");

const db = new Database(":memory:");
//sqliteVec.load(db);
db.loadExtension("../../dist/vec0");

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}`);

db.exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])");

const insertStmt = db.prepare(
"INSERT INTO vec_items(rowid, embedding) VALUES (?, vec_f32(?))"
);

const insertVectors = db.transaction((items) => {
for (const [id, vector] of items) {
insertStmt.run(BigInt(id), 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])],
]);

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(query);

console.log(rows);
1 change: 1 addition & 0 deletions examples/simple-c/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo
6 changes: 6 additions & 0 deletions examples/simple-c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
demo: demo.c
gcc \
-DSQLITE_CORE \
-I../../ -I../../vendor \
demo.c ../../sqlite-vec.c ../../vendor/sqlite3.c \
-o $@
81 changes: 81 additions & 0 deletions examples/simple-c/demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "sqlite3.h"
#include "sqlite-vec.h"
#include <stdio.h>
#include <unistd.h>
#include <assert.h>

int main(int argc, char *argv[]) {
int rc = SQLITE_OK;
sqlite3 *db;
sqlite3_stmt *stmt;

rc = sqlite3_auto_extension((void (*)())sqlite3_vec_init);
assert(rc == SQLITE_OK);

rc = sqlite3_open(":memory:", &db);
assert(rc == SQLITE_OK);

rc = sqlite3_prepare_v2(db, "SELECT sqlite_version(), vec_version()", -1, &stmt, NULL);
assert(rc == SQLITE_OK);

rc = sqlite3_step(stmt);
printf("sqlite_version=%s, vec_version=%s\n", sqlite3_column_text(stmt, 0), sqlite3_column_text(stmt, 1));
sqlite3_finalize(stmt);

rc = sqlite3_prepare_v2(db, "CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])", -1, &stmt, NULL);
assert(rc == SQLITE_OK);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
sqlite3_finalize(stmt);


static const struct {
sqlite3_int64 id;
float vector[8];
} items[] = {
{1, {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}},
{2, {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}},
};

rc = sqlite3_exec(db, "BEGIN", NULL, NULL, NULL);
assert(rc == SQLITE_OK);
rc = sqlite3_prepare_v2(db, "INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)", -1, &stmt, NULL);
assert(rc == SQLITE_OK);
for (unsigned long i = 0; i < sizeof(items) / sizeof(items[0]); i++) {
sqlite3_bind_int64(stmt, 1, items[i].id);
sqlite3_bind_blob(stmt, 2, items[i].vector, sizeof(items[i].vector), SQLITE_STATIC);
rc = sqlite3_step(stmt);
assert(rc == SQLITE_DONE);
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
rc = sqlite3_exec(db, "COMMIT", NULL, NULL, NULL);
assert(rc == SQLITE_OK);


rc = sqlite3_prepare_v2(db,
"SELECT "
" rowid, "
" distance "
"FROM vec_items "
"WHERE embedding MATCH ?1 "
"ORDER BY distance "
"LIMIT 5 "
, -1, &stmt, NULL);
assert(rc == SQLITE_OK);

float query[8] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8};
sqlite3_bind_blob(stmt, 1, query, sizeof(query), SQLITE_STATIC);

while(1) {
rc = sqlite3_step(stmt);
if(rc == SQLITE_DONE) break;
assert(rc==SQLITE_ROW);
sqlite3_int64 rowid = sqlite3_column_int64(stmt, 0);
double distance = sqlite3_column_double(stmt, 1);
printf("rowid=%lld distance=%f\n", rowid, distance);
}
sqlite3_finalize(stmt);
sqlite3_close(db);
return 0;
}
48 changes: 48 additions & 0 deletions examples/simple-deno/demo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Database } from "jsr:@db/[email protected]";
//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();
1 change: 1 addition & 0 deletions examples/simple-go/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
demo
2 changes: 2 additions & 0 deletions examples/simple-go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
demo: demo.go
go build -o $@
96 changes: 96 additions & 0 deletions examples/simple-go/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package main

import (
"bytes"
"database/sql"
"encoding/binary"
"fmt"
"log"

sqlite_vec "github.com/asg017/sqlite-vec/bindings/go/cgo"
_ "github.com/mattn/go-sqlite3"
)

// #cgo LDFLAGS: -L../../dist
import "C"

func serializeFloat32(vector []float32) ([]byte, error) {
buf := new(bytes.Buffer)
err := binary.Write(buf, binary.LittleEndian, vector)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func main() {
sqlite_vec.Auto()
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()

var sqliteVersion string
var vecVersion string
err = db.QueryRow("select sqlite_version(), vec_version()").Scan(&sqliteVersion, &vecVersion)
if err != nil {
log.Fatal(err)
}
fmt.Printf("sqlite_version=%s, vec_version=%s\n", sqliteVersion, vecVersion)

_, err = db.Exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])")
if err != nil {
log.Fatal(err)
}

items := map[int][]float32{
1: {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8},
2: {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8},
}

for id, values := range items {
v, err := serializeFloat32(values)
if err != nil {
log.Fatal(err)
}
_, err = db.Exec("INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)", id, v)
if err != nil {
log.Fatal(err)
}
}

q := []float32{0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8}
query, err := serializeFloat32(q)
if err != nil {
log.Fatal(err)
}

rows, err := db.Query(`
SELECT
rowid,
distance
FROM vec_items
WHERE embedding MATCH ?
ORDER BY distance
LIMIT 5
`, query)

if err != nil {
log.Fatal(err)
}

for rows.Next() {
var rowid int64
var distance float64
err = rows.Scan(&rowid, &distance)
if err != nil {
log.Fatal(err)
}
fmt.Printf("rowid=%d, distance=%f\n", rowid, distance)
}
err = rows.Err()
if err != nil {
log.Fatal((err))
}

}
10 changes: 10 additions & 0 deletions examples/simple-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/asg017/sqlite-vec/examples/go

go 1.20

replace github.com/asg017/sqlite-vec/bindings/go/cgo => ../../bindings/go/cgo

require (
github.com/asg017/sqlite-vec/bindings/go/cgo v0.0.0-00010101000000-000000000000
github.com/mattn/go-sqlite3 v1.14.22
)
2 changes: 2 additions & 0 deletions examples/simple-go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
1 change: 1 addition & 0 deletions examples/simple-node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
48 changes: 48 additions & 0 deletions examples/simple-node/demo.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as sqliteVec from "sqlite-vec";
import Database from "better-sqlite3";

const db = new Database(":memory:");
//sqliteVec.load(db);
db.loadExtension("../../dist/vec0");

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}`);

db.exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[8])");

const insertStmt = db.prepare(
"INSERT INTO vec_items(rowid, embedding) VALUES (?, vec_f32(?))"
);

const insertVectors = db.transaction((items) => {
for (const [id, vector] of items) {
insertStmt.run(BigInt(id), 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])],
]);

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(query);

console.log(rows);
Loading

0 comments on commit e6067e2

Please sign in to comment.