Project

pgvector

0.03
The project is in a healthy, maintained state
pgvector support for Ruby
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

pgvector-ruby

pgvector support for Ruby

Supports pg and Sequel

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
  • Sequel

Or check out some examples:

pg

Enable 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_a

Add 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

Sequel

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)"
end

Add the plugin to your model

class Item < Sequel::Model
  plugin :pgvector, :embedding
end

Insert a vector

Item.create(embedding: [1, 1, 1])

Get the nearest neighbors to a record

item.nearest_neighbors(:embedding, distance: "euclidean").limit(5)

Also supports inner_product and cosine distance

Get the nearest neighbors to a vector

Item.nearest_neighbors(:embedding, [1, 1, 1], distance: "euclidean").limit(5)

Add an approximate index

DB.add_index :items, :embedding, type: "hnsw", opclass: "vector_l2_ops"

Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance

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