代理和工具
2.12 版本引入WARNING
这是一项实验性功能,不建议在生产环境中使用。如需了解该功能的最新进展或提供反馈,请参阅相关的 GitHub issue。
您可以使用代理和工具来自动化机器学习 (ML) 任务。代理 负责编排和运行 ML 模型及工具。工具 执行一组特定的任务。工具的一些示例包括支持向量搜索的 VectorDBTool 和执行 cat indices 操作的 CATIndexTool。有关支持的工具列表,请参阅工具。
代理
代理 是使用大型语言模型 (LLM) 解决问题的协调器。在 LLM 进行推理并决定采取什么行动后,代理协调行动的执行。OpenSearch 支持以下代理类型:
- 流程代理:按照配置中指定的顺序依次运行工具。流程代理的工作流程是固定的。适用于检索增强生成 (RAG)。
- 对话流程代理:按照配置中指定的顺序依次运行工具。对话流程代理的工作流程是固定的。存储对话历史,以便用户可以提出后续问题。适用于创建聊天机器人。
- 对话代理:基于可用知识(包括 LLM 知识库和提供给 LLM 的一组工具)进行推理以提供响应。存储对话历史,以便用户可以提出后续问题。对话代理的工作流程是可变的,取决于后续问题。对于特定问题,使用思维链 (CoT) 过程从配置的工具中选择最佳工具来回答问题。适用于创建使用 RAG 的聊天机器人。
流程代理
流程代理配置了一组按顺序运行的工具。例如,以下代理先运行 VectorDBTool,然后运行 MLModelTool。代理协调这些工具,使一个工具的输出可以成为另一个工具的输入。在此示例中,VectorDBTool 查询 k-NN 索引,代理将其输出 ${parameters.VectorDBTool.output} 作为上下文传递给 MLModelTool,同时传递 ${parameters.question}(请参阅 prompt 参数):
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_RAG",
"type": "flow",
"description": "this is a test agent",
"tools": [
{
"type": "VectorDBTool",
"parameters": {
"model_id": "YOUR_TEXT_EMBEDDING_MODEL_ID",
"index": "my_test_data",
"embedding_field": "embedding",
"source_field": ["text"],
"input": "${parameters.question}"
}
},
{
"type": "MLModelTool",
"description": "A general tool to answer any question",
"parameters": {
"model_id": "YOUR_LLM_MODEL_ID",
"prompt": "\n\nHuman:You are a professional data analyst. You will always answer a 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 you don't know. \n\n Context:\n${parameters.VectorDBTool.output}\n\nHuman:${parameters.question}\n\nAssistant:"
}
}
]
}对话流程代理
与流程代理类似,对话流程代理配置了一组按顺序运行的工具。它们之间的区别在于对话流程代理将对话存储在索引中,在以下示例中为 conversation_index。以下代理运行 VectorDBTool,然后运行 MLModelTool:
POST /_plugins/_ml/agents/_register
{
"name": "population data analysis agent",
"type": "conversational_flow",
"description": "This is a demo agent for population data analysis",
"app_type": "rag",
"memory": {
"type": "conversation_index"
},
"tools": [
{
"type": "VectorDBTool",
"name": "population_knowledge_base",
"parameters": {
"model_id": "YOUR_TEXT_EMBEDDING_MODEL_ID",
"index": "test_population_data",
"embedding_field": "population_description_embedding",
"source_field": [
"population_description"
],
"input": "${parameters.question}"
}
},
{
"type": "MLModelTool",
"name": "bedrock_claude_model",
"description": "A general tool to answer any question",
"parameters": {
"model_id": "YOUR_LLM_MODEL_ID",
"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\nContext:\n${parameters.population_knowledge_base.output:-}\n\n${parameters.chat_history:-}\n\nHuman:${parameters.question}\n\nAssistant:"
}
}
]
}对话代理
与对话流程代理类似,对话代理将对话存储在索引中,在以下示例中为 conversation_index。对话代理可以配置一个 LLM 和一组执行特定工作的辅助工具。例如,您可以在配置代理时设置一个 LLM 和一个 CATIndexTool。当您向模型发送问题时,代理还会将 CATIndexTool 作为上下文包含在内。然后 LLM 决定是否需要使用 CATIndexTool 来回答诸如"我的集群中有多少索引?"这样的问题。上下文允许 LLM 回答其知识库之外的特定问题。例如,以下代理配置了一个 LLM 和一个检索 OpenSearch 索引信息的 CATIndexTool:
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_ReAct_ClaudeV2",
"type": "conversational",
"description": "this is a test agent",
"llm": {
"model_id": "YOUR_LLM_MODEL_ID",
"parameters": {
"max_iteration": 5,
"stop_when_no_tool_found": true,
"response_filter": "$.completion"
}
},
"memory": {
"type": "conversation_index"
},
"tools": [
{
"type": "VectorDBTool",
"name": "VectorDBTool",
"description": "A tool to search opensearch index with natural language quesiotn. If you don't know answer for some question, you should always try to search data with this tool. Action Input: <natrual language question>",
"parameters": {
"model_id": "YOUR_TEXT_EMBEDDING_MODEL_ID",
"index": "my_test_data",
"embedding_field": "embedding",
"source_field": [ "text" ],
"input": "${parameters.question}"
}
},
{
"type": "CatIndexTool",
"name": "RetrieveIndexMetaTool",
"description": "Use this tool to get OpenSearch index information: (health, status, index, uuid, primary count, replica count, docs.count, docs.deleted, store.size, primary.store.size)."
}
],
"app_type": "my app"
}TIP
提供详尽的工具描述非常重要,这样 LLM 才能决定在哪些情况下使用这些工具。
启用功能
要启用代理和工具,请配置以下设置:
plugins.ml_commons.agent_framework_enabled: true对于对话代理,您还需要启用 RAG 以在对话搜索中使用。要启用 RAG,请配置以下设置:
plugins.ml_commons.rag_pipeline_feature_enabled: true有关启用实验性功能的更多信息,请参阅实验性功能标志。