From aa2fca6086daf39e6ad4bebe276c1a4fac748812 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Tue, 14 Mar 2023 16:03:50 -0300 Subject: [PATCH] DEV: DiscourseAI -> DiscourseAi rename to have consistent folders and files (#9) --- app/models/model_accuracy.rb | 2 +- ...essage.rb => reviewable_ai_chat_message.rb} | 2 +- ...wable_a_i_post.rb => reviewable_ai_post.rb} | 2 +- ...alizer.rb => ai_chat_channel_serializer.rb} | 2 +- ...> reviewable_ai_chat_message_serializer.rb} | 4 ++-- ...zer.rb => reviewable_ai_post_serializer.rb} | 2 +- ...sage.hbs => reviewable-ai-chat-message.hbs} | 0 ...essage.js => reviewable-ai-chat-message.js} | 2 +- ...wable-aipost.hbs => reviewable-ai-post.hbs} | 0 .../discourse/components/reviewable-ai-post.js | 3 +++ .../discourse/components/reviewable-aipost.js | 3 --- ...4184514_migrate_discourse_ai_reviewables.rb | 13 +++++++++++++ lib/discourse_ai/engine.rb | 5 +++-- lib/modules/nsfw/entry_point.rb | 4 ++-- .../nsfw/jobs/regular/evaluate_post_uploads.rb | 2 +- lib/modules/nsfw/nsfw_classification.rb | 4 ++-- lib/modules/sentiment/entry_point.rb | 3 ++- .../jobs/regular/post_sentiment_analysis.rb | 4 ++-- .../sentiment/sentiment_classification.rb | 4 ++-- lib/modules/toxicity/entry_point.rb | 7 ++++--- .../regular/toxicity_classify_chat_message.rb | 4 ++-- .../jobs/regular/toxicity_classify_post.rb | 4 ++-- lib/modules/toxicity/scan_queue.rb | 2 +- .../toxicity/toxicity_classification.rb | 4 ++-- lib/shared/chat_message_classificator.rb | 4 ++-- lib/shared/classificator.rb | 2 +- lib/shared/inference/discourse_classifier.rb | 2 +- lib/shared/inference/discourse_reranker.rb | 2 +- lib/shared/inference/openai_completions.rb | 2 +- lib/shared/inference/openai_embeddings.rb | 2 +- lib/shared/post_classificator.rb | 4 ++-- lib/tasks/modules/toxicity/calibration.rake | 4 ++-- plugin.rb | 18 +++++++++--------- spec/lib/modules/nsfw/entry_point_spec.rb | 2 +- .../jobs/regular/evaluate_post_uploads_spec.rb | 14 +++++++------- .../modules/nsfw/nsfw_classification_spec.rb | 2 +- spec/lib/modules/sentiment/entry_point_spec.rb | 2 +- .../sentiment/sentiment_classification_spec.rb | 2 +- spec/lib/modules/toxicity/entry_point_spec.rb | 2 +- .../toxicity_classify_chat_message_spec.rb | 10 +++++----- .../regular/toxicity_classify_post_spec.rb | 10 +++++----- spec/lib/modules/toxicity/scan_queue_spec.rb | 2 +- .../toxicity/toxicity_classification_spec.rb | 2 +- spec/models/model_accuracy_spec.rb | 8 ++++---- ...c.rb => reviewable_ai_chat_message_spec.rb} | 2 +- ...post_spec.rb => reviewable_ai_post_spec.rb} | 2 +- spec/plugin_spec.rb | 6 +++--- spec/shared/chat_message_classificator_spec.rb | 10 +++++----- spec/shared/classificator_spec.rb | 6 +++--- spec/shared/post_classificator_spec.rb | 10 +++++----- spec/support/sentiment_inference_stubs.rb | 2 +- 51 files changed, 116 insertions(+), 100 deletions(-) rename app/models/{reviewable_a_i_chat_message.rb => reviewable_ai_chat_message.rb} (98%) rename app/models/{reviewable_a_i_post.rb => reviewable_ai_post.rb} (99%) rename app/serializers/{a_i_chat_channel_serializer.rb => ai_chat_channel_serializer.rb} (66%) rename app/serializers/{reviewable_a_i_chat_message_serializer.rb => reviewable_ai_chat_message_serializer.rb} (71%) rename app/serializers/{reviewable_a_i_post_serializer.rb => reviewable_ai_post_serializer.rb} (66%) rename assets/javascripts/discourse/components/{reviewable-aichat-message.hbs => reviewable-ai-chat-message.hbs} (100%) rename assets/javascripts/discourse/components/{reviewable-aichat-message.js => reviewable-ai-chat-message.js} (79%) rename assets/javascripts/discourse/components/{reviewable-aipost.hbs => reviewable-ai-post.hbs} (100%) create mode 100644 assets/javascripts/discourse/components/reviewable-ai-post.js delete mode 100644 assets/javascripts/discourse/components/reviewable-aipost.js create mode 100644 db/post_migrate/20230314184514_migrate_discourse_ai_reviewables.rb rename spec/models/{reviewable_a_i_chat_message_spec.rb => reviewable_ai_chat_message_spec.rb} (97%) rename spec/models/{reviewable_a_i_post_spec.rb => reviewable_ai_post_spec.rb} (99%) diff --git a/app/models/model_accuracy.rb b/app/models/model_accuracy.rb index 519429c0..bbf32415 100644 --- a/app/models/model_accuracy.rb +++ b/app/models/model_accuracy.rb @@ -3,7 +3,7 @@ class ModelAccuracy < ActiveRecord::Base def self.adjust_model_accuracy(new_status, reviewable) return unless %i[approved rejected].include?(new_status) - return unless [ReviewableAIPost, ReviewableAIChatMessage].include?(reviewable.class) + return unless [ReviewableAiPost, ReviewableAiChatMessage].include?(reviewable.class) verdicts = reviewable.payload.to_h["verdicts"] || {} diff --git a/app/models/reviewable_a_i_chat_message.rb b/app/models/reviewable_ai_chat_message.rb similarity index 98% rename from app/models/reviewable_a_i_chat_message.rb rename to app/models/reviewable_ai_chat_message.rb index d56bc1aa..4751c31e 100644 --- a/app/models/reviewable_a_i_chat_message.rb +++ b/app/models/reviewable_ai_chat_message.rb @@ -2,7 +2,7 @@ require_dependency "reviewable" -class ReviewableAIChatMessage < Reviewable +class ReviewableAiChatMessage < Reviewable def self.action_aliases { agree_and_keep_hidden: :agree_and_delete, diff --git a/app/models/reviewable_a_i_post.rb b/app/models/reviewable_ai_post.rb similarity index 99% rename from app/models/reviewable_a_i_post.rb rename to app/models/reviewable_ai_post.rb index 87470d52..3f65b71a 100644 --- a/app/models/reviewable_a_i_post.rb +++ b/app/models/reviewable_ai_post.rb @@ -2,7 +2,7 @@ require_dependency "reviewable" -class ReviewableAIPost < Reviewable +class ReviewableAiPost < Reviewable # Penalties are handled by the modal after the action is performed def self.action_aliases { diff --git a/app/serializers/a_i_chat_channel_serializer.rb b/app/serializers/ai_chat_channel_serializer.rb similarity index 66% rename from app/serializers/a_i_chat_channel_serializer.rb rename to app/serializers/ai_chat_channel_serializer.rb index a47ddce7..ae5fafb5 100644 --- a/app/serializers/a_i_chat_channel_serializer.rb +++ b/app/serializers/ai_chat_channel_serializer.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -class AIChatChannelSerializer < ApplicationSerializer +class AiChatChannelSerializer < ApplicationSerializer attributes :id, :chatable, :chatable_type, :chatable_url, :title, :slug end diff --git a/app/serializers/reviewable_a_i_chat_message_serializer.rb b/app/serializers/reviewable_ai_chat_message_serializer.rb similarity index 71% rename from app/serializers/reviewable_a_i_chat_message_serializer.rb rename to app/serializers/reviewable_ai_chat_message_serializer.rb index a41f39c9..6cfd1bda 100644 --- a/app/serializers/reviewable_a_i_chat_message_serializer.rb +++ b/app/serializers/reviewable_ai_chat_message_serializer.rb @@ -2,12 +2,12 @@ require_dependency "reviewable_serializer" -class ReviewableAIChatMessageSerializer < ReviewableSerializer +class ReviewableAiChatMessageSerializer < ReviewableSerializer payload_attributes :accuracies, :message_cooked target_attributes :cooked attributes :target_id - has_one :chat_channel, serializer: AIChatChannelSerializer, root: false, embed: :objects + has_one :chat_channel, serializer: AiChatChannelSerializer, root: false, embed: :objects def chat_channel object.chat_message.chat_channel diff --git a/app/serializers/reviewable_a_i_post_serializer.rb b/app/serializers/reviewable_ai_post_serializer.rb similarity index 66% rename from app/serializers/reviewable_a_i_post_serializer.rb rename to app/serializers/reviewable_ai_post_serializer.rb index 1f7480b0..bc7721d8 100644 --- a/app/serializers/reviewable_a_i_post_serializer.rb +++ b/app/serializers/reviewable_ai_post_serializer.rb @@ -2,6 +2,6 @@ require_dependency "reviewable_flagged_post_serializer" -class ReviewableAIPostSerializer < ReviewableFlaggedPostSerializer +class ReviewableAiPostSerializer < ReviewableFlaggedPostSerializer payload_attributes :accuracies end diff --git a/assets/javascripts/discourse/components/reviewable-aichat-message.hbs b/assets/javascripts/discourse/components/reviewable-ai-chat-message.hbs similarity index 100% rename from assets/javascripts/discourse/components/reviewable-aichat-message.hbs rename to assets/javascripts/discourse/components/reviewable-ai-chat-message.hbs diff --git a/assets/javascripts/discourse/components/reviewable-aichat-message.js b/assets/javascripts/discourse/components/reviewable-ai-chat-message.js similarity index 79% rename from assets/javascripts/discourse/components/reviewable-aichat-message.js rename to assets/javascripts/discourse/components/reviewable-ai-chat-message.js index 756b8f81..87cc8110 100644 --- a/assets/javascripts/discourse/components/reviewable-aichat-message.js +++ b/assets/javascripts/discourse/components/reviewable-ai-chat-message.js @@ -1,7 +1,7 @@ import Component from "@glimmer/component"; import { inject as service } from "@ember/service"; -export default class ReviewableAIChatMessage extends Component { +export default class ReviewableAiChatMessage extends Component { @service store; get chatChannel() { diff --git a/assets/javascripts/discourse/components/reviewable-aipost.hbs b/assets/javascripts/discourse/components/reviewable-ai-post.hbs similarity index 100% rename from assets/javascripts/discourse/components/reviewable-aipost.hbs rename to assets/javascripts/discourse/components/reviewable-ai-post.hbs diff --git a/assets/javascripts/discourse/components/reviewable-ai-post.js b/assets/javascripts/discourse/components/reviewable-ai-post.js new file mode 100644 index 00000000..60ca856c --- /dev/null +++ b/assets/javascripts/discourse/components/reviewable-ai-post.js @@ -0,0 +1,3 @@ +import Component from "@glimmer/component"; + +export default class ReviewableAiPost extends Component {} diff --git a/assets/javascripts/discourse/components/reviewable-aipost.js b/assets/javascripts/discourse/components/reviewable-aipost.js deleted file mode 100644 index 57dda6ed..00000000 --- a/assets/javascripts/discourse/components/reviewable-aipost.js +++ /dev/null @@ -1,3 +0,0 @@ -import Component from "@glimmer/component"; - -export default class ReviewableAIPost extends Component {} diff --git a/db/post_migrate/20230314184514_migrate_discourse_ai_reviewables.rb b/db/post_migrate/20230314184514_migrate_discourse_ai_reviewables.rb new file mode 100644 index 00000000..29d393e9 --- /dev/null +++ b/db/post_migrate/20230314184514_migrate_discourse_ai_reviewables.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true +class MigrateDiscourseAiReviewables < ActiveRecord::Migration[7.0] + def up + DB.exec("UPDATE reviewables SET type='ReviewableAiPost' WHERE type='ReviewableAIPost'") + DB.exec( + "UPDATE reviewables SET type='ReviewableAiChatMessage' WHERE type='ReviewableAIChatMessage'", + ) + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/lib/discourse_ai/engine.rb b/lib/discourse_ai/engine.rb index 11a00738..a694a91e 100644 --- a/lib/discourse_ai/engine.rb +++ b/lib/discourse_ai/engine.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true -module DiscourseAI +module DiscourseAi class Engine < ::Rails::Engine - isolate_namespace DiscourseAI + engine_name PLUGIN_NAME + isolate_namespace DiscourseAi end end diff --git a/lib/modules/nsfw/entry_point.rb b/lib/modules/nsfw/entry_point.rb index 2b3445d0..864c653f 100644 --- a/lib/modules/nsfw/entry_point.rb +++ b/lib/modules/nsfw/entry_point.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module DiscourseAI +module DiscourseAi module NSFW class EntryPoint def load_files @@ -12,7 +12,7 @@ module DiscourseAI nsfw_detection_cb = Proc.new do |post| if SiteSetting.ai_nsfw_detection_enabled && - DiscourseAI::NSFW::NSFWClassification.new.can_classify?(post) + DiscourseAi::NSFW::NSFWClassification.new.can_classify?(post) Jobs.enqueue(:evaluate_post_uploads, post_id: post.id) end end diff --git a/lib/modules/nsfw/jobs/regular/evaluate_post_uploads.rb b/lib/modules/nsfw/jobs/regular/evaluate_post_uploads.rb index 8441b9ce..80dc75d5 100644 --- a/lib/modules/nsfw/jobs/regular/evaluate_post_uploads.rb +++ b/lib/modules/nsfw/jobs/regular/evaluate_post_uploads.rb @@ -11,7 +11,7 @@ module Jobs return if post.uploads.none? { |u| FileHelper.is_supported_image?(u.url) } - DiscourseAI::PostClassificator.new(DiscourseAI::NSFW::NSFWClassification.new).classify!(post) + DiscourseAi::PostClassificator.new(DiscourseAi::NSFW::NSFWClassification.new).classify!(post) end end end diff --git a/lib/modules/nsfw/nsfw_classification.rb b/lib/modules/nsfw/nsfw_classification.rb index f3d5967f..de10db5c 100644 --- a/lib/modules/nsfw/nsfw_classification.rb +++ b/lib/modules/nsfw/nsfw_classification.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module DiscourseAI +module DiscourseAi module NSFW class NSFWClassification def type @@ -52,7 +52,7 @@ module DiscourseAI upload_url = Discourse.store.cdn_url(upload.url) upload_url = "#{Discourse.base_url_no_prefix}#{upload_url}" if upload_url.starts_with?("/") - DiscourseAI::Inference::DiscourseClassifier.perform!( + DiscourseAi::Inference::DiscourseClassifier.perform!( "#{SiteSetting.ai_nsfw_inference_service_api_endpoint}/api/v1/classify", model, upload_url, diff --git a/lib/modules/sentiment/entry_point.rb b/lib/modules/sentiment/entry_point.rb index d813b5c4..dfc13670 100644 --- a/lib/modules/sentiment/entry_point.rb +++ b/lib/modules/sentiment/entry_point.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true -module DiscourseAI + +module DiscourseAi module Sentiment class EntryPoint def load_files diff --git a/lib/modules/sentiment/jobs/regular/post_sentiment_analysis.rb b/lib/modules/sentiment/jobs/regular/post_sentiment_analysis.rb index ec642d3b..cdbb668d 100644 --- a/lib/modules/sentiment/jobs/regular/post_sentiment_analysis.rb +++ b/lib/modules/sentiment/jobs/regular/post_sentiment_analysis.rb @@ -9,8 +9,8 @@ module ::Jobs post = Post.find_by(id: post_id, post_type: Post.types[:regular]) return if post&.raw.blank? - DiscourseAI::PostClassificator.new( - DiscourseAI::Sentiment::SentimentClassification.new, + DiscourseAi::PostClassificator.new( + DiscourseAi::Sentiment::SentimentClassification.new, ).classify!(post) end end diff --git a/lib/modules/sentiment/sentiment_classification.rb b/lib/modules/sentiment/sentiment_classification.rb index 633563dd..00993d01 100644 --- a/lib/modules/sentiment/sentiment_classification.rb +++ b/lib/modules/sentiment/sentiment_classification.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module DiscourseAI +module DiscourseAi module Sentiment class SentimentClassification def type @@ -39,7 +39,7 @@ module DiscourseAI private def request_with(model, content) - ::DiscourseAI::Inference::DiscourseClassifier.perform!( + ::DiscourseAi::Inference::DiscourseClassifier.perform!( "#{SiteSetting.ai_sentiment_inference_service_api_endpoint}/api/v1/classify", model, content, diff --git a/lib/modules/toxicity/entry_point.rb b/lib/modules/toxicity/entry_point.rb index 352f3fd8..32a24779 100644 --- a/lib/modules/toxicity/entry_point.rb +++ b/lib/modules/toxicity/entry_point.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true -module DiscourseAI + +module DiscourseAi module Toxicity class EntryPoint def load_files @@ -11,13 +12,13 @@ module DiscourseAI end def inject_into(plugin) - post_analysis_cb = Proc.new { |post| DiscourseAI::Toxicity::ScanQueue.enqueue_post(post) } + post_analysis_cb = Proc.new { |post| DiscourseAi::Toxicity::ScanQueue.enqueue_post(post) } plugin.on(:post_created, &post_analysis_cb) plugin.on(:post_edited, &post_analysis_cb) chat_message_analysis_cb = - Proc.new { |message| DiscourseAI::Toxicity::ScanQueue.enqueue_chat_message(message) } + Proc.new { |message| DiscourseAi::Toxicity::ScanQueue.enqueue_chat_message(message) } plugin.on(:chat_message_created, &chat_message_analysis_cb) plugin.on(:chat_message_edited, &chat_message_analysis_cb) diff --git a/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message.rb b/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message.rb index a1bee072..7f538ee5 100644 --- a/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message.rb +++ b/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message.rb @@ -10,8 +10,8 @@ module ::Jobs chat_message = ChatMessage.find_by(id: chat_message_id) return if chat_message&.message.blank? - DiscourseAI::ChatMessageClassificator.new( - DiscourseAI::Toxicity::ToxicityClassification.new, + DiscourseAi::ChatMessageClassificator.new( + DiscourseAi::Toxicity::ToxicityClassification.new, ).classify!(chat_message) end end diff --git a/lib/modules/toxicity/jobs/regular/toxicity_classify_post.rb b/lib/modules/toxicity/jobs/regular/toxicity_classify_post.rb index 2b055ca0..c96904cd 100644 --- a/lib/modules/toxicity/jobs/regular/toxicity_classify_post.rb +++ b/lib/modules/toxicity/jobs/regular/toxicity_classify_post.rb @@ -11,8 +11,8 @@ module ::Jobs post = Post.find_by(id: post_id, post_type: Post.types[:regular]) return if post&.raw.blank? - DiscourseAI::PostClassificator.new( - DiscourseAI::Toxicity::ToxicityClassification.new, + DiscourseAi::PostClassificator.new( + DiscourseAi::Toxicity::ToxicityClassification.new, ).classify!(post) end end diff --git a/lib/modules/toxicity/scan_queue.rb b/lib/modules/toxicity/scan_queue.rb index c890f3f6..ab6cc5c6 100644 --- a/lib/modules/toxicity/scan_queue.rb +++ b/lib/modules/toxicity/scan_queue.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi module Toxicity class ScanQueue class << self diff --git a/lib/modules/toxicity/toxicity_classification.rb b/lib/modules/toxicity/toxicity_classification.rb index 815b9269..604fee4e 100644 --- a/lib/modules/toxicity/toxicity_classification.rb +++ b/lib/modules/toxicity/toxicity_classification.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module DiscourseAI +module DiscourseAi module Toxicity class ToxicityClassification CLASSIFICATION_LABELS = %i[ @@ -42,7 +42,7 @@ module DiscourseAI def request(target_to_classify) data = - ::DiscourseAI::Inference::DiscourseClassifier.perform!( + ::DiscourseAi::Inference::DiscourseClassifier.perform!( "#{SiteSetting.ai_toxicity_inference_service_api_endpoint}/api/v1/classify", SiteSetting.ai_toxicity_inference_service_api_model, content_of(target_to_classify), diff --git a/lib/shared/chat_message_classificator.rb b/lib/shared/chat_message_classificator.rb index 3f49a01e..ef52c177 100644 --- a/lib/shared/chat_message_classificator.rb +++ b/lib/shared/chat_message_classificator.rb @@ -1,12 +1,12 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi class ChatMessageClassificator < Classificator private def flag!(chat_message, classification, verdicts, accuracies) reviewable = - ReviewableAIChatMessage.needs_review!( + ReviewableAiChatMessage.needs_review!( created_by: Discourse.system_user, target: chat_message, reviewable_by_moderator: true, diff --git a/lib/shared/classificator.rb b/lib/shared/classificator.rb index d9ae948f..f56b893f 100644 --- a/lib/shared/classificator.rb +++ b/lib/shared/classificator.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi class Classificator def initialize(classification_model) @classification_model = classification_model diff --git a/lib/shared/inference/discourse_classifier.rb b/lib/shared/inference/discourse_classifier.rb index 601c3245..7ab2f188 100644 --- a/lib/shared/inference/discourse_classifier.rb +++ b/lib/shared/inference/discourse_classifier.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi module Inference class DiscourseClassifier def self.perform!(endpoint, model, content, api_key) diff --git a/lib/shared/inference/discourse_reranker.rb b/lib/shared/inference/discourse_reranker.rb index b50953f5..76513886 100644 --- a/lib/shared/inference/discourse_reranker.rb +++ b/lib/shared/inference/discourse_reranker.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi module Inference class DiscourseReranker def self.perform!(endpoint, model, content, candidates, api_key) diff --git a/lib/shared/inference/openai_completions.rb b/lib/shared/inference/openai_completions.rb index 49364e5f..6bdd944f 100644 --- a/lib/shared/inference/openai_completions.rb +++ b/lib/shared/inference/openai_completions.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi module Inference class OpenAICompletions def self.perform!(model, content, api_key) diff --git a/lib/shared/inference/openai_embeddings.rb b/lib/shared/inference/openai_embeddings.rb index 116bc62e..3b1df73d 100644 --- a/lib/shared/inference/openai_embeddings.rb +++ b/lib/shared/inference/openai_embeddings.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi module Inference class OpenAIEmbeddings def self.perform!(content, model = nil) diff --git a/lib/shared/post_classificator.rb b/lib/shared/post_classificator.rb index 974a4685..af047601 100644 --- a/lib/shared/post_classificator.rb +++ b/lib/shared/post_classificator.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module ::DiscourseAI +module ::DiscourseAi class PostClassificator < Classificator private @@ -8,7 +8,7 @@ module ::DiscourseAI post.hide!(ReviewableScore.types[:inappropriate]) reviewable = - ReviewableAIPost.needs_review!( + ReviewableAiPost.needs_review!( created_by: Discourse.system_user, target: post, reviewable_by_moderator: true, diff --git a/lib/tasks/modules/toxicity/calibration.rake b/lib/tasks/modules/toxicity/calibration.rake index 50cf3f1c..30ed632e 100644 --- a/lib/tasks/modules/toxicity/calibration.rake +++ b/lib/tasks/modules/toxicity/calibration.rake @@ -1,7 +1,7 @@ # frozen_string_literal: true def classify(content) - ::DiscourseAI::Inference::DiscourseClassifier.perform!( + ::DiscourseAi::Inference::DiscourseClassifier.perform!( "#{SiteSetting.ai_toxicity_inference_service_api_endpoint}/api/v1/classify", SiteSetting.ai_toxicity_inference_service_api_model, content, @@ -37,7 +37,7 @@ task "ai:toxicity:calibration_stats", [:set_size] => [:environment] do |_, args| 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| + DiscourseAi::Toxicity::Classifier::CLASSIFICATION_LABELS.each do |label| puts "Label: #{label}" label_agreed_scores = flag_agreed_scores.map { _1[label] } diff --git a/plugin.rb b/plugin.rb index 7181712e..afaf0846 100644 --- a/plugin.rb +++ b/plugin.rb @@ -9,13 +9,13 @@ enabled_site_setting :discourse_ai_enabled +module ::DiscourseAi + PLUGIN_NAME = "discourse-ai" +end + require_relative "lib/discourse_ai/engine" after_initialize do - module ::DiscourseAI - PLUGIN_NAME = "discourse-ai" - end - require_relative "lib/shared/inference/discourse_classifier" require_relative "lib/shared/inference/discourse_reranker" require_relative "lib/shared/inference/openai_completions" @@ -30,16 +30,16 @@ after_initialize do require_relative "lib/modules/sentiment/entry_point" [ - DiscourseAI::NSFW::EntryPoint.new, - DiscourseAI::Toxicity::EntryPoint.new, - DiscourseAI::Sentiment::EntryPoint.new, + DiscourseAi::NSFW::EntryPoint.new, + DiscourseAi::Toxicity::EntryPoint.new, + DiscourseAi::Sentiment::EntryPoint.new, ].each do |a_module| a_module.load_files a_module.inject_into(self) end - register_reviewable_type ReviewableAIChatMessage - register_reviewable_type ReviewableAIPost + register_reviewable_type ReviewableAiChatMessage + register_reviewable_type ReviewableAiPost on(:reviewable_transitioned_to) do |new_status, reviewable| ModelAccuracy.adjust_model_accuracy(new_status, reviewable) diff --git a/spec/lib/modules/nsfw/entry_point_spec.rb b/spec/lib/modules/nsfw/entry_point_spec.rb index 9d466931..93142527 100644 --- a/spec/lib/modules/nsfw/entry_point_spec.rb +++ b/spec/lib/modules/nsfw/entry_point_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe DiscourseAI::NSFW::EntryPoint do +describe DiscourseAi::NSFW::EntryPoint do fab!(:user) { Fabricate(:user) } describe "registering event callbacks" do diff --git a/spec/lib/modules/nsfw/jobs/regular/evaluate_post_uploads_spec.rb b/spec/lib/modules/nsfw/jobs/regular/evaluate_post_uploads_spec.rb index 7acd60da..af0efa88 100644 --- a/spec/lib/modules/nsfw/jobs/regular/evaluate_post_uploads_spec.rb +++ b/spec/lib/modules/nsfw/jobs/regular/evaluate_post_uploads_spec.rb @@ -21,19 +21,19 @@ describe Jobs::EvaluatePostUploads do subject.execute({ post_id: post.id }) - expect(ReviewableFlaggedPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "does nothing if there's no arg called post_id" do subject.execute({}) - expect(ReviewableFlaggedPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "does nothing if no post match the given id" do subject.execute({ post_id: nil }) - expect(ReviewableFlaggedPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "does nothing if the post has no uploads" do @@ -41,7 +41,7 @@ describe Jobs::EvaluatePostUploads do subject.execute({ post_id: post_no_uploads.id }) - expect(ReviewableFlaggedPost.where(target: post_no_uploads).count).to be_zero + expect(ReviewableAiPost.where(target: post_no_uploads).count).to be_zero end it "does nothing if the upload is not an image" do @@ -50,7 +50,7 @@ describe Jobs::EvaluatePostUploads do subject.execute({ post_id: post.id }) - expect(ReviewableFlaggedPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end end @@ -61,7 +61,7 @@ describe Jobs::EvaluatePostUploads do it "flags and hides the post" do subject.execute({ post_id: post.id }) - expect(ReviewableAIPost.where(target: post).count).to eq(1) + expect(ReviewableAiPost.where(target: post).count).to eq(1) expect(post.reload.hidden?).to eq(true) end end @@ -72,7 +72,7 @@ describe Jobs::EvaluatePostUploads do it "does nothing" do subject.execute({ post_id: post.id }) - expect(ReviewableAIPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end end end diff --git a/spec/lib/modules/nsfw/nsfw_classification_spec.rb b/spec/lib/modules/nsfw/nsfw_classification_spec.rb index 80b4144a..9edfc82e 100644 --- a/spec/lib/modules/nsfw/nsfw_classification_spec.rb +++ b/spec/lib/modules/nsfw/nsfw_classification_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" require_relative "../../../support/nsfw_inference_stubs" -describe DiscourseAI::NSFW::NSFWClassification do +describe DiscourseAi::NSFW::NSFWClassification do before { SiteSetting.ai_nsfw_inference_service_api_endpoint = "http://test.com" } let(:available_models) { SiteSetting.ai_nsfw_models.split("|") } diff --git a/spec/lib/modules/sentiment/entry_point_spec.rb b/spec/lib/modules/sentiment/entry_point_spec.rb index a61c1ebf..3ba92471 100644 --- a/spec/lib/modules/sentiment/entry_point_spec.rb +++ b/spec/lib/modules/sentiment/entry_point_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe DiscourseAI::Sentiment::EntryPoint do +describe DiscourseAi::Sentiment::EntryPoint do fab!(:user) { Fabricate(:user) } describe "registering event callbacks" do diff --git a/spec/lib/modules/sentiment/sentiment_classification_spec.rb b/spec/lib/modules/sentiment/sentiment_classification_spec.rb index a1c827fb..2d0b060b 100644 --- a/spec/lib/modules/sentiment/sentiment_classification_spec.rb +++ b/spec/lib/modules/sentiment/sentiment_classification_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" require_relative "../../../support/sentiment_inference_stubs" -describe DiscourseAI::Sentiment::SentimentClassification do +describe DiscourseAi::Sentiment::SentimentClassification do fab!(:target) { Fabricate(:post) } describe "#request" do diff --git a/spec/lib/modules/toxicity/entry_point_spec.rb b/spec/lib/modules/toxicity/entry_point_spec.rb index 1c7eecfb..c5a6a609 100644 --- a/spec/lib/modules/toxicity/entry_point_spec.rb +++ b/spec/lib/modules/toxicity/entry_point_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe DiscourseAI::Toxicity::EntryPoint do +describe DiscourseAi::Toxicity::EntryPoint do fab!(:user) { Fabricate(:user) } describe "registering event callbacks" do diff --git a/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message_spec.rb b/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message_spec.rb index 22592a82..12de520d 100644 --- a/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message_spec.rb +++ b/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_chat_message_spec.rb @@ -18,19 +18,19 @@ describe Jobs::ToxicityClassifyChatMessage do subject.execute({ chat_message_id: chat_message.id }) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to be_zero + expect(ReviewableAiChatMessage.where(target: chat_message).count).to be_zero end it "does nothing if there's no arg called post_id" do subject.execute({}) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to be_zero + expect(ReviewableAiChatMessage.where(target: chat_message).count).to be_zero end it "does nothing if no post match the given id" do subject.execute({ chat_message_id: nil }) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to be_zero + expect(ReviewableAiChatMessage.where(target: chat_message).count).to be_zero end it "does nothing if the post content is blank" do @@ -38,7 +38,7 @@ describe Jobs::ToxicityClassifyChatMessage do subject.execute({ chat_message_id: chat_message.id }) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to be_zero + expect(ReviewableAiChatMessage.where(target: chat_message).count).to be_zero end end @@ -47,7 +47,7 @@ describe Jobs::ToxicityClassifyChatMessage do subject.execute({ chat_message_id: chat_message.id }) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to eq(1) + expect(ReviewableAiChatMessage.where(target: chat_message).count).to eq(1) end end end diff --git a/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_post_spec.rb b/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_post_spec.rb index 1d6ec03b..e129af51 100644 --- a/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_post_spec.rb +++ b/spec/lib/modules/toxicity/jobs/regular/toxicity_classify_post_spec.rb @@ -18,19 +18,19 @@ describe Jobs::ToxicityClassifyPost do subject.execute({ post_id: post.id }) - expect(ReviewableAIPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "does nothing if there's no arg called post_id" do subject.execute({}) - expect(ReviewableAIPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "does nothing if no post match the given id" do subject.execute({ post_id: nil }) - expect(ReviewableAIPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "does nothing if the post content is blank" do @@ -38,7 +38,7 @@ describe Jobs::ToxicityClassifyPost do subject.execute({ post_id: post.id }) - expect(ReviewableAIPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end end @@ -47,7 +47,7 @@ describe Jobs::ToxicityClassifyPost do subject.execute({ post_id: post.id }) - expect(ReviewableAIPost.where(target: post).count).to eq(1) + expect(ReviewableAiPost.where(target: post).count).to eq(1) end end end diff --git a/spec/lib/modules/toxicity/scan_queue_spec.rb b/spec/lib/modules/toxicity/scan_queue_spec.rb index 7220b943..6b2ac75e 100644 --- a/spec/lib/modules/toxicity/scan_queue_spec.rb +++ b/spec/lib/modules/toxicity/scan_queue_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe DiscourseAI::Toxicity::ScanQueue do +describe DiscourseAi::Toxicity::ScanQueue do fab!(:group) { Fabricate(:group) } before do diff --git a/spec/lib/modules/toxicity/toxicity_classification_spec.rb b/spec/lib/modules/toxicity/toxicity_classification_spec.rb index ee379bd1..9fd424a7 100644 --- a/spec/lib/modules/toxicity/toxicity_classification_spec.rb +++ b/spec/lib/modules/toxicity/toxicity_classification_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" require_relative "../../../support/toxicity_inference_stubs" -describe DiscourseAI::Toxicity::ToxicityClassification do +describe DiscourseAi::Toxicity::ToxicityClassification do fab!(:target) { Fabricate(:post) } describe "#request" do diff --git a/spec/models/model_accuracy_spec.rb b/spec/models/model_accuracy_spec.rb index 6f5ab04c..8d8735cb 100644 --- a/spec/models/model_accuracy_spec.rb +++ b/spec/models/model_accuracy_spec.rb @@ -42,7 +42,7 @@ describe ModelAccuracy do end it "updates the agreed flag if reviewable was approved and verdict is true" do - reviewable = build_reviewable(ReviewableAIPost, true) + reviewable = build_reviewable(ReviewableAiPost, true) described_class.adjust_model_accuracy(:approved, reviewable) @@ -51,7 +51,7 @@ describe ModelAccuracy do end it "updates the disagreed flag if the reviewable was approved and verdict is false" do - reviewable = build_reviewable(ReviewableAIPost, false) + reviewable = build_reviewable(ReviewableAiPost, false) described_class.adjust_model_accuracy(:approved, reviewable) @@ -60,7 +60,7 @@ describe ModelAccuracy do end it "updates the disagreed flag if reviewable was rejected and verdict is true" do - reviewable = build_reviewable(ReviewableAIPost, true) + reviewable = build_reviewable(ReviewableAiPost, true) described_class.adjust_model_accuracy(:rejected, reviewable) @@ -69,7 +69,7 @@ describe ModelAccuracy do end it "updates the agreed flag if the reviewable was rejected and verdict is false" do - reviewable = build_reviewable(ReviewableAIPost, false) + reviewable = build_reviewable(ReviewableAiPost, false) described_class.adjust_model_accuracy(:rejected, reviewable) diff --git a/spec/models/reviewable_a_i_chat_message_spec.rb b/spec/models/reviewable_ai_chat_message_spec.rb similarity index 97% rename from spec/models/reviewable_a_i_chat_message_spec.rb rename to spec/models/reviewable_ai_chat_message_spec.rb index 0796d140..a4715219 100644 --- a/spec/models/reviewable_a_i_chat_message_spec.rb +++ b/spec/models/reviewable_ai_chat_message_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -RSpec.describe ReviewableAIChatMessage, type: :model do +RSpec.describe ReviewableAiChatMessage, type: :model do fab!(:moderator) { Fabricate(:moderator) } fab!(:user) { Fabricate(:user) } fab!(:chat_channel) { Fabricate(:chat_channel) } diff --git a/spec/models/reviewable_a_i_post_spec.rb b/spec/models/reviewable_ai_post_spec.rb similarity index 99% rename from spec/models/reviewable_a_i_post_spec.rb rename to spec/models/reviewable_ai_post_spec.rb index 4f1e1766..5c786c7a 100644 --- a/spec/models/reviewable_a_i_post_spec.rb +++ b/spec/models/reviewable_ai_post_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" -describe ReviewableAIPost do +describe ReviewableAiPost do fab!(:target) { Fabricate(:post) } describe "#build_actions" do diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb index 801b81d8..fb500b7d 100644 --- a/spec/plugin_spec.rb +++ b/spec/plugin_spec.rb @@ -13,10 +13,10 @@ describe Plugin::Instance do it "adjusts model accuracy" do ToxicityInferenceStubs.stub_post_classification(post, toxic: true) SiteSetting.ai_toxicity_flag_automatically = true - classification = DiscourseAI::Toxicity::ToxicityClassification.new - classificator = DiscourseAI::PostClassificator.new(classification) + classification = DiscourseAi::Toxicity::ToxicityClassification.new + classificator = DiscourseAi::PostClassificator.new(classification) classificator.classify!(post) - reviewable = ReviewableAIPost.find_by(target: post) + reviewable = ReviewableAiPost.find_by(target: post) reviewable.perform admin, :agree_and_keep accuracy = ModelAccuracy.find_by(classification_type: classification.type) diff --git a/spec/shared/chat_message_classificator_spec.rb b/spec/shared/chat_message_classificator_spec.rb index ef853088..8d2bf59f 100644 --- a/spec/shared/chat_message_classificator_spec.rb +++ b/spec/shared/chat_message_classificator_spec.rb @@ -3,10 +3,10 @@ require "rails_helper" require_relative "../support/toxicity_inference_stubs" -describe DiscourseAI::ChatMessageClassificator do +describe DiscourseAi::ChatMessageClassificator do fab!(:chat_message) { Fabricate(:chat_message) } - let(:model) { DiscourseAI::Toxicity::ToxicityClassification.new } + let(:model) { DiscourseAi::Toxicity::ToxicityClassification.new } let(:classification) { described_class.new(model) } describe "#classify!" do @@ -27,7 +27,7 @@ describe DiscourseAI::ChatMessageClassificator do classification.classify!(chat_message) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to eq(1) + expect(ReviewableAiChatMessage.where(target: chat_message).count).to eq(1) end it "doesn't flags the message if the model decides we shouldn't" do @@ -35,14 +35,14 @@ describe DiscourseAI::ChatMessageClassificator do classification.classify!(chat_message) - expect(ReviewableAIChatMessage.where(target: chat_message).count).to be_zero + expect(ReviewableAiChatMessage.where(target: chat_message).count).to be_zero end it "includes the model accuracy in the payload" do SiteSetting.ai_toxicity_flag_automatically = true classification.classify!(chat_message) - reviewable = ReviewableAIChatMessage.find_by(target: chat_message) + reviewable = ReviewableAiChatMessage.find_by(target: chat_message) expect( reviewable.payload.dig("accuracies", SiteSetting.ai_toxicity_inference_service_api_model), diff --git a/spec/shared/classificator_spec.rb b/spec/shared/classificator_spec.rb index 58fde10d..41be592e 100644 --- a/spec/shared/classificator_spec.rb +++ b/spec/shared/classificator_spec.rb @@ -3,7 +3,7 @@ require "rails_helper" require_relative "../support/sentiment_inference_stubs" -describe DiscourseAI::Classificator do +describe DiscourseAi::Classificator do describe "#classify!" do describe "saving the classification result" do let(:classification_raw_result) do @@ -15,8 +15,8 @@ describe DiscourseAI::Classificator do end end - let(:model) { DiscourseAI::Sentiment::SentimentClassification.new } - let(:classification) { DiscourseAI::PostClassificator.new(model) } + let(:model) { DiscourseAi::Sentiment::SentimentClassification.new } + let(:classification) { DiscourseAi::PostClassificator.new(model) } fab!(:target) { Fabricate(:post) } before do diff --git a/spec/shared/post_classificator_spec.rb b/spec/shared/post_classificator_spec.rb index e04ee4df..717f4fbb 100644 --- a/spec/shared/post_classificator_spec.rb +++ b/spec/shared/post_classificator_spec.rb @@ -3,10 +3,10 @@ require "rails_helper" require_relative "../support/toxicity_inference_stubs" -describe DiscourseAI::PostClassificator do +describe DiscourseAi::PostClassificator do fab!(:post) { Fabricate(:post) } - let(:model) { DiscourseAI::Toxicity::ToxicityClassification.new } + let(:model) { DiscourseAi::Toxicity::ToxicityClassification.new } let(:classification) { described_class.new(model) } describe "#classify!" do @@ -26,7 +26,7 @@ describe DiscourseAI::PostClassificator do classification.classify!(post) - expect(ReviewableAIPost.where(target: post).count).to eq(1) + expect(ReviewableAiPost.where(target: post).count).to eq(1) expect(post.reload.hidden?).to eq(true) end @@ -35,14 +35,14 @@ describe DiscourseAI::PostClassificator do classification.classify!(post) - expect(ReviewableAIPost.where(target: post).count).to be_zero + expect(ReviewableAiPost.where(target: post).count).to be_zero end it "includes the model accuracy in the payload" do SiteSetting.ai_toxicity_flag_automatically = true classification.classify!(post) - reviewable = ReviewableAIPost.find_by(target: post) + reviewable = ReviewableAiPost.find_by(target: post) expect( reviewable.payload.dig("accuracies", SiteSetting.ai_toxicity_inference_service_api_model), diff --git a/spec/support/sentiment_inference_stubs.rb b/spec/support/sentiment_inference_stubs.rb index 49ec2c9d..209fcf8e 100644 --- a/spec/support/sentiment_inference_stubs.rb +++ b/spec/support/sentiment_inference_stubs.rb @@ -15,7 +15,7 @@ class SentimentInferenceStubs def stub_classification(post) content = post.post_number == 1 ? "#{post.topic.title}\n#{post.raw}" : post.raw - DiscourseAI::Sentiment::SentimentClassification.new.available_models.each do |model| + DiscourseAi::Sentiment::SentimentClassification.new.available_models.each do |model| WebMock .stub_request(:post, endpoint) .with(body: JSON.dump(model: model, content: content))