dify-docs/zh_CN/guides/application-design/extension/code_based_extension
Luyu Zhang b865f6ebcd
GITBOOK-55: change request with no subject merged in GitBook
2023-12-23 15:39:07 +00:00
..
README.md GITBOOK-55: change request with no subject merged in GitBook 2023-12-23 15:39:07 +00:00
external_data_tool.md GITBOOK-55: change request with no subject merged in GitBook 2023-12-23 15:39:07 +00:00
moderation.md GITBOOK-55: change request with no subject merged in GitBook 2023-12-23 15:39:07 +00:00

README.md

代码扩展

对于本地部署 Dify 的开发者,如果想实现扩展能力,无需重新写一个 API 服务,可以使用代码扩展,即在 Dify 功能的基础上,以代码形式扩展或增强程序的能力(即插件能力),而不破坏 Dify 原有代码逻辑。它遵循一定的接口或规范,以实现与主程序的兼容和可插拔性。 目前Dify 开放了两种代码扩展,分别为:

可在上述功能的基础上,遵循代码层 interface 的规范,来实现横向扩展的目的。如果你愿意将扩展功能贡献给我们的话,非常欢迎给 Dify 提交 PR。

前端组件规范定义

代码扩展的前端样式通过 schema.json 定义:

  • label自定义类型名称支持系统语言切换
  • form_schema表单内容列表
    • type组件类型
      • select下拉选项
      • text-input文本
      • paragraph段落
    • label组件名称支持系统语言切换
    • variable变量名称
    • required是否必填
    • default默认值
    • placeholder组件提示内容
    • options组件「select」专有属性定义下拉内容
      • label下拉名称支持系统语言切换
      • value下拉选项值
    • max_length组件「text-input」专有属性最大长度

模版样例

{
    "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"
        }
    ]
}