-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
1,354 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
demo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
demo: demo.go | ||
go build -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.