自动生成嵌入向量

您可以在 UDB-SX 内部的数据摄入过程中动态生成嵌入向量。此方法通过自动将数据转换为向量,提供简化的工作流程。

UDB-SX 可以通过两种方式自动从您的文本数据生成嵌入向量:

  • 手动设置(推荐用于自定义配置):单独配置每个组件,以完全控制实现过程。

  • 自动化工作流(推荐用于快速设置):使用默认设置和工作流,以最少的配置快速实现。

前提条件

对于此简单设置,您将使用 UDB-SX 提供的机器学习模型和一个没有专用 ML 节点的集群。为确保此基本本地设置正常工作,请发送以下请求以更新与 ML 相关的集群设置:

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

选择机器学习模型

自动生成嵌入向量需要配置一个语言模型,该模型将在数据摄入时和查询时将文本转换为嵌入向量。

选择模型时,您有以下选项:

在此示例中,您将使用来自 Hugging Face 的 DistilBERT 模型,这是 UDB-SX 中可用的预训练模型之一。更多信息,请参阅 集成机器学习模型

请注意: 请记下模型的维度,因为在设置向量索引时您将需要用到它。

手动设置

为了更精细地控制配置,您可以按照以下步骤手动设置每个组件。

步骤 1:注册并部署模型

要注册并部署模型,请发送以下请求:

POST /_plugins/_ml/models/_register?deploy=true
{
  "name": "huggingface/sentence-transformers/msmarco-distilbert-base-tas-b",
  "version": "1.0.1",
  "model_format": "TORCH_SCRIPT"
}

注册模型是一个异步任务。UDB-SX 会返回此任务的任务 ID:

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

您可以使用任务 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
}

您需要此模型 ID,以便在后续的几个步骤中使用该模型。

步骤 2:创建摄入管道

首先,您需要创建一个摄入管道,该管道包含一个处理器:在文档被摄入到索引之前转换文档字段的任务。您将设置一个 text_embedding 处理器,用于从文本创建向量嵌入。您将需要上一节中设置的模型的 model_id 和一个 field_map,该映射指定了从中获取文本的字段名称(text)以及记录嵌入向量的字段名称(passage_embedding):

PUT /_ingest/pipeline/nlp-ingest-pipeline
{
  "description": "An NLP ingest pipeline",
  "processors": [
    {
      "text_embedding": {
        "model_id": "aVeif4oB5Vm0Tdw8zYO2",
        "field_map": {
          "text": "passage_embedding"
        }
      }
    }
  ]
}

步骤 3:创建向量索引

现在您将通过把 index.knn 设置为 true 来创建一个向量索引。在索引中,名为 text 的字段包含图像描述,名为 passage_embeddingknn_vector 字段包含文本的向量嵌入。向量字段的 dimension 必须与您在步骤 2 中配置的模型的维度匹配。此外,将默认的摄入管道设置为上一步创建的 nlp-ingest-pipeline

PUT /my-nlp-index
{
  "settings": {
    "index.knn": true,
    "default_pipeline": "nlp-ingest-pipeline"
  },
  "mappings": {
    "properties": {
      "passage_embedding": {
        "type": "knn_vector",
        "dimension": 768,
        "space_type": "l2"
      },
      "text": {
        "type": "text"
      }
    }
  }
}

设置向量索引后,您稍后就可以对 passage_embedding 字段执行向量搜索。

步骤 4:将文档摄入到索引中

在此步骤中,您将把几个示例文档摄入到索引中。示例数据取自 Flickr 图像数据集。每个文档包含一个对应于图像描述的 text 字段和一个对应于图像 ID 的 id 字段:

PUT /my-nlp-index/_doc/1
{
  "text": "A man who is riding a wild horse in the rodeo is very near to falling off ."
}
PUT /my-nlp-index/_doc/2
{
  "text": "A rodeo cowboy , wearing a cowboy hat , is being thrown off of a wild white horse ."
}
PUT /my-nlp-index/_doc/3
{
  "text": "People line the stands which advertise Freemont 's orthopedics , a cowboy rides a light brown bucking bronco ."
}

