1. 安装 Dify 插件 CLI 工具

Dify插件CLI工具用于创建插件项目和插件打包发布。

官网地址:https://github.com/langgenius/dify-plugin-daemon

下载地址:https://github.com/langgenius/dify-plugin-daemon/releases

本文例子window上开发,下载dify-plugin-windows-amd64.exe,文件名改为dify.exe放到任意目录下,并添加到PATH环境变量。

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图1

插件cli工具常用命令:

  1. # 显示版本号
  2. dify version
  3. # 创建一个新的 dify 插件项目
  4. dify plugin init
  5. # 打包插件,xxx为项目名
  6. dify plugin package ./xxx

参考自官方文档:https://docs.dify.ai/zh-hans/plugins/quick-start/develop-plugins/initialize-development-tools

2. 创建插件项目

创建插件项目命令:

  1. dify plugin init
  • 输入插件名称、作者、描述等

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图2

  • 选择开发语言

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图3

  • 选择插件类型,这里选择tool

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图4

  • 配置权限

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图5

  • 设置支持的dify版本

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图6

创建完成后,目录结构

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图7

目录说明

  1. # 供应商文件
  2. provider
  3. - paddlex-ocr.py # 供应商的实现代码,用于实现凭据验证逻辑。
  4. - paddlex-ocr.yaml # 包含工具供应商的信息,包括供应商名称、图标、作者等详情,在安装插件时进行展示。
  5. # 工具文件
  6. tools
  7. - paddlex-ocr.py # 编写工具的功能代码,实现工具的业务逻辑
  8. - paddlex-ocr.yaml # 工具显示的界面相关
  9. .env # 配置远程调试的地址和key
  10. manifest.yaml # 配置插件信息、插件权限
  11. requirements.txt # 插件依赖包

3. 编写插件代码

tools/paddlex-ocr.py实现代码

  1. from collections.abc import Generator
  2. from typing import Any
  3. from dify_plugin import Tool
  4. from dify_plugin.entities.tool import ToolInvokeMessage
  5. import json
  6. import requests
  7. class PaddlexOcrTool(Tool):
  8. def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
  9. file_url = tool_parameters.get('file_url')
  10. file_type = tool_parameters.get("file_type")
  11. ocr_url = tool_parameters.get('ocr_url')
  12. ocr_content = self.ocr(file_url,file_type, ocr_url)
  13. yield self.create_text_message(json.dumps(ocr_content, ensure_ascii=False))
  14. def ocr(self,file_url,file_type, ocr_url):
  15. payload = {"file": file_url, "fileType": 1}
  16. if file_type == "application/pdf":
  17. payload = {"file": file_url, "fileType": 0}
  18. response = requests.post(ocr_url, json=payload)
  19. ocr_contents = []
  20. if response.status_code == 200:
  21. result = response.json()["result"]
  22. for i, res in enumerate(result["ocrResults"]):
  23. ocr_content = '\n'.join(res["prunedResult"]["rec_texts"])
  24. ocr_contents.append(ocr_content)
  25. else:
  26. ocr_content = f"{response.status_code},解析失败!"
  27. ocr_contents.append(ocr_content)
  28. return ocr_contents

tools/paddlex-ocr.yaml实现代码

4、运行

  1. python -m main

效果

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图8

Dify插件开发实战:将PaddleOCR接入AI工作流 - 图9

参考官方文档:https://docs.dify.ai/zh-hans/plugins/quick-start/develop-plugins/tool-plugin