Cannot Load Custom Moderation extension #4714

Closed
opened 2026-02-21 18:07:41 -05:00 by yindo · 2 comments
Owner

Originally created by @qjy706 on GitHub (Jul 22, 2024).

Originally assigned to: @takatost on GitHub.

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • 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
微信图片_20240722165131
微信图片_20240722165138
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

Originally created by @qjy706 on GitHub (Jul 22, 2024). Originally assigned to: @takatost on GitHub. ### Self Checks - [X] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [X] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [X] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [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 ![微信图片_20240722165131](https://github.com/user-attachments/assets/eee95868-2874-4728-b1fc-b95667b6e3a0) ![微信图片_20240722165138](https://github.com/user-attachments/assets/27bbc3df-5af8-4814-9400-0cebecbf970c) 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
yindo added the stale label 2026-02-21 18:07:41 -05:00
yindo closed this issue 2026-02-21 18:07:41 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jul 22, 2024):

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:

  1. Directory Structure: Ensure that the directory structure is correctly initialized under api/core/moderation as shown below:

    .
    └── api
        └── core
            └── moderation
                └── cloud_service
                    ├── __init__.py
                    ├── cloud_service.py
                    └── schema.json
    
  2. Frontend Component Specifications: Ensure that the schema.json file 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"
            }
        ]
    }
    
  3. Implementation Class: Ensure that the cloud_service.py file 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
    
  4. 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 Service content moderation extension type is loaded correctly.

  5. 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 -d
    

    Verify that all containers are running successfully:

    docker compose ps
    

    This 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.

@dosubot[bot] commented on GitHub (Jul 22, 2024): <!-- Answer --> 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: 1. **Directory Structure**: Ensure that the directory structure is correctly initialized under `api/core/moderation` as shown below: ```plaintext . └── api └── core └── moderation └── cloud_service ├── __init__.py ├── cloud_service.py └── schema.json ``` 2. **Frontend Component Specifications**: Ensure that the `schema.json` file is correctly defined with the necessary frontend component specifications. ```json { "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" } ] } ``` 3. **Implementation Class**: Ensure that the `cloud_service.py` file contains the correct implementation class with the necessary methods. ```python 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 ``` 4. **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 Service` content moderation extension type is loaded correctly. 5. **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: ```bash git clone https://github.com/langgenius/dify.git cd dify/docker cp .env.example .env docker compose up -d ``` Verify that all containers are running successfully: ```bash docker compose ps ``` This 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 <sup>[[1]](https://github.com/langgenius/dify-docs/blob/main/en/guides/extension/code-based-extension/moderation.md)</sup><sup>[[2]](https://github.com/langgenius/dify-docs/blob/main/en/getting-started/install-self-hosted/docker-compose.md)</sup><sup>[[3]](https://github.com/langgenius/dify-docs/blob/main/zh_CN/guides/extension/code_based_extension/moderation.md)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@linfey90 commented on GitHub (Jul 25, 2024):

I encountered the same issue; the cloud service I added according to the example did not appear on the page.How to solve this?

@linfey90 commented on GitHub (Jul 25, 2024): I encountered the same issue; the cloud service I added according to the example did not appear on the page.How to solve this?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#4714