Skip to content

Commit 6bdd749

Browse files
committed
First commit
0 parents  commit 6bdd749

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

.github/workflows/build.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: build
2+
on: [push, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v5
8+
- run: sudo apt-get update && sudo apt-get install fpc
9+
- uses: ankane/setup-postgres@v1
10+
with:
11+
database: pgvector_pascal_test
12+
dev-files: true
13+
- run: |
14+
cd /tmp
15+
git clone --branch v0.8.1 https://github.com/pgvector/pgvector.git
16+
cd pgvector
17+
make
18+
sudo make install
19+
- run: fpc example.pp
20+
- run: ./example

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/example
2+
/libpq.dylib
3+
*.o

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# pgvector-pascal
2+
3+
[pgvector](https://github.com/pgvector/pgvector) examples for Pascal
4+
5+
Supports [SQLDB](https://www.freepascal.org/docs-html/fcl/sqldb/)
6+
7+
[![Build Status](https://github.com/pgvector/pgvector-pascal/actions/workflows/build.yml/badge.svg)](https://github.com/pgvector/pgvector-pascal/actions)
8+
9+
## Getting Started
10+
11+
Follow the instructions for your database library:
12+
13+
- [SQLDB](#sqldb)
14+
15+
## SQLDB
16+
17+
Enable the extension
18+
19+
```pascal
20+
Query.SQL.Text := 'CREATE EXTENSION IF NOT EXISTS vector';
21+
Query.ExecSQL;
22+
```
23+
24+
Create a table
25+
26+
```pascal
27+
Query.SQL.Text := 'CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))';
28+
Query.ExecSQL;
29+
```
30+
31+
Insert vectors
32+
33+
```pascal
34+
Query.SQL.Text := 'INSERT INTO items (embedding) VALUES ((:embedding1)::vector), ((:embedding2)::vector)';
35+
Query.Params.ParamByName('embedding1').AsString := '[1,2,3]';
36+
Query.Params.ParamByName('embedding2').AsString := '[4,5,6]';
37+
Query.ExecSQL;
38+
```
39+
40+
Get the nearest neighbors
41+
42+
```pascal
43+
Query.SQL.Text := 'SELECT * FROM items ORDER BY embedding <-> (:embedding)::vector LIMIT 5';
44+
Query.Params.ParamByName('embedding').AsString := '[3,1,2]';
45+
Query.Open;
46+
while not Query.EOF do
47+
begin
48+
writeln(Query.FieldByName('id').AsString);
49+
Query.Next;
50+
end;
51+
Query.Close;
52+
```
53+
54+
Add an approximate index
55+
56+
```pascal
57+
Query.SQL.Text := 'CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)';
58+
// or
59+
Query.SQL.Text := 'CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)';
60+
Query.ExecSQL;
61+
```
62+
63+
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance
64+
65+
See a [full example](example.pp)
66+
67+
## Contributing
68+
69+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
70+
71+
- [Report bugs](https://github.com/pgvector/pgvector-pascal/issues)
72+
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-pascal/pulls)
73+
- Write, clarify, or fix documentation
74+
- Suggest or add new features
75+
76+
To get started with development:
77+
78+
```sh
79+
git clone https://github.com/pgvector/pgvector-pascal.git
80+
cd pgvector-pascal
81+
createdb pgvector_pascal_test
82+
fpc example.pp
83+
./example
84+
```
85+
86+
Specify the path to libpq if needed:
87+
88+
```sh
89+
ln -s /opt/homebrew/opt/libpq/lib/libpq.dylib libpq.dylib
90+
```

example.pp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
program Example;
2+
3+
uses
4+
pqconnection,
5+
sqldb;
6+
7+
var
8+
Connection : TSQLConnection;
9+
Query : TSQLQuery;
10+
Transaction : TSQLTransaction;
11+
12+
begin
13+
Connection := TPQConnection.Create(Nil);
14+
Connection.DatabaseName := 'pgvector_pascal_test';
15+
16+
Transaction := TSQLTransaction.Create(Connection);
17+
Transaction.Database := Connection;
18+
19+
Query := TSQLQuery.Create(Connection);
20+
Query.Database := Connection;
21+
Query.Transaction := Transaction;
22+
23+
Query.SQL.Text := 'CREATE EXTENSION IF NOT EXISTS vector';
24+
Query.ExecSQL;
25+
26+
Query.SQL.Text := 'DROP TABLE IF EXISTS items';
27+
Query.ExecSQL;
28+
29+
Query.SQL.Text := 'CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))';
30+
Query.ExecSQL;
31+
32+
Query.SQL.Text := 'INSERT INTO items (embedding) VALUES ((:embedding1)::vector), ((:embedding2)::vector)';
33+
Query.Params.ParamByName('embedding1').AsString := '[1,2,3]';
34+
Query.Params.ParamByName('embedding2').AsString := '[4,5,6]';
35+
Query.ExecSQL;
36+
37+
Query.SQL.Text := 'SELECT * FROM items ORDER BY embedding <-> (:embedding)::vector LIMIT 5';
38+
Query.Params.ParamByName('embedding').AsString := '[3,1,2]';
39+
Query.Open;
40+
while not Query.EOF do
41+
begin
42+
writeln(Query.FieldByName('id').AsString);
43+
Query.Next;
44+
end;
45+
Query.Close;
46+
47+
Transaction.Commit;
48+
Connection.Free;
49+
end.

0 commit comments

Comments
 (0)