Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 29, 2023
0 parents commit 6a5f861
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/build.yml
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
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) 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.
79 changes: 79 additions & 0 deletions README.md
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
```
3 changes: 3 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executable
(name example)
(libraries postgresql))
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 3.7)
27 changes: 27 additions & 0 deletions example.ml
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

0 comments on commit 6a5f861

Please sign in to comment.