discourse-ai/app/jobs/regular/generate_embeddings.rb

27 lines
943 B
Ruby
Raw Normal View History

# frozen_string_literal: true
module Jobs
class GenerateEmbeddings < ::Jobs::Base
sidekiq_options queue: "low"
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-12-29 10:28:45 -05:00
topic = target.is_a?(Topic) ? target : target.topic
post = target.is_a?(Post) ? target : target.first_post
return unless topic.present? && post.present?
2023-12-29 10:28:45 -05:00
return if topic.private_message? && !SiteSetting.ai_embeddings_generate_for_pms
return if post.raw.blank?
strategy = DiscourseAi::Embeddings::Strategies::Truncation.new
vector_rep =
DiscourseAi::Embeddings::VectorRepresentations::Base.current_representation(strategy)
2023-12-29 10:28:45 -05:00
vector_rep.generate_representation_from(target)
end
end
end