Cannot Load Custom Moderation extension
Self Checks
- [X] This is only for bug report, if you would like to ask a question, please head to Discussions.
- [X] I have searched for existing issues search for existing issues, including closed ones.
- [X] I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
- [X] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
- [X] Please do not modify this template :) and fill in all the required fields.
Dify version
0.6.11
Cloud or Self Hosted
Self Hosted (Docker), Self Hosted (Source)
Steps to reproduce
I configured Custom Modulation based on the document in the reference link, but it doesn't work https://docs.dify.ai/guides/extension/code-based-extension/moderation
I added a builtin file inside and wrote a 4 in it because if I don't create the builtin file, the value of Cloud_Service in the position_map variable on line 114 of api-core/extension/extensible. py is None
#3981 This is the same problem, but it does not have a solution
✔️ Expected Behavior
Can load Cloud_Service extension
❌ Actual Behavior
Cannot load extension
To resolve the issue of the Custom Moderation extension not loading in Dify version 0.6.11 on a self-hosted Docker setup, ensure that you have followed all the steps correctly as outlined in the documentation. Here are the key steps you need to verify:
-
Directory Structure: Ensure that the directory structure is correctly initialized under
api/core/moderationas shown below:. └── api └── core └── moderation └── cloud_service ├── __init__.py ├── cloud_service.py └── schema.json -
Frontend Component Specifications: Ensure that the
schema.jsonfile is correctly defined with the necessary frontend component specifications.{ "label": { "en-US": "Cloud Service", "zh-Hans": "云服务" }, "form_schema": [ { "type": "select", "label": { "en-US": "Cloud Provider", "zh-Hans": "云厂商" }, "variable": "cloud_provider", "required": true, "options": [ { "label": { "en-US": "AWS", "zh-Hans": "亚马逊" }, "value": "AWS" }, { "label": { "en-US": "Google Cloud", "zh-Hans": "谷歌云" }, "value": "GoogleCloud" }, { "label": { "en-US": "Azure Cloud", "zh-Hans": "微软云" }, "value": "Azure" } ], "default": "GoogleCloud", "placeholder": "" }, { "type": "text-input", "label": { "en-US": "API Endpoint", "zh-Hans": "API Endpoint" }, "variable": "api_endpoint", "required": true, "max_length": 100, "default": "", "placeholder": "https://api.example.com" }, { "type": "paragraph", "label": { "en-US": "API Key", "zh-Hans": "API Key" }, "variable": "api_keys", "required": true, "default": "", "placeholder": "Paste your API key here" } ] } -
Implementation Class: Ensure that the
cloud_service.pyfile contains the correct implementation class with the necessary methods.from core.moderation.base import Moderation, ModerationAction, ModerationInputsResult, ModerationOutputsResult class CloudServiceModeration(Moderation): """ The name of custom type must be unique, keep the same with directory and file name. """ name: str = "cloud_service" @classmethod def validate_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) if not config.get("cloud_provider"): raise ValueError("cloud_provider is required") if not config.get("api_endpoint"): raise ValueError("api_endpoint is required") if not config.get("api_keys"): raise ValueError("api_keys is required") def moderation_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.DIRECT_OUTPUT, preset_response=preset_response) def moderation_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.DIRECT_OUTPUT, preset_response=preset_response) def _is_violated(self, inputs: dict): """ The main logic of moderation. :param inputs: :return: the moderation result """ return False -
Run and Debug: After ensuring the above steps, run the service and debug the extension in the Dify application orchestration interface to verify that the custom
Cloud Servicecontent moderation extension type is loaded correctly. -
Docker Setup Verification: Ensure that your Docker setup meets the prerequisites and is correctly configured. For Dify version 0.6.11, make sure you have Docker 19.03 or later and Docker Compose 1.25.1 or later. Follow these steps to start Dify:
git clone https://github.com/langgenius/dify.git cd dify/docker cp .env.example .env docker compose up -dVerify that all containers are running successfully:
docker compose psThis should show the status of the core services (api, worker, web) and dependent components (weaviate, db, redis, nginx).
If you have followed all these steps and the issue persists, there might be other underlying issues not covered in the provided documentation [1][2][3].
To continue talking to Dosu, mention @dosu.
I encountered the same issue; the cloud service I added according to the example did not appear on the page.How to solve this?