2023-02-27 14:21:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "rails_helper"
|
|
|
|
require_relative "../support/sentiment_inference_stubs"
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
describe DiscourseAi::Classificator do
|
2023-02-27 14:21:40 -05:00
|
|
|
describe "#classify!" do
|
|
|
|
describe "saving the classification result" do
|
2024-11-04 07:14:34 -05:00
|
|
|
let(:model) { DiscourseAi::Sentiment::SentimentClassification.new }
|
|
|
|
|
2023-02-27 14:21:40 -05:00
|
|
|
let(:classification_raw_result) do
|
|
|
|
model
|
2024-11-04 07:14:34 -05:00
|
|
|
.available_classifiers
|
|
|
|
.reduce({}) do |memo, model_config|
|
|
|
|
memo[model_config.model_name] = model.transform_result(
|
|
|
|
SentimentInferenceStubs.model_response(model_config.model_name),
|
|
|
|
)
|
2023-02-27 14:21:40 -05:00
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
let(:classification) { DiscourseAi::PostClassificator.new(model) }
|
2023-02-27 14:21:40 -05:00
|
|
|
fab!(:target) { Fabricate(:post) }
|
|
|
|
|
|
|
|
before do
|
2024-11-04 07:14:34 -05:00
|
|
|
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\"}]"
|
2023-02-27 14:21:40 -05:00
|
|
|
SentimentInferenceStubs.stub_classification(target)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "stores one result per model used" do
|
|
|
|
classification.classify!(target)
|
|
|
|
|
|
|
|
stored_results = ClassificationResult.where(target: target)
|
2024-11-04 07:14:34 -05:00
|
|
|
expect(stored_results.length).to eq(model.available_classifiers.length)
|
2023-02-27 14:21:40 -05:00
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
model.available_classifiers.each do |model_config|
|
|
|
|
result = stored_results.detect { |c| c.model_used == model_config.model_name }
|
2023-02-27 14:21:40 -05:00
|
|
|
|
|
|
|
expect(result.classification_type).to eq(model.type.to_s)
|
|
|
|
expect(result.created_at).to be_present
|
|
|
|
expect(result.updated_at).to be_present
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
expected_classification = SentimentInferenceStubs.model_response(model_config.model_name)
|
|
|
|
transformed_classification = model.transform_result(expected_classification)
|
2023-02-27 14:21:40 -05:00
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
expect(result.classification).to eq(transformed_classification)
|
2023-02-27 14:21:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "updates an existing classification result" do
|
|
|
|
original_creation = 3.days.ago
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
model.available_classifiers.each do |model_config|
|
2023-02-27 14:21:40 -05:00
|
|
|
ClassificationResult.create!(
|
|
|
|
target: target,
|
2024-11-04 07:14:34 -05:00
|
|
|
model_used: model_config.model_name,
|
2023-02-27 14:21:40 -05:00
|
|
|
classification_type: model.type,
|
|
|
|
created_at: original_creation,
|
|
|
|
updated_at: original_creation,
|
|
|
|
classification: {
|
|
|
|
},
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
classification.classify!(target)
|
|
|
|
|
|
|
|
stored_results = ClassificationResult.where(target: target)
|
2024-11-04 07:14:34 -05:00
|
|
|
expect(stored_results.length).to eq(model.available_classifiers.length)
|
2023-02-27 14:21:40 -05:00
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
model.available_classifiers.each do |model_config|
|
|
|
|
result = stored_results.detect { |c| c.model_used == model_config.model_name }
|
2023-02-27 14:21:40 -05:00
|
|
|
|
|
|
|
expect(result.classification_type).to eq(model.type.to_s)
|
|
|
|
expect(result.updated_at).to be > original_creation
|
|
|
|
expect(result.created_at).to eq_time(original_creation)
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
expect(result.classification).to eq(classification_raw_result[model_config.model_name])
|
2023-02-27 14:21:40 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|