feat: Sentiment Analysis (#7475)
This commit is contained in:
39
enterprise/app/jobs/enterprise/sentiment_analysis_job.rb
Normal file
39
enterprise/app/jobs/enterprise/sentiment_analysis_job.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
class Enterprise::SentimentAnalysisJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(message)
|
||||
return if message.account.locale != 'en'
|
||||
return if valid_incoming_message?(message)
|
||||
|
||||
save_message_sentiment(message)
|
||||
rescue StandardError => e
|
||||
Rails.logger.error("Sentiment Analysis Error for message #{message.id}: #{e}")
|
||||
ChatwootExceptionTracker.new(e, account: message.account).capture_exception
|
||||
end
|
||||
|
||||
def save_message_sentiment(message)
|
||||
# We are truncating the data here to avoind the OnnxRuntime::Error
|
||||
# Indices element out of data bounds, idx=512 must be within the inclusive range [-512,511]
|
||||
# While gathering the maningfull node the Array/tensor index is going out of bound
|
||||
|
||||
text = message.content&.truncate(2900)
|
||||
sentiment = model.predict(text)
|
||||
message.sentiment = sentiment.merge(value: label_val(sentiment))
|
||||
|
||||
message.save!
|
||||
end
|
||||
|
||||
# Model initializes OnnxRuntime::Model, with given file for inference session and to create the tensor
|
||||
def model
|
||||
model_path = ENV.fetch('SENTIMENT_FILE_PATH', nil)
|
||||
Informers::SentimentAnalysis.new(model_path) if model_path.present?
|
||||
end
|
||||
|
||||
def label_val(sentiment)
|
||||
sentiment[:label] == 'positive' ? 1 : -1
|
||||
end
|
||||
|
||||
def valid_incoming_message?(message)
|
||||
!message.incoming? || message.private?
|
||||
end
|
||||
end
|
||||
5
enterprise/app/models/enterprise/message.rb
Normal file
5
enterprise/app/models/enterprise/message.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
module Enterprise::Message
|
||||
def update_message_sentiments
|
||||
::Enterprise::SentimentAnalysisJob.perform_later(self)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,45 @@
|
||||
module Enterprise::SentimentAnalysisHelper
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
def opening_sentiments
|
||||
records = incoming_messages.first(average_message_count)
|
||||
average_sentiment(records)
|
||||
end
|
||||
|
||||
def closing_sentiments
|
||||
return unless resolved?
|
||||
|
||||
records = incoming_messages.last(average_message_count)
|
||||
average_sentiment(records)
|
||||
end
|
||||
|
||||
def average_sentiment(records)
|
||||
{
|
||||
label: average_sentiment_label(records),
|
||||
score: average_sentiment_score(records)
|
||||
}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def average_sentiment_label(records)
|
||||
value = records.pluck(:sentiment).sum { |a| a['value'].to_i }
|
||||
value.negative? ? 'negative' : 'positive'
|
||||
end
|
||||
|
||||
def average_sentiment_score(records)
|
||||
total = records.pluck(:sentiment).sum { |a| a['score'].to_f }
|
||||
total / average_message_count
|
||||
end
|
||||
|
||||
def average_message_count
|
||||
# incoming_messages.count >= 10 ? 5 : ((incoming_messages.count / 2) - 1)
|
||||
5
|
||||
end
|
||||
|
||||
def incoming_messages
|
||||
messages.incoming.where(private: false)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user