2023-02-24 11:25:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
module DiscourseAi
|
2023-02-24 11:25:02 -05:00
|
|
|
module Sentiment
|
|
|
|
class SentimentClassification
|
|
|
|
def type
|
|
|
|
:sentiment
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_models
|
|
|
|
SiteSetting.ai_sentiment_models.split("|")
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_classify?(target)
|
|
|
|
content_of(target).present?
|
|
|
|
end
|
|
|
|
|
2023-03-07 13:39:28 -05:00
|
|
|
def get_verdicts(_)
|
|
|
|
available_models.reduce({}) do |memo, model|
|
|
|
|
memo[model] = false
|
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_flag_based_on?(_verdicts)
|
2023-02-24 11:25:02 -05:00
|
|
|
# We don't flag based on sentiment classification.
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(target_to_classify)
|
2023-12-27 19:01:57 -05:00
|
|
|
target_content = content_of(target_to_classify)
|
2023-02-24 11:25:02 -05:00
|
|
|
|
|
|
|
available_models.reduce({}) do |memo, model|
|
|
|
|
memo[model] = request_with(model, target_content)
|
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def request_with(model, content)
|
2023-03-14 15:03:50 -04:00
|
|
|
::DiscourseAi::Inference::DiscourseClassifier.perform!(
|
2024-01-10 17:23:07 -05:00
|
|
|
"#{endpoint}/api/v1/classify",
|
2023-02-24 11:25:02 -05:00
|
|
|
model,
|
|
|
|
content,
|
|
|
|
SiteSetting.ai_sentiment_inference_service_api_key,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_of(target_to_classify)
|
|
|
|
if target_to_classify.post_number == 1
|
|
|
|
"#{target_to_classify.topic.title}\n#{target_to_classify.raw}"
|
|
|
|
else
|
|
|
|
target_to_classify.raw
|
|
|
|
end
|
|
|
|
end
|
2024-01-10 17:23:07 -05:00
|
|
|
|
|
|
|
def endpoint
|
|
|
|
if SiteSetting.ai_sentiment_inference_service_api_endpoint_srv.present?
|
|
|
|
service =
|
|
|
|
DiscourseAi::Utils::DnsSrv.lookup(
|
|
|
|
SiteSetting.ai_sentiment_inference_service_api_endpoint_srv,
|
|
|
|
)
|
|
|
|
"https://#{service.target}:#{service.port}"
|
|
|
|
else
|
|
|
|
SiteSetting.ai_sentiment_inference_service_api_endpoint
|
|
|
|
end
|
|
|
|
end
|
2023-02-24 11:25:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|