View a markdown version of this page

亚马逊基岩 SDK AgentCore - Amazon Bedrock AgentCore
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

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

亚马逊基岩 SDK AgentCore

使用 Amazon Bedrock AgentCore Python SDK 进行更高级别的抽象,从而简化内存操作并为常见用例提供便捷的方法。

安装依赖项

pip install bedrock-agentcore

添加短期记忆

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"), ], )

通过策略增加长期记忆

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" )