2023-03-15 16:21:45 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
|
|
|
class GenerateEmbeddings < ::Jobs::Base
|
2023-10-26 11:07:37 -04:00
|
|
|
sidekiq_options queue: "low"
|
|
|
|
|
2023-03-15 16:21:45 -04:00
|
|
|
def execute(args)
|
|
|
|
return unless SiteSetting.ai_embeddings_enabled
|
2023-12-29 10:28:45 -05:00
|
|
|
return if args[:target_type].blank? || args[:target_id].blank?
|
|
|
|
target = args[:target_type].constantize.find_by_id(args[:target_id])
|
|
|
|
return if target.nil? || target.deleted_at.present?
|
2023-03-15 16:21:45 -04:00
|
|
|
|
2023-12-29 10:28:45 -05:00
|
|
|
topic = target.is_a?(Topic) ? target : target.topic
|
|
|
|
post = target.is_a?(Post) ? target : target.first_post
|
2024-05-28 05:15:42 -04:00
|
|
|
return if topic.blank? || post.blank?
|
2023-12-29 10:28:45 -05:00
|
|
|
return if topic.private_message? && !SiteSetting.ai_embeddings_generate_for_pms
|
|
|
|
return if post.raw.blank?
|
2023-03-15 16:21:45 -04:00
|
|
|
|
2023-09-05 10:08:23 -04:00
|
|
|
strategy = DiscourseAi::Embeddings::Strategies::Truncation.new
|
|
|
|
vector_rep =
|
2023-09-07 10:54:43 -04:00
|
|
|
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(strategy)
|
2023-09-05 10:08:23 -04:00
|
|
|
|
2023-12-29 10:28:45 -05:00
|
|
|
vector_rep.generate_representation_from(target)
|
2023-03-15 16:21:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|