

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將文件直接擷取至知識庫
<a name="kb-direct-ingestion-add"></a>

本主題說明如何將文件直接擷取至知識庫。限制適用於您可以直接擷取的文件類型，視資料來源而定。如需可用來指定要擷取之文件的方法限制，請參閱下表：


****  

| Data source type (資料來源類型) | 文件已定義內嵌 | Amazon S3 位置中的文件 | 
| --- | --- | --- | 
| Amazon S3 | ![](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/icon-no.png)否 | ![](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/icon-yes.png)是 | 
| Custom | ![](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/icon-yes.png)是 | ![](http://docs.aws.amazon.com/zh_tw/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.  AWS 管理主控台 使用具有使用 Amazon Bedrock 主控台之許可的 IAM 身分登入 。接著，開啟位於 [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock) 的 Amazon 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) 物件包含下列欄位：
+ 內容 – 對應至 [DocumentContent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_DocumentContent.html) 物件，其中包含要新增之文件內容的相關資訊。
+ 中繼資料 – (選用) 對應至 [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 字串。[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、在 `data` 欄位中包含 Base64 編碼的文件，以及在 `mimeType` 欄位中包含 MIME 類型。

------

#### 從 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、在 `bucketOwnerAccountId` 欄位中包含文件的 S3 儲存貯體擁有者，以及在 `uri` 欄位中包含文件的 S3 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"
    }
 }
```

在 `bucketOwnerAccountId` 欄位中包含中繼資料檔案的 S3 儲存貯體擁有者，以及在 `uri` 欄位中包含中繼資料檔案的 S3 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"
        }
    }
}
```

在 `bucketOwnerAccountId` 欄位中包含文件的 S3 儲存貯體擁有者，以及在 `uri` 欄位中包含文件的 S3 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 編碼的文件新增至自訂資料來源並將其擷取
<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"
            }
        }
    ]
}
```