机器学习模型工具
plugins.ml_commons.rag_pipeline_feature_enabled: true
MLModelTool 运行机器学习 (ML) 模型并返回推理结果。
步骤 1:为模型创建连接器
以下示例请求为托管在 Amazon SageMaker 上的模型创建连接器
POST /_plugins/_ml/connectors/_create
{
"name": "sagemaker model",
"description": "Test connector for Sagemaker 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"
}
步骤 2:注册并部署模型
要将模型注册并部署到 OpenSearch,请发送以下请求,并提供上一步中的连接器 ID
POST /_plugins/_ml/models/_register?deploy=true
{
"name": "remote-inferene",
"function_name": "remote",
"description": "test model",
"connector_id": "eJATWo0BkIylWTeYToTn"
}
OpenSearch 将返回一个模型 ID
{
"task_id": "7X7pWI0Bpc3sThaJ4I8R",
"status": "CREATED",
"model_id": "h5AUWo0BkIylWTeYT4SU"
}
步骤 3:注册将运行 MLModelTool 的流代理
流代理按顺序运行一系列工具并返回最后一个工具的输出。要创建流代理,请发送以下注册代理请求,并在 model_id
参数中提供模型 ID
POST /_plugins/_ml/agents/_register
{
"name": "Test agent for embedding model",
"type": "flow",
"description": "this is a test agent",
"tools": [
{
"type": "MLModelTool",
"description": "A general tool to answer any question",
"parameters": {
"model_id": "h5AUWo0BkIylWTeYT4SU",
"prompt": "\n\nHuman:You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say don't know. \n\nHuman:${parameters.question}\n\nAssistant:"
}
}
]
}
有关参数描述,请参阅注册参数。
OpenSearch 返回一个代理 ID
{
"agent_id": "9X7xWI0Bpc3sThaJdY9i"
}
步骤 4:运行代理
通过发送以下请求来运行代理
POST /_plugins/_ml/agents/9X7xWI0Bpc3sThaJdY9i/_execute
{
"parameters": {
"question": "what's the population increase of Seattle from 2021 to 2023"
}
}
OpenSearch 返回推理结果
{
"inference_results": [
{
"output": [
{
"name": "response",
"result": " I do not have direct data on the population increase of Seattle from 2021 to 2023 in the context provided. As a data analyst, I would need to research population statistics from credible sources like the US Census Bureau to analyze population trends and make an informed estimate. Without looking up actual data, I don't have enough information to provide a specific answer to the question."
}
]
}
]
}
注册参数
下表列出了注册代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
model_id | 字符串 | 必需 | 用于生成响应的大型语言模型 (LLM) 的模型 ID。 |
prompt | 字符串 | 可选 | 提供给 LLM 的提示。 |
response_field | 字符串 | 可选 | 响应字段的名称。默认为 response 。 |
执行参数
下表列出了运行代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
question | 字符串 | 必需 | 要发送到 LLM 的自然语言问题。 |