-
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 6a5f861
Showing
7 changed files
with
152 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,20 @@ | ||
name: build | ||
on: [push, pull_request] | ||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ocaml/setup-ocaml@v2 | ||
- uses: ankane/setup-postgres@v1 | ||
with: | ||
database: pgvector_ocaml_test | ||
dev-files: true | ||
- run: | | ||
cd /tmp | ||
git clone --branch v0.5.1 https://github.com/pgvector/pgvector.git | ||
cd pgvector | ||
make | ||
sudo make install | ||
- run: opam install postgresql | ||
- run: opam exec -- dune exec ./example.exe |
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) 2023 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,79 @@ | ||
# pgvector-ocaml | ||
|
||
[pgvector](https://github.com/pgvector/pgvector) examples for OCaml | ||
|
||
Supports [PostgreSQL-OCaml](https://github.com/mmottl/postgresql-ocaml) | ||
|
||
[![Build Status](https://github.com/pgvector/pgvector-ocaml/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/pgvector/pgvector-ocaml/actions) | ||
|
||
## Getting Started | ||
|
||
Follow the instructions for your database library: | ||
|
||
- [PostgreSQL-OCaml](#postgresql-ocaml) | ||
|
||
## PostgreSQL-OCaml | ||
|
||
Enable the extension | ||
|
||
```ocaml | ||
c#exec ~expect:[Command_ok] | ||
"CREATE EXTENSION IF NOT EXISTS vector" | ||
``` | ||
|
||
Create a table | ||
|
||
```ocaml | ||
c#exec ~expect:[Command_ok] | ||
"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))" | ||
``` | ||
|
||
Insert vectors | ||
|
||
```ocaml | ||
c#exec ~expect:[Command_ok] ~params:[|"[1,1,1]"; "[2,2,2]"; "[1,1,2]"|] | ||
"INSERT INTO items (embedding) VALUES ($1), ($2), ($3)" | ||
``` | ||
|
||
Get the nearest neighbors | ||
|
||
```ocaml | ||
let result = | ||
c#exec ~expect:[Tuples_ok] ~params:[|"[1,1,1]"|] | ||
"SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5" | ||
in | ||
result#get_all_lst |> List.map (List.map print_endline) | ||
``` | ||
|
||
Add an approximate index | ||
|
||
```ocaml | ||
c#exec ~expect:[Command_ok] | ||
"CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)" | ||
(* or *) | ||
c#exec ~expect:[Command_ok] | ||
"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)" | ||
``` | ||
|
||
Use `vector_ip_ops` for inner product and `vector_cosine_ops` for cosine distance | ||
|
||
See a [full example](example.ocaml) | ||
|
||
## Contributing | ||
|
||
Everyone is encouraged to help improve this project. Here are a few ways you can help: | ||
|
||
- [Report bugs](https://github.com/pgvector/pgvector-ocaml/issues) | ||
- Fix bugs and [submit pull requests](https://github.com/pgvector/pgvector-ocaml/pulls) | ||
- Write, clarify, or fix documentation | ||
- Suggest or add new features | ||
|
||
To get started with development: | ||
|
||
```sh | ||
git clone https://github.com/pgvector/pgvector-ocaml.git | ||
cd pgvector-ocaml | ||
createdb pgvector_ocaml_test | ||
opam install postgresql | ||
opam exec -- dune exec ./example.exe | ||
``` |
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,3 @@ | ||
(executable | ||
(name example) | ||
(libraries postgresql)) |
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 @@ | ||
(lang dune 3.7) |
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,27 @@ | ||
open Postgresql | ||
|
||
let () = | ||
let conninfo = "postgresql://localhost/pgvector_ocaml_test" in | ||
let c = new connection ~conninfo () in | ||
c#set_notice_processing `Quiet ; | ||
let _ = c#exec ~expect:[Command_ok] "CREATE EXTENSION IF NOT EXISTS vector" in | ||
let _ = c#exec ~expect:[Command_ok] "DROP TABLE IF EXISTS items" in | ||
let _ = | ||
c#exec ~expect:[Command_ok] | ||
"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))" | ||
in | ||
let _ = | ||
c#exec ~expect:[Command_ok] | ||
~params:[|"[1,1,1]"; "[2,2,2]"; "[1,1,2]"|] | ||
"INSERT INTO items (embedding) VALUES ($1), ($2), ($3)" | ||
in | ||
let result = | ||
c#exec ~expect:[Tuples_ok] ~params:[|"[1,1,1]"|] | ||
"SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5" | ||
in | ||
let _ = result#get_all_lst |> List.map (List.map print_endline) in | ||
let _ = | ||
c#exec ~expect:[Command_ok] | ||
"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)" | ||
in | ||
c#finish |