mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-09 02:40:50 +00:00
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
36 lines
871 B
Ruby
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
|