Link Search Menu Expand Document Documentation Menu

智能体与工具教程

2.13 版本引入

本教程演示如何创建用于检索增强生成 (RAG) 的流式代理。流式代理按照指定顺序依次运行其配置的工具。在本示例中,您将创建一个包含两个工具的代理

  1. VectorDBTool:代理将使用此工具检索与用户问题相关的 OpenSearch 文档。您将补充信息摄取到 OpenSearch 索引中。为了方便向量搜索,您将部署一个文本嵌入模型,将文本转换为向量嵌入。OpenSearch 会将摄取的文档转换为嵌入并存储在索引中。当您向代理提供用户问题时,代理将从问题构建查询,在 OpenSearch 索引上运行向量搜索,并将相关的检索文档传递给 MLModelTool
  2. MLModelTool:代理将运行此工具以连接到大型语言模型 (LLM),并将用 OpenSearch 文档增强的用户查询发送给模型。在本示例中,您将使用托管在 Amazon Bedrock 上的 Anthropic Claude 模型。LLM 将根据其知识和提供的文档回答问题。

先决条件

要使用内存功能,请首先配置以下集群设置。本教程假设您没有专用的机器学习 (ML) 节点

PUT _cluster/settings
{
  "persistent": {
    "plugins.ml_commons.only_run_on_ml_node": "false",
    "plugins.ml_commons.memory_feature_enabled": "true"
  }
}

更多信息,请参阅ML Commons 集群设置

步骤 1:注册并部署文本嵌入模型

您需要一个文本嵌入模型来方便向量搜索。在本教程中,您将使用 OpenSearch 提供的预训练模型之一。选择模型时,请注意其维度,因为在创建索引时需要提供此维度。

在本教程中,您将使用 huggingface/sentence-transformers/all-MiniLM-L12-v2 模型,该模型生成 384 维密集向量嵌入。要注册和部署模型,请发送以下请求

POST /_plugins/_ml/models/_register?deploy=true
{
  "name": "huggingface/sentence-transformers/all-MiniLM-L12-v2",
  "version": "1.0.2",
  "model_format": "TORCH_SCRIPT"
}

注册模型是一个异步任务。OpenSearch 会为此任务返回一个任务 ID

{
  "task_id": "aFeif4oB5Vm0Tdw8yoN7",
  "status": "CREATED"
}

您可以通过调用 Tasks API 来检查任务状态:

GET /_plugins/_ml/tasks/aFeif4oB5Vm0Tdw8yoN7

任务完成后,任务状态将变为 COMPLETED,并且任务 API 响应将包含已部署模型的模型 ID

{
  "model_id": "aVeif4oB5Vm0Tdw8zYO2",
  "task_type": "REGISTER_MODEL",
  "function_name": "TEXT_EMBEDDING",
  "state": "COMPLETED",
  "worker_node": [
    "4p6FVOmJRtu3wehDD74hzQ"
  ],
  "create_time": 1694358489722,
  "last_update_time": 1694358499139,
  "is_async": true
}

步骤 2:创建摄取管道

为了将文本转换为向量嵌入,您将设置一个摄取管道。该管道会转换 text 字段,并将生成的向量嵌入写入 embedding 字段。通过在以下请求中指定上一步骤中的 model_id 来创建管道

PUT /_ingest/pipeline/test-pipeline-local-model
{
  "description": "text embedding pipeline",
  "processors": [
    {
      "text_embedding": {
        "model_id": "aVeif4oB5Vm0Tdw8zYO2",
        "field_map": {
          "text": "embedding"
        }
      }
    }
  ]
}

步骤 3:创建向量索引并摄取数据

现在,您将补充数据摄取到 OpenSearch 索引中。在 OpenSearch 中,向量存储在向量索引中。您可以通过发送以下请求来创建向量索引

PUT my_test_data
{
  "mappings": {
    "properties": {
      "text": {
        "type": "text"
      },
      "embedding": {
        "type": "knn_vector",
        "dimension": 384
      }
    }
  },
  "settings": {
    "index": {
      "knn.space_type": "cosinesimil",
      "default_pipeline": "test-pipeline-local-model",
      "knn": "true"
    }
  }
}

然后,使用批量请求将数据摄取到索引中

