pgvector-ruby

pgvector support for Ruby

Supports the pg gem

For Rails, check out Neighbor

Build Status

Installation

Add this line to your application’s Gemfile:

gem "pgvector"

And follow the instructions for your database library:

pg

Register the vector type with your connection

require "pgvector/pg"

registry = PG::BasicTypeRegistry.new.define_default_types
Pgvector::PG.register_vector(registry)
conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: registry)

Insert a vector

factors = [1, 2, 3]
conn.exec_params("INSERT INTO items (factors) VALUES ($1)", [factors])

Get the nearest neighbors to a vector

conn.exec_params("SELECT * FROM items ORDER BY factors <-> $1 LIMIT 5", [factors]).to_a

History

View the changelog

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-ruby.git
cd pgvector-ruby
createdb pgvector_ruby_test
bundle install
bundle exec rake test