GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - pgvector/pgvector-ruby: pgvector support for Ruby

BranchesTags
Open more actions menu

Latest commit

 

History

197 Commits
197 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
examples
examples
 
 
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
Gemfile
Gemfile
 
 
LICENSE.txt
LICENSE.txt
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 
pgvector.gemspec
pgvector.gemspec
 
 

Repository files navigation

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:

  • Embeddings with OpenAI
  • Binary embeddings with Cohere
  • Sentence embeddings with Informers
  • Hybrid search with Informers (Reciprocal Rank Fusion)
  • Sparse search with Transformers.rb
  • Morgan fingerprints with RDKit.rb
  • Topic modeling with tomoto.rb
  • User-based recommendations with Disco
  • Item-based recommendations with Disco
  • Horizontal scaling with Citus
  • Bulk loading with COPY

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, cosine, taxicab, hamming, and jaccard 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

Reference

Sparse Vectors

Create a sparse vector from an array

vec = Pgvector::SparseVector.new([1, 0, 2, 0, 3, 0])

Or a hash of non-zero elements

vec = Pgvector::SparseVector.new({0 => 1, 2 => 2, 4 => 3}, 6)

Note: Indices start at 0

Get the number of dimensions

dim = vec.dimensions

Get the indices of non-zero elements

indices = vec.indices

Get the values of non-zero elements

values = vec.values

Get an array

arr = vec.to_a

History

View the changelog

Contributing

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

To run an example:

cd examples/loading
bundle install
createdb pgvector_example
bundle exec ruby example.rb

About

pgvector support for Ruby

Resources

Readme
MIT license

Security policy

Security policy
Activity
Custom properties

Stars

186 stars

Watchers

3 watching

Forks

4 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.