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