Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

pgvector examples for Zig

License

NotificationsYou must be signed in to change notification settings

pgvector/pgvector-zig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector examples for Zig

Supportspg.zig andlibpq

Build Status

Getting Started

Follow the instructions for your database library:

Or check out some examples:

pg.zig

Enable the extension

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

Create a table

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

Insert vectors

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

Get the nearest neighbors

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

Add an approximate index

_=tryconn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", .{});// or_=tryconn.exec("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)", .{});

Usevector_ip_ops for inner product andvector_cosine_ops for cosine distance

See afull example

libpq

Import libpq

constpg=@cImport({@cInclude("libpq-fe.h");});

Enable the extension

constres=pg.PQexec(conn,"CREATE EXTENSION IF NOT EXISTS vector");

Create a table

constres=pg.PQexec(conn,"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");

Insert vectors

constparamValues= [2:0][*c]constu8{"[1,2,3]","[4,5,6]" };constres=pg.PQexecParams(conn,"INSERT INTO items (embedding) VALUES ($1), ($2)",2,null,&paramValues,null,null,0);

Get the nearest neighbors

constparamValues= [1:0][*c]constu8{"[3,1,2]"};constres=pg.PQexecParams(conn,"SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5",1,null,&paramValues,null,null,0);

Add an approximate index

constres=pg.PQexec(conn,"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");// orconstres=pg.PQexec(conn,"CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)");

Usevector_ip_ops for inner product andvector_cosine_ops for cosine distance

See afull 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.gitcd pgvector-zigcreatedb pgvector_zig_testzig buildzig-out/bin/pgzig-out/bin/libpq

Specify the path to libpq if needed:

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

To run an example:

createdb pgvector_examplezig-out/bin/openai

About

pgvector examples for Zig

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp