百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pgvector-ruby

> 编程语言
开源

pgvector 对 Ruby 的支持

181 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

pgvector 对 Ruby 的支持

pgvector-ruby

pgvector support for Ruby

Supports pg and Sequel

For Rails, check out Neighbor

Installation

Add this line to your application’s Gemfile:

ruby
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

ruby
conn.exec("CREATE EXTENSION IF NOT EXISTS vector")

Optionally enable type casting for results

ruby
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

ruby
conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")

Insert a vector

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

Get the nearest neighbors to a vector

ruby
conn.exec_params("SELECT * FROM items ORDER BY embedding  $1 LIMIT 5", [embedding]).to_a

Add an approximate index

ruby
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

ruby
DB.run("CREATE EXTENSION IF NOT EXISTS vector")

Create a table

ruby
DB.create_table :items do
  primary_key :id
  column :embedding, "vector(3)"
end

Add the plugin to your model

ruby
class Item  1, 2 => 2, 4 => 3}, 6)

Note: Indices start at 0

Get the number of dimensions

ruby
dim = vec.dimensions

Get the indices of non-zero elements

ruby
indices = vec.indices

Get the values of non-zero elements

ruby
values = vec.values

Get an array

ruby
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:

bash
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:

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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言