POST _bulk
{"index": {"_index": "my_test_data", "_id": "1"}}
{"text": "Chart and table of population level and growth rate for the Ogden-Layton metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\nThe current metro area population of Ogden-Layton in 2023 is 750,000, a 1.63% increase from 2022.\nThe metro area population of Ogden-Layton in 2022 was 738,000, a 1.79% increase from 2021.\nThe metro area population of Ogden-Layton in 2021 was 725,000, a 1.97% increase from 2020.\nThe metro area population of Ogden-Layton in 2020 was 711,000, a 2.16% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "2"}}
{"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.\\nThe current metro area population of New York City in 2023 is 18,937,000, a 0.37% increase from 2022.\\nThe metro area population of New York City in 2022 was 18,867,000, a 0.23% increase from 2021.\\nThe metro area population of New York City in 2021 was 18,823,000, a 0.1% increase from 2020.\\nThe metro area population of New York City in 2020 was 18,804,000, a 0.01% decline from 2019."}
{"index": {"_index": "my_test_data", "_id": "3"}}
{"text": "Chart and table of population level and growth rate for the Chicago metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Chicago in 2023 is 8,937,000, a 0.4% increase from 2022.\\nThe metro area population of Chicago in 2022 was 8,901,000, a 0.27% increase from 2021.\\nThe metro area population of Chicago in 2021 was 8,877,000, a 0.14% increase from 2020.\\nThe metro area population of Chicago in 2020 was 8,865,000, a 0.03% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "4"}}
{"text": "Chart and table of population level and growth rate for the Miami metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Miami in 2023 is 6,265,000, a 0.8% increase from 2022.\\nThe metro area population of Miami in 2022 was 6,215,000, a 0.78% increase from 2021.\\nThe metro area population of Miami in 2021 was 6,167,000, a 0.74% increase from 2020.\\nThe metro area population of Miami in 2020 was 6,122,000, a 0.71% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "5"}}
{"text": "Chart and table of population level and growth rate for the Austin metro area from 1950 to 2023. United Nations population projections are also included through the year 2035.\\nThe current metro area population of Austin in 2023 is 2,228,000, a 2.39% increase from 2022.\\nThe metro area population of Austin in 2022 was 2,176,000, a 2.79% increase from 2021.\\nThe metro area population of Austin in 2021 was 2,117,000, a 3.12% increase from 2020.\\nThe metro area population of Austin in 2020 was 2,053,000, a 3.43% increase from 2019."}
{"index": {"_index": "my_test_data", "_id": "6"}}
{"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.\\nThe current metro area population of Seattle in 2023 is 3,519,000, a 0.86% increase from 2022.\\nThe metro area population of Seattle in 2022 was 3,489,000, a 0.81% increase from 2021.\\nThe metro area population of Seattle in 2021 was 3,461,000, a 0.82% increase from 2020.\\nThe metro area population of Seattle in 2020 was 3,433,000, a 0.79% increase from 2019."}

步骤 4:创建到外部托管模型的连接器

您需要一个 LLM 来生成用户问题的响应。LLM 对于 OpenSearch 集群来说太大,因此您将创建到外部托管 LLM 的连接。在此示例中,您将创建到托管在 Amazon Bedrock 上的 Anthropic Claude 模型的连接器

POST /_plugins/_ml/connectors/_create
{
  "name": "BedRock test claude Connector",
  "description": "The connector to BedRock service for claude model",
  "version": 1,
  "protocol": "aws_sigv4",
  "parameters": {
      "region": "us-east-1",
      "service_name": "bedrock",
      "anthropic_version": "bedrock-2023-05-31",
      "endpoint": "bedrock.us-east-1.amazonaws.com",
      "auth": "Sig_V4",
      "content_type": "application/json",
      "max_tokens_to_sample": 8000,
      "temperature": 0.0001,
      "response_filter": "$.completion"
  },
  "credential": {
      "access_key": "<bedrock_access_key>",
      "secret_key": "<bedrock_secret_key>",
      "session_token": "<bedrock_session_token>"
  },
  "actions": [
    {
      "action_type": "predict",
      "method": "POST",
      "url": "https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-v2/invoke",
      "headers": { 
        "content-type": "application/json",
        "x-amz-content-sha256": "required"
      },
      "request_body": "{\"prompt\":\"${parameters.prompt}\", \"max_tokens_to_sample\":${parameters.max_tokens_to_sample}, \"temperature\":${parameters.temperature},  \"anthropic_version\":\"${parameters.anthropic_version}\" }"
    }
  ]
}

响应包含新创建连接器的连接器 ID。

{
  "connector_id": "a1eMb4kBJ1eYAeTMAljY"
}

步骤 5:注册并部署外部托管模型

与文本嵌入模型类似,LLM 需要注册并部署到 OpenSearch。要设置外部托管模型,请首先为此模型创建一个模型组

POST /_plugins/_ml/model_groups/_register
{
    "name": "test_model_group_bedrock",
    "description": "This is a public model group"
}

响应包含模型组 ID,您将使用它向此模型组注册模型。

{
 "model_group_id": "wlcnb4kBJ1eYAeTMHlV6",
 "status": "CREATED"
}

接下来,注册并部署外部托管的 Claude 模型

POST /_plugins/_ml/models/_register?deploy=true
{
    "name": "Bedrock Claude V2 model",
    "function_name": "remote",
    "model_group_id": "wlcnb4kBJ1eYAeTMHlV6",
    "description": "test model",
    "connector_id": "a1eMb4kBJ1eYAeTMAljY"
}

步骤 1 类似,响应包含一个任务 ID,您可以使用它来检查部署状态。模型部署完成后,状态将变为 COMPLETED,并且响应将包含 Claude 模型的模型 ID

{
  "model_id": "NWR9YIsBUysqmzBdifVJ",
  "task_type": "REGISTER_MODEL",
  "function_name": "remote",
  "state": "COMPLETED",
  "worker_node": [
    "4p6FVOmJRtu3wehDD74hzQ"
  ],
  "create_time": 1694358489722,
  "last_update_time": 1694358499139,
  "is_async": true
}

要测试 LLM,请发送以下预测请求

POST /_plugins/_ml/models/NWR9YIsBUysqmzBdifVJ/_predict
{
  "parameters": {
    "prompt": "\n\nHuman:hello\n\nAssistant:"
  }
}

步骤 6:注册并执行代理

最后,您将使用在步骤 1 中创建的文本嵌入模型和在步骤 5 中创建的 Claude 模型来创建一个流式代理。此流式代理将运行 VectorDBTool,然后运行 MLModelToolVectorDBTool 配置了在步骤 1 中为向量搜索创建的文本嵌入模型的模型 ID。MLModelTool 配置了在步骤 5 中创建的 Claude 模型

POST /_plugins/_ml/agents/_register
{
  "name": "Test_Agent_For_RAG",
  "type": "flow",
  "description": "this is a test agent",
  "tools": [
    {
      "type": "VectorDBTool",
      "parameters": {
        "model_id": "aVeif4oB5Vm0Tdw8zYO2",
        "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": "NWR9YIsBUysqmzBdifVJ",
        "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:"
      }
    }
  ]
}

OpenSearch 会为新创建的代理返回一个代理 ID

{
  "agent_id": "879v9YwBjWKCe6Kg12Tx"
}

您可以通过向 agents 端点发送请求并提供代理 ID 来检查代理

GET /_plugins/_ml/agents/879v9YwBjWKCe6Kg12Tx

要执行代理,请发送以下请求。注册代理时,您已将其配置为接收 parameters.question,因此您需要在请求中提供此参数。此参数表示人类生成的用户问题

POST /_plugins/_ml/agents/879v9YwBjWKCe6Kg12Tx/_execute
{
  "parameters": {
    "question": "what's the population increase of Seattle from 2021 to 2023"
  }
}

LLM 的知识库中没有最新信息,因此它会根据摄取的数据推断问题响应,从而演示 RAG

{
  "inference_results": [
    {
      "output": [
        {
          "result": """ Based on the given context, the key information is:

The metro area population of Seattle in 2021 was 3,461,000.
The metro area population of Seattle in 2023 is 3,519,000.

To calculate the population increase from 2021 to 2023:

Population in 2023 (3,519,000) - Population in 2021 (3,461,000) = 58,000

Therefore, the population increase of Seattle from 2021 to 2023 is 58,000."""
        }
      ]
    }
  ]
}