-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 82fca57
Showing
6 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: build | ||
on: [push, pull_request] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ankane/setup-postgres@v1 | ||
with: | ||
database: pgvector_fortran_test | ||
dev-files: true | ||
- run: | | ||
cd /tmp | ||
git clone --branch v0.8.0 https://github.com/pgvector/pgvector.git | ||
cd pgvector | ||
make | ||
sudo make install | ||
- uses: fortran-lang/setup-fpm@v5 | ||
with: | ||
fpm-version: 0.10.1 | ||
- run: fpm run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2024 Andrew Kane | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# pgvector-fortran | ||
|
||
[pgvector](https://github.com/pgvector/pgvector) support for Fortran | ||
|
||
Supports [Libpq-Fortran](https://github.com/ShinobuAmasaki/libpq-fortran) | ||
|
||
[![Build Status](https://github.com/pgvector/pgvector-fortran/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-fortran/actions) | ||
|
||
## Getting Started | ||
|
||
Follow the instructions for your database library: | ||
|
||
- [Libpq-Fortran](#libpq-fortran) | ||
|
||
## Libpq-Fortran | ||
|
||
Enable the extension | ||
|
||
```fortran | ||
type(c_ptr) :: res | ||
res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector") | ||
``` | ||
|
||
Create a table | ||
|
||
```fortran | ||
res = PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))") | ||
``` | ||
|
||
Insert vectors | ||
|
||
```fortran | ||
character(256) :: values(2) | ||
values(1) = "[1,2,3]" | ||
values(2) = "[4,5,6]" | ||
res = PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2)", 2, [0, 0], values) | ||
``` | ||
|
||
Get the nearest neighbors | ||
|
||
```fortran | ||
character(256) :: values(1) | ||
values(1) = "[3,1,2]" | ||
res = PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, [0], values) | ||
``` | ||
|
||
Add an approximate index | ||
|
||
```fortran | ||
res = PQexec(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)") | ||
! or | ||
res = PQexec(conn, "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](app/main.f90) | ||
|
||
## Contributing | ||
|
||
Everyone is encouraged to help improve this project. Here are a few ways you can help: | ||
|
||
- [Report bugs](https://github.com/pgvector/pgvector-fortran/issues) | ||
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-fortran/pulls) | ||
- Write, clarify, or fix documentation | ||
- Suggest or add new features | ||
|
||
To get started with development: | ||
|
||
```sh | ||
git clone https://github.com/pgvector/pgvector-fortran.git | ||
cd pgvector-fortran | ||
createdb pgvector_fortran_test | ||
fpm run | ||
``` | ||
|
||
Specify the path to libpq if needed: | ||
|
||
```sh | ||
FPM_CFLAGS="-I/opt/homebrew/opt/libpq/include" FPM_LDFLAGS="-L/opt/homebrew/opt/libpq/lib" fpm run | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
program main | ||
use iso_c_binding | ||
use libpq | ||
implicit none | ||
|
||
type(c_ptr) :: conn | ||
type(c_ptr) :: res | ||
character(256) :: values(3) | ||
integer :: ntuples | ||
integer :: i | ||
|
||
conn = PQconnectdb("postgres://localhost/pgvector_fortran_test") | ||
if (PQstatus(conn) /= CONNECTION_OK) then | ||
stop 1 | ||
endif | ||
|
||
res = PQexec(conn, "CREATE EXTENSION IF NOT EXISTS vector") | ||
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then | ||
stop 1 | ||
endif | ||
call PQclear(res) | ||
|
||
res = PQexec(conn, "DROP TABLE IF EXISTS items") | ||
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then | ||
stop 1 | ||
endif | ||
call PQclear(res) | ||
|
||
res = PQexec(conn, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))") | ||
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then | ||
stop 1 | ||
endif | ||
call PQclear(res) | ||
|
||
values(1) = "[1,1,1]" | ||
values(2) = "[2,2,2]" | ||
values(3) = "[1,1,2]" | ||
res = PQexecParams(conn, "INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", 3, [0, 0, 0], values) | ||
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then | ||
stop 1 | ||
endif | ||
call PQclear(res) | ||
|
||
values(1) = "[1,1,1]" | ||
res = PQexecParams(conn, "SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", 1, [0], values) | ||
if (PQresultStatus(res) /= PGRES_TUPLES_OK) then | ||
stop 1 | ||
endif | ||
ntuples = PQntuples(res) | ||
do i = 0, ntuples - 1 | ||
print *, PQgetvalue(res, i, 0), ": ", PQgetvalue(res, i, 1) | ||
end do | ||
call PQclear(res) | ||
|
||
res = PQexec(conn, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)") | ||
if (PQresultStatus(res) /= PGRES_COMMAND_OK) then | ||
stop 1 | ||
endif | ||
call PQclear(res) | ||
|
||
call PQfinish(conn) | ||
end program main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name = "pgvector" | ||
|
||
[build] | ||
link = ["pq"] | ||
|
||
[dependencies] | ||
libpq-fortran = { git = "https://github.com/ankane/libpq-fortran", branch = "fixes" } | ||
|
||
[install] | ||
library = false |