Skip to content

Commit 980efe6

Browse files
committed
First commit
0 parents  commit 980efe6

6 files changed

Lines changed: 131 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: build
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
timeout-minutes: 5
7+
services:
8+
postgres:
9+
image: pgvector/pgvector:pg18
10+
env:
11+
POSTGRES_HOST_AUTH_METHOD: password
12+
POSTGRES_PASSWORD: secret
13+
POSTGRES_DB: pgvector_prolog_test
14+
options: >-
15+
--health-cmd pg_isready
16+
--health-interval 10s
17+
--health-timeout 5s
18+
--health-retries 5
19+
ports:
20+
- 5432:5432
21+
steps:
22+
- uses: actions/checkout@v5
23+
- run: echo /home/linuxbrew/.linuxbrew/bin >> $GITHUB_PATH
24+
- run: brew install scryer-prolog
25+
- run: scryer-prolog example.pl -g "main,halt."

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "postgresql-prolog"]
2+
path = postgresql-prolog
3+
url = https://github.com/aarroyoc/postgresql-prolog

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) 2025 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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# pgvector-prolog
2+
3+
[pgvector](https://github.com/pgvector/pgvector) examples for Prolog
4+
5+
Supports [postgresql-prolog](https://github.com/aarroyoc/postgresql-prolog)
6+
7+
[![Build Status](https://github.com/pgvector/pgvector-prolog/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-prolog/actions)
8+
9+
## Getting Started
10+
11+
Follow the instructions for your database library:
12+
13+
- [postgresql-prolog](#postgresql-prolog)
14+
15+
## postgresql-prolog
16+
17+
Enable the extension
18+
19+
```prolog
20+
postgresql:query(Connection, "CREATE EXTENSION IF NOT EXISTS vector", ok)
21+
```
22+
23+
Create a table
24+
25+
```prolog
26+
postgresql:query(Connection, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))", ok)
27+
```
28+
29+
Insert vectors
30+
31+
```prolog
32+
postgresql:sql(Connection, [insert_into(items, [embedding]), values("[1,2,3]")], data([]))
33+
```
34+
35+
Get the nearest neighbors
36+
37+
```prolog
38+
postgresql:query(Connection, "SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5", Rows)
39+
```
40+
41+
Add an approximate index
42+
43+
```prolog
44+
postgresql:query(Connection, "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)", ok)
45+
% or
46+
postgresql:query(Connection, "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)", ok)
47+
```
48+
49+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
50+
51+
See a [full example](example.pl)
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-prolog/issues)
58+
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-prolog/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-prolog.git
66+
cd pgvector-prolog
67+
createdb pgvector_prolog_test
68+
scryer-prolog example.pl -g "main,halt."
69+
```

example.pl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
:- use_module('postgresql-prolog/postgresql').
2+
3+
main :-
4+
postgresql:connect("postgres", "secret", '127.0.0.1', 5432, "pgvector_prolog_test", Connection),
5+
postgresql:query(Connection, "CREATE EXTENSION IF NOT EXISTS vector", ok),
6+
postgresql:query(Connection, "DROP TABLE IF EXISTS items", ok),
7+
postgresql:query(Connection, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))", ok),
8+
postgresql:sql(Connection, [insert_into(items, [embedding]), values("[1,2,3]")], data([])),
9+
postgresql:sql(Connection, [insert_into(items, [embedding]), values("[4,5,6]")], data([])),
10+
postgresql:query(Connection, "SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5", Rows),
11+
write(Rows),
12+
nl.

postgresql-prolog

Submodule postgresql-prolog added at fd91057

0 commit comments

Comments
 (0)