2023-07-13 11:41:36 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MigrateEmbeddingsFromDedicatedDatabase < ActiveRecord::Migration[7.0]
|
|
|
|
def up
|
|
|
|
return unless SiteSetting.ai_embeddings_enabled
|
|
|
|
return unless SiteSetting.ai_embeddings_pg_connection_string.present?
|
|
|
|
|
2023-09-05 13:31:04 -04:00
|
|
|
truncation = DiscourseAi::Embeddings::Strategies::Truncation.new
|
|
|
|
|
|
|
|
vector_reps =
|
|
|
|
[
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::AllMpnetBaseV2,
|
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::TextEmbeddingAda002,
|
|
|
|
].map { |k| k.new(truncation) }
|
|
|
|
|
|
|
|
vector_reps.each do |vector_rep|
|
2023-12-29 10:28:45 -05:00
|
|
|
new_table_name = vector_rep.topic_table_name
|
2023-09-05 13:31:04 -04:00
|
|
|
old_table_name = "topic_embeddings_#{vector_rep.name.underscore}"
|
|
|
|
|
|
|
|
begin
|
|
|
|
row_count =
|
|
|
|
DiscourseAi::Database::Connection
|
|
|
|
.db
|
|
|
|
.query_single("SELECT COUNT(*) FROM #{old_table_name}")
|
|
|
|
.first
|
|
|
|
|
|
|
|
if row_count > 0
|
|
|
|
puts "Migrating #{row_count} embeddings from #{old_table_name} to #{new_table_name}"
|
|
|
|
|
|
|
|
last_topic_id = 0
|
|
|
|
|
|
|
|
loop do
|
|
|
|
batch = DiscourseAi::Database::Connection.db.query(<<-SQL)
|
2023-07-13 11:41:36 -04:00
|
|
|
SELECT topic_id, embedding
|
|
|
|
FROM #{old_table_name}
|
|
|
|
WHERE topic_id > #{last_topic_id}
|
|
|
|
ORDER BY topic_id ASC
|
|
|
|
LIMIT 50
|
|
|
|
SQL
|
2023-09-05 13:31:04 -04:00
|
|
|
break if batch.empty?
|
2023-07-13 11:41:36 -04:00
|
|
|
|
2023-09-05 13:31:04 -04:00
|
|
|
DB.exec(<<-SQL)
|
2023-07-13 11:41:36 -04:00
|
|
|
INSERT INTO #{new_table_name} (topic_id, model_version, strategy_version, digest, embeddings, created_at, updated_at)
|
|
|
|
VALUES #{batch.map { |r| "(#{r.topic_id}, 0, 0, '', '#{r.embedding}', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)" }.join(", ")}
|
|
|
|
ON CONFLICT (topic_id)
|
|
|
|
DO NOTHING
|
|
|
|
SQL
|
|
|
|
|
2023-09-05 13:31:04 -04:00
|
|
|
last_topic_id = batch.last.topic_id
|
2023-07-13 11:41:36 -04:00
|
|
|
end
|
|
|
|
end
|
2023-09-05 13:31:04 -04:00
|
|
|
rescue PG::Error => e
|
|
|
|
Rails.logger.error(
|
|
|
|
"Error #{e} migrating embeddings from #{old_table_name} to #{new_table_name}",
|
|
|
|
)
|
2023-07-13 11:41:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
# no-op
|
|
|
|
end
|
|
|
|
end
|