Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/Kav-K/GPT3Discord
Browse files Browse the repository at this point in the history
  • Loading branch information
Kav-K committed Jan 6, 2023
2 parents 7c7956d + 7fb849d commit 602742e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 32 deletions.
65 changes: 50 additions & 15 deletions cogs/gpt_3_commands_and_converser.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,17 @@ async def summarize_conversation(self, message, prompt):
async def on_message_edit(self, before, after):

# Moderation
if after.guild.id in self.moderation_queues and self.moderation_queues[after.guild.id] is not None:
if (
after.guild.id in self.moderation_queues
and self.moderation_queues[after.guild.id] is not None
):
# Create a timestamp that is 0.5 seconds from now
timestamp = (datetime.datetime.now() + datetime.timedelta(seconds=0.5)).timestamp()
await self.moderation_queues[after.guild.id].put(Moderation(after, timestamp))
timestamp = (
datetime.datetime.now() + datetime.timedelta(seconds=0.5)
).timestamp()
await self.moderation_queues[after.guild.id].put(
Moderation(after, timestamp)
)

if after.author.id in self.redo_users:
if after.id == original_message[after.author.id]:
Expand All @@ -514,7 +521,10 @@ async def on_message_edit(self, before, after):

print("Doing the encapsulated send")
await self.encapsulated_send(
user_id=after.author.id, prompt=edited_content, ctx=ctx, response_message=response_message
user_id=after.author.id,
prompt=edited_content,
ctx=ctx,
response_message=response_message,
)

self.redo_users[after.author.id].prompt = after.content
Expand All @@ -529,10 +539,17 @@ async def on_message(self, message):
content = message.content.strip()

# Moderations service
if message.guild.id in self.moderation_queues and self.moderation_queues[message.guild.id] is not None:
if (
message.guild.id in self.moderation_queues
and self.moderation_queues[message.guild.id] is not None
):
# Create a timestamp that is 0.5 seconds from now
timestamp = (datetime.datetime.now() + datetime.timedelta(seconds=0.5)).timestamp()
await self.moderation_queues[message.guild.id].put(Moderation(message, timestamp))
timestamp = (
datetime.datetime.now() + datetime.timedelta(seconds=0.5)
).timestamp()
await self.moderation_queues[message.guild.id].put(
Moderation(message, timestamp)
)

