Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

pgvector support for Ruby

License

NotificationsYou must be signed in to change notification settings

pgvector/pgvector-ruby

Repository files navigation

pgvector support for Ruby

Supportspg andSequel

For Rails, check outNeighbor

Build Status

Installation

Add this line to your application’s Gemfile:

gem"pgvector"

And follow the instructions for your database library:

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_typesPgvector::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)")# orconn.exec("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)")

Usevector_ip_ops for inner product andvector_cosine_ops for cosine distance

Sequel

Enable the extension

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

Create a table

DB.create_table:itemsdoprimary_key:idcolumn:embedding,"vector(3)"end

Add the plugin to your model

classItem <Sequel::Modelplugin:pgvector,:embeddingend

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 supportsinner_product,cosine,taxicab,hamming, andjaccard 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"

Usevector_ip_ops for inner product andvector_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 thechangelog

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.gitcd pgvector-rubycreatedb pgvector_ruby_testbundle installbundleexec raketest

To run an example:

cd examples/loadingbundle installcreatedb pgvector_examplebundleexec ruby example.rb

About

pgvector support for Ruby

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors2

  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp