Skip to content

Commit aeee4bf

Browse files
committed
First commit
0 parents  commit aeee4bf

7 files changed

Lines changed: 161 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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: erlef/setup-beam@v1
9+
with:
10+
otp-version: 26
11+
- uses: ankane/setup-postgres@v1
12+
with:
13+
database: pgvector_erlang_test
14+
dev-files: true
15+
- run: |
16+
cd /tmp
17+
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git
18+
cd pgvector
19+
make
20+
sudo make install
21+
- run: rebar3 escriptize
22+
- run: _build/default/bin/example

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
_build
2+
*.beam
3+
*.lock
4+
erl_crash.dump

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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# pgvector-erlang
2+
3+
[pgvector](https://github.com/pgvector/pgvector) support for Erlang
4+
5+
Supports [epgsql](https://github.com/epgsql/epgsql)
6+
7+
[![Build Status](https://github.com/pgvector/pgvector-erlang/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-erlang/actions)
8+
9+
## Getting Started
10+
11+
Follow the instructions for your database library:
12+
13+
- [epgsql](#epgsql)
14+
15+
## epgsql
16+
17+
Enable the extension
18+
19+
```erlang
20+
epgsql:equery(C, "CREATE EXTENSION IF NOT EXISTS vector"),
21+
```
22+
23+
Create a table
24+
25+
```erlang
26+
epgsql:equery(C, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"),
27+
```
28+
29+
Insert vectors
30+
31+
```erlang
32+
epgsql:equery(C, "INSERT INTO items (embedding) VALUES ($1), ($2)", ["[1,2,3]", "[4,5,6]"]),
33+
```
34+
35+
Get the nearest neighbors
36+
37+
```erlang
38+
epgsql:equery(C, "SELECT id FROM items ORDER BY embedding <-> $1 LIMIT 5", ["[3,1,2]"]),
39+
```
40+
41+
Add an approximate index
42+
43+
```erlang
44+
epgsql:equery(C, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"),
45+
% or
46+
epgsql:equery(C, "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)"),
47+
```
48+
49+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
50+
51+
See a [full example](src/example.erl)
52+
53+
## Contributing
54+
55+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
56+
57+
- [Report bugs](https://github.com/pgvector/pgvector-erlang/issues)
58+
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-erlang/pulls)
59+
- Write, clarify, or fix documentation
60+
- Suggest or add new features
61+
62+
To get started with development:
63+
64+
```sh
65+
git clone https://github.com/pgvector/pgvector-erlang.git
66+
cd pgvector-erlang
67+
createdb pgvector_erlang_test
68+
rebar3 escriptize
69+
_build/default/bin/example
70+
```

rebar.config

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{erl_opts, [no_debug_info]}.
2+
{deps, [{epgsql, "4.7.1"}]}.
3+
4+
{escript_incl_apps, [example]}.
5+
{escript_main_app, example}.
6+
{escript_name, example}.
7+
{escript_emu_args, "%%! +sbtu +A1\n"}.
8+
9+
{profiles, [
10+
{test, [
11+
{erl_opts, [debug_info]}
12+
]}
13+
]}.

src/example.app.src

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{application, example, [
2+
{description, "pgvector examples for Erlang"},
3+
{vsn, "0.1.0"},
4+
{registered, []},
5+
{applications, [
6+
epgsql,
7+
kernel,
8+
stdlib
9+
]},
10+
{env, []},
11+
{modules, []},
12+
{links, []}
13+
]}.

src/example.erl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-module(example).
2+
-export([main/1]).
3+
4+
main(_Args) ->
5+
{ok, C} = epgsql:connect(#{
6+
host => "localhost",
7+
username => os:getenv("USER"),
8+
database => "pgvector_erlang_test"
9+
}),
10+
{ok, _, _} = epgsql:equery(C, "CREATE EXTENSION IF NOT EXISTS vector"),
11+
{ok, _, _} = epgsql:equery(C, "DROP TABLE IF EXISTS items"),
12+
{ok, _, _} = epgsql:equery(C, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"),
13+
{ok, _} = epgsql:equery(C, "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", ["[1,1,1]", "[2,2,2]", "[1,1,2]"]),
14+
{ok, _Count, Rows} = epgsql:equery(C, "SELECT id FROM items ORDER BY embedding <-> $1 LIMIT 5", ["[1,1,1]"]),
15+
io:format("~p~n", [Rows]),
16+
{ok, _, _} = epgsql:equery(C, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"),
17+
ok = epgsql:close(C),
18+
erlang:halt(0).

0 commit comments

Comments
 (0)