使用自定义类和 Azure AI 语言扩充 Azure AI 搜索中的搜索索引

已完成

自定义文本分类允许将一段文本映射到不同的用户定义类。 例如,可以针对书籍封底的概要训练一个模型,以自动识别书籍类型。 然后,你可以使用已识别的流派,通过流派 facet 扩充在线商店搜索引擎。

A diagram showing the steps needed to enrich a search index.

你将在此处了解到使用自定义文本分类模型扩充搜索索引时需要考虑的内容。

  • 存储你的文档,以便 Language Studio 和 Azure AI 搜索索引器可以访问它们
  • 创建自定义文本分类项目
  • 训练和测试模型
  • 基于存储的文档创建搜索索引
  • 创建一个函数应用,让该应用使用已部署的训练模型
  • 更新搜索解决方案、索引、索引器以及自定义技能组

存储你的数据

可以从 Language Studio 和 Azure AI 服务访问 Azure Blob 存储。 容器需要是可访问的,因此最简单的选项是选择容器,但也可以使用带有一些额外配置的专用容器。

除了数据,还需要一种方法来为每个文档分配分类。 Language Studio 提供了一个图形工具,可用于手动对每个文档进行分类。

如果文档映射到单个类使用单个标签分类项目,则可以在两种不同类型的项目之间进行选择。 如果可以将文档映射到多个类,请使用多标签分类项目。

如果不想手动对每个文档进行分类,可以在创建 Azure AI 语言项目之前标记所有文档。 此过程涉及创建采用以下格式的标签 JSON 文档:

