discourse-ai/lib/sentiment/sentiment_classification.rb
Sam 6ddc17fd61
DEV: port directory structure to Zeitwerk (#319)
Previous to this change we relied on explicit loading for a files in Discourse AI.

This had a few downsides:

- Busywork whenever you add a file (an extra require relative)
- We were not keeping to conventions internally ... some places were OpenAI others are OpenAi
- Autoloader did not work which lead to lots of full application broken reloads when developing.

This moves all of DiscourseAI into a Zeitwerk compatible structure.

It also leaves some minimal amount of manual loading (automation - which is loading into an existing namespace that may or may not be there)

To avoid needing /lib/discourse_ai/... we mount a namespace thus we are able to keep /lib pointed at ::DiscourseAi

Various files were renamed to get around zeitwerk rules and minimize usage of custom inflections

Though we can get custom inflections to work it is not worth it, will require a Discourse core patch which means we create a hard dependency.
2023-11-29 15:17:46 +11:00

60 lines
1.4 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
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
def get_verdicts(_)
available_models.reduce({}) do |memo, model|
memo[model] = false
memo
end
end
def should_flag_based_on?(_verdicts)
# We don't flag based on sentiment classification.
false
end
def request(target_to_classify)
target_content = content_of(target_to_classify)
available_models.reduce({}) do |memo, model|
memo[model] = request_with(model, target_content)
memo
end
end
private
def request_with(model, content)
::DiscourseAi::Inference::DiscourseClassifier.perform!(
"#{SiteSetting.ai_sentiment_inference_service_api_endpoint}/api/v1/classify",
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
end
end
end