在Dify中使用OpenAPI Swagger定义构建Jina阅读器工具

在Dify中使用OpenAPI Swagger定义构建Jina阅读器工具


环境准备

架构图

在Dify平台上创建自定义工具时,需要按照以下步骤配置OpenAPI规范:


API规范定义

服务器配置调整

为本地Docker部署指定服务器URL:

servers:
  - url: http://192.168.31.15:9001

完整Swagger定义
openapi: 3.1.0
info:
  title: Reader Local Deployment API
  version: 1.1.0
  description: |
    Local deployment version of web content processor with screenshot capabilities.
    Converts web content to LLM-friendly formats and generates screenshots.
    
    Key Features:
    - Local Docker deployment
    - Multiple output formats support
    - Local screenshot storage
    - No authentication required
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: http://xxx.xxx.xxx.xxx:9001
    description: Local Docker deployment
  - url: https://reader.berlin.cx
    description: Public demo instance
paths:
  /{targetUrl}:
    get:
      operationId: processWebContent
      summary: Process web content from URL
      description: |
        Converts web content to specified format and optionally generates screenshots.
        Supported formats include raw text, markdown, HTML and various screenshot types.
      parameters:
        - in: path
          name: targetUrl
          required: true
          schema:
            type: string
            format: uri
            example: "https://github.com/intergalacticalvariable/reader/"
          description: |
            The full URL to process. Must be properly URL-encoded.
            Example value: `https%3A%2F%2Fgithub.com%2Fintergalacticalvariable%2Freader%2F`
          allowReserved: true
        - in: header
          name: X-Respond-With
          required: true
          schema:
            type: string
            enum: [text, markdown, html, screenshot, pageshot]
            default: text
          description: |
            Response format selector:
            - `text`: Clean text content (default)
            - `markdown`: Raw markdown output
            - `html`: Full page HTML
            - `screenshot`: Viewport-sized screenshot URL
            - `pageshot`: Full-page screenshot URL
      responses:
        '200':
          description: Successfully processed content
          content:
            text/plain:
              schema:
                oneOf:
                  - type: string
                    description: Text/markdown/HTML content
                    example: "This is cleaned text content..."
                  - type: string
                    format: uri
                    description: Local screenshot path
                    example: "/screenshots/20240501-142356_google.com.png"
            application/json:
              schema:
                type: object
                properties:
                  content:
                    oneOf:
                      - type: string
                        description: Text content
                      - type: object
                        properties:
                          screenshot_url:
                            type: string
                            format: uri
                            example: "/screenshots/fullpage_google.com.png"
                          screenshot_type:
                            type: string
                            enum: [viewport, fullpage]
                  processed_at:
                    type: string
                    format: date-time
                  source_url:
                    type: string
                    format: uri
        '400':
          description: Invalid URL format or unsupported response type
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Content processing failure
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'

components:
  schemas:
    Error:
      type: object
      properties:
        error_type:
          type: string
          enum: [invalid_url, processing_error, screenshot_error]
        message:
          type: string
          example: "Failed to capture screenshot"
        debug_info:
          type: string
          example: "Timeout waiting for page load"
      required: [error_type, message]

实现步骤

  1. 登录Dify控制台,选择「新建工具」
  2. 将服务器地址改为本地环境
  3. 将上述Swagger定义粘贴到API规范区域

image.png


测试执行

测试结果示例:

测试结果截图


总结

通过本方法,我们可以在Dify平台上快速构建本地部署的网页内容处理工具。清晰的Swagger规范定义显著提升了API开发和文档维护的效率,特别适合需要处理网页内容并集成到智能助手场景的开发者。


相关阅读推荐:

希望本文能帮到正在探索Dify生态集成的开发者!

### 如何在 Dify使用 FireCrawl 作为工作流工具 #### 集成准备 为了使 FireCrawl 能够与 Dify 工作流集成,需先完成必要的准备工作。这包括但不限于获取 FireCrawl 的 API 密钥以便合法调用其服务[^2]。 #### 创建自定义模块 考虑到 Dify 支持扩展功能的方式之一是通过创建应用程序来实现特定业务逻辑的需求,在此背景下可考虑开发一个专门用于操作 FireCrawl 接口的应用程序。该应用负责封装对 FireCrawl 提供的各项能力(如网页抓取、数据解析等)的操作接口,并将其暴露给其他部分的工作流组件调用[^3]。 ```typescript // 示例:初始化FireCrawl客户端实例 import { FireCrawlClient } from 'firecrawl'; const client = new FireCrawlClient({ apiKey: process.env.FIRECRAWL_API_KEY, }); ``` #### 设计自动化流程 设计具体应用场景下的自动化工序链路图,明确各环节间的数据流转关系以及触发条件。例如当接收到外部事件通知时启动一次完整的页面采集任务;又或者是定时执行批量处理作业以保持本地缓存资料库的新鲜度[^4]。 #### 实现交互逻辑 编写代码片段描述如何利用上述提到过的定制化插件与其他现有设施之间建立联系的方法论。比如可以通过 RESTful Web Service 或者 GraphQL 查询语句等形式向目标站点发起请求并接收响应结果,再经过适当转换后存储至指定位置等待后续分析或展示用途。 ```javascript async function fetchWebsiteData(url) { try { const result = await client.crawl({ url }); console.log(`Fetched data successfully`, JSON.stringify(result)); return result; } catch (error) { console.error('Error occurred while fetching website:', error); throw error; } } ``` #### 测试验证效果 最后但同样重要的是要进行全面的功能测试确保一切按预期正常运作。从简单的小规模样本集开始逐步扩大范围直至覆盖全部可能遇到的情况为止,期间注意收集反馈意见及时调整优化方案直到达到满意的效果为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值