PPL 工具
2.13 版本引入
The PPLTool
将自然语言转换为 PPL 查询。该工具提供一个 execute
标志,用于指定是否运行查询。如果将该标志设置为 true
,PPLTool
将运行查询并返回查询及其结果。
先决条件
要创建 PPL 工具,您需要一个经过微调的模型,该模型能将自然语言转换为 PPL 查询。此外,您也可以使用大型语言模型进行基于提示的翻译。PPL 工具支持 Anthropic Claude 和 OpenAI 模型。
步骤 1:为模型创建连接器
以下示例请求为托管在 Amazon SageMaker 上的模型创建连接器
POST /_plugins/_ml/connectors/_create
{
"name": "sagemaker: t2ppl",
"description": "Test connector for Sagemaker t2ppl model",
"version": 1,
"protocol": "aws_sigv4",
"credential": {
"access_key": "<YOUR ACCESS KEY>",
"secret_key": "<YOUR SECRET KEY>"
},
"parameters": {
"region": "us-east-1",
"service_name": "sagemaker"
},
"actions": [
{
"action_type": "predict",
"method": "POST",
"headers": {
"content-type": "application/json"
},
"url": "<YOUR SAGEMAKER ENDPOINT>",
"request_body": """{"prompt":"${parameters.prompt}"}"""
}
]
}
OpenSearch 返回连接器 ID
{
"connector_id": "eJATWo0BkIylWTeYToTn"
}
有关连接到 Anthropic Claude 模型或 OpenAI 模型的信息,请参阅连接器。
步骤 2:注册并部署模型
要注册模型并将其部署到 OpenSearch,请发送以下请求,提供上一步中的连接器 ID
POST /_plugins/_ml/models/_register?deploy=true
{
"name": "remote-inference",
"function_name": "remote",
"description": "test model",
"connector_id": "eJATWo0BkIylWTeYToTn"
}
OpenSearch 返回模型 ID
{
"task_id": "7X7pWI0Bpc3sThaJ4I8R",
"status": "CREATED",
"model_id": "h5AUWo0BkIylWTeYT4SU"
}
步骤 3:注册将运行 PPLTool 的流式智能体
流式智能体按顺序运行一系列工具并返回最后一个工具的输出。要创建流式智能体,请发送以下注册智能体请求,并在 model_id
参数中提供模型 ID。要运行生成的查询,请将 execute
设置为 true
。
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_PPL",
"type": "flow",
"description": "this is a test agent",
"memory": {
"type": "demo"
},
"tools": [
{
"type": "PPLTool",
"name": "TransferQuestionToPPLAndExecuteTool",
"description": "Use this tool to transfer natural language to generate PPL and execute PPL to query inside. Use this tool after you know the index name, otherwise, call IndexRoutingTool first. The input parameters are: {index:IndexName, question:UserQuestion}",
"parameters": {
"model_id": "h5AUWo0BkIylWTeYT4SU",
"model_type": "FINETUNE",
"execute": true
}
}
]
}
有关参数描述,请参阅注册参数。
OpenSearch 返回一个代理 ID
{
"agent_id": "9X7xWI0Bpc3sThaJdY9i"
}
步骤 4:运行代理
在运行智能体之前,请确保添加 OpenSearch Dashboards 示例 Sample web logs
数据集。要了解更多信息,请参阅添加示例数据。
然后,通过发送以下请求运行代理
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
{
"parameters": {
"verbose": true,
"question": "what is the error rate yesterday",
"index": "opensearch_dashboards_sample_data_logs"
}
}
OpenSearch 返回 PPL 查询和查询结果
{
"inference_results": [
{
"output": [
{
"name": "response",
"result":"{\"ppl\":\"source\=opensearch_dashboards_sample_data_logs| where timestamp \> DATE_SUB(NOW(), INTERVAL 1 DAY) AND timestamp \< NOW() | eval is_error\=IF(response\=\'200\', 0, 1.0) | stats AVG(is_error) as error_rate\",\"executionResult\":\"{\\n \\\"schema\\\": [\\n {\\n \\\"name\\\": \\\"error_rate\\\",\\n \\\"type\\\": \\\"double\\\"\\n }\\n ],\\n \\\"datarows\\\": [\\n [\\n null\\n ]\\n ],\\n \\\"total\\\": 1,\\n \\\"size\\\": 1\\n}\"}"
}
]
}
]
}
如果您将 execute
设置为 false
,OpenSearch 只返回查询但不运行它。
{
"inference_results": [
{
"output": [
{
"name": "response",
"result": "source=opensearch_dashboards_sample_data_logs| where timestamp > DATE_SUB(NOW(), INTERVAL 1 DAY) AND timestamp < NOW() | eval is_error=IF(response='200', 0, 1.0) | stats AVG(is_error) as error_rate"
}
]
}
]
}
注册参数
下表列出了注册代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
model_id | 字符串 | 必需 | 用于将文本翻译成 PPL 查询的大型语言模型 (LLM) 的模型 ID。 |
model_type | 字符串 | 可选 | 模型类型。有效值为 CLAUDE (Anthropic Claude 模型)、OPENAI (OpenAI 模型)和 FINETUNE (自定义微调模型)。 |
prompt | 字符串 | 可选 | 提供给 LLM 的提示。 |
execute | 布尔型 | 可选 | 指定是否运行 PPL 查询。默认为 true 。 |
input | 对象 | 可选 | 包含两个参数,用于指定要搜索的索引和 LLM 的问题。例如,"input": "{\"index\": \"${parameters.index}\", \"question\": ${parameters.question} }" 。 |
head | 整数 | 可选 | 如果 execute 设置为 true ,则限制返回的执行结果数量。默认为 -1 (无限制)。 |
执行参数
下表列出了运行代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
index | 字符串 | 必需 | 运行 PPL 查询的索引。 |
question | 字符串 | 必需 | 要发送到 LLM 的自然语言问题。 |
verbose | 布尔型 | 可选 | 是否提供详细输出。默认为 false 。 |