View a markdown version of this page

SDK de Amazon Bedrock AgentCore - Base amazónica AgentCore

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

SDK de Amazon Bedrock AgentCore

Utilice el SDK de AgentCore Python de Amazon Bedrock para obtener una abstracción de nivel superior que simplifique las operaciones de memoria y proporcione métodos prácticos para casos de uso comunes.

Instale las dependencias

pip install bedrock-agentcore

Añada memoria a corto plazo

from bedrock_agentcore.memory import MemoryClient client = MemoryClient(region_name="us-east-1") memory = client.create_memory( name="CustomerSupportAgentMemory", description="Memory for customer support conversations", ) client.create_event( memory_id=memory.get("id"), # This is the id from create_memory or list_memories actor_id="User84", # This is the identifier of the actor, could be an agent or end-user. session_id="OrderSupportSession1", #Unique id for a particular request/conversation. messages=[ ("Hi, I'm having trouble with my order #12345", "USER"), ("I'm sorry to hear that. Let me look up your order.", "ASSISTANT"), ("lookup_order(order_id='12345')", "TOOL"), ("I see your order was shipped 3 days ago. What specific issue are you experiencing?", "ASSISTANT"), ("Actually, before that - I also want to change my email address", "USER"), ( "Of course! I can help with both. Let's start with updating your email. What's your new email?", "ASSISTANT", ), ("newemail@example.com", "USER"), ("update_customer_email(old='old@example.com', new='newemail@example.com')", "TOOL"), ("Email updated successfully! Now, about your order issue?", "ASSISTANT"), ("The package arrived damaged", "USER"), ], )

Agregue memoria a largo plazo con estrategias

from bedrock_agentcore.memory import MemoryClient import time client = MemoryClient(region_name="us-east-1") memory = client.create_memory_and_wait( name="MyAgentMemory", strategies=[{ "summaryMemoryStrategy": { # Name of the extraction model/strategy "name": "SessionSummarizer", # Organize facts by session ID for easy retrieval # Example: "summaries/session123" contains summary of session123 "namespaceTemplates": ["/summaries/{actorId}/{sessionId}/"] } }] ) event = client.create_event( memory_id=memory.get("id"), # This is the id from create_memory or list_memories actor_id="User84", # This is the identifier of the actor, could be an agent or end-user. session_id="OrderSupportSession1", messages=[ ("Hi, I'm having trouble with my order #12345", "USER"), ("I'm sorry to hear that. Let me look up your order.", "ASSISTANT"), ("lookup_order(order_id='12345')", "TOOL"), ("I see your order was shipped 3 days ago. What specific issue are you experiencing?", "ASSISTANT"), ("Actually, before that - I also want to change my email address", "USER"), ( "Of course! I can help with both. Let's start with updating your email. What's your new email?", "ASSISTANT", ), ("newemail@example.com", "USER"), ("update_customer_email(old='old@example.com', new='newemail@example.com')", "TOOL"), ("Email updated successfully! Now, about your order issue?", "ASSISTANT"), ("The package arrived damaged", "USER"), ], ) # Wait for meaningful memories to be extracted from the conversation. time.sleep(60) # Query for the summary of the issue using the namespace set in summary strategy above memories = client.retrieve_memories( memory_id=memory.get("id"), namespace=f"/summaries/User84/OrderSupportSession1/", query="can you summarize the support issue" )