

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

# 使用知識庫為您的代理程式產生增強回應
<a name="agents-kb-add"></a>

**注意**  
Amazon Bedrock Agents （現在為 Amazon Bedrock Agents Classic) 不再向新客戶開放。如需類似 Bedrock Agents Classic 的功能，請探索 [Amazon Bedrock AgentCore](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/agentcore-get-started-cli.html)。現有客戶可以繼續正常使用該服務。如需詳細資訊，請參閱[Amazon Bedrock Agents Classic 維護模式](agents-classic-maintenance-mode.md)。

Amazon Bedrock 知識庫可協助您充分利用檢索增強生成 (RAG) 這項熱門技術，其涉及從資料存放區提取資訊，以增強大型語言模型 (LLM) 產生的回應。當您使用資料來源和向量存放區設定知識庫時，您的應用程式可以查詢知識庫以傳回資訊，以使用來源的直接引述，或從查詢結果產生的自然回應來回答查詢。

若要搭配 Amazon Bedrock 代理人使用 Amazon Bedrock 知識庫，您必須先建立知識庫，然後將知識庫與代理程式建立關聯。如果您尚未建立知識庫，請參閱[使用 Amazon Bedrock 知識庫擷取資料並產生 AI 回應](knowledge-base.md)以了解知識庫並建立知識庫。您可以在[建立代理程式](agents-create.md)期間或建立代理程式之後，建立知識庫的關聯。若要將知識庫與現有代理程式建立關聯，請選擇您偏好方法的索引標籤，然後遵循下列步驟：

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

**新增知識庫**

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

1. 從左側導覽窗格選取**代理程式**。接著，在**代理程式**區段中選擇代理程式。

1. 選擇**在代理程式建置器中編輯**

1. 在**知識庫**區段中，選擇**新增**。

1. 選擇您建立的知識庫，並提供有關代理程式應如何與之互動的指示。這些指示會告知客服人員在對話期間何時及如何查詢此知識庫。代理程式會在提示內容中包含這些指示，以決定是否要從知識庫擷取特定使用者請求的資訊。例如：`Use this knowledge base to answer questions about company HR policies, benefits, and employee guidelines. Only query it when the user asks about internal company policies.`

1. 選擇**新增**。成功橫幅會顯示在頂端。

1. 若要在測試代理程式之前先套用您對代理程式所做的變更，請在測試前選擇**準備**。

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

若要將知識庫與代理程式建立關聯，請使用 [Amazon Bedrock 代理人建置時期端點](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt)傳送 [AssociateAgentKnowledgeBase](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent_AssociateAgentKnowledgeBase.html) 請求。

下列清單說明請求中的欄位：
+ 下列是必要欄位：



<table>
<thead>
  <tr><th>欄位</th><th>簡短描述</th></tr>
</thead>
<tbody>
  <tr><td>agentId</td><td>代理程式的 ID</td></tr>
  <tr><td>agentVersion</td><td>代理程式的版本</td></tr>
  <tr><td>knowledgeBaseId</td><td>知識庫的 ID</td></tr>
</tbody>
</table>

+ 以下是選填欄位：



<table>
<thead>
  <tr><th>欄位</th><th>簡短描述</th></tr>
</thead>
<tbody>
  <tr><td>description</td><td>告知代理程式何時及如何查詢此知識庫的指示。代理程式會在提示中包含此文字，以判斷是否要從知識庫擷取特定使用者請求的資訊。將其撰寫為說明知識庫包含的內容以及使用時機的指令。例如：<code>Use this knowledge base to answer customer questions about product specifications, pricing, and availability. Do not query it for general conversation.</code></td></tr>
  <tr><td>knowledgeBaseState</td><td>若要防止代理程式查詢知識庫，請指定 <code>DISABLED</code></td></tr>
</tbody>
</table>


------

您可以在調用代理程式時，使用 [InvokeAgent](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_InvokeAgent.html) 請求中的 `sessionState` 欄位來修改連接至代理程式之知識庫的[查詢組態](kb-test-config.md)。如需詳細資訊，請參閱[控制代理程式工作階段內容](agents-session-state.md)。