dify 无法加载自定义审查扩展

j2datikz  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(34)

自检

  • 仅用于提交 bug 报告,如果您想提问,请前往 Discussions
  • 我已在 search for existing issues 中搜索现有问题,包括已关闭的问题。
  • 我确认我使用英文提交此报告(我已阅读并同意 Language Policy )。
  • 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • 请不要修改此模板 :) 并填写所有必需的字段。

Dify 版本

0.6.11

云或自托管

自托管(Docker),自托管(源代码)

重现步骤

根据参考链接中的文档配置了基于 Custom Modulation 的自定义调制,但它不起作用 https://docs.dify.ai/guides/extension/code-based-extension/moderation

我在里面添加了一个 builtin 文件,并在其中写入了一个4,因为如果不创建 builtin 文件,api-core/extension/extensible. py 文件中位置Map变量 position_map 的 Cloud_Service 值将为 None #3981 。这是相同的问题,但没有解决方案。

✔️ 预期行为

可以加载 Cloud_Service 扩展

❌ 实际行为

无法加载扩展

dtcbnfnu

dtcbnfnu1#

要解决在Dify版本0.6.11的自托管Docker设置中自定义内容审核扩展不加载的问题,请确保您已按照文档中概述的所有步骤正确执行。以下是您需要验证的关键步骤:

  1. 目录结构:确保api/core/moderation下正确初始化了目录结构,如下所示:
.
└── api
    └── core
        └── moderation
            └── cloud_service
                ├── __init__.py
                ├── cloud_service.py
                └── schema.json
  1. 前端组件规范:确保schema.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"
        }
    ]
}
  1. 实现类:确保cloud_service.py文件包含正确的实现类和必要的方法。
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
  1. 运行和调试:在确保上述步骤后,运行服务并在Dify应用程序编排界面中调试扩展以验证自定义Cloud Service内容审核扩展类型是否正确加载。
  2. Docker设置验证:确保您的Docker设置满足先决条件并正确配置。对于Dify版本0.6.11,请确保您有Docker 19.03或更高版本和Docker Compose 1.25.1或更高版本。按照以下步骤启动Dify:
git clone https://github.com/langgenius/dify.git
cd dify/docker
cp .env.example .env
docker compose up -d

验证所有容器是否成功运行:

docker compose ps

这应该显示核心服务(api、worker、web)及其依赖项(weaviate、db、redis、nginx)的状态。
如果您遵循了所有这些步骤,问题仍然存在,那么可能还有其他未在提供的文档中覆盖的底层问题。

  • 要继续与 Dosu 交谈,请提及 @dosu。*

相关问题