pgvector support for Nim
Supports db_connector
Run:
nimble add pgvectorAnd follow the instructions for your database library:
Or check out an example:
- Embeddings with OpenAI
- Binary embeddings with Cohere
- Hybrid search with Ollama (Reciprocal Rank Fusion)
- Sparse search with Text Embeddings Inference
Enable the extension
db.exec(sql"CREATE EXTENSION IF NOT EXISTS vector")Create a table
db.exec(sql"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")Insert vectors
import pgvector
let embedding1 = @[1, 1, 1]
let embedding2 = @[1, 1, 2]
let embedding3 = @[2, 2, 2]
db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", embedding1.toVector, embedding2.toVector, embedding3.toVector)Get the nearest neighbors
let embedding = @[1, 1, 1]
let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding.toVector)
for row in rows:
echo rowAdd an approximate index
db.exec(sql"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
# or
db.exec(sql"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 vector from a sequence or array
let vec = @[1.0, 2.0, 3.0].toVectorGet a sequence
let s = vec.toSeqCreate a half vector from a sequence or array
let vec = @[1.0, 2.0, 3.0].toHalfVectorGet a sequence
let s = vec.toSeqCreate a sparse vector from a sequence or array
let vec = @[1.0, 0.0, 2.0, 0.0, 3.0, 0.0].toSparseVectorOr a table of non-zero elements
let elements = {0: 1.0, 2: 2.0, 4: 3.0}.toTable
let vec = elements.toSparseVector(6)Note: Indices start at 0
Get the number of dimensions
let dim = vec.dimGet the non-zero elements
let elements = vec.elementsView 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-nim.git
cd pgvector-nim
createdb pgvector_nim_test
nimble install db_connector
nimble testSpecify the path to libpq if needed:
nimble test --passL:-Wl,-rpath,/opt/homebrew/opt/libpq/libTo run an example:
cd examples/openai
createdb pgvector_example
nim c --run --path:../../src example.nimSpecify the path to libpq if needed:
nim c --run --path:../../src --passL:-Wl,-rpath,/opt/homebrew/opt/libpq/lib example.nim