pgvector support for Haskell
Supports postgresql-simple
Follow the instructions for your database library:
Import the library
import Pgvector
Enable the extension
execute_ conn "CREATE EXTENSION IF NOT EXISTS vector"
Create a table
execute_ conn "CREATE TABLE items (embedding vector(3))"
Insert a vector
execute conn
"INSERT INTO items (embedding) VALUES (?)"
[Vector [1, 1, 1]]
Get the nearest neighbors
let q = "SELECT embedding FROM items ORDER BY embedding <-> ? LIMIT 5"
forEach conn q [Vector [1, 1, 1]] $ \(Only embedding) ->
putStrLn $ show (embedding :: Vector)
Add an approximate index
execute_ conn "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"
-- or
execute_ conn "CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)"
Use vector_ip_ops
for inner product and vector_cosine_ops
for cosine distance
See a full example
View the changelog
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
To get started with development:
git clone https://github.com/pgvector/pgvector-haskell.git
cd pgvector-haskell
createdb pgvector_haskell_test
cabal test