Dify 插件开发
开发流程
开发准备
安装 Dify 插件开发脚手架工具参考
[初始化开发工具 | Dify](https://docs.dify.ai/zh-hans/plugins/quick-start/develop-plugins/initialize-development-tools)
创建项目,选择插件类型
dify-plugin-windows-amd64.exe plugin init
以Tool工具为例,假如名称为table2json
创建完成后,修改tools
目录下table2json.py
,实现具体的业务逻辑
在table2json.yaml定义输入参数
parameters:- name: tablestype: filesrequired: truelabel:en_US: input fileszh_Hans: 输入文件pt_BR: input fileshuman_description:en_US: convert table to json formatzh_Hans: convert table to json formatpt_BR: convert table to json formatllm_description: convert table to json formatform: llm
修改代码
import json
from collections.abc import Generator
from typing import Any
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
from dify_plugin.file.file import File
import xxx # 与业务相关的包class Table2jsonTool(Tool):def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:# 获取输入参数if 'tables' not in tool_parameters or not tool_parameters['tables']:yield self.create_text_message("No files provided")returnfile_obj: File = tool_parameters['tables'][0]print("---1---类型")print(file_obj)# 具体的业务逻辑# 输出格式定义yield self.create_json_message({"result": json_str})
安装依赖
自己导入的包,需写入requirements.txt
执行下面的命令
pip install -r requirements.txt