設定自訂策略
對於進階使用案例,內建的 覆寫策略可讓您精細控制記憶體擷取程序。這可讓您透過提供自己的提示並選擇特定的基礎模型來覆寫內建策略的預設邏輯。
-
範例使用案例:客服人員機器人需要擷取有關使用者航班偏好設定的非常特定詳細資訊,並將新偏好設定與現有偏好設定合併,例如將座位偏好設定新增至先前陳述的航空公司偏好設定。
先決條件
若要覆寫內建記憶體策略,您必須滿足下列先決條件:
-
具有 AgentCore 記憶體服務角色。如需詳細資訊,請參閱建立記憶體執行角色。
-
如果您打算覆寫提示的模型,您必須能夠存取選擇覆寫的模型。如需詳細資訊,請參閱使用覆寫策略存取內建的 Amazon Bedrock 基礎模型和 Amazon Bedrock 容量。 具有覆寫策略的內建 Amazon Bedrock 容量
建立記憶體執行角色
當您使用內建 搭配覆寫策略時,AgentCore 記憶體會代表您在帳戶中叫用 Amazon Bedrock 模型。若要授予服務執行此作業的許可,您必須建立 IAM 角色 (執行角色),並在 create_memory API 的 memoryExecutionRoleArn 欄位中建立記憶體時傳遞其 ARN。
此角色需要兩個政策:許可政策和信任政策。
1. 許可政策
首先,請確定您擁有具有 受管政策 AmazonBedrockAgentCoreMemoryBedrockModelInferenceExecutionRolePolicy 的 IAM 角色,或建立具有下列許可的政策:
{ "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. 信任政策
服務會擔任此角色,以呼叫您 AWS 帳戶中的模型。建立角色或使用 受管政策時,請使用以下信任政策:
{ "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 角色。
使用 API 覆寫內建策略
若要覆寫內建策略,請在傳送 CreateMemory 或 UpdateMemory 請求時使用 customMemoryStrategy 欄位。在 CustomConfigurationInput 物件中,您可以在要覆寫的策略中指定步驟。
在要覆寫之步驟的組態 (例如 UserPreferenceOverrideExtractionConfigurationInput) 中,指定下列項目:
-
appendToPrompt– 要取代系統提示中指示的提示 (輸出結構描述保持不變)。 -
modelId– 使用提示叫用之 Amazon Bedrock 模型的 ID。
例如,您可以使用 anthropic.claude-3-sonnet-20240229-v1: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 } } } } } ] }
如需自訂提示的範例,請參閱組態範例。
組態範例
此範例示範如何覆寫使用者偏好設定的擷取和合併步驟。
# 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/'] } } ] )