Skip to content

Commit 8da2a9d

Browse files
committed
First commit
0 parents  commit 8da2a9d

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

.github/workflows/build.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: build
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- uses: Raku/setup-raku@v1
9+
- uses: ankane/setup-postgres@v1
10+
with:
11+
database: pgvector_raku_test
12+
dev-files: true
13+
- run: |
14+
cd /tmp
15+
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git
16+
cd pgvector
17+
make
18+
sudo make install
19+
- run: zef install DB::Pg
20+
- run: raku dbiish.raku
21+
- run: raku dbpg.raku

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2024 Andrew Kane
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# pgvector-raku
2+
3+
[pgvector](https://github.com/pgvector/pgvector) examples for Raku
4+
5+
Supports [DBIish](https://github.com/raku-community-modules/DBIish) and [DB::Pg](https://github.com/CurtTilmes/raku-dbpg)
6+
7+
[![Build Status](https://github.com/pgvector/pgvector-raku/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-raku/actions)
8+
9+
## Getting Started
10+
11+
Follow the instructions for your database library:
12+
13+
- [DBIish](#dbiish)
14+
- [DB::Pg](#dbpg)
15+
16+
## DBIish
17+
18+
Enable the extension
19+
20+
```raku
21+
$dbh.execute('CREATE EXTENSION IF NOT EXISTS vector');
22+
```
23+
24+
Create a table
25+
26+
```raku
27+
$dbh.execute('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
28+
```
29+
30+
Insert vectors
31+
32+
```raku
33+
my $embedding1 = '[1,1,1]';
34+
my $embedding2 = '[2,2,2]';
35+
my $embedding3 = '[1,1,2]';
36+
$dbh.execute('INSERT INTO items (embedding) VALUES (?), (?), (?)', $embedding1, $embedding2, $embedding3);
37+
```
38+
39+
Get the nearest neighbors
40+
41+
```raku
42+
my $embedding = '[1,1,1]';
43+
.say for $dbh.execute('SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5', $embedding).allrows();
44+
```
45+
46+
Add an approximate index
47+
48+
```raku
49+
$dbh.execute('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
50+
# or
51+
$dbh.execute('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
52+
```
53+
54+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
55+
56+
See a [full example](dbiish.raku)
57+
58+
## DB::Pg
59+
60+
Enable the extension
61+
62+
```raku
63+
$pg.query('CREATE EXTENSION IF NOT EXISTS vector');
64+
```
65+
66+
Create a table
67+
68+
```raku
69+
$pg.query('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
70+
```
71+
72+
Insert vectors
73+
74+
```raku
75+
my $embedding1 = '[1,1,1]';
76+
my $embedding2 = '[2,2,2]';
77+
my $embedding3 = '[1,1,2]';
78+
$pg.query('INSERT INTO items (embedding) VALUES ($1), ($2), ($3)', $embedding1, $embedding2, $embedding3);
79+
```
80+
81+
Get the nearest neighbors
82+
83+
```raku
84+
my $embedding = '[1,1,1]';
85+
.say for $pg.query('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', $embedding).arrays;
86+
```
87+
88+
Add an approximate index
89+
90+
```raku
91+
$pg.query('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');
92+
# or
93+
$pg.query('CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)');
94+
```
95+
96+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
97+
98+
See a [full example](dbpg.raku)
99+
100+
## Contributing
101+
102+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
103+
104+
- [Report bugs](https://github.com/pgvector/pgvector-raku/issues)
105+
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-raku/pulls)
106+
- Write, clarify, or fix documentation
107+
- Suggest or add new features
108+
109+
To get started with development:
110+
111+
```sh
112+
git clone https://github.com/pgvector/pgvector-raku.git
113+
cd pgvector-raku
114+
createdb pgvector_raku_test
115+
zef install DB::Pg
116+
raku dbiish.raku
117+
raku dbpg.raku
118+
```

dbiish.raku

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use v6;
2+
use DBIish;
3+
4+
my $dbh = DBIish.connect("Pg", :database<pgvector_raku_test>);
5+
6+
$dbh.execute('CREATE EXTENSION IF NOT EXISTS vector');
7+
$dbh.execute('DROP TABLE IF EXISTS items');
8+
$dbh.execute('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
9+
10+
my $embedding1 = '[1,1,1]';
11+
my $embedding2 = '[2,2,2]';
12+
my $embedding3 = '[1,1,2]';
13+
$dbh.execute('INSERT INTO items (embedding) VALUES (?), (?), (?)', $embedding1, $embedding2, $embedding3);
14+
15+
my $embedding = '[1,1,1]';
16+
.say for $dbh.execute('SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5', $embedding).allrows();
17+
18+
$dbh.execute('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');

dbpg.raku

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use DB::Pg;
2+
3+
my $pg = DB::Pg.new(conninfo => 'dbname=pgvector_raku_test');
4+
5+
$pg.query('CREATE EXTENSION IF NOT EXISTS vector');
6+
$pg.query('DROP TABLE IF EXISTS items');
7+
$pg.query('CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))');
8+
9+
my $embedding1 = '[1,1,1]';
10+
my $embedding2 = '[2,2,2]';
11+
my $embedding3 = '[1,1,2]';
12+
$pg.query('INSERT INTO items (embedding) VALUES ($1), ($2), ($3)', $embedding1, $embedding2, $embedding3);
13+
14+
my $embedding = '[1,1,1]';
15+
.say for $pg.query('SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5', $embedding).arrays;
16+
17+
$pg.query('CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)');

0 commit comments

Comments
 (0)