news 2026/4/15 9:38:21

搭建AI应用-Dify插件开发入门

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
搭建AI应用-Dify插件开发入门

可以使用python和go语言开发dify插件,本文主要针对windows环境下使用python语言开发。

一,开发环境准备

‌1.安装依赖工具‌

Docker desktop
Git
Python ≥ 3.12(推荐使用 pyenv 或虚拟环境)

2.下载dify-plugin工具

https://github.com/langgenius/dify-plugin-daemon/releases

下载完成后把dify-plugin-windows-amd64.exe文件复制到插件开发目录,如D:\dify-plugin

cdD:\dify-plugin

3.验证dify-plugin工具

D:\dify-plugin>dify-plugin-windows-amd64.exe version v0.5.6

二,创建插件项目

  1. 按照提示进行填写
D:\dify-plugin>dify-plugin-windows-amd64.exe plugin init Select thetypeof plugin you want to create, and press`Enter`tocontinueEdit minimal Dify version requirement, leave it blank by default Minimal Dify version(press Enter to next step):1.0.02026/04/0112:00:00 INFO plugin created successfullyname=hello_worldguide=hello_world/GUIDE.md

Plugin name‌:插件唯一标识(如hello_world)
‌Author‌:插件作者
‌Description‌:功能简述
‌Language‌:选择 Python
Type‌:选择 Tool
Additional Features‌:按需勾选(如 Provider 验证、持久存储等)

  1. 生成的代码结构
hello_world/ ├── _assets/ # 图标资源 ├── provider/ # 凭证配置(如需) ├── tools/ # 核心工具实现 ├── .difyignore # 类似.gitignore,指定Dify平台忽略的文件 ├── .env.example # 环境变量示例文件(不含敏感值) ├── .gitignore # Git版本控制忽略规则 ├── GUIDE.md # 项目使用指南文档 ├── main.py # 应用主入口文件 ├── manifest.yaml # 项目清单文件(定义应用元数据/依赖等) ├── PRIVACY.md # 隐私政策说明文档 ├── README.md # 项目说明文档 └── requirements.txt # Python依赖包清单文件
  1. 创建虚拟环境(python)
python-mvenv venv

‌三、插件逻辑实现

  1. ‌新增工具tool1

tools/tool1.py:

fromcollections.abcimportGeneratorfromtypingimportAnyfromdify_pluginimportToolfromdify_plugin.entities.toolimportToolInvokeMessageclasstool1Tool(Tool):def_invoke(self,tool_parameters:dict[str,Any])->Generator[ToolInvokeMessage]:print(tool_parameters['query'])result=tool_parameters['query']yieldself.create_json_message({"result":"tool1, "+result})

tools/tool1.yaml:

identity:name:"tool1"author:"The idea of riding a donkey"label:en_US:"tool1"zh_Hans:"tool1"pt_BR:"tool1"ja_JP:"tool1"description:human:en_US:"tool1"zh_Hans:"tool1"pt_BR:"tool1"ja_JP:"tool1"llm:"tool1"parameters:-name:querytype:string required:true label:en_US:Query string zh_Hans:查询语句 pt_BR:Query string ja_JP:クエリ文字列 human_description:en_US:"tool1"zh_Hans:"tool1"pt_BR:"tool1"ja_JP:"tool1"llm_description:"tool1"form:llm extra:python:source:tools/tool1.py
  1. ‌新增工具tool2

tools/tool2.py:

fromcollections.abcimportGeneratorfromtypingimportAnyfromdify_pluginimportToolfromdify_plugin.entities.toolimportToolInvokeMessageclasstool2Tool(Tool):def_invoke(self,tool_parameters:dict[str,Any])->Generator[ToolInvokeMessage]:print(tool_parameters['query'])result=tool_parameters['query']yieldself.create_json_message({"result":"tool2, "+result})

tools/tool2.yaml:

identity:name:"tool2"author:"The idea of riding a donkey"label:en_US:"tool2"zh_Hans:"tool2"pt_BR:"tool2"ja_JP:"tool2"description:human:en_US:"tool2"zh_Hans:"tool2"pt_BR:"tool2"ja_JP:"tool2"llm:"tool2"parameters:1.name:querytype:string required:true label:en_US:Query string zh_Hans:查询语句 pt_BR:Query string ja_JP:クエリ文字列 human_description:en_US:"tool2"zh_Hans:"tool2"pt_BR:"tool2"ja_JP:"tool2"llm_description:"tool2"form:llm extra:python:source:tools/tool2.py
  1. 主服务接口实现

provider/hello.py

