pgvector support for Lua
Supports pgmoon
Run:
luarocks install pgvectorAnd 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
Require the library
localpgvector=require("pgvector")Enable the extension
pg:query("CREATE EXTENSION IF NOT EXISTS vector")Create a table
pg:query("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")Insert a vector
localembedding=pgvector.new({1, 1, 1})
pg:query("INSERT INTO items (embedding) VALUES ($1)", embedding)Get the nearest neighbors
localembedding=pgvector.new({1, 1, 1})
localres=pg:query("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", embedding)
fori, rowinipairs(res) doprint(row["id"])
endAdd an approximate index
pg:query("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
-- orpg: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
Create a half vector from a table
localvec=pgvector.halfvec({1, 2, 3})Create a sparse vector from a table of non-zero elements
localelements= {[1] =1, [3] =2, [5] =3}
localvec=pgvector.sparsevec(elements, 6)Get the number of dimensions
vec["dim"]Get the non-zero elements
vec["elements"]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-lua.git
cd pgvector-lua
createdb pgvector_lua_test
luarocks install pgmoon
luarocks install luasocket
lua test/pgvector.luaTo run an example:
createdb pgvector_example
luarocks install luasec
luarocks install lua-cjson
lua examples/openai/example.lua