A platform for community discussion. Free, open, simple.
DEV: Remove old Discourse AI rake tasks (#36384)
Rafael dos Santos Silva99dbd0edb401
diff --git a/plugins/discourse-ai/lib/tasks/modules/embeddings/database.rake b/plugins/discourse-ai/lib/tasks/modules/embeddings/database.rakedeleted file mode 100644index 1981e6ca040..00000000000--- a/plugins/discourse-ai/lib/tasks/modules/embeddings/database.rake+++ /dev/null@@ -1,42 +0,0 @@-# frozen_string_literal: true--desc "Backfill embeddings for all topics and posts"-task "ai:embeddings:backfill", %i[embedding_def_id concurrency] => [:environment] do |_, args|- public_categories = Category.where(read_restricted: false).pluck(:id)-- if args[:embedding_def_id].present?- vdef = EmbeddingDefinition.find(args[:embedding_def_id])- vector_rep = DiscourseAi::Embeddings::Vector.new(vdef)- else- vector_rep = DiscourseAi::Embeddings::Vector.instance- end- topics_table_name = DiscourseAi::Embeddings::Schema::TOPICS_TABLE-- topics =- Topic- .joins("LEFT JOIN #{topics_table_name} ON #{topics_table_name}.topic_id = topics.id")- .where("#{topics_table_name}.topic_id IS NULL")- .where("category_id IN (?)", public_categories)- .where(deleted_at: nil)- .order("topics.id DESC")-- Parallel.each(topics.all, in_processes: args[:concurrency].to_i, progress: "Topics") do |t|- ActiveRecord::Base.connection_pool.with_connection do- vector_rep.generate_representation_from(t)- end- end-- posts_table_name = DiscourseAi::Embeddings::Schema::POSTS_TABLE- posts =- Post- .joins("LEFT JOIN #{posts_table_name} ON #{posts_table_name}.post_id = posts.id")- .where("#{posts_table_name}.post_id IS NULL")- .where(deleted_at: nil)- .order("posts.id DESC")-- Parallel.each(posts.all, in_processes: args[:concurrency].to_i, progress: "Posts") do |t|- ActiveRecord::Base.connection_pool.with_connection do- vector_rep.generate_representation_from(t)- end- end-enddiff --git a/plugins/discourse-ai/lib/tasks/modules/sentiment/backfill.rake b/plugins/discourse-ai/lib/tasks/modules/sentiment/backfill.rakedeleted file mode 100644index a9ade1ba422..00000000000--- a/plugins/discourse-ai/lib/tasks/modules/sentiment/backfill.rake+++ /dev/null@@ -1,11 +0,0 @@-# frozen_string_literal: true--desc "Backfill sentiment for all posts"-task "ai:sentiment:backfill", [:start_post] => [:environment] do |_, args|- DiscourseAi::Sentiment::PostClassification- .backfill_query(from_post_id: args[:start_post].to_i)- .find_in_batches do |batch|- print "."- DiscourseAi::Sentiment::PostClassification.new.bulk_classify!(batch)- end-enddiff --git a/plugins/discourse-ai/lib/tasks/modules/toxicity/calibration.rake b/plugins/discourse-ai/lib/tasks/modules/toxicity/calibration.rakedeleted file mode 100644index c9a62d65a81..00000000000--- a/plugins/discourse-ai/lib/tasks/modules/toxicity/calibration.rake+++ /dev/null@@ -1,77 +0,0 @@-# frozen_string_literal: true--def classify(content)- DiscourseAi::Inference::DiscourseClassifier.perform!(- "#{SiteSetting.ai_toxicity_inference_service_api_endpoint}/api/v1/classify",- SiteSetting.ai_toxicity_inference_service_api_model,- content,- SiteSetting.ai_toxicity_inference_service_api_key,- )-end--desc "Uses existing flagged posts to suggest a configuration threshold"-task "ai:toxicity:calibration_stats", [:set_size] => [:environment] do |_, args|- flag_agreed =- PostAction- .where(post_action_type_id: 4, disagreed_at: nil, deferred_at: nil)- .where("post_actions.user_id > 0")- .includes(:post, :user)- .where(user: { admin: false, moderator: false })- .where.not(posts: { raw: nil })- .order(created_at: :desc)- .limit(args[:set_size])- .pluck(:raw)-- flag_not_agreed =- PostAction- .where(post_action_type_id: 4)- .where("(disagreed_at IS NOT NULL OR deferred_at IS NOT NULL)")- .where("post_actions.user_id > 0")- .includes(:post, :user)- .where(user: { admin: false, moderator: false })- .where.not(posts: { raw: nil })- .order(created_at: :desc)- .limit(args[:set_size])- .pluck(:raw)-- flag_agreed_scores = flag_agreed.map { classify(_1) }- flag_not_agreed_scores = flag_not_agreed.map { classify(_1) }-- DiscourseAi::Toxicity::Classifier::CLASSIFICATION_LABELS.each do |label|- puts "Label: #{label}"-- label_agreed_scores = flag_agreed_scores.map { _1[label] }- label_not_agreed_scores = flag_not_agreed_scores.map { _1[label] }-- puts "Flagged posts score:"- puts "Max: #{label_agreed_scores.max}"- puts "Min: #{label_agreed_scores.min}"- puts "Avg: #{label_agreed_scores.sum(0.0) / label_agreed_scores.size}"- puts "Median: #{label_agreed_scores.sort[label_agreed_scores.size / 2]}"- puts "Stddev: #{Math.sqrt(label_agreed_scores.map { (_1 - label_agreed_scores.sum(0.0) / label_agreed_scores.size)**2 }.sum(0.0) / label_agreed_scores.size)}"-- puts "Flagged posts score:"- puts "Max: #{label_not_agreed_scores.max}"- puts "Min: #{label_not_agreed_scores.min}"- puts "Avg: #{label_not_agreed_scores.sum(0.0) / label_not_agreed_scores.size}"- puts "Median: #{label_not_agreed_scores.sort[label_not_agreed_scores.size / 2]}"- puts "Stddev: #{Math.sqrt(label_not_agreed_scores.map { (_1 - label_not_agreed_scores.sum(0.0) / label_not_agreed_scores.size)**2 }.sum(0.0) / label_not_agreed_scores.size)}"-- best_cutoff = 0- best_cutoff_score = 0-- (0..100)- .step(1)- .each do |cutoff|- score =- label_agreed_scores.count { _1 > cutoff } + label_not_agreed_scores.count { _1 <= cutoff }-- if score > best_cutoff_score- best_cutoff_score = score- best_cutoff = cutoff- end- end-- puts "Recommended ai_toxicity_flag_threshold_#{label} value: #{best_cutoff}"- end-enddiff --git a/plugins/discourse-ai/spec/tasks/backfill_spec.rb b/plugins/discourse-ai/spec/tasks/backfill_spec.rbdeleted file mode 100644index aa002b8b2e2..00000000000--- a/plugins/discourse-ai/spec/tasks/backfill_spec.rb+++ /dev/null@@ -1,26 +0,0 @@-# frozen_string_literal: true--require_relative "../support/sentiment_inference_stubs"--RSpec.describe "ai:sentiment:backfill" do- before do- enable_current_plugin- Rake::Task.clear- Discourse::Application.load_tasks- end-- before do- SiteSetting.ai_sentiment_model_configs =- "[{\"model_name\":\"SamLowe/roberta-base-go_emotions\",\"endpoint\":\"http://samlowe-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"j-hartmann/emotion-english-distilroberta-base\",\"endpoint\":\"http://jhartmann-emotion.com\",\"api_key\":\"123\"},{\"model_name\":\"cardiffnlp/twitter-roberta-base-sentiment-latest\",\"endpoint\":\"http://cardiffnlp-sentiment.com\",\"api_key\":\"123\"}]"- end-- it "does nothing if the topic is soft-deleted" do- target = Fabricate(:post)- SentimentInferenceStubs.stub_classification(target)- target.topic.trash!-- Rake::Task["ai:sentiment:backfill"].invoke-- expect(ClassificationResult.count).to be_zero- end-end