Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

pgvector/pgvector-zig

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-zig

pgvector examples for Zig

Supports pg.zig and libpq

Build Status

Getting Started

Follow the instructions for your database library:

Or check out some examples:

pg.zig

Enable the extension

_ = try conn.exec("CREATE EXTENSION IF NOT EXISTS vector", .{});

Create a table

_ = try conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))", .{});

Insert vectors

const embedding1 = [_]f32{ 1, 2, 3 };
const embedding2 = [_]f32{ 4, 5, 6 };
_ = try conn.exec("INSERT INTO items (embedding) VALUES ($1::float4[]), ($2::float4[])", .{ embedding1, embedding2 });

Get the nearest neighbors

const embedding3 = [_]f32{ 3, 1, 2 };
var result = try conn.query("SELECT id FROM items ORDER BY embedding <-> $1::float4[]::vector LIMIT 5", .{embedding3});

Add an approximate index

_ = try conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", .{});
// or
_ = try conn.exec("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

libpq

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, &paramValues, 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, &paramValues, 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

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector-zig.git
cd pgvector-zig
createdb pgvector_zig_test
zig build
zig-out/bin/pg
zig-out/bin/libpq

Specify the path to libpq if needed:

zig build --search-prefix /opt/homebrew/opt/libpq

To run an example:

createdb pgvector_example
zig-out/bin/openai

About

pgvector examples for Zig

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.