fromtypingimportAnyfromdify_pluginimportToolProviderfromdify_plugin.errors.toolimportToolProviderCredentialValidationErrorclassHelloProvider(ToolProvider):def_validate_credentials(self,credentials:dict[str,Any])->None:try:""" IMPLEMENT YOUR VALIDATION HERE """exceptExceptionase:raiseToolProviderCredentialValidationError(str(e))########################################################################################## If OAuth is supported, uncomment the following functions.# Warning: please make sure that the sdk version is 0.4.2 or higher.########################################################################################## def _oauth_get_authorization_url(self, redirect_uri: str, system_credentials: Mapping[str, Any]) -> str:# """# Generate the authorization URL for hello OAuth.# """# try:# """# IMPLEMENT YOUR AUTHORIZATION URL GENERATION HERE# """# except Exception as e:# raise ToolProviderOAuthError(str(e))# return ""# def _oauth_get_credentials(# self, redirect_uri: str, system_credentials: Mapping[str, Any], request: Request# ) -> Mapping[str, Any]:# """# Exchange code for access_token.# """# try:# """# IMPLEMENT YOUR CREDENTIALS EXCHANGE HERE# """# except Exception as e:# raise ToolProviderOAuthError(str(e))# return dict()# def _oauth_refresh_credentials(# self, redirect_uri: str, system_credentials: Mapping[str, Any], credentials: Mapping[str, Any]# ) -> OAuthCredentials:# """# Refresh the credentials# """# return OAuthCredentials(credentials=credentials, expires_at=-1)

provider/hello.yaml

identity:author:"lishiwen2"name:"hello"label:en_US:"hello"zh_Hans:"hello"pt_BR:"hello"ja_JP:"hello"description:en_US:"hello"zh_Hans:"hello"pt_BR:"hello"ja_JP:"hello"icon:"icon.svg"########################################################################################## If you want to support OAuth, you can uncomment the following code.########################################################################################## oauth_schema:# client_schema:# - name: "client_id"# type: "secret-input"# required: true# url: https://example.com/oauth/authorize# placeholder:# en_US: "Please input your Client ID"# zh_Hans: "请输入你的 Client ID"# pt_BR: "Insira seu Client ID"# help:# en_US: "Client ID is used to authenticate requests to the example.com API."# zh_Hans: "Client ID 用于认证请求到 example.com API。"# pt_BR: "Client ID é usado para autenticar solicitações à API do example.com."# label:# zh_Hans: "Client ID"# en_US: "Client ID"# - name: "client_secret"# type: "secret-input"# required: true# url: https://example.com/oauth/authorize# placeholder:# en_US: "Please input your Client Secret"# zh_Hans: "请输入你的 Client Secret"# pt_BR: "Insira seu Client Secret"# help:# en_US: "Client Secret is used to authenticate requests to the example.com API."# zh_Hans: "Client Secret 用于认证请求到 example.com API。"# pt_BR: "Client Secret é usado para autenticar solicitações à API do example.com."# label:# zh_Hans: "Client Secret"# en_US: "Client Secret"# credentials_schema:# - name: "access_token"# type: "secret-input"# label:# zh_Hans: "Access Token"# en_US: "Access Token"tools:1.tools/tool1.yaml2.tools/tool2.yaml extra:python:source:provider/hello.py
  1. 安装依赖

requirements.txt增加

dify_plugin>=0.4.0,<0.7.0
pipinstall-r.\requirements.txt
  1. 完整代码目录
project_root/ ├── _assets/ # 静态资源目录 │ ├── icon-dark.svg # 深色模式下的应用图标(SVG格式) │ └── icon.svg # 默认应用图标(SVG格式) ├── .venv/ # Python虚拟环境目录(用于依赖隔离) ├── .vscode/ # VSCode编辑器配置目录 │ ├── settings.json # 工作区设置 │ └── extensions.json # 推荐插件配置 ├── provider/ # 核心服务提供模块 │ ├── hello.py # 主服务接口实现(Python) │ └── hello.yaml # 服务配置文件(定义API端点/参数等) └── tools/ # 工具模块目录 │ ├── tool1.py # 工具1功能实现(Python) │ ├── tool1.yaml # 工具1配置文件(定义工具元数据/参数) │ ├── tool2.py # 工具2功能实现(Python) │ └── tool2.yaml # 工具2配置文件(定义工具元数据/参数) ├── .difyignore # 类似.gitignore,指定Dify平台忽略的文件 ├── .env # 环境变量配置文件(包含敏感信息) ├── .env.example # 环境变量示例文件(不含敏感值) ├── .gitignore # Git版本控制忽略规则 ├── GUIDE.md # 项目使用指南文档 ├── main.py # 应用主入口文件 ├── manifest.yaml # 项目清单文件(定义应用元数据/依赖等) ├── PRIVACY.md # 隐私政策说明文档 ├── README.md # 项目说明文档 └── requirements.txt # Python依赖包清单文件