步骤 5:搜索数据

现在,您将使用语义搜索来搜索索引。要从查询文本自动生成向量嵌入,请使用 neural 查询,并提供您之前设置的模型的模型 ID,以便使用摄入时使用的模型为查询文本生成向量嵌入:

GET /my-nlp-index/_search
{
  "_source": {
    "excludes": [
      "passage_embedding"
    ]
  },
  "query": {
    "neural": {
      "passage_embedding": {
        "query_text": "wild west",
        "model_id": "aVeif4oB5Vm0Tdw8zYO2",
        "k": 3
      }
    }
  }
}

响应包含匹配的文档:

{
  "took": 127,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 0.015851952,
    "hits": [
      {
        "_index": "my-nlp-index",
        "_id": "1",
        "_score": 0.015851952,
        "_source": {
          "text": "A man who is riding a wild horse in the rodeo is very near to falling off ."
        }
      },
      {
        "_index": "my-nlp-index",
        "_id": "2",
        "_score": 0.015177963,
        "_source": {
          "text": "A rodeo cowboy , wearing a cowboy hat , is being thrown off of a wild white horse ."
        }
      },
      {
        "_index": "my-nlp-index",
        "_id": "3",
        "_score": 0.011347729,
        "_source": {
          "text": "People line the stands which advertise Freemont 's orthopedics , a cowboy rides a light brown bucking bronco ."
        }
      }
    ]
  }
}

使用自动化工作流

您可以使用 自动化工作流 快速设置自动嵌入向量生成。此方法会自动创建和配置所有必要的资源。更多信息,请参阅 工作流模板

您可以使用自动化工作流来创建和部署外部托管的模型,并为各种 AI 搜索类型创建资源。在此示例中,您将创建一个与您已通过手动步骤创建的相同的搜索。

步骤 1:注册并部署模型

要注册和部署模型,请选择对应模型提供程序的内置工作流模板。更多信息,请参阅 支持的工作流模板。或者,要配置自定义模型,请使用手动设置的步骤 1

步骤 2:配置工作流

创建并配置一个语义搜索工作流。您必须提供已配置模型的模型 ID。请查看您选择的工作流模板默认值(文档可联系售前工作人员获取),以确定是否需要更新任何参数。例如,如果模型维度与默认值(1024)不同,请在 output_dimension 参数中指定您模型的维度。为了与手动示例保持一致,将工作流模板的默认文本字段从 passage_text 更改为 text

POST /_plugins/_flow_framework/workflow?use_case=semantic_search&provision=true
{
    "create_ingest_pipeline.model_id" : "mBGzipQB2gmRjlv_dOoB",
    "text_embedding.field_map.output.dimension": "768",
    "text_embedding.field_map.input": "text"
}

UDB-SX 会返回已创建工作流的工作流 ID:

{
  "workflow_id" : "U_nMXJUBq_4FYQzMOS4B"
}

要检查工作流状态,请发送以下请求:

GET /_plugins/_flow_framework/workflow/U_nMXJUBq_4FYQzMOS4B/_status

工作流完成后,state 将变为 COMPLETED。该工作流已创建一个名为 my-nlp-index 的索引和一个名为 nlp-ingest-pipeline 的摄入管道:

{
  "workflow_id": "U_nMXJUBq_4FYQzMOS4B",
  "state": "COMPLETED",
  "resources_created": [
    {
      "workflow_step_id": "create_ingest_pipeline",
      "workflow_step_name": "create_ingest_pipeline",
      "resource_id": "nlp-ingest-pipeline",
      "resource_type": "pipeline_id"
    },
    {
      "workflow_step_name": "create_index",
      "workflow_step_id": "create_index",
      "resource_id": "my-nlp-index",
      "resource_type": "index_name"
    }
  ]
}

现在您可以继续执行步骤 4 和 5,将文档摄入到索引中并搜索索引。