Configura una strategia personalizzata
Per i casi d'uso avanzati, le strategie integrate con override offrono un controllo preciso sul processo di estrazione della memoria. Ciò consente di ignorare la logica predefinita di una strategia integrata fornendo istruzioni personalizzate e selezionando un modello di base specifico.
-
Esempio di utilizzo: un bot di agenzia di viaggi deve estrarre dettagli molto specifici sulle preferenze di volo di un utente e consolidare nuove preferenze con quelle esistenti, ad esempio aggiungere una preferenza di posto a una preferenza della compagnia aerea dichiarata in precedenza.
Argomenti
Prerequisiti
Per ignorare una strategia di memoria integrata, è necessario soddisfare i seguenti prerequisiti:
-
Avere un ruolo nel servizio AgentCore di memoria. Per ulteriori informazioni, vedere Creazione del ruolo di esecuzione della memoria.
-
Se intendete sostituire il modello richiesto, dovete avere accesso al modello con cui avete scelto di eseguire l'override. Per ulteriori informazioni, consulta Accedere ai modelli base di Amazon Bedrock e alla capacità di Amazon Bedrock per strategie integrate con sostituzioni.
Creazione del ruolo di esecuzione della memoria
Quando utilizzi una strategia integrata con sostituzioni, AgentCore Memory richiama un modello Amazon Bedrock nel tuo account per tuo conto. Per concedere al servizio l'autorizzazione a eseguire questa operazione, è necessario creare un ruolo IAM (un ruolo di esecuzione) e passarne l'ARN durante la creazione della memoria nel memoryExecutionRoleArn campo dell'create_memoryAPI.
Questo ruolo richiede due politiche: una politica di autorizzazioni e una politica di fiducia.
1. Policy delle autorizzazioni
Inizia assicurandoti di avere un ruolo IAM nella policy AmazonBedrockAgentCoreMemoryBedrockModelInferenceExecutionRolePolicygestita oppure crea una policy con le seguenti autorizzazioni:
{ "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. Policy di attendibilità
Questo ruolo viene assunto dal Servizio per richiamare il modello nel tuo AWS account. Utilizza la politica di fiducia riportata di seguito quando crei il ruolo o quando utilizzi la politica gestita:
{ "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}}:*" } } } ] }
Per informazioni sulla creazione di un ruolo IAM, consulta Creazione di ruoli IAM.
Sostituisci una strategia integrata con l'API
Per sovrascrivere una strategia integrata, usa il customMemoryStrategy campo quando invii una richiesta CreateMemoryor UpdateMemory. Nell'CustomConfigurationInputoggetto, è possibile specificare un passaggio della strategia da sostituire.
All'interno della configurazione per il passaggio da sostituire (ad esempio, UserPreferenceOverrideExtractionConfigurationInput), specificate quanto segue:
-
appendToPrompt— Il prompt con cui sostituire le istruzioni nel prompt di sistema (lo schema di output rimane lo stesso). -
modelId— L'ID del modello Amazon Bedrock da richiamare con il prompt.
Ad esempio, puoi inviare il seguente corpo della richiesta per sovrascrivere la strategia di memoria delle preferenze utente con i tuoi prompt di estrazione e consolidamento, utilizzando il modello 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 } } } } } ] }
Ad esempio, istruzioni personalizzate, vedi Esempio di configurazione.
Esempio di configurazione
Questo esempio dimostra come sovrascrivere le fasi di estrazione e consolidamento per le preferenze dell'utente.
# 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/'] } } ] )