-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.pl
More file actions
27 lines (21 loc) · 880 Bytes
/
example.pl
File metadata and controls
27 lines (21 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use strict;
use DBI;
my $dbh = DBI->connect('dbi:Pg:dbname=pgvector_perl_test', '', '', {AutoCommit => 1});
$dbh->do('CREATE EXTENSION IF NOT EXISTS vector');
$dbh->do('DROP TABLE IF EXISTS items');
$dbh->do('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
sub vector {
return '[' . join(',', @{$_[0]}) . ']';
}
my $sth = $dbh->prepare('INSERT INTO items (embedding) VALUES ($1), ($2), ($3)');
my @embedding1 = (1, 1, 1);
my @embedding2 = (2, 2, 2);
my @embedding3 = (1, 1, 2);
$sth->execute(vector(\@embedding1), vector(\@embedding2), vector(\@embedding3));
my $sth = $dbh->prepare('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5');
my @embedding = (1, 1, 1);
$sth->execute(vector(\@embedding));
while (my @row = $sth->fetchrow_array()) {
print($row[1] . "\n");
}
$dbh->do('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');