Skip to content

pgvector/pgvector-gleam

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

pgvector examples for Gleam

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages