Skip to content

pgvector/pgvector-raku

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-raku

pgvector examples for Raku

Supports DBIish and DB::Pg

Build Status

Getting Started

Follow the instructions for your database library:

DBIish

Enable the extension

$dbh.execute('CREATE EXTENSION IF NOT EXISTS vector');

Create a table

$dbh.execute('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');

Insert vectors

my $embedding1 = '[1,1,1]';
my $embedding2 = '[2,2,2]';
my $embedding3 = '[1,1,2]';
$dbh.execute('INSERT INTO items (embedding) VALUES (?), (?), (?)', $embedding1, $embedding2, $embedding3);

Get the nearest neighbors

my $embedding = '[1,1,1]';
.say for $dbh.execute('SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5', $embedding).allrows();

Add an approximate index

$dbh.execute('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
# or
$dbh.execute('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

DB::Pg

Enable the extension

$pg.query('CREATE EXTENSION IF NOT EXISTS vector');

Create a table

$pg.query('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');

Insert vectors

my $embedding1 = '[1,1,1]';
my $embedding2 = '[2,2,2]';
my $embedding3 = '[1,1,2]';
$pg.query('INSERT INTO items (embedding) VALUES ($1), ($2), ($3)', $embedding1, $embedding2, $embedding3);

Get the nearest neighbors

my $embedding = '[1,1,1]';
.say for $pg.query('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', $embedding).arrays;

Add an approximate index

$pg.query('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
# or
$pg.query('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-raku.git
cd pgvector-raku
createdb pgvector_raku_test
zef install DBIish DB::Pg
raku dbiish.raku
raku dbpg.raku

About

pgvector examples for Raku

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages