

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

# 将文档直接摄取到知识库中
<a name="kb-direct-ingestion-add"></a>

本主题描述了如何将文档直接摄取到知识库中。您可以直接摄取的文档类型是有限制的，具体取决于您的数据来源。对于可用于指定要摄取的文档的方法限制，请参阅下表：


****  

| 数据来源类型 | 以内嵌方式定义的文档 | Amazon S3 位置中的文档 | 
| --- | --- | --- | 
| Amazon S3 | ![](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/icon-no.png)否 | ![](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/icon-yes.png)是 | 
| 自定义 | ![](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/icon-yes.png)是 | ![](http://docs.aws.amazon.com/zh_cn/bedrock/latest/userguide/images/icons/icon-yes.png)是 | 

展开与您的使用案例相对应的部分：

**注意**  
如果使用控制台，则最多可以将 10 个文档直接摄取到知识库中。如果改用 `IngestKnowledgeBaseDocuments` API，则最多可以将 25 个文档摄取到知识库中。有关此配额的更多信息，请参阅《AWS 通用参考指南》**中的 [Amazon Bedrock 服务配额](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#limits_bedrock)。

## 使用控制台
<a name="kb-direct-ingestion-add-console"></a>

要直接在中添加或修改文档 AWS 管理控制台，请执行以下操作：

1. 使用有权使用 Amazon Bedrock 控制台的 IAM 身份登录。 AWS 管理控制台 然后，打开 Amazon Bedrock 控制台，网址为[https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock)。

1. 在左侧导航窗格中，选择**知识库**。

1. 在**知识库**部分，选择要将文档摄取到的知识库。

1. 在**数据来源**部分，选择要在其中添加、修改或删除文档的数据来源。

1. 在**文档**部分，选择**添加文档**。然后，执行以下操作之一：
   + 要直接添加或修改文档，请选择**直接添加文档**。然后执行以下操作：

     1. 在**文档标识符**字段中，为文档指定一个唯一的名称。如果您指定的名称已存在于数据来源中，则该文档将被替换。

     1. 要上传文档，请选择**上传**。要以内嵌方式定义文档，请选择**以内嵌方式添加文档**，然后选择格式并在框中输入文档的文本。

     1. （可选）要将元数据与文档关联，请选择**添加元数据**并输入键、类型和值。
   + 要通过指定文档的 S3 位置来添加或修改文档，请选择**添加 S3 文档**。然后执行以下操作：

     1. 在**文档标识符**字段中，为文档指定一个唯一的名称。如果您指定的名称已存在于数据来源中，则该文档将被替换。

     1. 指定文档的 **S3 位置**是在您的当前 AWS 账户中还是在其他账户中。然后指定文档的 S3 URI。

     1. （可选）要将元数据与文档关联，请选择**元数据来源**。指定元数据的 S3 URI，或选择**添加元数据**并输入键、类型和值。

1. 要摄取文档和任何关联的元数据，请选择**添加**。

## 使用 API
<a name="kb-direct-ingestion-add-api"></a>

要使用 Amazon Bedrock API 将文档直接提取到知识库中，请向 [Amazon Bedrock 代理构建时终端节点](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)发送[IngestKnowledgeBaseDocuments](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_IngestKnowledgeBaseDocuments.html)请求，并指定知识库及其连接的数据源的 ID。

**注意**  
如果您指定的文档标识符或 S3 位置已存在于知识库中，则该文档将被新内容覆盖。

请求正文包含一个字段`documents`，该字段映射到一组[KnowledgeBaseDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_KnowledgeBaseDocument.html)对象，每个对象都表示要添加到数据源并提取到知识库中的文档的内容和可选元数据。[KnowledgeBaseDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_KnowledgeBaseDocument.html) 对象包含以下字段：
+ content-映射到包含要添加的文档内容相关信息的[DocumentContent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentContent.html)对象。
+ metadata-（可选）映射到包含要添加的文档元数据相关信息的[DocumentMetadata](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentMetadata.html)对象。有关如何在检索期间使用元数据的更多信息，请参阅[配置和自定义查询与响应生成](kb-test-config.md)中的**元数据和筛选**部分。

选择一个主题，了解如何摄取不同数据来源类型的文档或查看示例：

**Topics**
+ [将文档摄取到与自定义数据来源相连的知识库中](#kb-direct-ingestion-add-custom)
+ [将文档摄取到与 Amazon S3 数据来源相连的知识库中](#kb-direct-ingestion-add-s3)
+ [请求正文示例](#w2aac32c12c23c19c17c11b3c19)

### 将文档摄取到与自定义数据来源相连的知识库中
<a name="kb-direct-ingestion-add-custom"></a>

`dataSourceId`如果您指定的属于自定义数据源，则可以为`documents`数组中的每个[KnowledgeBaseDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_KnowledgeBaseDocument.html)对象添加内容和元数据。

可以使用以下方式，定义添加到自定义数据来源的文档内容：

#### 以内嵌方式定义文档
<a name="kb-direct-ingestion-add-custom-inline"></a>

您可以采用内嵌方式定义以下类型的文档：

------
#### [ Text ]

如果文档是文本，则[DocumentContent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentContent.html)对象应采用以下格式：

```
{ 
    "custom": { 
        "customDocumentIdentifier": { 
            "id": "string"
        },
        "inlineContent": { 
            "textContent": { 
                "data": "string"
            },
            "type": "TEXT"
        },
        "sourceType": "IN_LINE"
    },
    "dataSourceType": "CUSTOM"
}
```

文档 ID 包括在 `id` 字段中，文档的文本包括在 `data` 字段中。

------
#### [ Bytes ]

如果文档包含的文本不止文本，请将其转换为 Base64-string。然后，该[DocumentContent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentContent.html)对象应采用以下格式：

```
{ 
    "custom": { 
        "customDocumentIdentifier": { 
            "id": "string"
        },
        "inlineContent": { 
            "byteContent": { 
                "data": blob,
                "mimeType": "string"
            },
            "type": "BYTE"
        },
        "sourceType": "IN_LINE"
    },
    "dataSourceType": "CUSTOM"
}
```

在字段中包括文档的 ID，在`id`字段中包含 Base64-encoded 文档的 ID，在`data`字段中包含一个 MIME 类型。`mimeType`

------

#### 从 S3 摄取文档
<a name="w2aac32c12c23c19c17c11b3c15b7b3"></a>

如果您要从 S3 位置提取文档，则`content`字段中的[DocumentContent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentContent.html)对象应采用以下格式：

```
{ 
    "custom": { 
        "customDocumentIdentifier": { 
            "id": "string"
        },
        "s3Location": { 
            "bucketOwnerAccountId": "string",
            "uri": "string"
        },
        "sourceType": "S3"
    },
    "dataSourceType": "CUSTOM"
}
```

文档 ID 包括在 `id` 字段中，包含文档的 S3 存储桶的所有者包括在 `bucketOwnerAccountId` 字段中，文档的 S3 URI 包括在 `uri` 字段中。

可以使用以下方式定义文档的元数据：

#### 以内嵌方式定义元数据
<a name="w2aac32c12c23c19c17c11b3c15c11b1"></a>

如果您以内联方式定义元数据，则`metadata`字段中的[DocumentMetadata](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentMetadata.html)对象应采用以下格式：

```
{ 
    "inlineAttributes": [ 
        { 
            "key": "string",
            "value": { 
                "stringValue": "string",
                "booleanValue": boolean,
                "numberValue": number,
                "stringListValue": [ "string" ],
                "type": "STRING" | "BOOLEAN" | "NUMBER" | "STRING_LIST"
            }
        }
    ],
    "type": "IN_LINE_ATTRIBUTE"
}
```

对于您添加的每个属性，请在 `key` 字段中定义密钥。在 `type` 字段中指定值的数据类型，并将与该数据类型对应的字段包括在内。例如，如果包含字符串，则属性将采用以下格式：

```
{ 
    "key": "string",
    "value": { 
        "stringValue": "string",
        "type": "STRING"
    }
}
```

#### 从 S3 摄取元数据
<a name="w2aac32c12c23c19c17c11b3c15c11b3"></a>

您还可以从 S3 位置中扩展名为 `.metadata.json` 的文件摄取元数据。有关元数据文件格式的更多信息，请参阅[将知识库连接到 Amazon S3](s3-data-source-connector.md)中的**文档元数据字段**部分。

如果元数据来自 S3 文件，则`metadata`字段中的[DocumentMetadata](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentMetadata.html)对象应采用以下格式：

```
{
    "s3Location": { 
        "bucketOwnerAccountId": "string",
        "uri": "string"
    },
        "type": "S3_LOCATION"
    }
 }
```

包含元数据文件的 S3 存储桶的所有者包括在 `bucketOwnerAccountId` 字段中，元数据文件的 S3 URI 包括在 `uri` 字段中。

**警告**  
如果您以内嵌方式定义了内容，则必须以内嵌方式定义元数据。

### 将文档摄取到与 Amazon S3 数据来源相连的知识库中
<a name="kb-direct-ingestion-add-s3"></a>

`dataSourceId`如果您指定的属于 S3 数据源，则可以为`documents`数组中的每个[KnowledgeBaseDocument](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_KnowledgeBaseDocument.html)对象添加内容和元数据。

**注意**  
对于 S3 数据来源，您只能从 S3 位置添加内容和元数据。

要添加到 S3 的 S3 文档的内容应按以下格式添加到[DocumentContent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentContent.html)对象中：

```
{ 
    "dataSourceType": "string",
    "s3": { 
        "s3Location": { 
            "uri": "string"
        }
    }
}
```

包含文档的 S3 存储桶的所有者包括在 `bucketOwnerAccountId` 字段中，文档的 S3 URI 包括在 `uri` 字段中。

可以使用以下方式，为添加到自定义数据来源的文档定义元数据：

```
{
    "s3Location": { 
        "bucketOwnerAccountId": "string",
        "uri": "string"
    },
        "type": "S3_LOCATION"
    }
 }
```

**警告**  
您直接摄取到与 S3 数据来源相连的知识库中的文档，不会添加到 S3 存储桶本身。我们建议您将这些文档也添加到 S3 数据来源中，这样在同步数据来源时，这些文档不会被删除或覆盖。

### 请求正文示例
<a name="w2aac32c12c23c19c17c11b3c19"></a>

展开以下部分，查看 `IngestKnowledgeBaseDocuments` 在不同使用案例中的请求正文：

#### 将自定义文本文档添加到自定义数据来源并进行摄取
<a name="w2aac32c12c23c19c17c11b3c19b5b1"></a>

以下示例显示了如何将一个文本文档添加到自定义数据来源：

```
PUT /knowledgebases/{{KB12345678}}/datasources/{{DS12345678}}/documents HTTP/1.1
Content-type: application/json

{
   "documents": [ 
      { 
         "content": { 
            "dataSourceType": "CUSTOM",
            "custom": { 
               "customDocumentIdentifier": { 
                  "id": "MyDocument"
               },
               "inlineContent": { 
                  "textContent": { 
                     "data": "Hello world!"
                  },
                  "type": "TEXT"
               },
               "sourceType": "IN_LINE"
            }
         }
     }
   ]
}
```

#### 将 Base64-encoded 文档添加到自定义数据源并进行收录
<a name="w2aac32c12c23c19c17c11b3c19b5b3"></a>

以下示例显示如何将 PDF 文档添加到自定义数据来源：

```
PUT /knowledgebases/{{KB12345678}}/datasources/{{DS12345678}}/documents HTTP/1.1
Content-type: application/json

{
   "documents": [ 
      { 
         "content": { 
            "dataSourceType": "CUSTOM",
            "custom": { 
               "customDocumentIdentifier": { 
                  "id": "MyDocument"
               },
               "inlineContent": { 
                  "byteContent": { 
                     "data": "<Base64-encoded string>",
                     "mimeType": "application/pdf"
                  },
                  "type": "BYTE"
               },
               "sourceType": "IN_LINE"
            }
         }
     }
   ]
}
```

#### 将文档从 S3 位置添加到与自定义数据来源相连的知识库中并进行摄取
<a name="w2aac32c12c23c19c17c11b3c19b5b5"></a>

以下示例显示了如何将一个文本文档从 S3 位置添加到自定义数据来源：

```
PUT /knowledgebases/{{KB12345678}}/datasources/{{DS12345678}}/documents HTTP/1.1
Content-type: application/json

{
   "documents": [ 
      { 
         "content": { 
            "dataSourceType": "CUSTOM",
            "custom": { 
               "customDocumentIdentifier": { 
                  "id": "MyDocument"
               },
               "s3": {
                "s3Location": {
                    "uri": "amzn-s3-demo-bucket"
                }
               },
               "sourceType": "S3"
            }
         }
     }
   ]
}
```

#### 将内嵌文档添加到与自定义数据来源相连的知识库中并以内嵌方式包含元数据
<a name="w2aac32c12c23c19c17c11b3c19b5b7"></a>

以下示例显示了如何以内嵌方式，将文档及其包含两个属性的元数据添加到自定义数据来源：

```
PUT /knowledgebases/{{KB12345678}}/datasources/{{DS12345678}}/documents HTTP/1.1
Content-type: application/json

{
   "documents": [ 
      { 
         "content": { 
            "dataSourceType": "CUSTOM",
            "custom": { 
               "customDocumentIdentifier": { 
                  "id": "MyDocument"
               },
               "inlineContent": { 
                  "textContent": { 
                     "data": "Hello world!"
                  },
                  "type": "TEXT"
               },
               "sourceType": "IN_LINE"
            }
         },
         "metadata": {
            "inlineAttributes": [ 
               { 
                  "key": "genre",
                  "value": {
                     "stringValue": "pop",
                     "type": "STRING"
                  }
               },
               { 
                  "key": "year",
                  "value": { 
                     "numberValue": 1988,
                     "type": "NUMBER"
                  }
               }
            ],
            "type": "IN_LINE_ATTRIBUTE"
         }
     }
   ]
}
```

#### 将文档添加到与 S3 数据来源相连的知识库中并包含其元数据
<a name="w2aac32c12c23c19c17c11b3c19b5b9"></a>

以下示例显示了如何将文档及其元数据添加到 S3 数据来源。您只能通过 S3 包含元数据：

```
PUT /knowledgebases/{{KB12345678}}/datasources/{{DS12345678}}/documents HTTP/1.1
Content-type: application/json

{
    "documents": [ 
        { 
            "content": { 
                "dataSourceType": "S3",
                "s3": { 
                "s3Location": {
                    "uri": "amzn-s3-demo-bucket"
                }
            }
        },
        "metadata": {
            "s3Location": {
                "bucketOwnerId": "111122223333",
                "uri": "amzn-s3-demo-bucket"
            },
                "type": "S3_LOCATION"
            }
        }
    ]
}
```