Skip to content

pgvector/pgvector-ocaml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-ocaml

pgvector examples for OCaml

Supports PostgreSQL-OCaml

Build Status

Getting Started

Follow the instructions for your database library:

PostgreSQL-OCaml

Enable the extension

c#exec ~expect:[Command_ok]
  "CREATE EXTENSION IF NOT EXISTS vector"

Create a table

c#exec ~expect:[Command_ok]
  "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"

Insert vectors

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

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

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

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

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

About

pgvector examples for OCaml

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages