Dify 插件开发笔记
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: tables
type: files
required: true
label:
en_US: input files
zh_Hans: 输入文件
pt_BR: input files
human_description:
en_US: convert table to json format
zh_Hans: convert table to json format
pt_BR: convert table to json format
llm_description: convert table to json format
form: 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")
return
file_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