Selaa lähdekoodia

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

tags/0.3.27
takatost 2 vuotta sitten
vanhempi
commit
b1352ff8b7
No account linked to committer's email address
1 muutettua tiedostoa jossa 14 lisäystä ja 12 poistoa
  1. 14
    12
      api/core/helper/moderation.py

+ 14
- 12
api/core/helper/moderation.py Näytä tiedosto

import logging import logging
import random


import openai import openai


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
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 return True

Loading…
Peruuta
Tallenna