moderation.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from typing import Optional
  2. from dify_plugin.errors.model import CredentialsValidateFailedError
  3. from dify_plugin import ModerationModel
  4. class {{ .PluginName | SnakeToCamel }}ModerationModel(ModerationModel):
  5. """
  6. Model class for {{ .PluginName | CamelToTitle }} text moderation model.
  7. """
  8. def _invoke(self, model: str, credentials: dict,
  9. text: str, user: Optional[str] = None) \
  10. -> bool:
  11. """
  12. Invoke moderation model
  13. :param model: model name
  14. :param credentials: model credentials
  15. :param text: text to moderate
  16. :param user: unique user id
  17. :return: false if text is safe, true otherwise
  18. """
  19. # transform credentials to kwargs for model instance
  20. return True
  21. def validate_credentials(self, model: str, credentials: dict) -> None:
  22. """
  23. Validate model credentials
  24. :param model: model name
  25. :param credentials: model credentials
  26. :return:
  27. """
  28. try:
  29. pass
  30. except Exception as ex:
  31. raise CredentialsValidateFailedError(str(ex))