RAG 工具
2.13 版本引入
RAGTool
执行检索增强生成 (RAG)。有关 RAG 的更多信息,请参阅对话式搜索。
RAG 调用大型语言模型 (LLM),并通过提供相关的 OpenSearch 文档以及用户问题来补充其知识。要从 OpenSearch 索引中检索相关文档,您需要一个支持向量搜索的文本嵌入模型。
RAG 工具支持以下搜索方法:
开始之前
要注册和部署文本嵌入模型和 LLM 并将数据摄取到索引中,请执行智能体和工具教程的步骤 1-5。
以下示例使用神经搜索。要配置神经稀疏搜索并部署稀疏编码模型,请参阅神经稀疏搜索。
步骤 1:注册一个将运行 RAGTool 的流智能体
流智能体按顺序运行一系列工具并返回最后一个工具的输出。要创建流智能体,请发送以下请求,在 embedding_model_id
参数中提供文本嵌入模型 ID,在 inference_model_id
参数中提供 LLM 模型 ID:
POST /_plugins/_ml/agents/_register
{
"name": "Test_Agent_For_RagTool",
"type": "flow",
"description": "this is a test flow agent",
"tools": [
{
"type": "RAGTool",
"description": "A description of the tool",
"parameters": {
"embedding_model_id": "Hv_PY40Bk4MTqircAVmm",
"inference_model_id": "SNzSY40B_1JGmyB0WbfI",
"index": "my_test_data",
"embedding_field": "embedding",
"query_type": "neural",
"source_field": [
"text"
],
"input": "${parameters.question}",
"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\n Context:\n${parameters.output_field}\n\nHuman:${parameters.question}\n\nAssistant:"
}
}
]
}
有关参数描述,请参阅注册参数。
OpenSearch 返回一个代理 ID
{
"agent_id": "9X7xWI0Bpc3sThaJdY9i"
}
要创建包含 RAGTool
的对话式智能体,请参阅对话式智能体。
步骤 2:运行代理
在运行智能体之前,请确保添加了 OpenSearch Dashboards 示例 Sample web logs
数据集。要了解更多信息,请参阅添加示例数据。
然后,通过发送以下请求运行代理
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": """{"_index":"my_test_data","_source":{"text":"Chart and table of population level and growth rate for the Seattle metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\n
The current metro area population of Seattle in 2023 is 3,519,000, a 0.86% increase from 2022.\\n
The metro area population of Seattle in 2022 was 3,489,000, a 0.81% increase from 2021.\\n
The metro area population of Seattle in 2021 was 3,461,000, a 0.82% increase from 2020.\\n
The metro area population of Seattle in 2020 was 3,433,000, a 0.79% increase from 2019."},"_id":"6","_score":0.8173238}
{"_index":"my_test_data","_source":{"text":"Chart and table of population level and growth rate for the New York City metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\n
The current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\\n
The metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\\n
The metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\\n
The metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."},"_id":"2","_score":0.6641471}
"""
}
]
}
]
}
注册参数
下表列出了注册代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
embedding_model_id | 字符串 | 必需 | 用于生成向量嵌入的模型 ID。 |
inference_model_id | 字符串 | 必需 | 用于推理的 LLM 的模型 ID。 |
index | 字符串 | 必需 | 从中检索相关文档以传递给 LLM 的索引。 |
embedding_field | 字符串 | 必需 | 当模型编码原始文本文档时,编码结果会保存在一个字段中。将此字段指定为 embedding_field 。神经搜索通过计算查询文本与文档 embedding_field 中文本之间的相似性分数来匹配文档与查询。 |
source_field | 字符串 | 必需 | 要返回的文档字段。您可以提供多个字段的列表作为字符串数组,例如 ["field1", "field2"] 。 |
input | 字符串 | 流智能体的必需参数 | 源自流智能体参数的运行时输入。如果使用 LLM,此字段将填充 LLM 响应。 |
output_field | 字符串 | 可选 | 输出字段的名称。默认为 response 。 |
query_type | 字符串 | 可选 | 指定用于执行神经搜索的查询类型。有效值为 neural (用于密集检索)和 neural_sparse (用于稀疏检索)。默认为 neural 。 |
doc_size | 整数 | 可选 | 要获取的文档数量。默认为 2 。 |
prompt | 字符串 | 可选 | 提供给 LLM 的提示。 |
k | 整数 | 可选 | 执行神经搜索时要搜索的最近邻居数量。默认为 10。 |
enable_Content_Generation | 布尔型 | 可选 | 如果为 true ,则返回 LLM 生成的结果。如果为 false ,则直接返回结果,无需 LLM 辅助内容生成。默认为 true 。 |
nested_path | 字符串 | 可选 | 嵌套查询的嵌套对象的路径。仅用于嵌套字段。默认为 null 。 |
执行参数
下表列出了运行代理时可用的所有工具参数。
参数 | 类型 | 必需/可选 | 描述 |
---|---|---|---|
question | 字符串 | 必需 | 要发送到 LLM 的自然语言问题。 |