Dify 插件脚手架工具
Python 环境,版本号 ≥ 3.12
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 300;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">开发 Dify 插件需要进行以下准备。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;white-space: normal;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Dify 插件脚手架工具
Python 环境,版本号 ≥ 3.12
ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-style: normal;font-variant-ligatures: normal;font-variant-caps: normal;font-weight: 300;letter-spacing: normal;orphans: 2;text-align: start;text-indent: 0px;text-transform: none;widows: 2;word-spacing: 0px;-webkit-text-stroke-width: 0px;text-decoration-thickness: initial;text-decoration-style: initial;text-decoration-color: initial;">Dify 插件开发脚手架工具又称为 dify-plugin-daemon,可以被视作插件开发 SDK。chmod +x dify-plugin-darwin-arm64 运行以下命令检查安装是否成功。
./dify-plugin-darwin-arm64 version
若提示 “Apple 无法验证” 错误,请前往 “设置 → 隐私与安全性 → 安全性”,轻点 “仍要打开” 按钮。
若提示 “Apple 无法验证” 错误,请前往 “设置 → 隐私与安全性 → 安全性”,轻点 “仍要打开” 按钮。
详细说明请参考Python安装教程,或询问 LLM 安装版本号 ≥ 3.12 的 Python 环境
运行脚手架命令行工具,创建一个新的 dify 插件项目。
./dify-plugin-darwin-arm64 plugin init 如果你已将该二进制文件重命名为 dify 并拷贝至 /usr/local/bin 路径下,可以运行以下命令创建新的插件项目:
dify plugin init 下文将使用 dify 作为命令行示例。如遇到问题,请将 dify 命令替换为命令行工具的所在路径。
脚手架工具内的所有模板均已提供完整的代码项目。在本文实例中,选择 Tool 插件。
插件类型:工具
插件还需读取 Dify 平台的权限,为该示例插件授予以下权限:
在终端内使用方向键选择权限,使用 “Tab” 按钮授予权限。 勾选所有权限项后,轻点回车完成插件的创建。系统将自动生成插件项目代码。
在生成的项目文件中有两个比较重要的文件夹
这两个的表现稍后会在dify工具中能体现
dify-plugin-example.yaml
identity:author:xxxxname:dify-plugin-examplelabel:en_US:dify-plugin-examplezh_Hans:dify-plugin-examplept_BR:dify-plugin-exampledescription:en_US:"UsethisplugintofetchtheURLcontent(provider)."zh_Hans:"使用此插件获取URL内容(provider)"pt_BR:"UseestepluginparabuscaroconteúdodoURL(provider)"icon:icon.pngtools:-tools/dify-plugin-example.yamlextra:python:source:provider/dify-plugin-example.py
dify-plugin-example.py
fromtypingimportAnyfromdify_pluginimportToolProviderfromdify_plugin.errors.toolimportToolProviderCredentialValidationErrorclassDifyPluginExampleProvider(ToolProvider):def_validate_credentials(self,credentials:dict[str,Any])->None:try:"""IMPLEMENTYOURVALIDATIONHERE"""exceptExceptionase:raiseToolProviderCredentialValidationError(str(e))
DifyPluginExampleProvider 里面实现的主要是一些验证功能,比如你的工具需要API Key就可以在这里实现
dify-plugin-example.yaml
identity:name:dify-plugin-exampleauthor:fakzhaolabel:en_US:dify-plugin-examplezh_Hans:dify-plugin-examplept_BR:dify-plugin-exampledescription:human:en_US:TheURLtofetchcontentfrom.zh_Hans:要获取内容的URLpt_BR:OURLparabuscarconteúdo.llm:ConvertawebpageintowellstructuredMarkdown.parameters:-name:urltype:stringrequired:truelabel:en_US:URLzh_Hans:URLpt_BR:URLhuman_description:en_US:"TheURLtofetchcontentfrom."zh_Hans:"要获取内容的URL"pt_BR:"OURLparabuscarconteúdo."llm_description:llmform:llmextra:python:source:tools/dify-plugin-example.py
dify-plugin-example.py
importjsonimportrequestsfromcollections.abcimportGeneratorfromtypingimportAnyfromdify_pluginimportToolfromdify_plugin.entities.toolimportToolInvokeMessageclassDifyPluginExampleTool(Tool):def_invoke(self,tool_parameters:dict[str,Any])->Generator[ToolInvokeMessage]:print(json.dumps(tool_parameters,indent=4))url=tool_parameters["url"]print(f"fetching{url}")try:j_url=f"https://r.jina.ai/{url}"print(f"jinaurl{j_url}")response=requests.get(j_url)response.raise_for_status()content=response.textexceptrequests.RequestExceptionase:content=str(e)yieldself.create_json_message({"result":content})可以看出,这个工具作用是提供一个url,返回一个markdown给其他节点使用(https://r.jina.ai是一个将网页转markdown的网页,可以在浏览器试试https://r.jina.ai/https://example.com)
插件开发完成后,接下来需要测试插件是否可以正常运行。Dify 提供便捷地远程调试方式,帮助你快速在测试环境中验证插件功能。 前往“插件管理”页获取远程服务器地址和调试 Key。
回到插件项目,拷贝 .env.example 文件并重命名为 .env,将获取的远程服务器地址和调试 Key 等信息填入其中。 .env 文件:
INSTALL_METHOD=remoteREMOTE_INSTALL_HOST=remoteREMOTE_INSTALL_PORT=5003REMOTE_INSTALL_KEY=****-****-****-****-****
运行 python -m main 命令启动插件。在插件页即可看到该插件已被安装至 Workspace 内,团队中的其他成员也可以访问该插件。
到这里你的插件已经成功运行,并可以进行debug.
在工作流中添加你自己的插件
最终的workflow
查看运行结果
已经成功到运行?
确认插件能够正常运行后,可以通过以下命令行工具打包并命名插件。运行以后你可以在当前文件夹发现 dify-plugin-example.difypkg 文件,该文件为最终的插件包。 将 ./dify-plugin-example 替换为插件项目的实际路径
dify plugin package ./dify-plugin-example
恭喜,你已完成一个工具类型插件的完整开发、调试与打包过程!
如果想要将插件发布至 Dify Marketplace,请确保你的插件遵循了插件发布规范。审核通过后,代码将合并至主分支并自动上线至 Dify Marketplace。
通过为 Dify 开发工具插件,我们不仅能够扩展 AI 应用的能力,还能提升系统的灵活性和可扩展性。无论是接入外部 API、执行复杂计算,还是增强交互体验,插件都能让 Dify 更加强大。
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |