pgvector examples for Zig
Supports libpq
Follow the instructions for your database library:
Import libpq
const pg = @cImport({
@cInclude("libpq-fe.h");
});
Enable the extension
const res = pg.PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector");
Create a table
const res = pg.PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
Insert vectors
const paramValues = [2:0][*c]const u8 {"[1,2,3]", "[4,5,6]"};
const res = pg.PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2)", 2, null, ¶mValues, null, null, 0);
Get the nearest neighbors
const paramValues = [1:0][*c]const u8 {"[3,1,2]"};
const res = pg.PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, null, ¶mValues, null, null, 0);
Add an approximate index
const res = pg.PQexec(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");
// or
const res = pg.PQexec(conn, "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)");
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/pgvector/pgvector-zig.git
cd pgvector-zig
createdb pgvector_zig_test
zig run example.zig -lc -lpq
Specify the path to libpq if needed:
zig run example.zig -I/opt/homebrew/opt/libpq/include -L/opt/homebrew/opt/libpq/lib -lc -lpq