From ee825be892f667dc33aaca65ef0a8d2bec481899 Mon Sep 17 00:00:00 2001 From: Paillat Date: Wed, 16 Aug 2023 09:44:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(makeprompt.py):=20import=20H?= =?UTF-8?q?asher=20class=20from=20src.utils.misc=20to=20resolve=20NameErro?= =?UTF-8?q?r=20=F0=9F=94=92=20chore(makeprompt.py):=20add=20user=20hashing?= =?UTF-8?q?=20to=20prevent=20abuse=20and=20enable=20user=20banning=20if=20?= =?UTF-8?q?necessary?= 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