conversing = self.check_conversing(
message.author.id, message.channel.id, content
Expand Down Expand Up @@ -983,19 +1000,29 @@ async def converse(
async def moderations_test(self, ctx: discord.ApplicationContext, prompt: str):
await ctx.defer()
response = await self.model.send_moderations_request(prompt)
await ctx.respond(response['results'][0]['category_scores'])
await ctx.send_followup(response['results'][0]['flagged'])
await ctx.respond(response["results"][0]["category_scores"])
await ctx.send_followup(response["results"][0]["flagged"])

@add_to_group("system")
@discord.slash_command(
name="moderations",
description="The AI moderations service",
guild_ids=ALLOWED_GUILDS,
)
@discord.option(name="status", description="Enable or disable the moderations service for the current guild (on/off)", required = True)
@discord.option(name="alert_channel_id", description="The channel ID to send moderation alerts to", required=False)
@discord.option(
name="status",
description="Enable or disable the moderations service for the current guild (on/off)",
required=True,
)
@discord.option(
name="alert_channel_id",
description="The channel ID to send moderation alerts to",
required=False,
)
@discord.guild_only()
async def moderations(self, ctx: discord.ApplicationContext, status: str, alert_channel_id: str):
async def moderations(
self, ctx: discord.ApplicationContext, status: str, alert_channel_id: str
):
await ctx.defer()

status = status.lower().strip()
Expand All @@ -1007,11 +1034,19 @@ async def moderations(self, ctx: discord.ApplicationContext, status: str, alert_
# Create the moderations service.
self.moderation_queues[ctx.guild_id] = asyncio.Queue()
if self.moderation_alerts_channel or alert_channel_id:
moderations_channel = await self.bot.fetch_channel(self.moderation_alerts_channel if not alert_channel_id else alert_channel_id)
moderations_channel = await self.bot.fetch_channel(
self.moderation_alerts_channel
if not alert_channel_id
else alert_channel_id
)
else:
moderations_channel = self.moderation_alerts_channel # None
moderations_channel = self.moderation_alerts_channel # None

self.moderation_tasks[ctx.guild_id] = asyncio.ensure_future(Moderation.process_moderation_queue(self.moderation_queues[ctx.guild_id], 1, 1, moderations_channel))
self.moderation_tasks[ctx.guild_id] = asyncio.ensure_future(
Moderation.process_moderation_queue(
self.moderation_queues[ctx.guild_id], 1, 1, moderations_channel
)
)
await ctx.respond("Moderations service enabled")

elif status == "off":
Expand Down
55 changes: 40 additions & 15 deletions models/moderations_service_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
usage_service = UsageService(Path(os.environ.get("DATA_DIR", os.getcwd())))
model = Model(usage_service)

class Moderation:

class Moderation:
def __init__(self, message, timestamp):
self.message = message
self.timestamp = timestamp
Expand All @@ -27,18 +27,20 @@ def build_moderation_embed():
colour=discord.Colour.red(),
)
# Set the embed thumbnail
embed.set_thumbnail(
url="https://i.imgur.com/2oL8JSp.png"
embed.set_thumbnail(url="https://i.imgur.com/2oL8JSp.png")
embed.set_footer(
text="If you think this was a mistake, please contact the server admins."
)
embed.set_footer(text="If you think this was a mistake, please contact the server admins.")
return embed

@staticmethod
def build_admin_moderated_message(moderated_message, response_message):

# Create a discord embed to send to the user when their message gets moderated
embed = discord.Embed(
title="A message was moderated in the "+moderated_message.message.guild.name+" server",
title="A message was moderated in the "
+ moderated_message.message.guild.name
+ " server",
description=f"Message from {moderated_message.message.author.mention} was moderated: {moderated_message.message.content}",
colour=discord.Colour.yellow(),
)
Expand All @@ -48,7 +50,6 @@ def build_admin_moderated_message(moderated_message, response_message):
embed.add_field(name="Moderated message link", value=link, inline=False)
return embed


@staticmethod
def determine_moderation_result(response):
HATE_THRESHOLD = 0.005
Expand All @@ -59,12 +60,28 @@ def determine_moderation_result(response):
VIOLENCE_THRESHOLD = 0.01
VIOLENCE_GRAPHIC_THRESHOLD = 0.1

thresholds = [HATE_THRESHOLD, HATE_VIOLENCE_THRESHOLD, SELF_HARM_THRESHOLD, SEXUAL_THRESHOLD, SEXUAL_MINORS_THRESHOLD, VIOLENCE_THRESHOLD, VIOLENCE_GRAPHIC_THRESHOLD]
threshold_iterator = ['hate','hate/threatening','self-harm','sexual','sexual/minors','violence','violence/graphic']

category_scores = response['results'][0]['category_scores']

flagged = response['results'][0]['flagged']
thresholds = [
HATE_THRESHOLD,
HATE_VIOLENCE_THRESHOLD,
SELF_HARM_THRESHOLD,
SEXUAL_THRESHOLD,
SEXUAL_MINORS_THRESHOLD,
VIOLENCE_THRESHOLD,
VIOLENCE_GRAPHIC_THRESHOLD,
]
threshold_iterator = [
"hate",
"hate/threatening",
"self-harm",
"sexual",
"sexual/minors",
"violence",
"violence/graphic",
]

category_scores = response["results"][0]["category_scores"]

flagged = response["results"][0]["flagged"]

# Iterate the category scores using the threshold_iterator and compare the values to thresholds
for category, threshold in zip(threshold_iterator, thresholds):
Expand All @@ -90,18 +107,26 @@ async def process_moderation_queue(

# Check if the current timestamp is greater than the deletion timestamp
if datetime.now().timestamp() > to_moderate.timestamp:
response = await model.send_moderations_request(to_moderate.message.content)
response = await model.send_moderations_request(
to_moderate.message.content
)
moderation_result = Moderation.determine_moderation_result(response)

if moderation_result:
# Take care of the flagged message
response_message = await to_moderate.message.reply(embed=Moderation.build_moderation_embed())
response_message = await to_moderate.message.reply(
embed=Moderation.build_moderation_embed()
)
# Do the same response as above but use an ephemeral message
await to_moderate.message.delete()

# Send to the moderation alert channel
if moderations_alert_channel:
await moderations_alert_channel.send(embed=Moderation.build_admin_moderated_message(to_moderate, response_message))
await moderations_alert_channel.send(
embed=Moderation.build_admin_moderated_message(
to_moderate, response_message
)
)

else:
await moderation_queue.put(to_moderate)
Expand Down
3 changes: 1 addition & 2 deletions models/openai_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ async def valid_text_request(self, response):
async def send_moderations_request(self, text):
# Use aiohttp to send the above request:
async with aiohttp.ClientSession() as session:
headers={
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.openai_key}",
}
Expand All @@ -332,7 +332,6 @@ async def send_moderations_request(self, text):
) as response:
return await response.json()


async def send_summary_request(self, prompt):
"""
Sends a summary request to the OpenAI API
Expand Down

0 comments on commit 602742e

Please sign in to comment.