From 303d3c07ac8d7b5c5a71c5759e4c3b2c86ebd096 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:20:29 +0200 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=90=9B=20fix(chat.py):=20fix=20typo?= =?UTF-8?q?=20in=20message.channel.send()=20method=20call=20=F0=9F=94=92?= =?UTF-8?q?=20chore(chat.py):=20add=20moderation=20check=20for=20message?= =?UTF-8?q?=20content=20in=20call=5Ffunction()=20to=20prevent=20sending=20?= =?UTF-8?q?blocked=20messages=20=F0=9F=94=92=20chore(chat.py):=20add=20mod?= =?UTF-8?q?eration=20check=20for=20query=20in=20call=5Ffunction()=20to=20p?= =?UTF-8?q?revent=20sending=20blocked=20queries=20=F0=9F=94=92=20chore(mak?= =?UTF-8?q?eprompt.py):=20add=20moderation=20check=20for=20content=20in=20?= =?UTF-8?q?chatgpt=5Fprocess()=20to=20prevent=20sending=20blocked=20conten?= =?UTF-8?q?t=20=F0=9F=94=92=20chore(makeprompt.py):=20add=20depth=20check?= =?UTF-8?q?=20to=20prevent=20recursive=20answering=20in=20chatgpt=5Fproces?= =?UTF-8?q?s()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cogs/chat.py | 7 +------ src/functionscalls.py | 11 ++++++++++- src/makeprompt.py | 26 ++++++++++++++++++-------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/cogs/chat.py b/src/cogs/chat.py index 6b69064..76a5fd1 100644 --- a/src/cogs/chat.py +++ b/src/cogs/chat.py @@ -107,7 +107,7 @@ class Chat(discord.Cog): if message.content.startswith("botator!unban"): user2ban = message.content.split(" ")[1] await banusr.unbanuser(user2ban) - await message.channel.send(f"User {user2ban} unbanned !") + await message.chafnnel.send(f"User {user2ban} unbanned !") return if str(message.author.id) in banusr.banend_users: await asyncio.sleep(2) @@ -115,11 +115,6 @@ class Chat(discord.Cog): return await mp.chat_process(self, message) - @discord.slash_command(name="say", description="Say a message") - async def say(self, ctx: discord.ApplicationContext, message: str): - await ctx.respond("Message sent !", ephemeral=True) - await ctx.send(message) - @discord.slash_command(name="redo", description="Redo a message") async def redo(self, ctx: discord.ApplicationContext): history = await ctx.channel.history(limit=2).flatten() diff --git a/src/functionscalls.py b/src/functionscalls.py index 2d94261..03ab465 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -5,6 +5,7 @@ import aiohttp import random import time +from utils.misc import moderate from simpleeval import simple_eval from bs4 import BeautifulSoup from src.config import tenor_api_key @@ -331,7 +332,7 @@ async def evaluate_math( return f"Result to math eval of {evaluable}: ```\n{str(result)}```" -async def call_function(message: discord.Message, function_call): +async def call_function(message: discord.Message, function_call, api_key): name = function_call.get("name", "") if name == "": raise FuntionCallError("No name provided") @@ -341,6 +342,14 @@ async def call_function(message: discord.Message, function_call): if name not in functions_matching: raise FuntionCallError("Invalid function name") function = functions_matching[name] + if arguments.get("message", "") != "" and moderate( + api_key=api_key, text=arguments["message"] + ): + return "Message blocked by the moderation system. Please try again." + if arguments.get("query", "") != "" and moderate( + api_key=api_key, text=arguments["query"] + ): + return "Query blocked by the moderation system. If the user asked for something edgy, please tell them in a funny way that you won't do it, but do not specify that it was blocked by the moderation system." returnable = await function(message, arguments) return returnable diff --git a/src/makeprompt.py b/src/makeprompt.py index fb39257..2444861 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -135,7 +135,7 @@ async def chatgpt_process( response = response["choices"][0]["message"] # type: ignore if response.get("function_call"): function_call = response.get("function_call") - returned = await call_function(message, function_call) + returned = await call_function(message, function_call, api_key) if returned != None: msgs.append( { @@ -153,13 +153,23 @@ async def chatgpt_process( await chatgpt_process(self, msgs, message, api_key, prompt, model, depth) else: content = response.get("content", "") - while len(content) != 0: - if len(content) > 2000: - await message.channel.send(content[:2000]) - content = content[2000:] - else: - await message.channel.send(content) - content = "" + if moderate(api_key, content, error_call): + depth += 1 + if depth > 2: + await message.channel.send( + "Oh uh, it seems like i am answering recursively. I will stop now." + ) + await chatgpt_process( + self, msgs, message, api_key, prompt, model, error_call, depth + ) + else: + while len(content) != 0: + if len(content) > 2000: + await message.channel.send(content[:2000]) + content = content[2000:] + else: + await message.channel.send(content) + content = "" async def chat_process(self, message): From 7c21934bc0cbaf30345e720ea12092af7eb54150 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:22:31 +0200 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=94=80=20chore(functionscalls.py):=20?= =?UTF-8?q?update=20import=20statement=20for=20the=20'moderate'=20function?= =?UTF-8?q?=20to=20reflect=20the=20correct=20file=20path=20=F0=9F=94=80=20?= =?UTF-8?q?chore(functionscalls.py):=20update=20import=20statement=20for?= =?UTF-8?q?=20the=20'tenor=5Fapi=5Fkey'=20variable=20to=20reflect=20the=20?= =?UTF-8?q?correct=20file=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functionscalls.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functionscalls.py b/src/functionscalls.py index 03ab465..a10dabf 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -5,7 +5,7 @@ import aiohttp import random import time -from utils.misc import moderate +from src.utils.misc import moderate from simpleeval import simple_eval from bs4 import BeautifulSoup from src.config import tenor_api_key From fa3eb3da0aa241fc71a4b91417521a528646c293 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:25:43 +0200 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=90=9B=20fix(functionscalls.py):=20aw?= =?UTF-8?q?ait=20the=20moderate=20function=20calls=20to=20properly=20handl?= =?UTF-8?q?e=20asynchronous=20moderation=20checks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/functionscalls.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/functionscalls.py b/src/functionscalls.py index a10dabf..c7da912 100644 --- a/src/functionscalls.py +++ b/src/functionscalls.py @@ -342,11 +342,11 @@ async def call_function(message: discord.Message, function_call, api_key): if name not in functions_matching: raise FuntionCallError("Invalid function name") function = functions_matching[name] - if arguments.get("message", "") != "" and moderate( + if arguments.get("message", "") != "" and await moderate( api_key=api_key, text=arguments["message"] ): return "Message blocked by the moderation system. Please try again." - if arguments.get("query", "") != "" and moderate( + if arguments.get("query", "") != "" and await moderate( api_key=api_key, text=arguments["query"] ): return "Query blocked by the moderation system. If the user asked for something edgy, please tell them in a funny way that you won't do it, but do not specify that it was blocked by the moderation system." From 9f257b4126d10aeac3b4f8acb41b4848a0ec03b3 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:26:03 +0200 Subject: [PATCH 4/7] =?UTF-8?q?=F0=9F=90=9B=20fix(makeprompt.py):=20import?= =?UTF-8?q?=20ModerationError=20from=20src.utils.misc=20to=20handle=20mode?= =?UTF-8?q?ration=20errors=20in=20chatgpt=5Fprocess=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 fix(makeprompt.py): raise ModerationError with message "Too many recursive messages" when answering recursively in chatgpt_process function --- src/makeprompt.py | 3 ++- src/utils/misc.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/makeprompt.py b/src/makeprompt.py index 2444861..b838536 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -6,7 +6,7 @@ import datetime import json from src.config import curs_data, max_uses, curs_premium, gpt_3_5_turbo_prompt -from src.utils.misc import moderate +from src.utils.misc import moderate, ModerationError from src.utils.openaicaller import openai_caller from src.functionscalls import ( call_function, @@ -159,6 +159,7 @@ async def chatgpt_process( await message.channel.send( "Oh uh, it seems like i am answering recursively. I will stop now." ) + raise ModerationError("Too many recursive messages") await chatgpt_process( self, msgs, message, api_key, prompt, model, error_call, depth ) diff --git a/src/utils/misc.py b/src/utils/misc.py index 13f1aa9..7e16edd 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -9,3 +9,7 @@ async def moderate(api_key, text, recall_func=None): input=text, ) return response["results"][0]["flagged"] # type: ignore + + +class ModerationError(Exception): + pass From a02bfb5cbcd36ce8142ea36a56f723e6a000c4a8 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:31:00 +0200 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=90=9B=20fix(makeprompt.py):=20await?= =?UTF-8?q?=20the=20moderate=20function=20call=20to=20properly=20handle=20?= =?UTF-8?q?moderation=20before=20increasing=20depth?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/makeprompt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/makeprompt.py b/src/makeprompt.py index b838536..3c02391 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -153,7 +153,7 @@ async def chatgpt_process( await chatgpt_process(self, msgs, message, api_key, prompt, model, depth) else: content = response.get("content", "") - if moderate(api_key, content, error_call): + if await moderate(api_key, content, error_call): depth += 1 if depth > 2: await message.channel.send( From ee825be892f667dc33aaca65ef0a8d2bec481899 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:44:01 +0200 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=90=9B=20fix(makeprompt.py):=20import?= =?UTF-8?q?=20Hasher=20class=20from=20src.utils.misc=20to=20resolve=20Name?= =?UTF-8?q?Error=20=F0=9F=94=92=20chore(makeprompt.py):=20add=20user=20has?= =?UTF-8?q?hing=20to=20prevent=20abuse=20and=20enable=20user=20banning=20i?= =?UTF-8?q?f=20necessary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/makeprompt.py | 3 ++- src/utils/misc.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/makeprompt.py b/src/makeprompt.py index 3c02391..c726793 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -6,7 +6,7 @@ import datetime import json from src.config import curs_data, max_uses, curs_premium, gpt_3_5_turbo_prompt -from src.utils.misc import moderate, ModerationError +from src.utils.misc import moderate, ModerationError, Hasher from src.utils.openaicaller import openai_caller from src.functionscalls import ( call_function, @@ -131,6 +131,7 @@ async def chatgpt_process( messages=msgs, functions=called_functions, function_call="auto", + user=Hasher(str(message.author.id)), #for user banning in case of abuse ) response = response["choices"][0]["message"] # type: ignore if response.get("function_call"): diff --git a/src/utils/misc.py b/src/utils/misc.py index 7e16edd..bdb5a75 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -1,3 +1,5 @@ +import hashlib + from src.utils.openaicaller import openai_caller @@ -13,3 +15,13 @@ async def moderate(api_key, text, recall_func=None): class ModerationError(Exception): pass + +class hasher: + def __init__(self): + self.hashes = {} + def __call__(self, text: str) -> str: + if self.hashes.get(text, None) is None: + self.hashes[text] = hashlib.sha256(text.encode()).hexdigest() + return self.hashes[text] + +Hasher = hasher() \ No newline at end of file From c8997ab4181daec56db438d639df14a2f84b4c84 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:46:28 +0200 Subject: [PATCH 7/7] :art: chore(*): run black to format the code --- src/makeprompt.py | 2 +- src/utils/misc.py | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/makeprompt.py b/src/makeprompt.py index c726793..f710269 100644 --- a/src/makeprompt.py +++ b/src/makeprompt.py @@ -131,7 +131,7 @@ async def chatgpt_process( messages=msgs, functions=called_functions, function_call="auto", - user=Hasher(str(message.author.id)), #for user banning in case of abuse + user=Hasher(str(message.author.id)), # for user banning in case of abuse ) response = response["choices"][0]["message"] # type: ignore if response.get("function_call"): diff --git a/src/utils/misc.py b/src/utils/misc.py index bdb5a75..15344e3 100644 --- a/src/utils/misc.py +++ b/src/utils/misc.py @@ -16,12 +16,15 @@ async def moderate(api_key, text, recall_func=None): class ModerationError(Exception): pass + class hasher: def __init__(self): self.hashes = {} + def __call__(self, text: str) -> str: if self.hashes.get(text, None) is None: self.hashes[text] = hashlib.sha256(text.encode()).hexdigest() return self.hashes[text] -Hasher = hasher() \ No newline at end of file + +Hasher = hasher()