{
    "projectFileVersion": "2022-05-01",
    "stringIndexType": "Utf16CodeUnit",
    "metadata": {
      "projectKind": "CustomMultiLabelClassification",
      "storageInputContainerName": "{CONTAINER-NAME}",
      "projectName": "{PROJECT-NAME}",
      "multilingual": false,
      "description": "Project-description",
      "language": "en-us"
    },
    "assets": {
      "projectKind": "CustomMultiLabelClassification",
      "classes": [
        {
          "category": "Class1"
        },
        {
          "category": "Class2"
        }
      ],
      "documents": [
          {
              "location": "{DOCUMENT-NAME}",
              "language": "{LANGUAGE-CODE}",
              "dataset": "{DATASET}",
              "classes": [
                  {
                      "category": "Class1"
                  },
                  {
                      "category": "Class2"
                  }
              ]
          }
      ]
  }

可以向 classes 数组添加尽可能多的类。 为 documents 数组中的每个文档添加一个条目,包括文档匹配的类。

创建 Azure AI 语言项目

可通过两种方法创建 Azure AI 语言项目。 如果你没有在 Azure 门户中创建语言服务就开始使用 Language Studio,Language Studio 会为你创建一个。

创建 Azure AI 语言项目的最灵活方法是首先使用 Azure 门户创建语言服务。 如果选择此选项,便可以选择添加自定义功能。

A screenshot showing creating a language service in the Azure portal.

如果要创建自定义文本分类,请在创建语言服务时选择该自定义功能。 你还将使用此方法将语言服务链接到存储帐户。

部署资源后,可以直接从语言服务的概述窗格导航到 Language Studio。 然后,可以创建新的自定义文本分类项目。

注意

如果已从 Language Studio 创建了语言服务,则可能需要按照以下步骤为 Azure 语言资源和存储帐户设置角色,以便将存储容器连接到自定义文本分类项目。

训练分类模型

与所有 AI 模型一样,你需要有可以用来训练它的标识数据。 模型需要查看如何将数据映射到类的示例,并提供一些可用于测试模型的示例。 可以选择让模型自动拆分训练数据,默认情况下,它将使用 80% 的文档来训练模型,并使用 20% 进行盲测。 如果有一些特定的文档要用于测试模型,可以标记文档以进行测试。

A screenshot of Language Studio showing selecting a document to add to the test set.

在 Language Studio 中,在项目中,选择“数据标签”。 你将看到所有文档。 选择要添加到测试集的每个文档,然后选择“测试模型的性能”。保存更新后的标签,然后创建新的训练作业。

创建搜索索引

无需执行任何特定操作即可创建将由自定义文本分类模型扩充的搜索索引。 按照创建 Azure AI 搜索解决方案中的步骤操作。 创建函数应用后,你将更新索引、索引器和自定义技能。

创建 Azure 函数应用

可以选择你想要函数应用使用的语言和技术。 该应用需要能够将 JSON 传递到自定义文本分类终结点,例如:

{
    "displayName": "Extracting custom text classification", 
    "analysisInput": {
        "documents": [
            {
                "id": "1", 
                "language": "en-us", 
                "text": "This film takes place during the events of Get Smart. Bruce and Lloyd have been testing out an invisibility cloak, but during a party, Maraguayan agent Isabelle steals it for El Presidente. Now, Bruce and Lloyd must find the cloak on their own because the only non-compromised agents, Agent 99 and Agent 86  are in Russia"
            }
        ]
      }, 
    "tasks": [
        {
        "kind": "CustomMultiLabelClassification", 
        "taskName": "Multi Label Classification", 
        "parameters": {
            "project-name": "movie-classifier", 
            "deployment-name": "test-release"}
        }
    ]
}

然后处理模型中的 JSON 响应,例如:

{
  "jobId": "be1419f3-61f8-481d-8235-36b7a9335bb7",
  "lastUpdatedDateTime": "2022-06-13T16:24:27Z",
  "createdDateTime": "2022-06-13T16:24:26Z",
  "expirationDateTime": "2022-06-14T16:24:26Z",
  "status": "succeeded",
  "errors": [],
  "displayName": "Extracting custom text classification",
  "tasks": {
    "completed": 1,
    "failed": 0,
    "inProgress": 0,
    "total": 1,
    "items": [
      {
        "kind": "CustomMultiLabelClassificationLROResults",
        "taskName": "Multi Label Classification",
        "lastUpdateDateTime": "2022-06-13T16:24:27.7912131Z",
        "status": "succeeded",
        "results": {
          "documents": [
            {
              "id": "1",
              "class": [
                {
                  "category": "Action",
                  "confidenceScore": 0.99
                },
                {
                  "category": "Comedy",
                  "confidenceScore": 0.96
                }
              ],
              "warnings": []
            }
          ],
          "errors": [],
          "projectName": "movie-classifier",
          "deploymentName": "test-release"
        }
      }
    ]
  }
}

然后,该函数会将结构化的 JSON 消息返回到 AI 搜索中的自定义技能组,例如:

[{"category": "Action", "confidenceScore": 0.99}, {"category": "Comedy", "confidenceScore": 0.96}]

此函数应用需要了解以下五项信息:

  1. 要分类的文本。
  2. 已训练的自定义文本分类部署模型的终结点。
  3. 自定义文本分类项目的主键。
  4. 项目名。
  5. 部署名称。

第一项信息从 AI 搜索中的自定义技能组作为输入传递到函数。 其余四项可以在 Language Studio 中找到。

A screenshot showing where to copy the prediction endpoint from.

终结点和部署名称位于部署模型窗格上。

A screenshot showing where to copy the primary key for language services from.

项目名称和主键位于项目设置窗格中。

更新 Azure AI 搜索解决方案

需要在 Azure 门户中进行三项更改以扩充搜索索引。

  1. 需要向索引添加字段以存储自定义文本分类扩充。
  2. 需要添加自定义技能集,以使用要分类的文本调用函数应用。
  3. 需要将技能组的响应映射到索引。

将字段添加到现有索引

在 Azure 门户中,转到 AI 搜索资源,选择索引,并按以下格式添加 JSON:

{
  "name": "classifiedtext",
  "type": "Collection(Edm.ComplexType)",
  "analyzer": null,
  "synonymMaps": [],
  "fields": [
    {
      "name": "category",
      "type": "Edm.String",
      "facetable": true,
      "filterable": true,
      "key": false,
      "retrievable": true,
      "searchable": true,
      "sortable": false,
      "analyzer": "standard.lucene",
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "synonymMaps": [],
      "fields": []
    },
    {
      "name": "confidenceScore",
      "type": "Edm.Double",
      "facetable": true,
      "filterable": true,
      "retrievable": true,
      "sortable": false,
      "analyzer": null,
      "indexAnalyzer": null,
      "searchAnalyzer": null,
      "synonymMaps": [],
      "fields": []
    }
  ]
}

此 JSON 将复合字段添加到索引以将类存储在可搜索的 category 字段中。 第二个 confidenceScore 字段将置信度百分比存储在双字段中。

编辑自定义技能组

在 Azure 门户中,选择技能组并按以下格式添加 JSON:

{
  "@odata.type": "#Microsoft.Skills.Custom.WebApiSkill",
  "name": "Genre Classification",
  "description": "Identify the genre of your movie from its summary",
  "context": "/document",
  "uri": "https://learn-acs-lang-serives.cognitiveservices.azure.com/language/analyze-text/jobs?api-version=2022-05-01",
  "httpMethod": "POST",
  "timeout": "PT30S",
  "batchSize": 1,
  "degreeOfParallelism": 1,
  "inputs": [
    {
      "name": "lang",
      "source": "/document/language"
    },
    {
      "name": "text",
      "source": "/document/content"
    }
  ],
  "outputs": [
    {
      "name": "text",
      "targetName": "class"
    }
  ],
  "httpHeaders": {}
}

WebApiSill 技能定义指定,文档的语言和内容作为输入传递给函数应用。 该应用将返回名为 class 的 JSON 文本。

将函数应用的输出映射到索引

最后一项更改是将输出映射到索引中。 在 Azure 门户中,选择索引器并编辑 JSON 以获得新的输出映射:

{
  "sourceFieldName": "/document/class",
  "targetFieldName": "classifiedtext"
}

索引器现在便知道函数应用 document/class 的输出应存储在 classifiedtext 字段中。 由于此字段已被定义为复合字段,因此函数应用必须返回一个包含 categoryconfidenceScore 字段的 JSON 数组。

现在可以搜索自定义分类文本的扩充搜索索引。