Documentation
¶
Overview ¶
Package sqlvec is a cgo-free SQLite driver with vector search built in.
Importing it for its side effects registers the "sqlite3" driver with database/sql, and arranges for every connection that driver opens to have the sqlite-vector extension loaded:
import (
"database/sql"
_ "github.com/axe-or/sqlite3vec-go"
)
db, err := sql.Open("sqlite3", "file:docs.db")
The vector_* functions are then available without a .load or any further setup, the way fts5 and rtree are:
CREATE TABLE docs(id INTEGER PRIMARY KEY, embedding BLOB, label TEXT);
INSERT INTO docs(embedding, label)
VALUES (vector_as_f32('[1.0, 0.0, 0.0, 0.0]'), 'x');
SELECT vector_init('docs', 'embedding', 'type=FLOAT32,dimension=4,distance=L2');
SELECT d.label, v.distance FROM docs AS d
JOIN vector_full_scan('docs', 'embedding',
vector_as_f32('[1.0,0.0,0.0,0.0]'), 10) AS v
ON d.id = v.rowid;
SQLite itself comes from github.com/ncruces/go-sqlite3, whose API this package does not wrap or hide -- use it directly for prepared statements, custom functions, virtual tables, VFSes and the rest.
vector_init is per connection ¶
vector_init() records its configuration on the connection that ran it, and database/sql hands out connections from a pool, so a later query can land on one that never saw it. Either hold the pool to a single connection with db.SetMaxOpenConns(1), or have driver.Open run vector_init on each new connection:
db, err := driver.Open("file:docs.db", func(c *sqlite3.Conn) error {
return c.Exec(`SELECT vector_init('docs', 'embedding',
'type=FLOAT32,dimension=384')`)
})
The extension is already loaded by the time that callback runs; it only has to configure it.
Performance ¶
The extension is compiled to WebAssembly and translated to Go, so its distance kernels are the portable scalar ones: the AVX-512, AVX2, SSE2, NEON and RVV kernels of a native build cannot come along, because the translator does not accept Wasm SIMD. SELECT vector_backend() reports CPU here for that reason.
A brute-force vector_full_scan is about three times slower than the same scan in a native AVX2 build -- see the README for the measurements. Quantized search (vector_quantize, then vector_quantize_scan) removes far more work than SIMD does, and behaves here exactly as it does natively.
Index ¶
Constants ¶
const Version = "1.1.2"
Version is the version of sqlite-vector this package carries. It is what SELECT vector_version() returns.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.