moderation.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import logging
  2. import random
  3. from core.app.entities.app_invoke_entities import ModelConfigWithCredentialsEntity
  4. from core.model_runtime.errors.invoke import InvokeBadRequestError
  5. from core.model_runtime.model_providers.openai.moderation.moderation import OpenAIModerationModel
  6. from extensions.ext_hosting_provider import hosting_configuration
  7. from models.provider import ProviderType
  8. logger = logging.getLogger(__name__)
  9. def check_moderation(model_config: ModelConfigWithCredentialsEntity, text: str) -> bool:
  10. moderation_config = hosting_configuration.moderation_config
  11. if (
  12. moderation_config
  13. and moderation_config.enabled is True
  14. and "openai" in hosting_configuration.provider_map
  15. and hosting_configuration.provider_map["openai"].enabled is True
  16. ):
  17. using_provider_type = model_config.provider_model_bundle.configuration.using_provider_type
  18. provider_name = model_config.provider
  19. if using_provider_type == ProviderType.SYSTEM and provider_name in moderation_config.providers:
  20. hosting_openai_config = hosting_configuration.provider_map["openai"]
  21. assert hosting_openai_config is not None
  22. # 2000 text per chunk
  23. length = 2000
  24. text_chunks = [text[i : i + length] for i in range(0, len(text), length)]
  25. if len(text_chunks) == 0:
  26. return True
  27. text_chunk = random.choice(text_chunks)
  28. try:
  29. model_type_instance = OpenAIModerationModel()
  30. # FIXME, for type hint using assert or raise ValueError is better here?
  31. moderation_result = model_type_instance.invoke(
  32. model="text-moderation-stable", credentials=hosting_openai_config.credentials or {}, text=text_chunk
  33. )
  34. if moderation_result is True:
  35. return True
  36. except Exception as ex:
  37. logger.exception(f"Fails to check moderation, provider_name: {provider_name}")
  38. raise InvokeBadRequestError("Rate limit exceeded, please try again later.")
  39. return False