from core.moderation.base import Moderation, ModerationAction, ModerationInputsResult, ModerationOutputsResultclassCloudServiceModeration(Moderation):""" The name of custom type must be unique, keep the same with directory and file name. """ name:str="cloud_service"@classmethoddefvalidate_config(cls,tenant_id:str,config:dict) ->None:""" schema.json validation. It will be called when user save the config. Example: .. code-block:: python config = { "cloud_provider": "GoogleCloud", "api_endpoint": "https://api.example.com", "api_keys": "123456", "inputs_config": { "enabled": True, "preset_response": "Your content violates our usage policy. Please revise and try again." }, "outputs_config": { "enabled": True, "preset_response": "Your content violates our usage policy. Please revise and try again." } } :param tenant_id: the id of workspace :param config: the variables of form config :return: """ cls._validate_inputs_and_outputs_config(config, True)ifnot config.get("cloud_provider"):raiseValueError("cloud_provider is required")ifnot config.get("api_endpoint"):raiseValueError("api_endpoint is required")ifnot config.get("api_keys"):raiseValueError("api_keys is required")defmoderation_for_inputs(self,inputs:dict,query:str="") -> ModerationInputsResult:""" Moderation for inputs. :param inputs: user inputs :param query: the query of chat app, there is empty if is completion app :return: the moderation result """ flagged =False preset_response =""if self.config['inputs_config']['enabled']: preset_response = self.config['inputs_config']['preset_response']if query: inputs['query__']= query flagged = self._is_violated(inputs) # return ModerationInputsResult(flagged=flagged, action=ModerationAction.overridden, inputs=inputs, query=query)
return ModerationInputsResult(flagged=flagged, action=ModerationAction.DIRECT_OUTPUT, preset_response=preset_response)
defmoderation_for_outputs(self,text:str) -> ModerationOutputsResult:""" Moderation for outputs. :param text: the text of LLM response :return: the moderation result """ flagged =False preset_response =""if self.config['outputs_config']['enabled']: preset_response = self.config['outputs_config']['preset_response'] flagged = self._is_violated({'text': text})# return ModerationOutputsResult(flagged=flagged, action=ModerationAction.overridden, text=text) return ModerationOutputsResult(flagged=flagged, action=ModerationAction.DIRECT_OUTPUT, preset_response=preset_response)
def_is_violated(self,inputs:dict):""" The main logic of moderation. :param inputs: :return: the moderation result """returnFalse
4. 调试扩展
至此,即可在 Dify 应用编排界面选择自定义的 Cloud Service 内容审查扩展类型进行调试。\
实现类模版
from core.moderation.base import Moderation, ModerationAction, ModerationInputsResult, ModerationOutputsResultclassCloudServiceModeration(Moderation):""" The name of custom type must be unique, keep the same with directory and file name. """ name:str="cloud_service"@classmethoddefvalidate_config(cls,tenant_id:str,config:dict) ->None:""" schema.json validation. It will be called when user save the config. :param tenant_id: the id of workspace :param config: the variables of form config :return: """ cls._validate_inputs_and_outputs_config(config, True)# implement your own logic heredefmoderation_for_inputs(self,inputs:dict,query:str="") -> ModerationInputsResult:""" Moderation for inputs. :param inputs: user inputs :param query: the query of chat app, there is empty if is completion app :return: the moderation result """ flagged =False preset_response =""# implement your own logic here # return ModerationInputsResult(flagged=flagged, action=ModerationAction.overridden, inputs=inputs, query=query)
return ModerationInputsResult(flagged=flagged, action=ModerationAction.DIRECT_OUTPUT, preset_response=preset_response)
defmoderation_for_outputs(self,text:str) -> ModerationOutputsResult:""" Moderation for outputs. :param text: the text of LLM response :return: the moderation result """ flagged =False preset_response =""# implement your own logic here# return ModerationOutputsResult(flagged=flagged, action=ModerationAction.overridden, text=text) return ModerationOutputsResult(flagged=flagged, action=ModerationAction.DIRECT_OUTPUT, preset_response=preset_response)