

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

# 查詢知識庫並根據擷取的資料產生回應
<a name="kb-test-retrieve-generate"></a>

**重要**  
防護機制只會套用至輸入和從 LLM 產生的回應。它們不會套用至執行時期從知識庫擷取的參考。

設定知識庫之後，您可以查詢知識庫，以及使用 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) API 操作，根據從來源資料擷取的區塊產生回應。傳回的回應包含原始來源資料的引文。您也可以[使用重新排名模型](rerank.md)，而不是預設的 Amazon Bedrock 知識庫排名器，在擷取期間對來源區塊進行相關性排名。

**多模態內容限制**  
`RetrieveAndGenerate` 對多模態內容的支援有限。使用 Nova Multimodal Embeddings 時，RAG 功能僅限於文字內容。如需包括音訊和視訊處理的完整多模式支援，請使用 BDA 搭配文字內嵌模型。如需詳細資訊，請參閱[建立多模態內容的知識庫](kb-multimodal.md)。

**注意**  
在 `RetrieveAndGenerate` 流程期間從 `Retrieve` 回應傳回的影像會包含在回應產生的提示中。`RetrieveAndGenerate` 回應不能包含影像，但可以引用包含影像的來源。

若要了解如何查詢知識庫，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

------
#### [ Console ]

**若要測試您的知識庫**

1. AWS 管理主控台使用具有使用 Amazon Bedrock 主控台之許可的 IAM 身分登入 。接著，開啟位於 [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock) 的 Amazon Bedrock 主控台。

1. 在左側導覽窗格中選擇**知識庫**。

1. 在**知識庫**區段中，執行下列其中一個動作：
   + 選擇您要測試的知識庫旁的選項按鈕，然後選取**測試知識庫**。測試視窗從右側展開。
   + 選擇您要測試的知識庫。測試視窗從右側展開。

1. 若要根據從您的知識庫擷取的資訊產生回應，請開啟**產生查詢的回應**。Amazon Bedrock 將會根據您的資料來源產生回應，並以註腳引用其提供的資訊。

1. 若要選擇要用於產生回應的模型，請選擇**選取模型**。然後選取**套用**。

