pgvector support for Ruby
pgvector support for Ruby
For Rails, check out Neighbor
Add this line to your application’s Gemfile:
gem "pgvector"And follow the instructions for your database library:
Or check out some examples:
COPYEnable the extension
conn.exec("CREATE EXTENSION IF NOT EXISTS vector")Optionally enable type casting for results
registry = PG::BasicTypeRegistry.new.define_default_types
Pgvector::PG.register_vector(registry)
conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: registry)Create a table
conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")Insert a vector
embedding = [1, 2, 3]
conn.exec_params("INSERT INTO items (embedding) VALUES ($1)", [embedding])Get the nearest neighbors to a vector
conn.exec_params("SELECT * FROM items ORDER BY embedding $1 LIMIT 5", [embedding]).to_aAdd an approximate index
conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")
# or
conn.exec("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
Enable the extension
DB.run("CREATE EXTENSION IF NOT EXISTS vector")Create a table
DB.create_table :items do
primary_key :id
column :embedding, "vector(3)"
endAdd the plugin to your model
class Item 1, 2 => 2, 4 => 3}, 6)Note: Indices start at 0
Get the number of dimensions
dim = vec.dimensionsGet the indices of non-zero elements
indices = vec.indicesGet the values of non-zero elements
values = vec.valuesGet an array
arr = vec.to_aView the changelog
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-ruby.git
cd pgvector-ruby
createdb pgvector_ruby_test
bundle install
bundle exec rake testTo run an example:
cd examples/loading
bundle install
createdb pgvector_example
bundle exec ruby example.rbNo open issues yet, or sync has not completed.