网页搜索工具
3.0 版本引入
WebSearchTool
根据用户的提问检索搜索结果。它支持 Google、Bing 和 DuckDuckGo 作为搜索引擎,或可以使用 自定义 API 执行搜索。
将 DuckDuckGo 用作搜索引擎
要将 DuckDuckGo 用作 WebSearchTool
的搜索引擎,请按照以下步骤操作。
步骤 1:注册将运行 WebSearchTool 的流代理
流代理按顺序运行一系列工具,并返回最后一个工具的输出。要创建流代理,请发送以下注册代理请求:
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_WebSearch_tool",
"type": "flow",
"description": "this is a test agent for the WebSearchTool",
"tools": [
{
"type": "WebSearchTool",
"name": "DuckduckgoWebSearchTool",
"parameters": {
"engine": "duckduckgo",
"input": "${parameters.question}"
}
}
]
}
有关参数描述,请参阅注册参数。
OpenSearch 返回一个代理 ID
{
"agent_id": "9X7xWI0Bpc3sThaJdY9i"
}
步骤 2:运行代理
然后,通过发送以下请求来运行代理(DuckDuckGo 不需要任何凭据)
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
{
"parameters": {
"question": "How to create a index pattern in OpenSearch?"
}
}
OpenSearch 返回 Web 搜索结果
{
"inference_results": [
{
"output": [
{
"name": "response",
"result": """
{
"next_page": "https://html.duckduckgo.com/html?q=how+to+create+index+pattern+in+OpenSearch&ia=web&dc=11",
"items": [
{
"url": "http://someurl",
"title": "the page result title",
"content": "the page content..."
},
{
"url": "https://anotherurl",
"title": "the page result title",
"content": "the page content..."
}
...
]
}
"""
}
]
}
]
}
将 Google 用作搜索引擎
要将 Google 用作 WebSearchTool
的搜索引擎,请按照以下步骤操作。
步骤 1:注册将运行 WebSearchTool 的流代理
流代理按顺序运行一系列工具,并返回最后一个工具的输出。要创建流代理,请发送以下注册代理请求:
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_WebSearch_tool",
"type": "flow",
"description": "this is a test agent for the WebSearchTool",
"tools": [
{
"type": "WebSearchTool",
"name": "GoogleWebSearchTool",
"parameters": {
"engine": "google",
"engine_id": "${your_google_engine_id}",
"api_key": "${your_google_api_key}",
"input": "${parameters.question}"
}
}
]
}
有关参数描述,请参阅注册参数。
OpenSearch 返回一个代理 ID
{
"agent_id": "9X7xWI0Bpc3sThaJdY9i"
}
步骤 2:运行代理
在运行代理之前,请确保已获取以编程方式访问 Google 搜索所需的凭据。
然后,通过发送以下请求运行代理
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
{
"parameters": {
"question": "How to create a index pattern in OpenSearch?"
}
}
OpenSearch 返回 Web 搜索结果
{
"inference_results": [
{
"output": [
{
"name": "response",
"result": """
{
"next_page": "https://customsearch.googleapis.com/customsearch/v1?q=how+to+create+index+pattern+in+OpenSearch&start=10",
"items": [
{
"url": "http://someurl",
"title": "the page result title",
"content": "the page content..."
},
{
"url": "https://anotherurl",
"title": "the page result title",
"content": "the page content..."
}
...
]
}
"""
}
]
}
]
}
将自定义 API 用作搜索引擎
要将自定义 API 用作 WebSearchTool
的搜索引擎,请按照以下步骤操作。
步骤 1:注册将运行 WebSearchTool 的流代理
要使用自定义端点进行搜索,您需要配置以下参数:
Authorization
:用于身份验证endpoint
:用于 API 连接custom_res_url_jsonpath
:用于解析 JSON 响应和提取链接
您的 API 必须以 JSON 格式返回响应。API 返回的链接必须能够使用 JSONPath 表达式进行检索。其他参数,如 query_key
、offset_key
和 limit_key
是可选的,但如果您的 API 使用与默认值不同的值,则应指定它们。
要创建流代理,请发送以下注册代理请求:
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_WebSearch_tool",
"type": "flow",
"description": "this is a test agent for the WebSearchTool",
"tools": [
{
"type": "WebSearchTool",
"name": "CustomWebSearchTool",
"parameters": {
"engine": "custom",
"endpoint": "${your_custom_endpoint}",
"custom_res_url_jsonpath": "$.data[*].link",
"Authorization": "Bearer xxxx",
"query_key": "q",
"offset_key": "offset",
"limit_key": "limit"
}
}
]
}
有关参数描述,请参阅注册参数。
OpenSearch 返回一个代理 ID
{
"agent_id": "9X7xWI0Bpc3sThaJdY9i"
}
步骤 2:运行代理
在运行代理之前,请确保您已获得以编程方式访问自定义搜索 API 所需的凭据。
然后,通过发送以下请求运行代理
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
{
"parameters": {
"question": "How to create a index pattern in OpenSearch?"
}
}
OpenSearch 返回 Web 搜索结果
{
"inference_results": [
{
"output": [
{
"name": "response",
"result": """
{
"next_page": "{your_custom_endpoint}?q=how+to+create+index+pattern+in+OpenSearch&offset=10&limit=10",
"items": [
{
"url": "http://someurl",
"title": "the page result title",
"content": "the page content..."
},
{
"url": "https://anotherurl",
"title": "the page result title",
"content": "the page content..."
}
...
]
}
"""
}
]
}
]
}
注册参数
下表列出了注册代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
engine | 字符串 | 必需 | 要使用的搜索引擎。有效值为 google 、bing 、duckduckgo 或 custom 。 |
engine_id | 字符串 | 可选 | Google 的自定义搜索引擎 ID。当 engine 设置为 google 时必需。 |
api_key | 字符串 | 可选 | 用于身份验证的 API 密钥。当 engine 设置为 google 或 bing 时必需。 |
endpoint | 字符串 | 可选 | 自定义搜索 API 的 URL 端点。当 engine 设置为 custom 时必需。 |
Authorization | 字符串 | 可选 | 自定义 API 的授权头值。当 engine 设置为 custom 时必需。 |
query_key | 字符串 | 可选 | 自定义 API URL 中搜索查询的参数名称(例如,${endpoint}?my_query_key=${question} )。默认值为 q 。 |
offset_key | 字符串 | 可选 | 自定义 API URL 中分页偏移量的参数名称(例如,${endpoint}?q=${question}&start=10 )。默认值为 offset 。 |
limit_key | 字符串 | 可选 | 自定义 API URL 中结果限制的参数名称(例如,${endpoint}?q=${question}&start=10&limit=10 )。默认值为 limit 。 |
custom_res_url_jsonpath | 字符串 | 可选 | 用于从自定义 API 响应中提取 URL 的 JSONPath 表达式(例如,$[*].link )。当 engine 设置为 custom 时必需。 |
执行参数
下表列出了运行代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
question | 字符串 | 必需 | 要发送到 LLM 的自然语言问题。 |