### Install datasette-sqlite-vec Plugin Source: https://alexgarcia.xyz/sqlite-vec/datasette.html Install the datasette-sqlite-vec plugin using the datasette install command. This makes sqlite-vec SQL functions available in future Datasette instances. ```bash datasette install datasette-sqlite-vec ``` -------------------------------- ### Install sqlite-vec for sqlite-utils Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to install the sqlite-utils-sqlite-vec plugin. ```bash sqlite-utils install sqlite-utils-sqlite-vec ``` -------------------------------- ### Install sqlite-vec Go bindings (CGO) Source: https://alexgarcia.xyz/sqlite-vec/go.html Installs the CGO bindings for sqlite-vec. This compiles and statically links sqlite-vec into your project. Initial builds may be slow, but subsequent builds are cached and faster. ```bash go get -u github.com/asg017/sqlite-vec-go-bindings/cgo ``` -------------------------------- ### Install sqlite-vec with Bun Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to install the sqlite-vec package using Bun. ```bash bun install sqlite-vec ``` -------------------------------- ### Install sqlite-vec with npm (Node.js) Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to install the sqlite-vec Node.js package. ```bash npm install sqlite-vec ``` -------------------------------- ### Install sqlite-vec using install.sh script Source: https://alexgarcia.xyz/sqlite-vec/installation.html This script downloads the appropriate pre-compiled extension from Github Releases. ```bash # yolo curl -L 'https://github.com/asg017/sqlite-vec/releases/latest/download/install.sh' | sh ``` -------------------------------- ### Safely install sqlite-vec using install.sh script Source: https://alexgarcia.xyz/sqlite-vec/installation.html This method downloads the script first, allowing inspection before execution. ```bash # ok lets play it safe curl -o install.sh -L https://github.com/asg017/sqlite-vec/releases/latest/download/install.sh # inspect your scripts cat install.sh # TODO Test if execute permissions? ./install.sh ``` -------------------------------- ### Install sqlite-vec Go bindings (WASM) Source: https://alexgarcia.xyz/sqlite-vec/go.html Installs the WASM-based bindings for sqlite-vec, which avoids CGO by using a custom WASM build of SQLite. ```bash go get -u github.com/asg017/sqlite-vec-go-bindings/ncruces ``` -------------------------------- ### Using sqlite-vec with better-sqlite3 in Node.js Source: https://alexgarcia.xyz/sqlite-vec/js.html Shows how to use `sqlite-vec` with the `better-sqlite3` NPM package in Node.js. This example directly passes the `Float32Array` to the `get` method. ```javascript import * as sqliteVec from "sqlite-vec"; import Database from "better-sqlite3"; const db = new Database(":memory:"); sqliteVec.load(db); const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]); const { result } = db .prepare("select vec_length(?)",) .get(embedding); console.log(result); // 4 ``` -------------------------------- ### Install sqlite-vec with Ruby Gem Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to install the sqlite-vec Ruby gem. ```bash gem install sqlite-vec ``` -------------------------------- ### Example Binary Quantization Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Demonstrates the conversion of a float vector to its binary representation. ```json [-0.73, -0.80, 0.12, -0.73, 0.79, -0.11, 0.23, 0.97] ``` ```json [0, 0, 1, 0, 1, 0, 1, 1] ``` -------------------------------- ### Clone and Compile sqlite-vec from Source Source: https://alexgarcia.xyz/sqlite-vec/compiling.html Clone the repository, run the vendor script to get SQLite amalgamation, and then build the loadable extension. ```bash git clone https://github.com/asg017/sqlite-vec cd sqlite-vec ./scripts/vendor.sh make loadable ``` -------------------------------- ### Install sqlite-vec with pip (Python) Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to install the sqlite-vec Python package. ```bash pip install sqlite-vec ``` -------------------------------- ### Get sqlite-vec Version Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Use `vec_version()` to retrieve the current version string of the sqlite-vec installation. ```sql select vec_version(); -- 'v0.0.1-alpha.37' ``` -------------------------------- ### Download and Load sqlite-vec Extension in rqlite Source: https://alexgarcia.xyz/sqlite-vec/rqlite.html This snippet shows how to download a sqlite-vec release and configure rqlite to load it as an extension at launch time. Ensure you have curl installed and replace the version and architecture details as needed. ```bash # Download a sqlite-vec release. curl -L https://github.com/asg017/sqlite-vec/releases/download/v0.1.1/sqlite-vec-0.1.1-loadable-linux-x86_64.tar.gz -o sqlite-vec.tar.gz # Tell rqlite to load sqlite-vec at launch time. rqlited -extensions-path=sqlite-vec.tar.gz data ``` -------------------------------- ### Get sqlite-vec Debug Information Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Use `vec_debug()` to obtain debugging information for the current sqlite-vec installation, including version, date, commit, and build flags. ```sql select vec_debug(); /* 'Version: v0.0.1-alpha.37 Date: 2024-07-23T14:09:43Z-0700 Commit: 77f9b0374c8129056b344854de2dff6b103e5729 Build flags: avx ' */ ``` -------------------------------- ### Using sqlite-vec with bun:sqlite in Bun Source: https://alexgarcia.xyz/sqlite-vec/js.html This example shows how to use `sqlite-vec` with `bun:sqlite` in Bun. On macOS, you might need to set a custom SQLite path if the built-in library doesn't support extensions. ```typescript import { Database } from "bun:sqlite"; import * as sqliteVec from "sqlite-vec"; // MacOS *might* have to do this, as the builtin SQLite library on MacOS doesn't allow extensions Database.setCustomSQLite("/usr/local/opt/sqlite3/lib/libsqlite3.dylib"); const db = new Database(":memory:"); sqliteVec.load(db); const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]); const { result } = db .prepare("select vec_length(?) as result",) .get(embedding); console.log(result); // 4 ``` -------------------------------- ### Define vec0 Table with Metadata, Partition Key, and Auxiliary Columns Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Example of creating a vec0 virtual table, specifying partition key, metadata, and auxiliary columns. Use this to define the schema for your vector data and associated attributes. ```sql create virtual table vec_chunks using vec0( document_id integer partition key, contents_embedding float[768], -- partition key column, denoted by 'partition key' user_id integer partition key, -- metadata column, appears as normal column definition label text, -- auxiliary column, denoted by '+' +contents text ); ``` -------------------------------- ### Using sqlite-vec with jsr:@db/sqlite in Deno Source: https://alexgarcia.xyz/sqlite-vec/js.html This example demonstrates using `sqlite-vec` with `jsr:@db/sqlite` in Deno, version 1.44 or greater. It requires enabling `enableLoadExtension` before loading `sqliteVec`. ```typescript import { Database } from "jsr:@db/sqlite"; import * as sqliteVec from "npm:sqlite-vec"; const db = new Database(":memory:"); db.enableLoadExtension = true; sqliteVec.load(db); db.enableLoadExtension = false; const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]); const [result] = db .prepare("select vec_length(?)") .value<[string]>(new Uint8Array(embedding.buffer)!); console.log(result); // 4 ``` -------------------------------- ### SQLite Custom Type Example Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Demonstrates that SQLite does not enforce custom 'column types' like float[4]; CHECK constraints are necessary for validation. ```sql -- these "column types" are totally legal in SQLite create table students( name ham_sandwich, age minions[42] ); ``` -------------------------------- ### vec_version Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Retrieves the version string of the current sqlite-vec installation. ```APIDOC ## vec_version() ### Description Returns a version string of the current `sqlite-vec` installation. ### Method SQL Function ### Parameters None ### Request Example ```sql select vec_version(); ``` ### Response #### Success Response (string) - Returns the version string of the sqlite-vec installation. ### Response Example ``` 'v0.0.1-alpha.37' ``` ``` -------------------------------- ### Register sqlite-vec extension and embed a vector in Rust Source: https://alexgarcia.xyz/sqlite-vec/rust.html This example demonstrates how to register the `sqlite-vec` extension using `rusqlite`'s `sqlite3_auto_extension` and then embed a vector into an in-memory SQLite database. It also shows how to retrieve the vector library version and the JSON representation of the embedded vector. ```rust use sqlite_vec::sqlite3_vec_init; use rusqlite::{ffi::sqlite3_auto_extension, Result}; use zerocopy::AsBytes; fn main()-> Result<()> { unsafe { sqlite3_auto_extension(Some(std::mem::transmute(sqlite3_vec_init as *const ()))); } let db = Connection::open_in_memory()?; let v: Vec = vec![0.1, 0.2, 0.3]; let (vec_version, embedding): (String, String) = db.query_row( "select vec_version(), vec_to_json(?)", &[v.as_bytes()], |x| Ok((x.get(0)?, x.get(1)?)), )?; println!("vec_version={vec_version}, embedding={embedding}"); Ok(()) } ``` -------------------------------- ### Enable sqlite-vec functions with CGO Source: https://alexgarcia.xyz/sqlite-vec/go.html Enables sqlite-vec functions for all future database connections using `sqlite_vec.Auto()`. `sqlite_vec.Cancel()` can be used to undo this. This example demonstrates querying the vector version. ```go package main import ( "database/sql" "log" sqlite_vec "github.com/asg017/sqlite-vec-go-bindings/cgo" _ "github.com/mattn/go-sqlite3" ) func main() { sqlite_vec.Auto() db, err := sql.Open("sqlite3", ":memory:") if err != nil { log.Fatal(err) } defer db.Close() var vecVersion string err = db.QueryRow("select vec_version()").Scan(&vecVersion) if err != nil { log.Fatal(err) } log.Printf("vec_version=%s\n",vecVersion) } ``` -------------------------------- ### vec_debug Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Provides debugging information for the current sqlite-vec installation, including version, build date, commit hash, and build flags. ```APIDOC ## vec_debug() ### Description Returns debugging information of the current `sqlite-vec` installation. ### Method SQL Function ### Parameters None ### Request Example ```sql select vec_debug(); ``` ### Response #### Success Response (string) - Returns a multi-line string containing debugging information. ### Response Example ``` 'Version: v0.0.1-alpha.37 Date: 2024-07-23T14:09:43Z-0700 Commit: 77f9b0374c8129056b344854de2dff6b103e5729 Build flags: avx ' ``` ``` -------------------------------- ### KNN Query with Metadata Filtering Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Perform a KNN query on a vec0 table, applying filters on metadata columns. This example demonstrates how to combine vector similarity search with attribute-based filtering. ```sql select * from vec_movies where synopsis_embedding match '[...]' and k = 5 and genre = 'scifi' and num_reviews between 100 and 500 and mean_rating > 3.5 and contains_violence = false; ``` -------------------------------- ### Download and Unzip Latest Amalgamation Build Source: https://alexgarcia.xyz/sqlite-vec/compiling.html Download the latest amalgamation build archive and unzip it to obtain the source files. ```bash wget https://github.com/asg017/sqlite-vec/releases/download/v0.1.10-alpha.4 /sqlite-vec-0.1.10-alpha.4 -amalgamation.zip unzip sqlite-vec-0.1.10-alpha.4 -amalgamation.zip ``` -------------------------------- ### vec_slice(vector, start, end) Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Extracts a subset of a vector from a start index (inclusive) to an end index (exclusive). Useful for Matryoshka embeddings. Returns an error for invalid inputs or index ranges. ```APIDOC ## `vec_slice(vector, start, end)` ### Description Extract a subset of `vector` from the `start` element (inclusive) to the `end` element (exclusive). TODO check This is especially useful for Matryoshka embeddings, also known as "adaptive length" embeddings. Use with `vec_normalize()` to get proper results. Returns an error in the following conditions: * If `vector` is not a valid vector * If `start` is less than zero or greater than or equal to `end` * If `end` is greater than the length of `vector`, or less than or equal to `start`. * If `vector` is a bitvector, `start` and `end` must be divisible by 8. ### Parameters #### Path Parameters - **vector** (vector) - Required - The input vector. - **start** (integer) - Required - The starting index (inclusive). - **end** (integer) - Required - The ending index (exclusive). ### Request Example ```sql select vec_slice('[1, 2,3, 4]', 0, 2); -- X'0000803F00000040' select vec_to_json( vec_slice('[1, 2,3, 4]', 0, 2) ); -- '[1.000000,2.000000]' select vec_to_json( vec_slice('[1, 2,3, 4]', 2, 4) ); -- '[3.000000,4.000000]' ``` ### Error Handling - `start` index out of bounds or invalid. - `end` index out of bounds or invalid. - `start` and `end` indices invalid for bitvectors. - Invalid input vector. ``` -------------------------------- ### Create and Query Vector Table in SQLite Source: https://alexgarcia.xyz/sqlite-vec Demonstrates how to create a virtual table for storing vector embeddings, insert data with embeddings, and perform a KNN search using pure SQL. ```sql create virtual table vec_movies using vec0( synopsis_embedding float[768] ); ``` ```sql insert into vec_movies(rowid, synopsis_embedding) select rowid, embed(synopsis) as synopsis_embedding from movies; ``` ```sql select rowid, distance from vec_movies where synopsis_embedding match embed('scary futuristic movies') order by distance limit 20; ``` -------------------------------- ### Initialize SQLite-Vec WASM in Browser Source: https://alexgarcia.xyz/sqlite-vec/wasm.html This snippet demonstrates how to initialize the SQLite-Vec WebAssembly build and query the vector version. It requires importing the `sqlite-vec-wasm-demo` package. ```html ``` -------------------------------- ### Basic sqlite-vec Usage in rqlite Shell Source: https://alexgarcia.xyz/sqlite-vec/rqlite.html Demonstrates creating a virtual table with vector support, inserting data with embeddings, and performing a vector similarity search using the `match` operator within the rqlite CLI. ```sql 127.0.0.1:4001> create virtual table vec_examples using vec0(sample_embedding float[8]); 127.0.0.1:4001> insert into vec_examples(rowid, sample_embedding) values (1, '[-0.200, 0.250, 0.341, -0.211, 0.645, 0.935, -0.316, -0.924]'), (2, '[0.443, -0.501, 0.355, -0.771, 0.707, -0.708, -0.185, 0.362]'), (3, '[0.716, -0.927, 0.134, 0.052, -0.669, 0.793, -0.634, -0.162]'), (4, '[-0.710, 0.330, 0.656, 0.041, -0.990, 0.726, 0.385, -0.958]') 4 rows affected 127.0.0.1:4001> select rowid, distance from vec_examples where sample_embedding match '[0.890, 0.544, 0.825, 0.961, 0.358, 0.0196, 0.521, 0.175]' order by distance limit 2 ``` -------------------------------- ### Get vector type Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Returns the type of the vector as text ('float32', 'int8', or 'bit'). ```sql select vec_type('[.1, .2]'); -- 'float32' select vec_type(X'AABBCCDD'); -- 'float32' select vec_type(vec_int8(X'AABBCCDD')); -- 'int8' select vec_type(vec_bit(X'AABBCCDD')); -- 'bit' select vec_type(X'CCDD'); -- ❌ invalid float32 vector BLOB length. Must be divisible by 4, found 2 ``` -------------------------------- ### vec_quantize_i8 Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Quantizes a vector into an int8 vector. This function supports optional start and end parameters. ```APIDOC ## vec_quantize_i8(vector, [start], [end]) ### Description Quantize a vector into an int8 vector. This function supports optional start and end parameters. ### Method SQL Function ### Parameters - **vector** (vector) - Required - The vector to quantize. - **start** (float) - Optional - The start value for quantization. - **end** (float) - Optional - The end value for quantization. ### Request Example ```sql select 'todo'; ``` ### Response #### Success Response - Returns the quantized int8 vector. ### Response Example ``` 'todo' ``` ``` -------------------------------- ### Get vector length Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Returns the number of elements in a vector, regardless of its type (JSON, BLOB, or constructor result). ```sql select vec_length('[.1, .2]'); -- 2 select vec_length(X'AABBCCDD'); -- 1 select vec_length(vec_int8(X'AABBCCDD')); -- 4 select vec_length(vec_bit(X'AABBCCDD')); -- 32 select vec_length(X'CCDD'); -- ❌ invalid float32 vector BLOB length. Must be divisible by 4, found 2 ``` -------------------------------- ### Creating a vec0 table with binary quantized vectors Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Sets up a `vec0` virtual table to store binary quantized vectors. ```sqlite create virtual table vec_movies using vec0( synopsis_embedding bit[768] ); ``` -------------------------------- ### Creating a vec0 Table with Partition Key Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Define a vec0 virtual table with a 'published_date' column as a partition key to enable time-based filtering of search results. ```sql create virtual table vec_articles using vec0( article_id integer primary key, published_date text partition key, headline_embedding float[1024] ); ``` -------------------------------- ### Query vector version with WASM driver Source: https://alexgarcia.xyz/sqlite-vec/go.html Demonstrates querying the vector version using the WASM-based `ncruces/go-sqlite3` driver and the `sqlite-vec` bindings. This method embeds a custom WASM build of SQLite. ```go package main import ( _ "embed" "log" _ "github.com/asg017/sqlite-vec-go-bindings/ncruces" "github.com/ncruces/go-sqlite3" ) func main() { db, err := sqlite3.Open(":memory:") if err != nil { log.Fatal(err) } stmt, _, err := db.Prepare(`SELECT vec_version()`) if err != nil { log.Fatal(err) } stmt.Step() log.Printf("vec_version=%s\n", stmt.ColumnText(0)) stmt.Close() } ``` -------------------------------- ### Add sqlite-vec with Deno Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to add the sqlite-vec package to your Deno project. ```bash deno add npm:sqlite-vec ``` -------------------------------- ### Compile sqlite-vec on Windows (MinGW) Source: https://alexgarcia.xyz/sqlite-vec/compiling.html Compile the sqlite-vec extension as a shared object for Windows using the MinGW compiler. ```bash gcc -g -shared sqlite-vec.c -o vec0.dll ``` -------------------------------- ### Slice Vector using vec_slice Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Extracts a subset of a vector from `start` (inclusive) to `end` (exclusive). Useful for Matryoshka embeddings. Errors occur for invalid indices, lengths, or bitvector slicing constraints. ```sql select vec_slice('[1, 2,3, 4]', 0, 2); -- X'0000803F00000040' select vec_to_json( vec_slice('[1, 2,3, 4]', 0, 2) ); -- '[1.000000,2.000000]' select vec_to_json( vec_slice('[1, 2,3, 4]', 2, 4) ); -- '[3.000000,4.000000]' select vec_to_json( vec_slice('[1, 2,3, 4]', -1, 4) ); -- ❌ slice 'start' index must be a postive number. select vec_to_json( vec_slice('[1, 2,3, 4]', 0, 5) ); -- ❌ slice 'end' index is greater than the number of dimensions select vec_to_json( vec_slice('[1, 2,3, 4]', 0, 0) ); -- ❌ slice 'start' index is equal to the 'end' index, vectors must have non-zero length ``` -------------------------------- ### Enable 'bundled' feature for rusqlite Source: https://alexgarcia.xyz/sqlite-vec/rust.html Modify your `Cargo.toml` to enable the `bundled` feature for the `rusqlite` crate. This is necessary for `rusqlite` to compile and link against the bundled SQLite library. ```toml # Cargo.toml [dependencies] + rusqlite = { version = "VERSION", features = ["bundled"] } ``` -------------------------------- ### Creating a vec0 table for re-scoring with binary quantization Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Sets up a `vec0` table to store both original float vectors and their binary quantized versions for re-scoring. ```sqlite create virtual table vec_movies using vec0( synopsis_embedding float[768], synopsis_embedding_coarse bit[768] ); ``` -------------------------------- ### Compile sqlite-vec on macOS Source: https://alexgarcia.xyz/sqlite-vec/compiling.html Compile the sqlite-vec extension as a dynamic library for macOS using gcc. ```bash gcc -g -fPIC -dynamiclib sqlite-vec.c -o vec0.dylib ``` -------------------------------- ### Compile sqlite-vec on Windows (MSVC) Source: https://alexgarcia.xyz/sqlite-vec/compiling.html Compile the sqlite-vec extension as a DLL for Windows using the MSVC compiler. ```bash cl sqlite-vec.c -link -dll -out:sqlite-vec.dll ``` -------------------------------- ### Using sqlite-vec with node:sqlite in Node.js Source: https://alexgarcia.xyz/sqlite-vec/js.html Demonstrates using `sqlite-vec` with the built-in `node:sqlite` module in Node.js version 23.5.0 or above. Ensure `allowExtension` is enabled for the database connection. ```javascript import { DatabaseSync } from "node:sqlite"; import * as sqliteVec from "sqlite-vec"; const db = new DatabaseSync(":memory:", { allowExtension: true }); sqliteVec.load(db); const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]); const { result } = db .prepare("select vec_length(?) as result") .get(new Uint8Array(embedding.buffer)); console.log(result); // 4 ``` -------------------------------- ### Compile sqlite-vec on Linux Source: https://alexgarcia.xyz/sqlite-vec/compiling.html Compile the sqlite-vec extension as a shared object for Linux using gcc. ```bash gcc -g -fPIC -shared sqlite-vec.c -o vec0.so ``` -------------------------------- ### Create vec0 Virtual Table Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Use this to create a vec0 virtual table for efficient KNN queries. It requires joining back to source tables for full data retrieval. ```sql create virtual table vec_documents using vec0( document_id integer primary key, contents_embedding float[768] ); insert into vec_documents(document_id, contents_embedding) select id, embed(contents) from documents; ``` -------------------------------- ### Placeholder for i8 Quantization Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html This is a placeholder for the `vec_quantize_i8` function, which is intended for i8 quantization. ```sql select 'todo'; -- 'todo' ``` -------------------------------- ### Creating a vec0 Table with Auxiliary Image Blob Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Define a vec0 virtual table with an auxiliary column '+image' to store image data as a BLOB. This allows retrieval of raw image data in query results. ```sql create virtual table vec_image_chunks using vec0( image_embedding float[1024], +image blob ); ``` -------------------------------- ### Define vec0 Table with Partition Key Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Create a vec0 virtual table using a partition key for sharding. This is useful for large datasets where you need to efficiently query subsets of data based on a specific key, like user ID. ```sql create virtual table vec_documents using vec0( document_id integer primary key, user_id integer partition key, contents_embedding float[1024] ) ``` -------------------------------- ### Add sqlite-vec with Cargo (Rust) Source: https://alexgarcia.xyz/sqlite-vec/installation.html Use this command to add the sqlite-vec Rust crate to your project. ```bash cargo add sqlite-vec ``` -------------------------------- ### Create Table for Manual KNN Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Define a regular table to store vectors manually. Use BLOB type for embeddings and optionally specify dimensions and type with CHECK constraints. ```sql create table documents( id integer primary key, contents text, -- a 4-dimensional floating-point vector contents_embedding blob ); insert into documents values (1, 'alex', vec_f32('[1.1, 1.1, 1.1, 1.1]')), (2, 'brian', vec_f32('[2.2, 2.2, 2.2, 2.2]')), (3, 'craig', vec_f32('[3.3, 3.3, 3.3, 3.3]')); ``` -------------------------------- ### Join vec0 Results with Source Table Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Combine KNN results from a vec0 virtual table with the original data by joining back to the source table. ```sql with knn_matches as ( select document_id, distance from vec_documents where contents_embedding match :query and k = 10 ) select documents.id, documents.contents, knn_matches.distance from knn_matches left join documents on documents.id = knn_matches.document_id ``` -------------------------------- ### Load sqlite-vec SQL Functions into SQLite Source: https://alexgarcia.xyz/sqlite-vec/python.html Load the sqlite-vec SQL functions into an in-memory SQLite database connection. Ensure extensions are enabled before loading and disabled afterward for security. ```python import sqlite3 import sqlite_vec db = sqlite3.connect(":memory:") db.enable_load_extension(True) sqlite_vec.load(db) db.enable_load_extension(False) vec_version, = db.execute("select vec_version()").fetchone() print(f"vec_version={vec_version}") ``` -------------------------------- ### Load sqlite-vec SQL functions into a SQLite connection Source: https://alexgarcia.xyz/sqlite-vec/js.html Use the `sqliteVec.load()` function to integrate sqlite-vec SQL functions into a SQLite connection. This function is compatible with multiple SQLite libraries. ```javascript import * as sqliteVec from "sqlite-vec"; import Database from "better-sqlite3"; const db = new Database(":memory:"); sqliteVec.load(db); const { vec_version } = db .prepare("select vec_version() as vec_version;") .get(); console.log(`vec_version=${vec_version}`); ``` -------------------------------- ### Load sqlite-vec SQL Functions in Ruby Source: https://alexgarcia.xyz/sqlite-vec/ruby.html Load the sqlite-vec SQL functions into an in-memory SQLite database connection using the SqliteVec.load() method. Ensure to enable and disable load extensions appropriately. ```ruby require 'sqlite3' require 'sqlite_vec' db = SQLite3::Database.new(':memory:') db.enable_load_extension(true) SqliteVec.load(db) db.enable_load_extension(false) result = db.execute('SELECT vec_version()') puts result.first.first ``` -------------------------------- ### Using vec_quantize_binary() in sqlite-vec Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Applies binary quantization to a vector using the `vec_quantize_binary()` SQL function. ```sqlite select vec_quantize_binary( '[-0.73, -0.80, 0.12, -0.73, 0.79, -0.11, 0.23, 0.97]' ); -- X'd4` ``` -------------------------------- ### Manual KNN Query with L2 Distance Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Perform a brute-force KNN query by calculating distances manually using `vec_distance_L2` and ordering the results. ```sql select id, contents, vec_distance_L2(contents_embedding, '[2.2, 2.2, 2.2, 2.2]') as distance from documents order by distance; /* ┌────┬──────────┬──────────────────┐ │ id │ contents │ distance │ ├────┼──────────┼──────────────────┤ │ 2 │ 'brian' │ 0.0 │ │ 3 │ 'craig' │ 2.19999980926514 │ │ 1 │ 'alex' │ 2.20000004768372 │ └────┴──────────┴──────────────────┘ */ ``` -------------------------------- ### Quantize Vectors to Float16 and Int8 Source: https://alexgarcia.xyz/sqlite-vec/guides/scalar-quant.html Use `vec_quantize_float16` and `vec_quantize_int8` for direct float16 and int8 quantization. The 'unit' parameter specifies the quantization mode. ```sql select vec_quantize_float16(vec_f32('[]'), 'unit'); ``` ```sql select vec_quantize_int8(vec_f32('[]'), 'unit'); ``` -------------------------------- ### Querying binary quantized vectors in vec0 Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Searches a `vec0` table using a binary quantized query vector. ```sqlite select rowid, distance from vec_movies where synopsis_embedding match vec_quantize_binary(:query) order by distance limit 20; ``` -------------------------------- ### Creating a vec0 Table with Auxiliary Text Content Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Define a vec0 virtual table including an auxiliary column '+contents' to store text metadata alongside embeddings. This metadata can be retrieved in query results. ```sql create virtual table vec_chunks using vec0( contents_embedding float[1024], +contents text ); ``` -------------------------------- ### Create vec0 Virtual Table with Cosine Distance Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Create a vec0 virtual table specifying the cosine distance metric. Queries will then use cosine distance by default. ```sql create virtual table vec_documents using vec0( document_id integer primary key, contents_embedding float[768] distance_metric=cosine ); -- insert vectors into vec_documents... -- this MATCH will now use cosine distance instead of the default L2 distance select document_id, distance from vec_documents where contents_embedding match :query and k = 10; ``` -------------------------------- ### Inserting data for re-scoring with binary quantization Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Inserts data into a `vec0` table, storing both the original vector and its binary quantized representation for re-scoring. ```sqlite insert into vec_movies(rowid, synopsis_embedding, synopsis_embedding_coarse) VALUES (:id, :vector, vec_quantize_binary(:vector)); ``` -------------------------------- ### Inserting binary quantized vectors into vec0 Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Inserts data into a `vec0` table, applying binary quantization to the vector. ```sqlite insert into vec_movies(rowid, synopsis_embedding) VALUES (:id, vec_quantize_binary(:vector)); ``` -------------------------------- ### Bind vector embeddings to sqlite-vec SQL functions Source: https://alexgarcia.xyz/sqlite-vec/js.html When vectors are represented as arrays of numbers, wrap them in a `Float32Array` and use the `.buffer` accessor to bind them as parameters to `sqlite-vec` SQL functions. ```javascript const embedding = new Float32Array([0.1, 0.2, 0.3, 0.4]); const stmt = db.prepare("select vec_length(?)"); console.log(stmt.run(embedding.buffer)); // 4 ``` -------------------------------- ### Create bit vector from BLOB Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Creates a bit vector from a BLOB. The resulting BLOB has 1 byte per 8 elements with subtype 224. ```sql select vec_bit(X'F0'); -- X'F0' select subtype(vec_bit(X'F0')); -- 224 select vec_to_json(vec_bit(X'F0')); -- '[0,0,0,0,1,1,1,1]' ``` -------------------------------- ### vec_bit(vector) Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Creates a binary vector from a BLOB. The returned value is a BLOB with 1 byte per 8 elements, with a special subtype of `224`. ```APIDOC ## `vec_bit(vector)` ### Description Creates a binary vector from a BLOB. The returned value is a BLOB with 1 byte per 8 elements, with a special subtype of `224`. ### SQL Example ```sql select vec_bit(X'F0'); -- X'F0' select subtype(vec_bit(X'F0')); -- 224 select vec_to_json(vec_bit(X'F0')); -- '[0,0,0,0,1,1,1,1]' ``` ``` -------------------------------- ### Define vec0 Table with Metadata Columns Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Create a vec0 virtual table including metadata columns for filtering. These columns can be used in WHERE clauses of KNN queries to refine search results. ```sql create virtual table vec_movies using vec0( movie_id integer primary key, synopsis_embedding float[1024], genre text, num_reviews int, mean_rating float, contains_violence boolean ); ``` -------------------------------- ### Quantize Vector to Binary Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Use `vec_quantize_binary` to convert float32 or int8 vectors to bitvectors. Positive numbers become '1', negative numbers become '0'. Vector length must be divisible by 8. ```sql select vec_quantize_binary('[1, 2, 3, 4, 5, 6, 7, 8]'); -- X'FF' select vec_quantize_binary('[1, 2, 3, 4, -5, -6, -7, -8]'); -- X'0F' select vec_quantize_binary('[-1, -2, -3, -4, -5, -6, -7, -8]'); -- X'00' select vec_quantize_binary('[-1, -2, -3, -4, -5, -6, -7, -8]'); -- X'00' select vec_quantize_binary(vec_int8(X'11223344')); -- ❌ Binary quantization requires vectors with a length divisible by 8 select vec_quantize_binary(vec_bit(X'FF')); -- ❌ Can only binary quantize float or int8 vectors ``` -------------------------------- ### KNN Query with vec0 (k parameter) Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Perform a KNN query using the 'k' parameter with a vec0 virtual table. This method is compatible with SQLite versions prior to 3.41. ```sql select document_id, distance from vec_documents where contents_embedding match :query and k = 10; ``` -------------------------------- ### Pass Vec to sqlite-vec using zerocopy Source: https://alexgarcia.xyz/sqlite-vec/rust.html When working with `Vec` in Rust, use the `zerocopy` crate, specifically `AsBytes`, to efficiently pass vectors as bytes to `sqlite-vec` without requiring extra data copying. This is useful for functions like `vec_length`. ```rust let query: Vec = vec![0.1, 0.2, 0.3, 0.4]; let mut stmt = db.prepare("SELECT vec_length(?)")?; stmt.execute(&[item.1.as_bytes()])?; ``` -------------------------------- ### General Vector Quantization with Data Type Specification Source: https://alexgarcia.xyz/sqlite-vec/guides/scalar-quant.html The `vec_quantize` function allows specifying the target data type ('float16', 'int8', 'bit') and the vector data. Use 'sqf16' and 'sqi8' for scalar quantization variants. ```sql select vec_quantize('float16', vec_f32('...')); ``` ```sql select vec_quantize('int8', vec_f32('...')); ``` ```sql select vec_quantize('bit', vec_f32('...')); ``` ```sql select vec_quantize('sqf16', vec_f32('...')); ``` ```sql select vec_quantize('sqi8', vec_f32('...')); ``` ```sql select vec_quantize('bq2', vec_f32('...')); ``` -------------------------------- ### Create int8 vector from JSON or BLOB Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Creates an int8 vector from JSON text or a BLOB. JSON elements must be between -128 and 127. The resulting BLOB has 1 byte per element with subtype 225. ```sql select vec_int8('[1, 2, 3, 4]'); -- X'01020304' select subtype(vec_int8('[1, 2, 3, 4]')); -- 225 select vec_int8(X'AABBCCDD'); -- X'AABBCCDD' select vec_to_json(vec_int8(X'AABBCCDD')); -- '[-86,-69,-52,-35]' select vec_int8('[999]'); -- ❌ JSON parsing error: value out of range for int8 ``` -------------------------------- ### Re-scoring using binary quantized vectors Source: https://alexgarcia.xyz/sqlite-vec/guides/binary-quant.html Performs a search using binary quantized vectors for initial filtering, followed by re-scoring with the original float vectors. ```sqlite with coarse_matches as ( select rowid, synopsis_embedding from vec_movies where synopsis_embedding_coarse match vec_quantize_binary(:query) order by distance limit 20 * 8 ), select rowid, vec_distance_L2(synopsis_embedding, :query) from coarse_matches order by 2 limit 20; ``` -------------------------------- ### vec_int8(vector) Source: https://alexgarcia.xyz/sqlite-vec/api-reference.html Creates a 8-bit integer vector from a BLOB or JSON text. If a BLOB is provided, the length must be divisible by 4, as a float takes up 4 bytes of space each. If JSON text is provided, each element must be an integer between -128 and 127 inclusive. The returned value is a BLOB with 1 byte per element, with a special subtype of `225`. ```APIDOC ## `vec_int8(vector)` ### Description Creates a 8-bit integer vector from a BLOB or JSON text. If a BLOB is provided, the length must be divisible by 4, as a float takes up 4 bytes of space each. If JSON text is provided, each element must be an integer between -128 and 127 inclusive. The returned value is a BLOB with 1 byte per element, with a special subtype of `225`. ### SQL Example ```sql select vec_int8('[1, 2, 3, 4]'); -- X'01020304' select subtype(vec_int8('[1, 2, 3, 4]')); -- 225 select vec_int8(X'AABBCCDD'); -- X'AABBCCDD' select vec_to_json(vec_int8(X'AABBCCDD')); -- '[-86,-69,-52,-35]' select vec_int8('[999]'); -- ❌ JSON parsing error: value out of range for int8 ``` ``` -------------------------------- ### Check SQLite Version in Python Source: https://alexgarcia.xyz/sqlite-vec/python.html Check the version of the SQLite library used by your Python environment. This is useful for ensuring compatibility with features that require newer SQLite versions. ```bash python -c 'import sqlite3; print(sqlite3.sqlite_version)' ``` -------------------------------- ### KNN Query with vec0 (LIMIT parameter) Source: https://alexgarcia.xyz/sqlite-vec/features/knn.html Perform a KNN query using the LIMIT clause with a vec0 virtual table. This method requires SQLite version 3.41 or later. ```sql -- This example ONLY works in SQLite versions 3.41+ -- Otherwise, use the `k = 10` method described above! select document_id, distance from vec_documents where contents_embedding match :query limit 10; -- LIMIT only works on SQLite versions 3.41+ ``` -------------------------------- ### Serialize float32 vectors for sqlite-vec Source: https://alexgarcia.xyz/sqlite-vec/go.html Serializes a slice of float32 into the compact BLOB format expected by sqlite-vec using `SerializeFloat32`. This is useful when inserting vectors into the database. ```go values := []float32{0.1, 0.1, 0.1, 0.1} v, err := sqlite_vec.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) } ``` -------------------------------- ### KNN Query with Date Range Constraint Source: https://alexgarcia.xyz/sqlite-vec/features/vec0.html Perform a KNN search on the vec_articles table, filtering results by a specific date range using the 'published_date' partition key. ```sql select article_id, published_date, distance from vec_articles where headline_embedding match :query and published_date between '2009-01-20' and '2017-01-20'; -- Obama administration ```