四,本地运行与调试

  1. 复制创建.env
copy .env.example .env
  1. 修改.env文件
INSTALL_METHOD=remote#以下信息从dify平台工具-插件-调试出获取REMOTE_INSTALL_URL=localhost:5003#你的dify调试urlREMOTE_INSTALL_KEY=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx#你的dify调试key
  1. 启动插件服务
python-mmain
  1. dify平台调试插件

五,插件打包

dify-plugin-windows-amd64.exe plugin package +插件名称
D:\dify-plugin>dify-plugin-windows-amd64.exe plugin package hello_world2026/04/0112:10:00 INFO plugin packaged successfullyoutput_path=hello_world.difypkg

六,打包签名

  1. openssl方式
# 生成签名密钥对openssl genrsa-outprivate.pem2048openssl rsa-inprivate.pem-pubout-outpublic.pem# 打包时指定私钥dify-cli plugin pack ./your_plugin--keyprivate.pem
  1. dify-plugin方式
# 生成秘钥dify-plugin-windows-amd64.exe signature generate-f秘钥名称# 打包项目dify-plugin-windows-amd64.exe plugin package 项目名称# 使用秘钥签名dify-plugin-windows-amd64.exe signature sign 包名称.difypkg-p秘钥.private.pem
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/15 9:38:17

真正拉开人与人差距的,是这 3 种底层能力

人这一辈子&#xff0c;拼到最后&#xff0c;拼的不是学历、不是背景、不是运气&#xff0c;而是底层能力。学历会过时&#xff0c;风口会过去&#xff0c;平台会变化&#xff0c;但刻在你骨子里的能力&#xff0c;谁也拿不走。职场也好&#xff0c;人生也罢&#xff0c;真正能…

作者头像 李华
网站建设 2026/4/15 9:37:17

从Go到Kotlin:对比学习Channel的5个核心用法与避坑指南

从Go到Kotlin&#xff1a;Channel核心用法与实战避坑指南 1. 理解Channel的本质 对于熟悉Go语言的开发者来说&#xff0c;Kotlin的Channel概念并不陌生。两者都源自相同的并发模型理念&#xff0c;但在实现细节和使用方式上存在显著差异。 Channel本质上是一个线程安全的队列&a…

作者头像 李华
网站建设 2026/4/15 9:37:16

Windows Defender终极移除指南:快速释放30%系统性能的完整教程

Windows Defender终极移除指南&#xff1a;快速释放30%系统性能的完整教程 【免费下载链接】windows-defender-remover A tool which is uses to remove Windows Defender in Windows 8.x, Windows 10 (every version) and Windows 11. 项目地址: https://gitcode.com/gh_mir…

作者头像 李华
网站建设 2026/4/15 9:31:16

EMQX 社区版部署实战:从单机到高可用集群

1. 5分钟搞定Docker单机部署 第一次接触EMQX的朋友&#xff0c;我强烈建议从Docker方式入手。就像搭积木一样简单&#xff0c;三行命令就能让MQTT服务跑起来。最近给客户做POC测试时&#xff0c;我习惯用这种方式快速验证功能。 先说说硬件要求。官方建议最小配置是2核CPU4GB内…

作者头像 李华
网站建设 2026/4/15 9:29:13

G-Helper:华硕笔记本性能调校的轻量级神器,释放硬件潜能

G-Helper&#xff1a;华硕笔记本性能调校的轻量级神器&#xff0c;释放硬件潜能 【免费下载链接】g-helper Lightweight, open-source control tool for ASUS laptops and ROG Ally. Manage performance modes, fans, GPU, battery, and RGB lighting across Zephyrus, Flow, T…

作者头像 李华
网站建设 2026/4/15 9:29:11

基于深度学习的车辆区域计数 区域入侵检测 区域违停占用识别 YOLOv11实时roi区域视频人车流量统计项目

YOLOv11 实时视频 ROI 区域人流量/车流量统计项目介绍 1. 项目背景 随着智慧城市和智能交通的快速发展&#xff0c;实时统计特定区域的人流量和车流量成为关键需求。传统方法依赖人工统计或感应器&#xff0c;效率低且成本高。基于计算机视觉的解决方案&#xff0c;尤其是目标检…

作者头像 李华