Skip to content

pgvector/pgvector-lisp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pgvector-lisp

pgvector examples for Common Lisp

Supports Postmodern and CL-DBI

Build Status

Getting Started

Follow the instructions for your database library:

Postmodern

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

CL-DBI

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

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-lisp.git
cd pgvector-lisp
createdb pgvector_lisp_test
sbcl --noinform --non-interactive --load postmodern.lisp --load cl-dbi.lisp

About

pgvector examples for Common Lisp

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published