From 04bc402aae21de95c2aaadc12fcb23c870752899 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Tue, 23 Jan 2024 22:09:27 -0300 Subject: [PATCH] FEATURE: Setting to control per post embeddings (#439) * FEATURE: Setting to control per post embeddings --- app/jobs/scheduled/embeddings_backfill.rb | 2 ++ config/settings.yml | 1 + lib/embeddings/entry_point.rb | 3 ++- spec/lib/modules/embeddings/entry_point_spec.rb | 10 ++++++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/jobs/scheduled/embeddings_backfill.rb b/app/jobs/scheduled/embeddings_backfill.rb index d0384da4..bba792e5 100644 --- a/app/jobs/scheduled/embeddings_backfill.rb +++ b/app/jobs/scheduled/embeddings_backfill.rb @@ -65,6 +65,8 @@ module Jobs return if rebaked >= limit + return unless SiteSetting.ai_embeddings_per_post_enabled + # Now for posts table_name = vector_rep.post_table_name diff --git a/config/settings.yml b/config/settings.yml index 75c135d7..ff6f975e 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -239,6 +239,7 @@ discourse_ai: - multilingual-e5-large - bge-large-en - gemini + ai_embeddings_per_post_enabled: false ai_embeddings_generate_for_pms: false ai_embeddings_semantic_related_topics_enabled: default: false diff --git a/lib/embeddings/entry_point.rb b/lib/embeddings/entry_point.rb index 8da045b1..11d8f851 100644 --- a/lib/embeddings/entry_point.rb +++ b/lib/embeddings/entry_point.rb @@ -44,7 +44,8 @@ module DiscourseAi # embeddings generation. callback = Proc.new do |target| - if SiteSetting.ai_embeddings_enabled + if SiteSetting.ai_embeddings_enabled && + (target.is_a?(Topic) || SiteSetting.ai_embeddings_per_post_enabled) Jobs.enqueue( :generate_embeddings, target_id: target.id, diff --git a/spec/lib/modules/embeddings/entry_point_spec.rb b/spec/lib/modules/embeddings/entry_point_spec.rb index 2b593bd5..de1e3024 100644 --- a/spec/lib/modules/embeddings/entry_point_spec.rb +++ b/spec/lib/modules/embeddings/entry_point_spec.rb @@ -16,10 +16,16 @@ describe DiscourseAi::Embeddings::EntryPoint do it "queues a job on create if embeddings is enabled" do SiteSetting.ai_embeddings_enabled = true - expect { creator.create }.to change(Jobs::GenerateEmbeddings.jobs, :size).by(2) # topic_created and post_created + expect { creator.create }.to change(Jobs::GenerateEmbeddings.jobs, :size).by(1) # topic_created end - it "does nothing if sentiment analysis is disabled" do + it "queues two jobs on create if embeddings is enabled" do + SiteSetting.ai_embeddings_enabled = true + + expect { creator.create }.to change(Jobs::GenerateEmbeddings.jobs, :size).by(1) # topic_created AND post_created + end + + it "does nothing if embeddings analysis is disabled" do SiteSetting.ai_embeddings_enabled = false expect { creator.create }.not_to change(Jobs::GenerateEmbeddings.jobs, :size)