langgenius / dify

Dify is an open-source LLM app development platform. Dify's intuitive interface combines AI workflow, RAG pipeline, agent capabilities, model management, observability features and more, letting you quickly go from prototype to production.
https://dify.ai
Other
45.26k stars 6.35k forks source link

Cannot Load Custom Moderation extension #6528

Closed qjy706 closed 1 week ago

qjy706 commented 1 month ago

Self Checks

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

dosubot[bot] commented 1 month ago

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.

linfey90 commented 1 month ago

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