### Running Bulk Loading Example (Shell) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Shell commands to navigate to a specific example directory (bulk loading), install its dependencies, create a database for the example, and execute the example Ruby script. ```shell cd examples/loading bundle install createdb pgvector_example bundle exec ruby example.rb ``` -------------------------------- ### Setting Up Development Environment (Shell) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md A sequence of shell commands to clone the repository, navigate into the directory, set up a test database, install dependencies, and run the test suite for local development of the pgvector-ruby gem. ```shell git clone https://github.com/pgvector/pgvector-ruby.git cd pgvector-ruby createdb pgvector_ruby_test bundle install bundle exec rake test ``` -------------------------------- ### Getting SparseVector Properties (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Illustrates how to retrieve properties of a `Pgvector::SparseVector` instance, including the total number of dimensions, the indices of non-zero elements, and the values of non-zero elements. ```ruby dim = vec.dimensions ``` ```ruby indices = vec.indices ``` ```ruby values = vec.values ``` -------------------------------- ### Adding Approximate Index for pg (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Add an Approximate Nearest Neighbor (ANN) index to the vector column to significantly speed up nearest neighbor queries. Examples show creating HNSW or IVFFlat indexes with the `vector_l2_ops` operator class for Euclidean distance. ```ruby conn.exec("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)") ``` ```ruby conn.exec("CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops) WITH (lists = 100)") ``` -------------------------------- ### Enabling pgvector Extension (Sequel Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Execute this SQL command using the `Sequel` database object (`DB.run`) to enable the 'vector' extension in your PostgreSQL database. This extension is required to use the vector data type and related functions with Sequel. ```ruby DB.run("CREATE EXTENSION IF NOT EXISTS vector") ``` -------------------------------- ### Creating SparseVector from Array or Hash (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Demonstrates initializing a `Pgvector::SparseVector` object from either a dense Ruby array or a hash representing non-zero indices and values. The hash constructor requires specifying the total number of dimensions. ```ruby vec = Pgvector::SparseVector.new([1, 0, 2, 0, 3, 0]) ``` ```ruby vec = Pgvector::SparseVector.new({0 => 1, 2 => 2, 4 => 3}, 6) ``` -------------------------------- ### Enabling pgvector Extension (pg Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Execute this SQL command using the 'pg' gem connection to enable the 'vector' extension in your PostgreSQL database. This extension is required to use the vector data type and related functions. ```ruby conn.exec("CREATE EXTENSION IF NOT EXISTS vector") ``` -------------------------------- ### Creating Items Table with Vector Column (Sequel Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Use Sequel's schema definition DSL (`DB.create_table`) to create a table named 'items' with a primary key and a column explicitly defined as 'vector(3)', integrating pgvector with the Sequel schema. ```ruby DB.create_table :items do primary_key :id column :embedding, "vector(3)" end ``` -------------------------------- ### Querying Nearest Neighbors with Euclidean Distance (pg Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Execute a parameterized SQL query using `conn.exec_params` to find the nearest neighbors to a given vector. The `<->` operator calculates the Euclidean distance, and results are ordered by this distance and limited. ```ruby conn.exec_params("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", [embedding]).to_a ``` -------------------------------- ### Adding pgvector Gem Dependency (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Add the 'pgvector' gem to your application's Gemfile to include the library as a project dependency. This is the first step to using pgvector functionality in your Ruby application. ```ruby gem "pgvector" ``` -------------------------------- ### Inserting Vector via Sequel Model (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Create a new record in the table associated with the Sequel model, assigning a Ruby array directly to the vector column. The pgvector plugin handles the conversion and insertion. ```ruby Item.create(embedding: [1, 1, 1]) ``` -------------------------------- ### Creating Items Table with Vector Column (pg Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Execute an SQL command via the 'pg' gem to create a table named 'items' with a 'bigserial' primary key and a column named 'embedding' of type 'vector' with a specified dimension (e.g., 3). ```ruby conn.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))") ``` -------------------------------- ### Querying Nearest Neighbors with Sequel Model (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Use the `nearest_neighbors` helper method provided by the pgvector plugin on a model instance or class to find records closest to a specific item's embedding or a given vector, specifying the distance metric and limiting results. ```ruby item.nearest_neighbors(:embedding, distance: "euclidean").limit(5) ``` ```ruby Item.nearest_neighbors(:embedding, [1, 1, 1], distance: "euclidean").limit(5) ``` -------------------------------- ### Adding Approximate Index for Sequel (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Use Sequel's `DB.add_index` method to create an Approximate Nearest Neighbor (ANN) index (e.g., HNSW) on the vector column, specifying the index type and the operator class (`vector_l2_ops` for Euclidean distance) for improved query performance. ```ruby DB.add_index :items, :embedding, type: "hnsw", opclass: "vector_l2_ops" ``` -------------------------------- ### Adding pgvector Plugin to Sequel Model (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Add the `:pgvector` plugin to your Sequel model class, specifying the name of the vector column. This extends the model with helper methods for vector operations like nearest neighbor queries. ```ruby class Item < Sequel::Model plugin :pgvector, :embedding end ``` -------------------------------- ### Registering Vector Type Casting (pg Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Configure the 'pg' gem's type registry to automatically cast results from the 'vector' type to a usable Ruby object (like `Pgvector::Vector` or an array). This simplifies reading vector data from queries. ```ruby registry = PG::BasicTypeRegistry.new.define_default_types Pgvector::PG.register_vector(registry) conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn, registry: registry) ``` -------------------------------- ### Converting SparseVector to Dense Array (Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Shows how to convert a `Pgvector::SparseVector` back into a standard dense Ruby array, including the zero values for dimensions that were not explicitly stored. ```ruby arr = vec.to_a ``` -------------------------------- ### Inserting Vector into Items Table (pg Ruby) Source: https://github.com/pgvector/pgvector-ruby/blob/master/README.md Use `conn.exec_params` with a parameterized query to safely insert a Ruby array representing a vector into the 'embedding' column of the 'items' table. The array is automatically handled by the registered type casting. ```ruby embedding = [1, 2, 3] conn.exec_params("INSERT INTO items (embedding) VALUES ($1)", [embedding]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.