2025年AI大模型普及的元年,教你使用Dify自定义工具,实现模型联网搜索

通常 在Dify中使用TAVILY SEARCH进行联网搜索,本次教程教你在dify中利用智谱查询工具实现模型联网搜索。
智谱AI开放平台 apikey申请地址

dify自定义工具中 添加

image


设置api鉴权,记得自行去注册 生成一个apikey

image


工作流设置

image


效果

image

image

{
  "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
          }
        }
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

遇见火星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值