|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class NewEmbeddingsTables < ActiveRecord::Migration[7.2] |
| 4 | + def up |
| 5 | + create_table :ai_topics_embeddings, id: false do |t| |
| 6 | + t.bigint :topic_id, null: false |
| 7 | + t.bigint :model_id, null: false |
| 8 | + t.integer :model_version, null: false |
| 9 | + t.integer :strategy_id, null: false |
| 10 | + t.integer :strategy_version, null: false |
| 11 | + t.text :digest, null: false |
| 12 | + t.column :embeddings, "halfvec", null: false |
| 13 | + t.timestamps |
| 14 | + |
| 15 | + t.index %i[model_id strategy_id topic_id], |
| 16 | + unique: true, |
| 17 | + name: "index_ai_topics_embeddings_on_model_strategy_topic" |
| 18 | + end |
| 19 | + |
| 20 | + create_table :ai_posts_embeddings, id: false do |t| |
| 21 | + t.bigint :post_id, null: false |
| 22 | + t.bigint :model_id, null: false |
| 23 | + t.integer :model_version, null: false |
| 24 | + t.integer :strategy_id, null: false |
| 25 | + t.integer :strategy_version, null: false |
| 26 | + t.text :digest, null: false |
| 27 | + t.column :embeddings, "halfvec", null: false |
| 28 | + t.timestamps |
| 29 | + |
| 30 | + t.index %i[model_id strategy_id post_id], |
| 31 | + unique: true, |
| 32 | + name: "index_ai_posts_embeddings_on_model_strategy_post" |
| 33 | + end |
| 34 | + |
| 35 | + create_table :ai_document_fragments_embeddings, id: false do |t| |
| 36 | + t.bigint :rag_document_fragment_id, null: false |
| 37 | + t.bigint :model_id, null: false |
| 38 | + t.integer :model_version, null: false |
| 39 | + t.integer :strategy_id, null: false |
| 40 | + t.integer :strategy_version, null: false |
| 41 | + t.text :digest, null: false |
| 42 | + t.column :embeddings, "halfvec", null: false |
| 43 | + t.timestamps |
| 44 | + |
| 45 | + t.index %i[model_id strategy_id rag_document_fragment_id], |
| 46 | + unique: true, |
| 47 | + name: "index_ai_fragments_embeddings_on_model_strategy_fragment" |
| 48 | + end |
| 49 | + |
| 50 | + # Copied from 20241008054440_create_binary_indexes_for_embeddings |
| 51 | + %w[topics posts document_fragments].each do |type| |
| 52 | + # our supported embeddings models IDs and dimensions |
| 53 | + [ |
| 54 | + [1, 768], |
| 55 | + [2, 1536], |
| 56 | + [3, 1024], |
| 57 | + [4, 1024], |
| 58 | + [5, 768], |
| 59 | + [6, 1536], |
| 60 | + [7, 2000], |
| 61 | + [8, 1024], |
| 62 | + ].each { |model_id, dimensions| execute <<-SQL } |
| 63 | + CREATE INDEX ai_#{type}_embeddings_#{model_id}_1_search_bit ON ai_#{type}_embeddings |
| 64 | + USING hnsw ((binary_quantize(embeddings)::bit(#{dimensions})) bit_hamming_ops) |
| 65 | + WHERE model_id = #{model_id} AND strategy_id = 1; |
| 66 | + SQL |
| 67 | + end |
| 68 | + |
| 69 | + # Copy data from old tables to new tables |
| 70 | + execute <<-SQL |
| 71 | + INSERT INTO ai_topics_embeddings (topic_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at) |
| 72 | + SELECT * FROM ai_topic_embeddings; |
| 73 | +
|
| 74 | + INSERT INTO ai_posts_embeddings (post_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at) |
| 75 | + SELECT * FROM ai_post_embeddings; |
| 76 | +
|
| 77 | + INSERT INTO ai_document_fragments_embeddings (rag_document_fragment_id, model_id, model_version, strategy_id, strategy_version, digest, embeddings, created_at, updated_at) |
| 78 | + SELECT * FROM ai_document_fragment_embeddings; |
| 79 | + SQL |
| 80 | + end |
| 81 | + |
| 82 | + def down |
| 83 | + raise ActiveRecord::IrreversibleMigration |
| 84 | + end |
| 85 | +end |
0 commit comments