mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-12 00:58:57 +08:00
feat: using random sampling to check if it violates the review mechan… (#1308)
This commit is contained in:
parent
cc63c8499f
commit
b1352ff8b7
@ -1,4 +1,5 @@
|
|||||||
import logging
|
import logging
|
||||||
|
import random
|
||||||
|
|
||||||
import openai
|
import openai
|
||||||
|
|
||||||
@ -16,19 +17,20 @@ def check_moderation(model_provider: BaseModelProvider, text: str) -> bool:
|
|||||||
length = 2000
|
length = 2000
|
||||||
text_chunks = [text[i:i + length] for i in range(0, len(text), length)]
|
text_chunks = [text[i:i + length] for i in range(0, len(text), length)]
|
||||||
|
|
||||||
max_text_chunks = 32
|
if len(text_chunks) == 0:
|
||||||
chunks = [text_chunks[i:i + max_text_chunks] for i in range(0, len(text_chunks), max_text_chunks)]
|
return True
|
||||||
|
|
||||||
for text_chunk in chunks:
|
text_chunk = random.choice(text_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.')
|
|
||||||
|
|
||||||
for result in moderation_result.results:
|
try:
|
||||||
if result['flagged'] is True:
|
moderation_result = openai.Moderation.create(input=text_chunk,
|
||||||
return False
|
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
|
return True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user