Skip to content

Commit

Permalink
Merge pull request #107 from realstorypro/auto-moderation
Browse files Browse the repository at this point in the history
added the auto–moderation for stories
  • Loading branch information
Leonid Medovyy committed Jul 27, 2023
2 parents dc341a5 + a4af88f commit 3dc04fd
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
5 changes: 4 additions & 1 deletion app/jobs/assemble_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ def perform(*_args)
# [x] Kill Spammy Stories
Stories::KillSpammyStoriesJob.perform_now

# [x] Images from processed stories
# [x] Automatically Moderate Stories
Stories::ModerateStoriesJob.perform_now

# [x] Images from processed & approved stories
Images::CreateImageIdeasFromStoriesJob.perform_now

# [x] Cleanup broken imaginations
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/stories/kill_spammy_stories_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class Stories::KillSpammyStoriesJob < ApplicationJob
queue_as :default

def perform(*args)
Story.spammy_stems.update_all(invalid_json: true)
Story.spammy_stems.update_all(invalid_json: true, approved: false)
end
end
9 changes: 9 additions & 0 deletions app/jobs/stories/moderate_stories_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Stories::ModerateStoriesJob < ApplicationJob
queue_as :default

def perform(*args)
Story.needs_approval.each do |story|
Stories::ModerateStoryJob.perform_now(story:)
end
end
end
83 changes: 83 additions & 0 deletions app/jobs/stories/moderate_story_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
class Stories::ModerateStoryJob < ApplicationJob
queue_as :default
include SettingsHelper

def perform(story:)
return unless story.approved.blank?

@client = OpenAI::Client.new

parsed_story = JSON.parse(story.stem)
parsed_title = parsed_story['title']
parsed_summary = parsed_story['summary']

puts parsed_title
puts parsed_summary
puts '---'

system_role = <<~SYSTEM_ROLE
You are an experienced content moderator. You are reviewing potential stories to be published.
SYSTEM_ROLE

brief = <<~BRIEF
You have received the following `news brief` with the title `#{parsed_title}` and summary
"""
#{parsed_summary}
"""
BRIEF

question = <<~QUESTION
- It is your job to return the word 'deny' if the `news brief` is promotional nature.
- It is your job to return the word 'deny' if the `news brief` contains political content.
- It is your job to return the word 'deny' if the `news brief` contains violent content.
- It is your job to return the word 'deny' if the `news brief` sexually explicit content.
- If the `news brief` is not promotional, political or violet return `approve`
QUESTION

messages = [
{ role: "system", content: system_role },
{ role: "user", content: brief },
{ role: "user", content: question }
]

invalid_json = true
counter = 0
response = nil

while invalid_json && counter < 4
response = chat(messages:)

counter += 1

break if response["error"].present?

invalid_json = false if %[approve deny].include? response["choices"][0]["message"]["content"].downcase
Rails.logger.debug "invalid_json: #{invalid_json} | counter: #{counter}"
end

if response["error"].present?
story.update(approved: false, invalid_json:)
else
decision = response["choices"][0]["message"]["content"].downcase
puts decision

if decision == 'approve'
story.update(approved: true)
else
story.update(approved: false)
end

puts '---'
end
end

def chat(messages:)
@client.chat(
parameters: {
model: ENV['OPENAI_GPT_MODEL'], # Required.
messages:,
temperature: 0.7
}
)
end
end

0 comments on commit 3dc04fd

Please sign in to comment.