|
| 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 | +[](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 | +``` |
0 commit comments