Skip to content

Latest commit

 

History

History
80 lines (56 loc) · 1.86 KB

README.md

File metadata and controls

80 lines (56 loc) · 1.86 KB

pgvector-gleam

pgvector examples for Gleam

Supports pog

Build Status

Getting Started

Follow the instructions for your database library:

pog

Enable the extension

let assert Ok(_) =
  pog.query("CREATE EXTENSION IF NOT EXISTS vector")
  |> pog.execute(db)

Create a table

let assert Ok(_) =
  pog.query("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
  |> pog.execute(db)

Insert vectors

let assert Ok(_) =
  pog.query("INSERT INTO items (embedding) VALUES ($1::text::vector), ($2::text::vector)")
  |> pog.parameter(pog.text("[1,2,3]"))
  |> pog.parameter(pog.text("[4,5,6]"))
  |> pog.execute(db)

Get the nearest neighbors

let assert Ok(response) =
  pog.query("SELECT id FROM items ORDER BY embedding <-> $1::text::vector LIMIT 5")
  |> pog.parameter(pog.text("[3,1,2]"))
  |> pog.returning(dynamic.element(0, dynamic.int))
  |> pog.execute(db)

Add an approximate index

let assert Ok(_) =
  pog.query("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
  |> pog.execute(db)

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-gleam.git
cd pgvector-gleam
gleam run