|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "logger" |
| 5 | +require "rails" |
| 6 | +require "active_record" |
| 7 | +require_relative "../../lib/parade_db" |
| 8 | + |
| 9 | +class VectorSearchExampleApp < Rails::Application |
| 10 | + config.root = File.expand_path("../..", __dir__) |
| 11 | + config.eager_load = false |
| 12 | + config.logger = Logger.new(nil) |
| 13 | + config.secret_key_base = "paradedb_examples_secret_key_base" |
| 14 | +end |
| 15 | + |
| 16 | +VectorSearchExampleApp.initialize! |
| 17 | + |
| 18 | +require_relative "model" |
| 19 | + |
| 20 | +module VectorSearchSetup |
| 21 | + module_function |
| 22 | + |
| 23 | + SEED_ITEMS = [ |
| 24 | + { description: "Sleek running shoes", category: "Footwear", embedding: [1.0, 0.1, 0.0] }, |
| 25 | + { description: "Trail running shoes with grip", category: "Footwear", embedding: [0.9, 0.2, 0.1] }, |
| 26 | + { description: "White leather sneakers", category: "Footwear", embedding: [0.7, 0.4, 0.2] }, |
| 27 | + { description: "Innovative wireless earbuds", category: "Electronics", embedding: [0.0, 1.0, 0.1] }, |
| 28 | + { description: "Over-ear noise cancelling headphones", category: "Electronics", embedding: [0.1, 0.9, 0.2] }, |
| 29 | + { description: "Portable bluetooth speaker", category: "Electronics", embedding: [0.2, 0.8, 0.4] }, |
| 30 | + { description: "Insulated camping tent", category: "Outdoor", embedding: [0.1, 0.2, 1.0] }, |
| 31 | + { description: "Lightweight hiking backpack", category: "Outdoor", embedding: [0.3, 0.1, 0.9] } |
| 32 | + ].freeze |
| 33 | + |
| 34 | + def database_url |
| 35 | + return ENV["DATABASE_URL"] if ENV["DATABASE_URL"] |
| 36 | + |
| 37 | + host = ENV.fetch("PGHOST", "localhost") |
| 38 | + port = ENV.fetch("PGPORT", "5432") |
| 39 | + user = ENV.fetch("PGUSER", "postgres") |
| 40 | + password = ENV.fetch("PGPASSWORD", "postgres") |
| 41 | + database = ENV.fetch("PGDATABASE", "postgres") |
| 42 | + |
| 43 | + "postgresql://#{user}:#{password}@#{host}:#{port}/#{database}" |
| 44 | + end |
| 45 | + |
| 46 | + def connect! |
| 47 | + return if ActiveRecord::Base.connected? |
| 48 | + |
| 49 | + ActiveRecord::Base.establish_connection(database_url) |
| 50 | + ActiveRecord::Base.logger = nil |
| 51 | + end |
| 52 | + |
| 53 | + def vector_search_supported? |
| 54 | + ActiveRecord::Base.connection.select_value(<<~SQL) |
| 55 | + SELECT 1 |
| 56 | + FROM pg_opclass oc |
| 57 | + JOIN pg_am am ON am.oid = oc.opcmethod |
| 58 | + WHERE am.amname = 'bm25' AND oc.opcname = 'vector_l2_ops' |
| 59 | + LIMIT 1 |
| 60 | + SQL |
| 61 | + end |
| 62 | + |
| 63 | + def setup! |
| 64 | + connect! |
| 65 | + |
| 66 | + conn = ActiveRecord::Base.connection |
| 67 | + conn.execute("CREATE EXTENSION IF NOT EXISTS pg_search;") |
| 68 | + conn.execute("CREATE EXTENSION IF NOT EXISTS vector;") |
| 69 | + |
| 70 | + unless vector_search_supported? |
| 71 | + abort "This example requires a ParadeDB build with bm25 vector opclasses " \ |
| 72 | + "(unreleased; see paradedb/paradedb branch mvp/vector-search)." |
| 73 | + end |
| 74 | + |
| 75 | + conn.execute("DROP TABLE IF EXISTS vector_items CASCADE;") |
| 76 | + ActiveRecord::Schema.define do |
| 77 | + suppress_messages do |
| 78 | + create_table :vector_items, force: true do |t| |
| 79 | + t.text :description |
| 80 | + t.text :category |
| 81 | + t.vector :embedding, limit: 3 |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + VectorItem.reset_column_information |
| 87 | + SEED_ITEMS.each { |attrs| VectorItem.create!(attrs) } |
| 88 | + |
| 89 | + conn.create_paradedb_index(VectorItemIndex, if_not_exists: true) |
| 90 | + VectorItem.count |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +if $PROGRAM_NAME == __FILE__ |
| 95 | + puts "=" * 60 |
| 96 | + puts "Vector Search Setup - Creating vector_items Table" |
| 97 | + puts "=" * 60 |
| 98 | + |
| 99 | + count = VectorSearchSetup.setup! |
| 100 | + puts "+ Seeded #{count} items with vector(3) embeddings" |
| 101 | + puts "\nSetup complete! Run: BUNDLE_GEMFILE=examples/Gemfile bundle exec ruby examples/vector_search/vector_search.rb" |
| 102 | +end |
0 commit comments