pgvector examples for Common Lisp
Supports Postmodern and CL-DBI
Follow the instructions for your database library:
Or check out some examples:
- Embeddings with OpenAI
- Binary embeddings with Cohere
- Hybrid search with Ollama (Reciprocal Rank Fusion)
- Sparse search with Text Embeddings Inference
Enable the extension
(load-extension "vector")Create a table
(query (:create-table'items ((id :type bigserial :primary-keyt) (embedding :type (vector3)))))Insert a vector
(query (:insert-into'items :set'embedding "[1,1,1]"))Get the nearest neighbors
(doquery (:limit (:order-by (:select'id 'embedding :from'items) (:<->'embedding "[1,1,1]")) 5) (id embedding) (formatt"~A: ~A~%" id embedding))Add an approximate index
(query (:create-index'my-index :on'items :using hnsw :fields"embedding vector_l2_ops")) ;; or (query (:create-index'my-index :on'items :using ivfflat :fields"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.lispTo run an example:
cd examples/openai createdb pgvector_example sbcl --noinform --non-interactive --load example.lisp