Skip to content

Commit bf7c119

Browse files
committed
First commit
0 parents  commit bf7c119

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

.github/workflows/build.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
- run: sudo apt update && sudo apt install dub ldc
9+
- uses: ankane/setup-postgres@v1
10+
with:
11+
database: pgvector_d_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: dub run

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/dub.selections.json
2+
/pgvector

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: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# pgvector-d
2+
3+
[pgvector](https://github.com/pgvector/pgvector) examples for D
4+
5+
Supports [dpq2](https://github.com/denizzzka/dpq2)
6+
7+
[![Build Status](https://github.com/pgvector/pgvector-d/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-d/actions)
8+
9+
## Getting Started
10+
11+
Follow the instructions for your database library:
12+
13+
- [dpq2](#dpq2)
14+
15+
## dpq2
16+
17+
Enable the extension
18+
19+
```d
20+
conn.exec("CREATE EXTENSION IF NOT EXISTS vector");
21+
```
22+
23+
Create a table
24+
25+
```d
26+
conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
27+
```
28+
29+
Insert vectors
30+
31+
```d
32+
QueryParams p;
33+
p.sqlCommand = "INSERT INTO items (embedding) VALUES ($1::vector), ($2::vector)";
34+
p.argsVariadic("[1,2,3]", "[4,5,6]");
35+
conn.execParams(p);
36+
```
37+
38+
Get the nearest neighbors
39+
40+
```d
41+
QueryParams p;
42+
p.sqlCommand = "SELECT * FROM items ORDER BY embedding <-> $1::vector LIMIT 5";
43+
p.argsVariadic("[3,1,2]");
44+
p.resultFormat = ValueFormat.TEXT;
45+
auto r = conn.execParams(p);
46+
foreach (row; rangify(r)) {
47+
writeln(row);
48+
}
49+
```
50+
51+
Add an approximate index
52+
53+
```d
54+
conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");
55+
// or
56+
conn.exec("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)");
57+
```
58+
59+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
60+
61+
See a [full example](source/app.d)
62+
63+
## Contributing
64+
65+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
66+
67+
- [Report bugs](https://github.com/pgvector/pgvector-d/issues)
68+
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-d/pulls)
69+
- Write, clarify, or fix documentation
70+
- Suggest or add new features
71+
72+
To get started with development:
73+
74+
```sh
75+
git clone https://github.com/pgvector/pgvector-d.git
76+
cd pgvector-d
77+
createdb pgvector_d_test
78+
dub run
79+
```
80+
81+
Specify the path to libpq if needed:
82+
83+
```sh
84+
DFLAGS="-L-L/opt/homebrew/opt/libpq/lib" dub run
85+
```

dub.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "pgvector",
3+
"dependencies": {
4+
"dpq2": "~>1.1.7"
5+
}
6+
}

source/app.d

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import dpq2;
2+
import std.stdio;
3+
4+
void main()
5+
{
6+
Connection conn = new Connection("postgres://localhost/pgvector_d_test");
7+
8+
conn.exec("CREATE EXTENSION IF NOT EXISTS vector");
9+
conn.exec("DROP TABLE IF EXISTS items");
10+
conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
11+
12+
QueryParams p;
13+
p.sqlCommand = "INSERT INTO items (embedding) VALUES ($1::vector), ($2::vector), ($3::vector)";
14+
p.argsVariadic("[1,1,1]", "[2,2,2]", "[1,1,2]");
15+
conn.execParams(p);
16+
17+
p.sqlCommand = "SELECT * FROM items ORDER BY embedding <-> $1::vector LIMIT 5";
18+
p.argsVariadic("[1,1,1]");
19+
p.resultFormat = ValueFormat.TEXT;
20+
auto r = conn.execParams(p);
21+
foreach (row; rangify(r)) {
22+
writeln(row);
23+
}
24+
25+
conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)");
26+
27+
conn.destroy();
28+
}

0 commit comments

Comments
 (0)