-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EN-7889 slack alert on offensive content
- Loading branch information
1 parent
62509c1
commit 498d041
Showing
11 changed files
with
152 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Offensable | ||
extend ActiveSupport::Concern | ||
|
||
alias_attribute :name, :content | ||
|
||
FIELDS = { | ||
chat_messages: "content" | ||
} | ||
|
||
included do | ||
has_one :openai_request, as: :instance | ||
|
||
after_commit :check_offense, if: :check_offense? | ||
end | ||
|
||
private | ||
|
||
def build_openai_request(attributes = {}) | ||
super(attributes.merge(instance_class: self.class.name)) | ||
end | ||
|
||
def check_offense | ||
build_openai_request(module_type: :offense).save! | ||
end | ||
|
||
def check_offense? | ||
key = self.class.table_name.to_sym | ||
|
||
return false unless FIELDS.has_key?(key) | ||
|
||
previous_changes.keys.include?(FIELDS[key]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module OpenaiServices | ||
class OffensePerformer < BasicPerformer | ||
def user_message | ||
{ | ||
role: "user", | ||
content: [ | ||
{ type: "text", text: get_formatted_prompt }, | ||
] | ||
} | ||
end | ||
|
||
def get_response_class | ||
OffenseResponse | ||
end | ||
|
||
private | ||
|
||
def handle_success response | ||
super(response) | ||
|
||
puts "-- response.offensive?: #{response.offensive?}" | ||
|
||
if response.offensive? | ||
puts "-- offensive!" | ||
SlackServices::OffensiveText.new(instance: instance, text: instance.content).notify | ||
end | ||
end | ||
|
||
def get_formatted_prompt | ||
@configuration.prompt.gsub("{{text}}", instance.content) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module OpenaiServices | ||
class OffenseResponse < BasicResponse | ||
def valid? | ||
result.in? [true, false] | ||
end | ||
|
||
def offensive? | ||
result == true | ||
end | ||
|
||
def display_result | ||
result | ||
end | ||
|
||
def result | ||
@parsed_response["result"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module SlackServices | ||
class OffensiveText < Notifier | ||
def initialize instance:, text: | ||
@instance = instance | ||
@text = text | ||
end | ||
|
||
def env | ||
ENV['SLACK_SIGNAL'] | ||
end | ||
|
||
def payload | ||
{ | ||
text: "<@#{slack_moderator_id(@instance.user)}> ou team modération (département : #{departement(@instance.user) || 'n/a'}) pouvez-vous vérifier ce texte ?", | ||
attachments: [ | ||
{ | ||
text: "Texte offensant : #{@text}" | ||
} | ||
] | ||
} | ||
end | ||
|
||
def payload_adds | ||
{ | ||
username: "Texte offensant", | ||
channel: webhook('channel'), | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
db/migrate/20241217142700_create_openai_offense_assistant_configuration_instance.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateOpenaiOffenseAssistantConfigurationInstance < ActiveRecord::Migration[6.1] | ||
def change | ||
unless Rails.env.test? | ||
OpenaiAssistant.new( | ||
module_type: :offense, | ||
version: 2, | ||
api_key: ENV['OPENAI_API_KEY'], | ||
assistant_id: ENV['OPENAI_API_OFFENSE_ASSISTANT_ID'], | ||
prompt: "{{text}}", | ||
poi_from_file: false, | ||
resource_from_file: false | ||
).save | ||
end | ||
end | ||
end |