Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Dec 6, 2024
0 parents commit 82fca57
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/build.yml
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
84 changes: 84 additions & 0 deletions README.md
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
```
62 changes: 62 additions & 0 deletions app/main.f90
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
10 changes: 10 additions & 0 deletions fpm.toml
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

0 comments on commit 82fca57

Please sign in to comment.