View a markdown version of this page

建立 AgentCore 記憶體 - Amazon Bedrock AgentCore

建立 AgentCore 記憶體

您可以使用 AgentCore CLI、 AWS 主控台或 CreateMemory AWS SDK 操作來建立 AgentCore 記憶體。建立記憶體時,您可以設定名稱、描述、加密設定、原始事件的過期時間戳記,以及如果您想要擷取長期記憶體的記憶體策略等設定。

建立 AgentCore 記憶體時,請考慮下列因素,以維持其符合您應用程式的需求:

事件保留 – 選擇短期記憶體的原始事件保留時間 (最多 365 天)。

注意

eventExpiryDuration 會在寫入時套用每個事件。更新它只會影響變更後建立的事件。先前存放的事件會保留其原始過期且無法延長,且過期的事件無法復原。

安全需求 – 如果您的應用程式處理敏感資訊,請考慮使用客戶受管 AWS KMS 金鑰進行加密。即使您未提供客戶受管 AWS KMS 金鑰,該服務仍會使用服務受管金鑰加密資料。

記憶體策略 – 定義如何使用內建或內建的覆寫策略,將事件處理成有意義的長期記憶體。如果您未定義任何策略,則只會儲存包含原始事件的短期記憶體。如需詳細資訊,請參閱使用長期記憶體

命名慣例 – 使用清晰的描述性名稱,以協助識別每個 AgentCore 記憶體的目的,特別是當您的應用程式使用多個存放區時。

範例
AgentCore CLI
  1. AgentCore CLI 記憶體命令必須在現有的 agentcore 專案內執行。如果您還沒有專案,請先建立專案:

    agentcore create --name my-agent --no-agent cd my-agent

    建立基本記憶體 (僅限短期):

    agentcore add memory --name my_agent_memory agentcore deploy

    使用長期策略建立記憶體:

    agentcore add memory --name ShoppingSupportAgentMemory \ --strategies SUMMARIZATION,USER_PREFERENCE agentcore deploy

    檢查記憶體狀態:

    agentcore status
Interactive
  1. 執行 agentcore以開啟 TUI,然後選取新增,然後選擇記憶體

  2. 輸入記憶體名稱:

    記憶體精靈:輸入名稱
  3. 選取事件到期持續時間:

    記憶體精靈:選取事件到期持續時間
  4. 選擇長期記憶體擷取的記憶體策略:

    記憶體精靈:選取記憶體策略
  5. 檢閱組態,然後按 Enter 鍵確認:

    記憶體精靈:檢閱組態
AWS SDK
  1. 如需詳細資訊,請參閱 AWS SDK

    import boto3 import time # Initialize the Boto3 clients for control plane and data plane operations control_client = boto3.client('bedrock-agentcore-control') data_client = boto3.client('bedrock-agentcore') print("Creating a new memory resource...") # Create the memory resource with defined strategies response = control_client.create_memory( name="ShoppingSupportAgentMemory", description="Memory for a customer support agent.", memoryStrategies=[ { 'summaryMemoryStrategy': { 'name': 'SessionSummarizer', 'namespaceTemplates': ['/summaries/{actorId}/{sessionId}/'] } }, { 'userPreferenceMemoryStrategy': { 'name': 'UserPreferenceExtractor', 'namespaceTemplates': ['/users/{actorId}/preferences/'] } } ] ) memory_id = response['memory']['id'] print(f"Memory resource created with ID: {memory_id}") # Poll the memory status until it becomes ACTIVE while True: mem_status_response = control_client.get_memory(memoryId=memory_id) status = mem_status_response.get('memory', {}).get('status') if status == 'ACTIVE': print("Memory resource is now ACTIVE.") break elif status == 'FAILED': raise Exception("Memory resource creation FAILED.") print("Waiting for memory to become active...") time.sleep(10)
Console
  1. ====== 建立 AgentCore 記憶體

  2. 開啟 Amazon Bedrock AgentCore 主控台。

  3. 在左側導覽窗格中,選擇記憶體

  4. 選擇建立記憶體

  5. 針對記憶體名稱,輸入 AgentCore 記憶體的名稱。

  6. (選用) 對於短期記憶體 (原始事件) 過期設定 AgentCore 記憶體將存放事件的持續時間 (天數)。

  7. (選用) 在其他組態中,設定下列項目:

    1. 記憶體描述 中,輸入 AgentCore 記憶體的描述。

    2. 如果您想要使用自己的 AWS KMS 金鑰來加密資料,請執行下列動作:

      1. KMS 金鑰 中,選擇自訂加密設定 (進階)

      2. 選擇 AWS KMS 金鑰中,選擇或輸入現有 AWS KMS 金鑰的 ARN。或者,選擇建立 AWS KMS 以建立新的 AWS KMS 金鑰。

  8. (選用) 對於長期記憶體擷取策略,請選擇一或多個記憶體策略。如需詳細資訊:

  9. 選擇建立記憶體以建立 AgentCore 記憶體。