From b1352ff8b76a6e70915018b440615ba8185d7d9c Mon Sep 17 00:00:00 2001 From: takatost Date: Wed, 11 Oct 2023 17:11:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20using=20random=20sampling=20to=20check?= =?UTF-8?q?=20if=20it=20violates=20the=20review=20mechan=E2=80=A6=20(#1308?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/core/helper/moderation.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/api/core/helper/moderation.py b/api/core/helper/moderation.py index 63d968774e..ac71af9ef4 100644 --- a/api/core/helper/moderation.py +++ b/api/core/helper/moderation.py @@ -1,4 +1,5 @@ import logging +import random import openai @@ -16,19 +17,20 @@ def check_moderation(model_provider: BaseModelProvider, text: str) -> bool: length = 2000 text_chunks = [text[i:i + length] for i in range(0, len(text), length)] - max_text_chunks = 32 - chunks = [text_chunks[i:i + max_text_chunks] for i in range(0, len(text_chunks), max_text_chunks)] + if len(text_chunks) == 0: + return True - for text_chunk in chunks: - try: - moderation_result = openai.Moderation.create(input=text_chunk, - api_key=hosted_model_providers.openai.api_key) - except Exception as ex: - logging.exception(ex) - raise LLMBadRequestError('Rate limit exceeded, please try again later.') + text_chunk = random.choice(text_chunks) - for result in moderation_result.results: - if result['flagged'] is True: - return False + try: + moderation_result = openai.Moderation.create(input=text_chunk, + api_key=hosted_model_providers.openai.api_key) + except Exception as ex: + logging.exception(ex) + raise LLMBadRequestError('Rate limit exceeded, please try again later.') + + for result in moderation_result.results: + if result['flagged'] is True: + return False return True