dify自定义工具代码

{
  "openapi": "3.1.0",
  "info": {
    "title": "Web-Search-Pro API",
    "description": "专业联网搜索工具,支持意图识别与流式搜索结果返回-逍遥",
    "version": "1.0.0",
    "contact": {
      "name": "BigModel",
      "url": "https://www.bigmodel.cn"
    }
  },
  "servers": [
    {
      "url": "https://open.bigmodel.cn/api/paas/v4",
      "description": "生产环境"
    }
  ],
  "paths": {
    "/tools": {
      "post": {
        "tags": ["Search"],
        "summary": "执行联网搜索",
        "operationId": "webSearchPro",
        "description": "支持同步/流式两种返回模式,自动识别搜索意图",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SearchRequest"
              },
              "examples": {
                "basicExample": {
                  "value": {
                    "tool": "web-search-pro",
                    "messages": [
                      {"role": "user", "content": "2024年新能源汽车销量"}
                    ],
                    "stream": true
                  }
                }
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "搜索成功响应",
            "content": {
              "application/json": {
                "schema": {
                  "oneOf": [
                    {"$ref": "#/components/schemas/SyncResponse"},
                    {"$ref": "#/components/schemas/StreamResponse"}
                  ]
                },
                "examples": {
                  "syncResponse": {
                    "value": {
                      "id": "chatcmpl-9QZ6v4T7xY",
                      "created": 1718700000,
                      "model": "web-search-pro",
                      "choices": [
                        {
                          "index": 0,
                          "finish_reason": "stop",
                          "message": {
                            "role": "Tool",
                            "tool_calls": [
                              {
                                "id": "call_1a2b3c",
                                "type": "search_result",
                                "search_result": [
                                  {
                                    "index": 0,
                                    "title": "2024年新能源汽车市场报告",
                                    "link": "https://auto.stats.gov.cn/report/2024",
                                    "content": "2024年1-6月新能源车销量达500万辆...",
                                    "refer": "[ref_1]"
                                  }
                                ]
                              }
                            ]
                          }
                        }
                      ],
                      "usage": {
                        "prompt_tokens": 25,
                        "completion_tokens": 120,
                        "total_tokens": 145
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "description": "无效请求参数",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "error": {
                      "type": "string",
                      "example": "Invalid 'messages' format"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "SearchRequest": {
        "type": "object",
        "required": ["tool", "messages"],
        "properties": {
          "tool": {
            "type": "string",
            "enum": ["web-search-pro"],
            "example": "web-search-pro",
            "description": "固定值'web-search-pro'"
          },
          "messages": {
            "type": "array",
            "minItems": 1,
            "maxItems": 1,
            "items": {
              "type": "object",
              "required": ["role", "content"],
              "properties": {
                "role": {
                  "type": "string",
                  "enum": ["user"],
                  "example": "user"
                },
                "content": {
                  "type": "string",
                  "minLength": 1,
                  "example": "上海人工智能政策",
                  "description": "用户原始提问"
                }
              }
            }
          },
          "request_id": {
            "type": "string",
            "pattern": "^[a-f0-9-]{36}$",
            "example": "123e4567-e89b-12d3-a456-426614174000",
            "description": "请求唯一标识"
          },
          "stream": {
            "type": "boolean",
            "default": true,
            "description": "是否启用流式返回"
          }
        }
      },
      "SearchIntent": {
        "type": "object",
        "required": ["index", "query", "intent"],
        "properties": {
          "index": {
            "type": "integer",
            "minimum": 0,
            "example": 0
          },
          "query": {
            "type": "string",
            "example": "北京实时天气"
          },
          "intent": {
            "type": "string",
            "enum": ["SEARCH_URL", "SEARCH_TOOL", "SEARCH_ALL", "SEARCH_NONE"],
            "example": "SEARCH_ALL"
          },
          "keywords": {
            "type": "string",
            "example": "北京 天气 实时"
          },
          "category": {
            "type": "string",
            "example": "weather"
          }
        }
      },
      "SearchResult": {
        "type": "object",
        "required": ["index", "title", "link", "content"],
        "properties": {
          "index": {
            "type": "integer",
            "example": 0
          },
          "title": {
            "type": "string",
            "example": "北京市气象局"
          },
          "link": {
            "type": "string",
            "format": "uri",
            "example": "https://weather.gov.cn/beijing"
          },
          "content": {
            "type": "string",
            "example": "今日北京最高气温28℃,空气质量优..."
          },
          "icon": {
            "type": "string",
            "format": "uri",
            "example": "https://example.com/icon.png"
          },
          "media": {
            "type": "string",
            "example": "中国气象网"
          },
          "refer": {
            "type": "string",
            "pattern": "^\
$$
ref_\\d+\
$$
$",
            "example": "[ref_1]"
          }
        }
      },
      "SyncResponse": {
        "type": "object",
        "required": ["id", "created", "model", "choices"],
        "properties": {
          "id": {
            "type": "string",
            "example": "chatcmpl-9QZ6v4T7xY"
          },
          "created": {
            "type": "integer",
            "example": 1718700000
          },
          "model": {
            "type": "string",
            "example": "web-search-pro"
          },
          "choices": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/Choice"
            }
          },
          "usage": {
            "$ref": "#/components/schemas/TokenUsage"
          }
        }
      },
      "StreamResponse": {
        "type": "object",
        "required": ["id", "created", "model", "choices"],
        "properties": {
          "id": {
            "type": "string",
            "example": "chatcmpl-9QZ6v4T7xY"
          },
          "created": {
            "type": "integer",
            "example": 1718700000
          },
          "model": {
            "type": "string",
            "example": "web-search-pro"
          },
          "choices": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/StreamChoice"
            }
          },
          "usage": {
            "$ref": "#/components/schemas/TokenUsage"
          }
        }
      },
      "Choice": {
        "type": "object",
        "required": ["index", "finish_reason"],
        "properties": {
          "index": {
            "type": "integer",
            "example": 0
          },
          "finish_reason": {
            "type": "string",
            "enum": ["stop", "sensitive", "network_error"]
          },
          "message": {
            "$ref": "#/components/schemas/Message"
          }
        }
      },
      "StreamChoice": {
        "type": "object",
        "required": ["index", "finish_reason"],
        "properties": {
          "index": {
            "type": "integer",
            "example": 0
          },
          "finish_reason": {
            "type": "string",
            "enum": ["stop", "sensitive", "network_error"]
          },
          "delta": {
            "$ref": "#/components/schemas/DeltaMessage"
          }
        }
      },
      "Message": {
        "type": "object",
        "required": ["role", "tool_calls"],
        "properties": {
          "role": {
            "type": "string",
            "enum": ["Tool"]
          },
          "tool_calls": {
            "type": "array",
            "items": {
              "oneOf": [
                {"$ref": "#/components/schemas/IntentToolCall"},
                {"$ref": "#/components/schemas/ResultToolCall"}
              ]
            }
          }
        }
      },
      "DeltaMessage": {
        "type": "object",
        "required": ["role", "tool_calls"],
        "properties": {
          "role": {
            "type": "string",
            "enum": ["Tool"]
          },
          "tool_calls": {
            "type": "array",
            "items": {
              "oneOf": [
                {"$ref": "#/components/schemas/IntentToolCall"},
                {"$ref": "#/components/schemas/ResultToolCall"}
              ]
            }
          }
        }
      },
      "IntentToolCall": {
        "type": "object",
        "required": ["id", "type", "search_intent"],
        "properties": {
          "id": {
            "type": "string",
            "example": "intent_123"
          },
          "type": {
            "type": "string",
            "enum": ["search_intent"]
          },
          "search_intent": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SearchIntent"
            }
          }
        }
      },
      "ResultToolCall": {
        "type": "object",
        "required": ["id", "type", "search_result"],
        "properties": {
          "id": {
            "type": "string",
            "example": "result_456"
          },
          "type": {
            "type": "string",
            "enum": ["search_result"]
          },
          "search_result": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/SearchResult"
            }
          }
        }
      },
      "TokenUsage": {
        "type": "object",
        "properties": {
          "prompt_tokens": {
            "type": "integer",
            "example": 30
          },
          "completion_tokens": {
            "type": "integer",
            "example": 150
          },
          "total_tokens": {
            "type": "integer",
            "example": 180
          }
        }
      }
    }
  }
}

实现步骤

将工具添加到dify中

2025-02-25T10:13:56.png

鉴权方式

2025-02-25T10:14:24.png

到智谱开放平台获取key

https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys
获取key后填入到上面章节的鉴权方式中
2025-02-25T10:14:34.png

工作流调用

2025-02-25T10:14:39.png
2025-02-25T10:20:48.png

最后修改:2025 年 02 月 25 日
如果觉得我的文章对你有用,请随意赞赏