View a markdown version of this page

启用长期记忆 - Amazon Bedrock AgentCore

启用长期记忆

您可以通过两种方式启用长期内存:在首次创建 AgentCore 内存时添加策略,或者更新现有资源以包含这些策略。

用长期策略创造全新记忆

最直接的方法是在创建新 AgentCore 内存时加入策略。调用后create_memory,必须等待 AgentCore 内存状态变为后ACTIVE才能使用。

AgentCore CLI
  1. agentcore add memory --name PersonalizedShoppingAgentMemory --strategies USER_PREFERENCE agentcore deploy
Interactive
  1. 运行打开 agentcore TUI,然后选择添加并选择内存

  2. 选择用户偏好策略:

    内存向导:选择 USER_PREFERENCE 策略
  3. 查看配置并按 Enter 进行确认:

    内存向导:确认内存配置

    然后运行agentcore deploy在中配置内存 AWS。

在现有 AgentCore 记忆中添加长期策略

要向现有 AgentCore 内存添加长期功能,请使用update_memory操作。您可以为现有内存添加、修改或删除策略。

注意

C AgentCore LI 支持创建和管理内存资源。要更新现有内存上的内存策略,请使用 AWS SDK。

示例:将会话摘要策略添加到现有 AgentCore 内存

import boto3 # Initialize the Boto3 client for control plane operations control_client = boto3.client('bedrock-agentcore-control', region_name='us-west-2') # Assume 'memory_id' is the ID of an existing AgentCore Memory that has no strategies attached to it memory_id = "your-existing-memory-id" # Update the memory to add a summary strategy response = control_client.update_memory( memoryId=memory_id, memoryStrategies=[ { 'summaryMemoryStrategy': { 'name': 'SessionSummarizer', 'description': 'Summarizes conversation sessions for context', 'namespaceTemplates': ['/summaries/{actorId}/{sessionId}/'] } } ] ) print(f"Successfully submitted update for memory ID: {memory_id}") # Validate strategy was added to the memory memory_response = control_client.get_memory(memoryId=memory_id) strategies = memory_response.get('memory', {}).get('strategies', []) print(f"Memory strategies for memoryID: {memory_id} are: {strategies}")
注意

Long-term 只有在新策略出现ACTIVE才会从存储的对话事件中提取记忆记录。在添加策略之前存储的对话不会被处理以备长期记忆。