

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Amazon DocumentDB 生成式人工智能
<a name="generative-ai"></a>

Amazon DocumentDB 的功能是让机器学习 (ML) 和生成式人工智能 (AI) 模型能够实时处理 Amazon DocumentDB 中存储的数据。客户无需再花时间管理单独的基础设施、编写代码连接其他服务以及从主数据库中复制数据。

有关人工智能以及 AWS 如何支持你的 AI 需求的更多信息，请参阅这篇 [“What-is” 文章。](https://aws.amazon.com/what-is/artificial-intelligence/)

**Topics**
+ [使用 Amazon A SageMaker I Canvas 进行无代码机器学习](no-code-machine-learning.md)
+ [Amazon DocumentDB 向量搜索](vector-search.md)

# 使用 Amazon A SageMaker I Canvas 进行无代码机器学习
<a name="no-code-machine-learning"></a>

[Amazon SageMaker AI Canvas](https://docs.aws.amazon.com/sagemaker/latest/dg/canvas.html) 使您无需编写任何代码即可构建自己的 AI/ML 模型。您可以为回归和预测等常见用例构建机器学习模型，也可以从 Amazon Bedrock 访问和评估基础模型 (FMs)。您还可以 FMs 从 Amazon A SageMaker I 访问公众， JumpStart 进行内容生成、文本提取和文本摘要，以支持生成式 AI 解决方案。

## 如何使用 SageMaker AI Canvas 构建无代码机器学习模型
<a name="w2aac21b9b5"></a>

亚马逊 DocumentDB 现在与亚马逊 A SageMaker I Canvas 集成，可使用存储在亚马逊 DocumentDB 中的数据实现无代码机器学习 (ML)。现在，您无需编写任何一行代码，就可以使用 Amazon DocumentDB 中存储的数据，为回归和预测需求构建 ML 模型，并使用基础模型进行内容摘要和生成。

SageMaker AI Canvas 提供了一个可视化界面，允许 Amazon DocumentDB 客户无需任何 AI/ML 专业知识或编写一行代码即可生成预测。客户现在可以从中启动 SageMaker AI Canvas 工作空间 AWS 管理控制台，导入和加入 Amazon DocumentDB 数据，用于数据准备和模型训练。Amazon DocumentDB 中的数据现在可以在 SageMaker AI Canvas 中用于构建和增强模型，以预测客户流失、检测欺诈、预测维护故障、预测业务指标和生成内容。借助 SageMaker AI Canvas 与 Quick 的原生集成，客户现在可以跨团队发布和共享机器学习驱动的见解。默认情况下， SageMaker AI Canvas 中的数据摄取管道在 Amazon DocumentDB 辅助实例上运行，从而确保应用程序 SageMaker 和 AI Canvas 摄取工作负载的性能不受阻碍。

亚马逊 DocumentDB 客户可以通过导航到新的亚马逊 DocumentDB 无代码机器学习控制台页面并连接到新的或可用的 A SageMaker I Canvas 工作空间来开始使用 AI Canvas。 SageMaker 

## 配置 A SageMaker I 域和用户配置文件
<a name="sagemaker-domain"></a>

您可以从以 “仅限 VPC” 模式运行的 A SageMaker I 域连接到 Amazon DocumentDB 集群。通过在您的 VPC 中启动 SageMaker AI 域，您可以控制来自 SageMaker AI Studio 和 Canvas 环境的数据流。这使您可以限制互联网访问，使用标准 AWS 网络和安全功能监控和检查流量，并通过 VPC 终端节点连接到其他 AWS 资源。请参阅《[亚马逊 A SageMaker I 开发者指南》中的 Amazon A SageMaker I Canvas 入门](https://docs.aws.amazon.com/sagemaker/latest/dg/canvas-getting-started.html)[和在无法访问互联网的 VPC 中配置](https://docs.aws.amazon.com/sagemaker/latest/dg/canvas-vpc.html)*亚马逊 A SageMaker I* Canvas，创建您的 SageMaker AI 域以连接到您的亚马逊 DocumentDB 集群。

## 为亚马逊 DocumentDB 和 SageMaker AI Canvas 配置 IAM 访问权限
<a name="iam-access-canvas"></a>

已将 `AmazonDocDBConsoleFullAccess` 附加到其关联角色和身份的 Amazon DocumentDB 用户可以访问 AWS 管理控制台。在上述角色或身份中添加以下操作，即可通过 Amazon A SageMaker I Canvas 访问无代码机器学习。

```
"sagemaker:CreatePresignedDomainUrl",
"sagemaker:DescribeDomain",
"sagemaker:ListDomains",
"sagemaker:ListUserProfiles"
```

## 为 SageMaker AI Canvas 创建数据库用户和角色
<a name="w2aac21b9c11"></a>

您可以使用 Amazon DocumentDB 中的基于角色的访问控制 (RBAC)，限制用户可以对数据库执行的操作的访问权限。RBAC 的原理是向用户授予一个或多个角色。这些角色决定了用户可以对数据库资源执行的操作。

作为 Canvas 用户，请使用用户名和密码凭证连接到 Amazon DocumentDB 数据库。您可以使用 Amazon DocumentDB RBAC 功能 user/role 为对特定数据库具有读取权限的 Canvas 用户创建数据库。

例如，使用 `createUser` 操作：

```
db.createUser({
user: "canvas_user", 
pwd: "<insert-password>", 
roles: [{role: "read", db: "sample-database-1"}]
})
```

这将创建一个拥有 `sample-database-1` 数据库读取权限的 `canvas_user`。Canvas 分析师可以使用此凭证访问 Amazon DocumentDB 集群中的数据。请参阅 [使用基于角色的访问控制进行数据库访问](role_based_access_control.md) 了解更多信息。

## 可用区
<a name="available-regions"></a>

无代码集成适用于同时支持亚马逊 DocumentDB 和 Amazon AI Canvas 的区域。 SageMaker 区域包括：
+ us-east-1（弗吉尼亚州北部）
+ us-east-2（俄亥俄州）
+ us-west-2 (俄勒冈)
+ ap-northeast-1 (东京)
+ ap-northeast-2 (首尔)
+ ap-south-1（孟买）
+ ap-southeast-1 (新加坡)
+ ap-southeast-2 (悉尼)
+ eu-central-1 (法兰克福)
+ eu-west-1 (爱尔兰)

请参阅《[亚马逊 A SageMaker I *开发者指南》中的 Amazon SageMaker AI* Canv](https://docs.aws.amazon.com/sagemaker/latest/dg/canvas.html) as，了解最新的地区可用性。

# Amazon DocumentDB 向量搜索
<a name="vector-search"></a>

向量搜索是机器学习使用的一种方法，它通过使用距离或相似度指标比较向量表示来查找与给定数据点相似的数据点。两个向量在向量空间中越接近，就可以认为底层项目就越相似。这种方法有助于捕捉数据的语义意义。这种方法在推荐系统、自然语言处理和图像识别等各种应用中都很有用。

Amazon DocumentDB 向量搜索兼有 JSON 文档数据库的灵活性和丰富的查询功能，以及向量搜索的强大功能。如果要使用现有的 Amazon DocumentDB 数据或灵活的文档数据结构来构建机器学习和生成式人工智能用例，例如语义搜索体验、产品推荐、个性化、聊天机器人、欺诈检测和异常检测，Amazon DocumentDB 向量搜索就是您的理想之选。Amazon DocumentDB 5.0 基于实例的集群可以使用向量搜索。

**Topics**
+ [插入向量](#w2aac21c11b9)
+ [创建向量索引](#w2aac21c11c11)
+ [获取索引定义](#w2aac21c11c13)
+ [查询向量](#w2aac21c11c15)
+ [特征和限制](#vector-limitations)
+ [最佳实践](#w2aac21c11c19)

## 插入向量
<a name="w2aac21c11b9"></a>

要在 Amazon DocumentDB 数据库插入向量，可以使用现有的插入方法：

**示例**

以下示例在测试数据库中创建了一个包含五个文档的集合。每个文档都含有两个字段：产品名称及其相应的向量嵌入。

```
db.collection.insertMany([
  {"product_name": "Product A", "vectorEmbedding": [0.2, 0.5, 0.8]},
  {"product_name": "Product B", "vectorEmbedding": [0.7, 0.3, 0.9]},
  {"product_name": "Product C", "vectorEmbedding": [0.1, 0.2, 0.5]},
  {"product_name": "Product D", "vectorEmbedding": [0.9, 0.6, 0.4]},
  {"product_name": "Product E", "vectorEmbedding": [0.4, 0.7, 0.2]}
]);
```

## 创建向量索引
<a name="w2aac21c11c11"></a>

Amazon DocumentDB 支持分层可导航小世界 (HNSW) 索引和使用平面压缩 () 索引方法的倒置文件。IVFFlat IVFFlat 索引将向量分成列表，然后搜索这些列表中最接近查询向量的选定子集。另一方面，HNSW 索引则将向量数据组织成多层次的图。尽管与之相比，HNSW 的构建时间较慢 IVFFlat，但它提供了更好的查询性能和召回率。与之不同的是 IVFFlat，HNSW 不涉及训练步骤，因此无需加载任何初始数据即可生成索引。对于大多数用例，建议使用 HNSW 索引类型进行向量搜索。

如果不创建向量索引，Amazon DocumentDB 会进行精确的近邻搜索，从而确保完美的查全率。但是，速度在生产场景中至关重要。我们建议使用向量索引，这样可以牺牲一些查全率来提高速度。值得注意的是，添加向量索引可能会产生不同的查询结果。

**模板**

您可以使用以下 `createIndex` 或 `runCommand` 模板，在向量字段上构建向量索引：

------
#### [ Using createIndex ]

在某些驱动程序中，比如 mongosh 和 Java，使用 `createIndex` 中的参数 `vectorOptions` 可能会导致错误。在这种情况下，建议使用 `runCommand`：

```
db.collection.createIndex(
  { "<vectorField>": "vector" },
  { "name": "<indexName>",
    "vectorOptions": {
      "type": " <hnsw> | <ivfflat> ",
      "dimensions": <number_of_dimensions>,
      "similarity": " <euclidean> | <cosine> | <dotProduct> ",
      "lists": <number_of_lists> [applicable for IVFFlat],
      "m": <max number of connections> [applicable for HNSW],
      "efConstruction": <size of the dynamic list for index build> [applicable for HNSW]
    }
  }
);
```

------
#### [ Using runCommand ]

在某些驱动程序中，比如 mongosh 和 Java，使用 `createIndex` 中的参数 `vectorOptions` 可能会导致错误。在这种情况下，建议使用 `runCommand`：

```
db.runCommand(
  { "createIndexes": "<collection>", 
  "indexes": [{
      key: { "<vectorField>": "vector" },
      vectorOptions: {
          type: " <hnsw> | <ivfflat> ",
          dimensions: <number of dimensions>,
          similarity: " <euclidean> | <cosine> | <dotProduct> ",
          lists: <number_of_lists> [applicable for IVFFlat],
          m: <max number of connections> [applicable for HNSW],
          efConstruction: <size of the dynamic list for index build> [applicable for HNSW]
          },
      name: "myIndex" 
      }] 
  }
);
```

------


| 参数 | 要求 | 数据类型 | 说明 | 值 | 
| --- | --- | --- | --- | --- | 
|  **name**  |  optional  |  字符串  |  指定索引的名称。  |  字母数字  | 
|  **type**  |  optional  |    |  指定索引类型。  |  支持：HNSW 或 IVFFLAT 默认：HNSW（引擎修补版本 3.0.4574 及更高版本）  | 
|  **dimensions**  |  必需  |  整数  |  指定向量数据中的维度数。  |  最多 2,000 个维度。  | 
|  **similarity**  |  必需  |  字符串  |  指定相似度计算使用的距离指标。  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/documentdb/latest/developerguide/vector-search.html)  | 
|  **lists**  |  必需的 IVFFlat  |  整数  |  指定 IVFFlat 索引用于对矢量数据进行分组的聚类数。对于不到 100 万个文档，推荐设置为文档数量/1000，超过 100 万个文档的设置为 `sqrt(# of documents)`。  |  最小值：1 最大值：请参阅下文 [特征和限制](#vector-limitations) 中各实例类型列表的表格。  | 
|  **m**  |  optional  |  整数  |  指定 HNSW 索引的最大连接数  |  默认值：16 范围：[2, 100]  | 
|  **efConstruction**  |  optional  |  整数  |  指定用于构建 HNSW 索引图的动态候选列表的大小。 `efConstruction` 必须大于或等于 (2 \$1 m)。  |  默认值：64 范围：[4, 1000]  | 

请务必适当设置 for IVFFlat `m` 和 for HNSW 等`lists`子参数`efConstruction`的值，因为这会影响搜索的准确性/召回率、构建时间和性能。列表值越大，查询速度越快，因为这可以减少每个列表中的向量数量，从而产生较小的区域。但是，较小的区域可能会产生更多的查全错误，从而降低准确度。对于 HNSW 来说，增加 `efConstruction` 和 `m` 的值可提高准确度，但也会增加索引构建的时间和大小。请参阅以下示例：

**示例**

------
#### [ HNSW ]

```
db.collection.createIndex(
  { "vectorEmbedding": "vector" },
  { "name": "myIndex",
    "vectorOptions": {
      "type": "hnsw",
      "dimensions": 3,
      "similarity": "euclidean",
      "m": 16,
      "efConstruction": 64
    }
  }
);
```

------
#### [ IVFFlat ]

```
db.collection.createIndex(
  { "vectorEmbedding": "vector" },
  { "name": "myIndex",
    "vectorOptions": {
      "type": "ivfflat",
      "dimensions": 3,
      "similarity": "euclidean",
      "lists":1
    }
  }
)
```

------

## 获取索引定义
<a name="w2aac21c11c13"></a>

您可以使用以下 `getIndexes` 命令查看索引的详细信息，包括向量索引：

**示例**

```
db.collection.getIndexes()
```

**示例输出**

```
[
 {
  "v" : 4,
  "key" : {
   "_id" : 1
  },
  "name" : "_id_",
  "ns" : "test.collection"
 },
 {
  "v" : 4,
  "key" : {
   "vectorEmbedding" : "vector"
  },
  "name" : "myIndex",
  "vectorOptions" : {
   "type" : "ivfflat",
   "dimensions" : 3,
   "similarity" : "euclidean",
   "lists" : 1
  },
  "ns" : "test.collection"
 }
]
```

## 查询向量
<a name="w2aac21c11c15"></a>

Amazon DocumentDB 支持两个向量搜索运算符来查询向量：

### 经典矢量搜索运算符
<a name="w2aac21c11c15b5"></a>

使用以下模板查询向量：

```
db.collection.aggregate([
  {
    $search: {
      "vectorSearch": {
        "vector": <query vector>, 
        "path": "<vectorField>", 
        "similarity": "<distance metric>",
        "k": <number of results>,
        "probes":<number of probes> [applicable for IVFFlat],
        "efSearch":<size of the dynamic list during search> [applicable for HNSW]
      }
    }
  }
]);
```


| 参数 | 要求 | Type | 说明 | 值 | 
| --- | --- | --- | --- | --- | 
|  **vectorSearch**  |  必需  |  operator  |  在 \$1search 命令中用于查询向量。  |    | 
|  **vector**  |  必需  |  array  |  表示可用于查找相似向量的查询向量。  |    | 
|  **path**  |  必需  |  字符串  |  定义向量字段的名称。  |    | 
|  **k**  |  必需  |  整数  |  指定搜索返回的最大结果数量。  |    | 
|  **similarity**  |  必需  |  字符串  |  指定相似度计算使用的距离指标。  |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/documentdb/latest/developerguide/vector-search.html)  | 
|  **probes**  |  optional  |  整数  |  需要向量搜索检查的集群数量。值越大，查全率越好，但会牺牲速度。可以将其设置为列表数量，以进行精确的近邻搜索（此时计划程序不会使用索引）。开始微调的推荐设置为 `sqrt(# of lists)`。  |  默认：1  | 
|  **efSearch**  |  optional  |  整数  |  指定 HNSW 索引在搜索期间使用的动态候选列表的大小。`efSearch` 的值越大，查全率越好，但会牺牲速度。  |  原定设置值：40 范围：[1, 1000]  | 

必须微调 `efSearch` (HNSW) 或 `probes` (IVFFlat) 的值，以实现所需的性能和精度。请参阅以下示例操作：

------
#### [ HNSW ]

```
db.collection.aggregate([
  {
    $search: {
      "vectorSearch": {
        "vector": [0.2, 0.5, 0.8], 
        "path": "vectorEmbedding", 
        "similarity": "euclidean",
        "k": 2,
        "efSearch": 40
      }
    }
  }
]);
```

------
#### [ IVFFlat ]

```
db.collection.aggregate([
  {
    $search: {
      "vectorSearch": {
        "vector": [0.2, 0.5, 0.8], 
        "path": "vectorEmbedding", 
        "similarity": "euclidean",
        "k": 2,
        "probes": 1
      }
    }
  }
]);
```

------

**示例输出**

此操作的输出将类似于以下内容：

```
{ "_id" : ObjectId("653d835ff96bee02cad7323c"), "product_name" : "Product A", "vectorEmbedding" : [ 0.2, 0.5, 0.8 ] }
{ "_id" : ObjectId("653d835ff96bee02cad7323e"), "product_name" : "Product C", "vectorEmbedding" : [ 0.1, 0.2, 0.5 ] }
```

### `$vectorSearch`操作员（在亚马逊 DocumentDB 8.0 及更高版本中可用）
<a name="w2aac21c11c15b7"></a>

使用以下模板查询向量：

```
db.collection.aggregate([
{
  "$vectorSearch": {
    "exact": true | false,
    "index": "<index-name>" [supports only HNSW index],
    "limit": <number-of-results> [same as k],
    "path": "<vector field-to-search>",
    "queryVector": <array-of-numbers>,
    "numCandidates": <number-of-candidates> [same as efSearch], 
  }
}])
```

## 特征和限制
<a name="vector-limitations"></a>

**版本兼容性**
+ 亚马逊 DocumentDB 的矢量搜索仅适用于基于实例的亚马逊 DocumentDB 5.0 以上集群。

**向量**
+ Amazon DocumentDB 最多可以为 2,000 个维度的向量编制索引。但是，如果不使用索引，可以存储多达 16,000 个维度。

**索引**
+ 对于创建 IVFFlat 索引，列表参数的推荐设置为文档数/1000（对于最多 100 万个文档和`sqrt(# of documents)`超过 100 万个文档）。由于工作内存的限制，Amazon DocumentDB 支持的某个最大列表参数值具体取决于维度的数量。下表为 500、1000 和 2,000 个维度的向量提供了列表参数最大值，以供您参考：    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/documentdb/latest/developerguide/vector-search.html)
+ 向量索引不支持 `compound`、`sparse` 或 `partial` 等其他索引选项。
+ 在亚马逊 DocumentDB 5.0 中，HNSW 索引不支持并行索引构建。

**向量查询**
+ 对于向量搜索查询，务必微调 `probes` 或 `efSearch` 等参数才能获得最佳结果。`probes` 或 `efSearch` 参数的值越大，查全率越高，速度就越低。开始微调探针参数的推荐设置为 `sqrt(# of lists)`。

## 最佳实践
<a name="w2aac21c11c19"></a>

了解在 Amazon DocumentDB 中使用向量搜索的最佳实践。随着新的最佳实践的确定，此节将不断更新。
+ 使用平面压缩的倒置文件 (IVFFlat) 索引创建涉及根据相似性对数据点进行聚类和组织。因此，为了使索引更加有效，建议在创建索引之前至少加载一些数据。
+ 对于向量搜索查询，务必微调 `probes` 或 `efSearch` 等参数才能获得最佳结果。`probes` 或 `efSearch` 参数的值越大，查全率越高，速度就越低。开始微调 `probes` 参数的推荐设置为 `sqrt(lists)`。

**资源**
+ [向量搜索最新消息博客文章](https://aws.amazon.com/blogs/aws/vector-search-for-amazon-documentdb-with-mongodb-compatibility-is-now-generally-available)
+ [语义搜索代码示例](https://github.com/aws-samples/amazon-documentdb-samples/tree/master/blogs/semanticsearch-docdb)
+ [Amazon DocumentDB 向量搜索代码示例](https://github.com/aws-samples/amazon-documentdb-samples/tree/master/samples/vector-search)