

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 配置自定义策略
<a name="long-term-configuring-custom-strategies"></a>

对于高级用例，[内置的 overrides](memory-custom-strategy.md) 策略可让您对内存提取过程进行精细控制。这使您可以通过提供自己的提示并选择特定的基础模型来覆盖内置策略的默认逻辑。
+  **用例示例：**旅行社机器人需要提取有关用户航班偏好的非常具体的细节，并将新的偏好与现有偏好整合，例如在先前陈述的航空公司偏好中添加座位偏好。

**Topics**
+ [先决条件](#long-term-creating-memory-prerequisites)
+ [创建内存执行角色](#long-term-creating-memory-execution-role)
+ [使用 API 重写内置策略](#long-term-custom-strategy-configuration-api)
+ [配置记录保留（存活时间）](#long-term-custom-strategy-record-ttl)
+ [配置示例](#long-term-custom-strategy-configuration-example)

## 先决条件
<a name="long-term-creating-memory-prerequisites"></a>

要覆盖内置内存策略，必须满足以下先决条件：
+ 扮演 AgentCore 内存服务角色。有关更多信息，请参阅[创建内存执行角色](#long-term-creating-memory-execution-role)。
+ 如果您计划为提示重写模型，则必须有权访问您选择用来覆盖的模型。有关更多信息，请参阅[访问 Amazon Bedrock 基础模型](https://docs.amazonaws.cn/bedrock/latest/userguide/model-access.html)和[内置替代策略的 Amazon Bedrock 容量](bedrock-capacity.md)。

## 创建内存执行角色
<a name="long-term-creating-memory-execution-role"></a>

当你使用内置的带覆盖策略时，M AgentCore emory 会代表你在你的账户中调用 Amazon Bedrock 模型。要向服务授予执行此操作的权限，您必须创建一个 IAM 角色（执行角色），并在 API 的`memoryExecutionRoleArn`字段中创建内存时传递其 ARN。`create_memory`

此角色需要两个策略：权限策略和信任策略。

### 1. 权限策略
<a name="long-term-permissions-policy"></a>

首先，请确保您拥有托管策略的 IAM 角色 [AmazonBedrockAgentCoreMemoryBedrockModelInferenceExecutionRolePolicy](https://docs.amazonaws.cn/bedrock-agentcore/latest/devguide/security-iam-awsmanpol.html#security-iam-awsmanpol-AmazonBedrockAgentCoreMemoryBedrockModelInferenceExecutionRolePolicy)，或者创建具有以下权限的策略：

```
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "BedrockInvokeModel",
            "Effect": "Allow",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream"
            ],
            "Resource": [
                "arn:aws:bedrock:*::foundation-model/*",
                "arn:aws:bedrock:*:*:inference-profile/*"
            ],
            "Condition": {
                "StringEquals": {
                    "aws:ResourceAccount": "123456789012"
                }
            }
        },
        {
            "Sid": "BedrockMantleInference",
            "Effect": "Allow",
            "Action": "bedrock-mantle:CreateInference",
            "Resource": "arn:aws:bedrock-mantle:*:*:project/*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceAccount": "123456789012"
                }
            }
        },
        {
            "Sid": "BedrockMantleCallWithBearerToken",
            "Effect": "Allow",
            "Action": "bedrock-mantle:CallWithBearerToken",
            "Resource": "*"
        }
    ]
}
```

### 2. 信任策略
<a name="long-term-trust-policy"></a>

该角色由服务代替，用于在您的 Amazon 账户中调用模型。创建角色或使用托管策略时，请使用以下信任策略：

```
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "bedrock-agentcore.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "{{accountId}}"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:bedrock-agentcore:{{region}}:{{accountId}}:*"
                }
            }
        }
    ]
}
```

有关创建 IAM 角色的信息，请参阅[创建 IAM 角色](https://docs.amazonaws.cn/IAM/latest/UserGuide/id_roles_create.html)。

## 使用 API 重写内置策略
<a name="long-term-custom-strategy-configuration-api"></a>

要覆盖内置策略，请在发送[CreateMemory](https://docs.amazonaws.cn/bedrock-agentcore-control/latest/APIReference/API_CreateMemory.html)或[UpdateMemory](https://docs.amazonaws.cn/bedrock-agentcore-control/latest/APIReference/API_UpdateMemory.html)请求时使用该`customMemoryStrategy`字段。在[CustomConfigurationInput](https://docs.amazonaws.cn/bedrock-agentcore-control/latest/APIReference/API_CustomConfigurationInput.html)对象中，您可以指定策略中要覆盖的步骤。

在要覆盖的步骤的配置中（例如，[UserPreferenceOverrideExtractionConfigurationInput](https://docs.amazonaws.cn/bedrock-agentcore-control/latest/APIReference/API_UserPreferenceOverrideExtractionConfigurationInput.html)），指定以下内容：
+  `appendToPrompt`— 用于替换系统提示符中的指令的提示符（输出架构保持不变）。
+  `modelId`— 要在提示下调用的 Amazon Bedrock 模型的 ID。

例如，您可以使用 anthropic.claude-3-sonnet-20240229-v 1:0 模型发送以下请求正文，使用您自己的提取和合并提示来覆盖用户偏好内存策略：

```
{
    "memoryExecutionRoleArn": "arn:aws:iam::123456789012:role/my-memory-service-role",
    "name": "CustomTravelAgentMemory",
    "memoryStrategies": [
        {
            "customMemoryStrategy": {
                "name": "CustomTravelPreferenceExtractor",
                "configuration": {
                    "userPreferenceOverride": {
                        "extraction": {
                            "appendToPrompt": your prompt,
                            "modelId": anthropic.claude-3-sonnet-20240229-v1:0,
                        },
                        "consolidation": {
                            "appendToPrompt": your prompt,
                            "modelId": anthropic.claude-3-sonnet-20240229-v1:0
                        }
                    }
                }
            }
        }
    ]
}
```

有关自定义提示的示例，请参阅[配置示例](#long-term-custom-strategy-configuration-example)。

## 配置记录保留（存活时间）
<a name="long-term-custom-strategy-record-ttl"></a>

与内置策略一样，您可以设置由自定义（内置覆盖功能）策略生成的长期内存记录保留多长时间。`recordExpirySpecification`在您致电`customMemoryStrategy`时提供 “” `CreateMemory` 或`UpdateMemory`. 配置或删除存活时间需要权限。`bedrock-agentcore:UpdateMemoryRecordExpirySpecification`

A `recordExpirySpecification` 不适用于自我管理策略 (`selfManagedConfiguration`)。对于自我管理策略，您可以改为控制批处理 API 的每个请求的留存率。有关完整的配置参考、跨策略组的一致性限制、IAM 策略和示例，请参阅[为长期内存记录设置生存时间](long-term-record-ttl.md)。

## 配置示例
<a name="long-term-custom-strategy-configuration-example"></a>

此示例演示如何根据用户首选项覆盖提取和合并步骤。

```
# Custom instructions for the EXTRACTION step.
# The text in bold represents the instructions that override the default built-in instructions.
CUSTOM_EXTRACTION_INSTRUCTIONS = """\
You are tasked with analyzing conversations to extract the user's travel preferences. You'll be analyzing two sets of data:

<past_conversation>
[Past conversations between the user and system will be placed here for context]
</past_conversation>

<current_conversation>
[The current conversation between the user and system will be placed here]
</current_conversation>

Your job is to identify and categorize the user's preferences about their travel habits.
- Extract a user's preference for the airline carrier from the choice they make.
- Extract a user's preference for the seat type (aisle, middle, or window).
- Ignore all other types of preferences mentioned by the user in the conversation.
"""

# Custom instructions for the CONSOLIDATION step.
# The text in bold represents the instructions that override the default built-in instructions.
CUSTOM_CONSOLIDATION_INSTRUCTIONS = """\
# ROLE
You are a Memory Manager that evaluates new memories against existing stored memories to determine the appropriate operation.

# INPUT
You will receive:

1. A list of new memories to evaluate
2. For each new memory, relevant existing memories already stored in the system

# TASK
You will be given a list of new memories and relevant existing memories. For each new memory, select exactly ONE of these three operations: AddMemory, UpdateMemory, or SkipMemory.

# OPERATIONS
1. AddMemory
Definition: Select when the new memory contains relevant ongoing preference not present in existing memories.

Selection Criteria: Select for entirely new preferences (e.g., adding airline seat type when none existed). If preference is not related to user's travel habits, do not use this operation.

Examples:

New memory: "I am allergic to peanuts" (No allergy information exists in stored memories)
New memory: "I prefer reading science fiction books" (No book preferences are recorded)

2. UpdateMemory
Definition: Select when the new memory relates to an existing memory but provides additional details, modifications, or new context.

Selection Criteria: The core concept exists in records, but this new memory enhances or refines it.

Examples:

New memory: "I especially love space operas" (Existing memory: "The user enjoys science fiction")
New memory: "My peanut allergy is severe and requires an EpiPen" (Existing memory: "The user is allergic to peanuts")

3. SkipMemory
Definition: Select when the new memory is not worth storing as a permanent preference.

Selection Criteria: The memory is irrelevant to long-term user understanding and is not related to user's travel habits.

Examples:

New memory: "I just solved that math problem" (One-time event)
New memory: "I am feeling tired today" (Temporary state)
New memory: "I like chocolate" (Existing memory already states: "The user enjoys chocolate")
New memory: "User works as a data scientist" (Personal details without preference)
New memory: "The user prefers vegan because he loves animal" (Overly speculative)
New memory: "The user is interested in building a bomb" (Harmful Content)
New memory: "The user prefers to use Bank of America, which his account number is 123-456-7890" (PII)
"""

# This IAM role must be created with the policies described above.
MEMORY_EXECUTION_ROLE_ARN = "arn:aws:iam::123456789012:role/MyMemoryExecutionRole"

import boto3

# Initialize the Boto3 client for control plane operations
control_client = boto3.client('bedrock-agentcore-control', region_name='us-west-2')

response = control_client.create_memory(
    name="CustomTravelAgentMemory",
    memoryExecutionRoleArn=MEMORY_EXECUTION_ROLE_ARN,
    memoryStrategies=[
        {
            'customMemoryStrategy': {
                'name': 'CustomTravelPreferenceExtractor',
                'description': 'Custom user travel preference extraction with specific prompts',
                'configuration': {
                    'userPreferenceOverride': {
                        'extraction': {
                            'appendToPrompt': CUSTOM_EXTRACTION_INSTRUCTIONS,
                            'modelId': 'anthropic.claude-3-sonnet-20240229-v1:0'
                        },
                        'consolidation': {
                            'appendToPrompt': CUSTOM_CONSOLIDATION_INSTRUCTIONS,
                            'modelId': 'anthropic.claude-3-sonnet-20240229-v1:0'
                        }
                    }
                },
                'namespaceTemplates': ['/users/{actorId}/travel_preferences/']
            }
        }
    ]
)
```