pgvector examples for Common Lisp
Supports Postmodern and CL-DBI
Follow the instructions for your database library:
Enable the extension
(query "CREATE EXTENSION IF NOT EXISTS vector")
Create a table
(query "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
Insert a vector
(query (:insert-into 'items :set 'embedding "[1,1,1]"))
Get the nearest neighbors
(register-sql-operators :2+-ary :<-> :<#> :<=>)
(doquery (:limit (:order-by (:select 'id 'embedding :from 'items) (:<-> 'embedding "[1,1,1]")) 5) (id embedding)
(format t "~A: ~A~%" id embedding))
Add an approximate index
(query "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
;; or
(query "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
Enable the extension
(dbi:do-sql *conn* "CREATE EXTENSION IF NOT EXISTS vector")
Create a table
(dbi:do-sql *conn* "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")
Insert a vector
(dbi:do-sql *conn* "INSERT INTO items (embedding) VALUES (?)" (list "[1,1,1]"))
Get the nearest neighbors
(dbi:fetch-all (dbi:execute (dbi:prepare *conn* "SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5") (list "[1,1,1]")))
Add an approximate index
(dbi:do-sql *conn* "CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
;; or
(dbi:do-sql *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
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-lisp.git
cd pgvector-lisp
createdb pgvector_lisp_test
sbcl --noinform --non-interactive --load postmodern.lisp --load cl-dbi.lisp