1. (選用) 選取組態圖示 (![\[Three horizontal sliders with adjustable circular controls for settings or parameters.\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/configurations.png)) 以開啟**組態**。如需組態的詳細資訊，請參閱 [設定和自訂查詢和回應產生](kb-test-config.md)。

1. 在聊天視窗的文字方塊中輸入查詢，然後選取**執行**以傳回知識庫的回應。

1. 選取註腳以查看該回應部分引用來源的摘錄。選擇連結以導覽至包含該檔案的 S3 物件。

1. 若要查看傳回區塊的詳細資訊，請選取**顯示來源詳細資訊**。
   + 若要查看您為查詢設定的組態，請展開**查詢組態**。
   + 若要檢視來源區塊的詳細資訊，請選擇其旁邊的向右箭頭 (![\[Play button icon with a triangular shape pointing to the right.\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/caret-right-filled.png)) 來展開它。您可以看到下列資訊：
     + 來源區塊的原始文字。若要複製此文字，請選擇複製圖示 (![\[Icon representing a crop or resize function, with two overlapping rectangles.\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/copy.png))。如果您使用 Amazon S3 存放資料，請選擇外部連結圖示 (![\[Icon of a square with an arrow pointing outward from its top-right corner.\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/external.png)) 以導覽至包含該檔案的 S3 物件。
     + 如果您使用 Amazon S3 存放資料，則為與來源區塊相關聯的中繼資料。屬性/欄位索引鍵和值是在與來源文件相關聯的 `.metadata.json` 檔案中定義。如需詳細資訊，請參閱 [設定和自訂查詢和回應產生](kb-test-config.md) 中的**中繼資料和篩選**區段。

**聊天選項**
+ 若要使用不同的模型產生回應，請選取**變更模型**。如果您變更模型，聊天視窗中的文字將全部清除。
+ 透過清除**產生回應**，切換到直接擷取來源區塊。如果您變更設定，聊天視窗中的文字將全部清除。
+ 若要清除聊天視窗，請選取掃帚圖示 (![\[Magnifying glass icon with a checkmark inside, symbolizing search or inspection.\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/broom.png))。
+ 要複製聊天視窗中的所有輸出，請選取複製圖示 (![\[Icon representing a crop or resize function, with two overlapping rectangles.\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/images/icons/copy.png))。

------
#### [ API ]

若要查詢知識庫，並使用基礎模型來根據資料來源的結果產生回應，請使用 [Amazon Bedrock 代理人執行時期端點](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-rt)傳送 [https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) 請求。

[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerateStream.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerateStream.html) API 會以串流格式傳回資料，並可讓您存取區塊中產生的回應，而無需等待整個結果。

下列是必要欄位：

**注意**  
API 回應包含引用事件。`citation` 成員已棄用。建議您改為使用 `generatedResponse` 和 `retrievedReferences` 欄位。如需參考資訊，請參閱[https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_CitationEvent.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_CitationEvent.html)。


****  

| 欄位 | 基本描述 | 
| --- | --- | 
| input | 包含指定查詢的 text 欄位。 | 
| retrieveAndGenerateConfiguration | 包含 [RetrieveAndGenerateConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerateConfiguration.html)，這會指定擷取和產生的組態。如需詳細資訊，請參閱下述。 | 

以下是選填欄位：


****  

| 欄位 | 使用案例 | 
| --- | --- | 
| sessionId | 使用與上一個工作階段相同的值來繼續該工作階段，並針對模型維護其內容。 | 
| sessionConfiguration | 包含用於加密工作階段的自訂 KMS 金鑰。 | 

在 [RetrieveAndGenerateConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerateConfiguration.html) 中包含 `knowledgeBaseConfiguration` 欄位。此欄位會對應至 [KnowledgeBaseRetrieveAndGenerateConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrieveAndGenerateConfiguration.html) 物件，其中包含下列欄位：
+ 下列是必要欄位：  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/kb-test-retrieve-generate.html)
+ 以下是選填欄位：  
****    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/bedrock/latest/userguide/kb-test-retrieve-generate.html)

您可以透過在 [KnowledgeBaseRetrievalConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseRetrievalConfiguration.html) 內的 [KnowledgeBaseVectorSearchConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseVectorSearchConfiguration.html) 中包含 `rerankingConfiguration` 欄位，在預設 Amazon Bedrock 知識庫排名模型上使用重新排名模型。`rerankingConfiguration` 欄位會對應至 [VectorSearchRerankingConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_VectorSearchRerankingConfiguration.html) 物件，您可以在其中指定要使用的重新排名模型、要包含的任何其他請求欄位、在重新排名期間篩選掉文件的中繼資料屬性，以及重新排名後要傳回的結果數目。如需詳細資訊，請參閱 [VectorSearchRerankingConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_VectorSearchRerankingConfiguration.html)。

**注意**  
如果您指定的 `numberOfRerankedResults` 值大於 [KnowledgeBaseVectorSearchConfiguration](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_KnowledgeBaseVectorSearchConfiguration.html) 中的 `numberOfResults` 值，傳回的結果數目上限將會是 `numberOfResults` 的值。例外情況是，如果您使用查詢分解 (如需詳細資訊，請參閱 [設定和自訂查詢和回應產生](kb-test-config.md) 中的**查詢修改**一節。如果您使用查詢分解，則 `numberOfRerankedResults` 最多可達 `numberOfResults` 的五倍。

回應會在 `output` 欄位中傳回產生的回應，並在 `citations` 欄位中將引用的來源區塊傳回為陣列。每個[引用](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Citation.html)物件包含下列欄位。


****  

| 欄位 | 基本描述 | 
| --- | --- | 
| generatedResponsePart | 在 textResponsePart 欄位中，包含引用相關的 text。span 欄位提供具有引號之輸出部分的開頭和結尾索引。 | 
| retrievedReferences | [RetrievedReference](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrievedReference.html) 物件的陣列，每個物件都包含與文件相關聯的來源區塊的 content、與文件相關聯的 metadata，以及資料來源中文件的 URI 或 URL location。如果內容是影像，則會以下列格式傳回 Base64 編碼內容的資料 URI：data:image/jpeg;base64,\$1\$1base64-encoded string\$1。 | 

回應也會傳回 `sessionId` 值，您可以在另一個請求中重複使用該值，以維持相同的對話。

如果您在請求中包含 `guardrailConfiguration`，`guardrailAction` 欄位會通知您內容是否遭到封鎖。

如果擷取的資料包含影像，回應也會傳回下列回應標頭，其中包含回應中傳回的來源區塊中繼資料：
+ `x-amz-bedrock-kb-byte-content-source` – 包含影像的 Amazon S3 URI。
+ `x-amz-bedrock-kb-description` – 包含影像的 Base64 編碼字串。

**注意**  
您無法在[設定中繼資料篩選條件](kb-test-config.md)時篩選這些中繼資料回應標頭。

------

**注意**  
如果您在產生回應時收到提示超過字元限制的錯誤，您可以使用下列方式縮短提示：  
減少擷取結果的數目上限 (這會縮短 [知識庫提示範本：協調和產生](kb-test-config.md#kb-test-config-prompt-template) 中 \$1search\$1results\$1 預留位置的填入內容)。
以使用較小區塊的分塊策略重新建立資料來源 (這會縮短 [知識庫提示範本：協調和產生](kb-test-config.md#kb-test-config-prompt-template) 中 \$1search\$1results\$1 預留位置的填入內容)。
縮短提示範本。
縮短使用者查詢 (這會縮短 [知識庫提示範本：協調和產生](kb-test-config.md#kb-test-config-prompt-template) 中 \$1query\$1 預留位置的填入內容)。