discourse-ai/app/models/model_accuracy.rb
Roman Rizzi a838116cd5
FEATURE: Use dedicated reviewables for AI flags. (#4)
This change adds two new reviewable types: ReviewableAIPost and ReviewableAIChatMessage. They have the same actions as their existing counterparts: ReviewableFlaggedPost and ReviewableChatMessage.

We'll display the model used and their accuracy when showing these flags in the review queue and adjust the latter after staff performs an action, tracking a global accuracy per existing model in a separate table.


* FEATURE: Dedicated reviewables for AI flags

* Store and adjust model accuracy

* Display accuracy in reviewable templates
2023-03-07 15:39:28 -03:00

36 lines
871 B
Ruby

# frozen_string_literal: true
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)
verdicts = reviewable.payload.to_h["verdicts"] || {}
verdicts.each do |model_name, verdict|
accuracy_model = find_by(model: model_name)
attribute =
if verdict
new_status == :approved ? :flags_agreed : :flags_disagreed
else
new_status == :rejected ? :flags_agreed : :flags_disagreed
end
accuracy_model.increment!(attribute)
end
end
def calculate_accuracy
return 0 if total_flags.zero?
(flags_agreed * 100) / total_flags
end
private
def total_flags
flags_agreed + flags_disagreed
end
end