feat: using random sampling to check if it violates the review mechan… (#1308)

This commit is contained in:
takatost 2023-10-11 17:11:20 +08:00 committed by GitHub
parent cc63c8499f
commit b1352ff8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
import logging
import random
import openai
@ -16,10 +17,11 @@ 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
text_chunk = random.choice(text_chunks)
for text_chunk in chunks:
try:
moderation_result = openai.Moderation.create(input=text_chunk,
api_key=hosted_model_providers.openai.api_key)