当前位置: 首页 > news >正文

【实战】Dify从0到100进阶--插件开发(1)Github爬取插件

在这里插入图片描述

基础配置

1. 安装插件CLI

  • macOS / Linux(Homebrew)

    brew tap langgenius/dify
    brew install dify
    

    安装完成后执行 dify version,应能看到类似 v0.0.1‑beta.15 的版本号。
    若需升级:

    brew upgrade dify
    
  • Windows / Linux / macOS(二进制文件)

    1. 从 GitHub Releases(https://github.com/langgenius/homebrew-dify 或 https://github.com/langgenius/dify-plugin-daemon/releases)下载对应平台与架构的可执行文件(如 dify-plugin-darwin-arm64)。

    2. 赋予执行权限并重命名:

      chmod +x ./dify-plugin-darwin-arm64
      mv ./dify-plugin-darwin-arm64 dify
      
    3. (可选)复制到系统路径以便全局调用:

      sudo mv dify /usr/local/bin/
      
    4. 运行 dify version 检查是否安装成功。

  • 常见问题

    • macOS 若出现 “无法验证” 提示,前往「系统偏好 → 隐私与安全性」允许打开即可。
    • 确保最终能在任意终端使用 dify version 查看输出。

2. Python 环境准备

  • 插件开发要求 Python 版本 ≥ 3.12。
  • 可参考官方安装教程或社区文档完成安装:
  • 建议使用 pyenvconda 或 venv 创建独立环境,以免与系统默认 Python 冲突。

3. 后续开发指引

  • 环境准备完毕后,可参考官方插件开发入门文档,了解:
    • 插件项目结构
    • dify plugin init 快速生成模板
    • 开发、调试与打包流程
    • 插件能力分类(Data Source、Tool、LLM Extension 等)
  • 根据业务需求,选择对应的插件类型,并参阅示例仓库中的标准实践。

项目创建

1.前置准备

  1. 安装 Dify 插件 CLI

    • Homebrew(macOS/Linux)

      brew tap langgenius/dify
      brew install dify
      

      验证:dify version 应输出类似 v0.0.1‑beta.xx
      升级:brew upgrade dify

    • 二进制方式(Windows/macOS/Linux)

      1. 从 GitHub Releases 下载对应平台的可执行文件(如 dify-plugin-darwin-arm64)。

      2. 赋予可执行权限并重命名:

        chmod +x dify-plugin-darwin-arm64
        mv dify-plugin-darwin-arm64 dify
        
      3. (可选)拷贝至系统路径:sudo mv dify /usr/local/bin/

      4. 验证:dify version

  2. Python 环境

    • 版本要求:≥ 3.12
    • 建议使用 pyenvcondavenv 创建独立虚拟环境,避免与系统 Python 冲突。

2.初始化

# 若二进制已放至 PATH,可直接使用 dify
dify plugin init
  1. 选择项目类型
    • 在交互式菜单中选择 Tool
  2. 配置资源权限
    • 勾选:Tools、Apps、Storage(持久化存储)、Endpoint 注册权限。
  3. 完成后,脚手架会在当前目录生成一个包含基础结构的插件模板。

3.配置Provider

  1. 在项目根目录下的 /provider 目录,github_llm.yaml示例内容:

identity:
author: “yplz”
name: “github_llm”
label:
en_US: “github_llm”
zh_Hans: “github_llm”
pt_BR: “github_llm”
description:
en_US: “github_to_llm”
zh_Hans: “github_to_llm”
pt_BR: “github_to_llm”
icon: “icon.svg”
tools:

  • tools/github_llm.yaml
    extra:
    python:
    source: provider/github_llm.py

### 四、定义单个工具(Tool)在 `/tools` 目录下`github_llm.yaml`,示例:```yaml
identity:
name: "github_llm"
author: "yplz"
label:en_US: "github_llm"zh_Hans: "github_llm"pt_BR: "github_llm"
description:
human:en_US: "github_to_llm"zh_Hans: "github_to_llm"pt_BR: "github_to_llm"
llm: "github_to_llm"
parameters:
- name: inputtype: stringrequired: truelabel:en_US: Query stringzh_Hans: 输入内容pt_BR: Query stringhuman_description:en_US: "github_to_llm"zh_Hans: "输入内容"pt_BR: "github_to_llm"llm_description: "github_to_llm"form: llm
extra:
python:source: tools/github_llm.py
  • identity:工具的唯一标识与展示信息。
  • parameters:定义输入参数,支持多语言描述和 LLM 推理模式。

4.实现工具逻辑

  1. 工具代码 /tools/github_llm.py

import asyncio
from collections.abc import Generator
from typing import Any
from dify_plugin import Tool
from dify_plugin.entities.tool import ToolInvokeMessage
import re
from datetime import datetime
import json
from playwright.async_api import async_playwright

async def fetch_github_trending():
“”“抓取 GitHub Trending,并返回项目列表”“”
# 启动 Playwright
playwright = await async_playwright().start()
browser = await playwright.chromium.launch(headless=True)
context = await browser.new_context()
page = await context.new_page()

# 访问 Trending 页面并等待加载完成
await page.goto("https://github.com/trending")
await page.wait_for_load_state('networkidle')project_list = []
articles = await page.query_selector_all('article.Box-row')
for element in articles:try:pdict = {}# 项目名称及链接link = await element.query_selector('h2 a')href = await link.get_attribute("href")  # e.g. "/owner/repo"pdict['项目名称'] = href.split("/")[-1]pdict['url'] = f"https://github.com{href}"# 编程语言lang_ele = await element.query_selector("span[itemprop='programmingLanguage']")pdict['编程语言'] = (await lang_ele.text_content()).strip() if lang_ele else ""# 历史 star 数star_ele = await element.query_selector('a.Link--muted[href$="/stargazers"]')stars = await star_ele.text_content()pdict['历史star数'] = ''.join(re.findall(r'\d+', stars))# 今日 star 数today_ele = await element.query_selector('span.d-inline-block.float-sm-right')today = await today_ele.text_content()pdict['今日star数'] = ''.join(re.findall(r'\d+', today))# 创作者(仓库所有者)owner = href.split("/")[1]pdict['创作者'] = owner# 简介desc_ele = await element.query_selector('p.col-9')pdict['简介'] = (await desc_ele.text_content()).strip() if desc_ele else ""project_list.append(pdict)except Exception:# 遇到单条解析错误时忽略,继续下一个continue# 清理并返回结果
await browser.close()
await playwright.stop()
return project_list

class GithubLlmTool(Tool):
def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
res=“未能查询到结果”
if tool_parameters[‘input’]:
res=asyncio.run(fetch_github_trending())
res = json.dumps(res, ensure_ascii=False)
else:
pass

    yield self.create_text_message(res)

2. **Provider 验证代码** `/provider/github_llm.py`:```python
from typing import Anyfrom dify_plugin import ToolProvider
from dify_plugin.errors.tool import ToolProviderCredentialValidationErrorclass GithubLlmProvider(ToolProvider):def _validate_credentials(self, credentials: dict[str, Any]) -> None:try:"""IMPLEMENT YOUR VALIDATION HERE"""except Exception as e:raise ToolProviderCredentialValidationError(str(e))

5.调试与打包

  1. 远程调试

    • 在 Dify 控制台 “插件管理” 获取远程调试地址和 Key,填入项目根目录 .env

      INSTALL_METHOD=remote
      REMOTE_INSTALL_URL=debug.dify.ai:5003
      REMOTE_INSTALL_KEY=<your_key>
      
    • 启动:python -m main;登录 Dify Workspace,确认插件已安装并可调用。

  2. 打包发布

    dify plugin package ./github_llm
    

    生成 github_llm.difypkg,即可上传或发布至 Dify Marketplace。

http://www.lryc.cn/news/606656.html

相关文章:

  • 【2025/08/01】GitHub 今日热门项目
  • 24 SAP CPI 调用SAP HTTP接口
  • R语言基础图像及部分调用函数
  • Dify API接口上传文件 postman配置
  • osloader!DoGlobalInitialization函数分析之HW_CURSOR--NTLDR源代码分析之设置光标
  • django操作orm整套
  • 学习设计模式《二十》——解释器模式
  • 如何使用Postman做接口测试
  • curl命令使用
  • 【机器学习与数据挖掘实战 | 医疗】案例20:基于交叉验证和LightGBM算法的糖尿病遗传风险预测
  • 机器学习②【字典特征提取、文本特征处理(TF-IDF)、数据标准化与归一化、特征降维】
  • 解决IDEA无法克隆GitHub上的工程的问题
  • 解决IDEA中MAVEN项目总是将LANGUAGE LEVEL重置的问题
  • SSL 剥离漏洞
  • 把上次做的图片的API改成国内版,让图片返回速度提升一个档次
  • 对于前端闭包的详细理解
  • LeetCode热题100——146. LRU 缓存
  • Typora v1.10.8 好用的 Markdown 编辑器
  • Linux 系统监控脚本实战:磁盘空间预警、Web 服务与访问测试全流程
  • ACM SIGCOMM 2024论文精选-01:5G【Prism5G】
  • 数据处理--生成Excel文档
  • 18.若依框架中的xss过滤器
  • 南太平洋金融基建革命:斐济-巴新交易所联盟的技术破局之路 ——从关税动荡到离岸红利,跨境科技如何重塑太平洋资本生态
  • 基于html,css,jquery,django,lstm,cnn,tensorflow,bert,推荐算法,mysql数据库
  • 元策联盈:深耕金融领域,赋能行业发展​
  • Apache RocketMQ for AI 战略升级,开启 AI MQ 新时代
  • 视频生成中如何选择GPU或NPU?
  • 《C++初阶之STL》【stack/queue/priority_queue容器适配器:详解 + 实现】(附加:deque容器介绍)
  • Eclipse中导入新项目,右键项目没有Run on Server,Tomcat的add and remove找不到项目
  • Apache RocketMQ 中 Producer(生产者)的详细说明