Use pgvector with the Sequel gem
masterTo use pgvector with Sequel, enable the extension and use the pgvector plugin on your models.
1. Enable the extension
DB.run("CREATE EXTENSION IF NOT EXISTS vector")2. Create a table
DB.create_table :items do
primary_key :id
column :embedding, "vector(3)"
end3. Configure the Model
Add the :pgvector plugin to your Sequel::Model and specify the column name:
class Item < Sequel::Model
plugin :pgvector, :embedding
end4. Insert and Query
# Insert
Item.create(embedding: [1, 1, 1])
# Get nearest neighbors for a specific record
item.nearest_neighbors(:embedding, distance: "euclidean").limit(5)
# Get nearest neighbors for a specific vector
Item.nearest_neighbors(:embedding, [1, 1, 1], distance: "euclidean").limit(5)Supported distance values for the distance option:
euclideaninner_productcosinetaxicabhammingjaccard
5. Create approximate indices
DB.add_index :items, :embedding, type: "hnsw", opclass: "vector_l2_ops"Note: Use vector_ip_ops for inner product and vector_cosine_ops for cosine distance.