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 密钥,该服务仍会使用服务托管密钥对数据进行加密。

记忆策略 — 定义如何使用内置或内置的 overrides 策略将事件处理成有意义的长期记忆。如果您未定义任何策略,则仅存储包含原始事件的短期内存。有关更多信息,请参阅使用长期内存

命名惯例-使用清晰的描述性名称来帮助识别每个 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 软件开发工具包

    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. (可选)对于Short-term 内存(原始事件)到期,请设置 AgentCore 内存存储事件的持续时间(天)。

  7. (可选)在其他配置中,设置以下内容:

    1. 内存描述中,输入 AgentCore 内存的描述。

    2. 如果您想使用自己的 AWS KMS 密钥来加密数据,请执行以下操作:

      1. KMS 密钥中,选择自定义加密设置(高级)

      2. 选择 AWS KMS 密钥中,选择或输入现有 AWS KMS 密钥的 ARN。或者,选择创建 AWS KMS 以创建新的 AWS KMS 密钥。

  8. (可选)对于Long-term 内存提取策略,请选择一种或多种内存策略。有关更多信息:

  9. 选择 “创建内存” 来创建内 AgentCore 存。