2023-03-15 16:21:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-12-29 10:28:45 -05:00
|
|
|
desc "Backfill embeddings for all topics and posts"
|
|
|
|
task "ai:embeddings:backfill" => [:environment] do
|
2023-03-15 16:21:45 -04:00
|
|
|
public_categories = Category.where(read_restricted: false).pluck(:id)
|
2023-09-05 10:08:23 -04:00
|
|
|
|
|
|
|
strategy = DiscourseAi::Embeddings::Strategies::Truncation.new
|
2023-09-06 23:25:26 -04:00
|
|
|
vector_rep = DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(strategy)
|
2023-12-29 10:28:45 -05:00
|
|
|
table_name = vector_rep.topic_table_name
|
2023-09-05 10:08:23 -04:00
|
|
|
|
2023-03-15 16:21:45 -04:00
|
|
|
Topic
|
2023-09-05 10:08:23 -04:00
|
|
|
.joins("LEFT JOIN #{table_name} ON #{table_name}.topic_id = topics.id")
|
|
|
|
.where("#{table_name}.topic_id IS NULL")
|
2023-03-31 14:29:56 -04:00
|
|
|
.where("category_id IN (?)", public_categories)
|
2023-03-15 16:21:45 -04:00
|
|
|
.where(deleted_at: nil)
|
2023-12-29 10:28:45 -05:00
|
|
|
.order("topics.id DESC")
|
2023-03-15 16:21:45 -04:00
|
|
|
.find_each do |t|
|
|
|
|
print "."
|
2023-12-29 10:28:45 -05:00
|
|
|
vector_rep.generate_representation_from(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
table_name = vector_rep.post_table_name
|
|
|
|
Post
|
|
|
|
.joins("LEFT JOIN #{table_name} ON #{table_name}.post_id = posts.id")
|
|
|
|
.where("#{table_name}.post_id IS NULL")
|
|
|
|
.where(deleted_at: nil)
|
|
|
|
.order("posts.id DESC")
|
|
|
|
.find_each do |t|
|
|
|
|
print "."
|
|
|
|
vector_rep.generate_representation_from(t)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc "Creates indexes for embeddings"
|
2023-03-20 15:44:55 -04:00
|
|
|
task "ai:embeddings:index", [:work_mem] => [:environment] do |_, args|
|
2023-09-05 10:08:23 -04:00
|
|
|
strategy = DiscourseAi::Embeddings::Strategies::Truncation.new
|
2023-10-26 11:07:37 -04:00
|
|
|
vector_rep = DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(strategy)
|
2023-07-13 17:59:25 -04:00
|
|
|
|
2023-10-26 11:07:37 -04:00
|
|
|
vector_rep.consider_indexing(memory: args[:work_mem] || "100MB")
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|