bedrock-agentcore-starter-toolkit 0.1.14__py3-none-any.whl → 0.1.15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of bedrock-agentcore-starter-toolkit might be problematic. Click here for more details.
- bedrock_agentcore_starter_toolkit/cli/runtime/commands.py +33 -2
- bedrock_agentcore_starter_toolkit/cli/runtime/configuration_manager.py +127 -1
- bedrock_agentcore_starter_toolkit/operations/memory/README.md +1109 -0
- bedrock_agentcore_starter_toolkit/operations/memory/constants.py +1 -9
- bedrock_agentcore_starter_toolkit/operations/memory/manager.py +248 -57
- bedrock_agentcore_starter_toolkit/operations/memory/models/__init__.py +106 -0
- bedrock_agentcore_starter_toolkit/operations/memory/models/strategies/__init__.py +52 -0
- bedrock_agentcore_starter_toolkit/operations/memory/models/strategies/base.py +77 -0
- bedrock_agentcore_starter_toolkit/operations/memory/models/strategies/custom.py +194 -0
- bedrock_agentcore_starter_toolkit/operations/memory/models/strategies/semantic.py +35 -0
- bedrock_agentcore_starter_toolkit/operations/memory/models/strategies/summary.py +35 -0
- bedrock_agentcore_starter_toolkit/operations/memory/models/strategies/user_preference.py +34 -0
- bedrock_agentcore_starter_toolkit/operations/memory/strategy_validator.py +395 -0
- bedrock_agentcore_starter_toolkit/operations/runtime/configure.py +54 -0
- bedrock_agentcore_starter_toolkit/operations/runtime/destroy.py +43 -3
- bedrock_agentcore_starter_toolkit/operations/runtime/invoke.py +45 -0
- bedrock_agentcore_starter_toolkit/operations/runtime/launch.py +164 -0
- bedrock_agentcore_starter_toolkit/operations/runtime/models.py +7 -0
- bedrock_agentcore_starter_toolkit/operations/runtime/status.py +62 -0
- bedrock_agentcore_starter_toolkit/utils/runtime/container.py +4 -0
- bedrock_agentcore_starter_toolkit/utils/runtime/schema.py +27 -1
- bedrock_agentcore_starter_toolkit/utils/runtime/templates/Dockerfile.j2 +9 -2
- bedrock_agentcore_starter_toolkit/utils/runtime/templates/execution_role_policy.json.j2 +31 -0
- {bedrock_agentcore_starter_toolkit-0.1.14.dist-info → bedrock_agentcore_starter_toolkit-0.1.15.dist-info}/METADATA +1 -1
- {bedrock_agentcore_starter_toolkit-0.1.14.dist-info → bedrock_agentcore_starter_toolkit-0.1.15.dist-info}/RECORD +29 -20
- {bedrock_agentcore_starter_toolkit-0.1.14.dist-info → bedrock_agentcore_starter_toolkit-0.1.15.dist-info}/WHEEL +0 -0
- {bedrock_agentcore_starter_toolkit-0.1.14.dist-info → bedrock_agentcore_starter_toolkit-0.1.15.dist-info}/entry_points.txt +0 -0
- {bedrock_agentcore_starter_toolkit-0.1.14.dist-info → bedrock_agentcore_starter_toolkit-0.1.15.dist-info}/licenses/LICENSE.txt +0 -0
- {bedrock_agentcore_starter_toolkit-0.1.14.dist-info → bedrock_agentcore_starter_toolkit-0.1.15.dist-info}/licenses/NOTICE.txt +0 -0
|
@@ -0,0 +1,1109 @@
|
|
|
1
|
+
# MemoryManager Comprehensive Guide
|
|
2
|
+
|
|
3
|
+
A high-level client for managing AWS Bedrock AgentCore Memory resources with full lifecycle management, strategy support, and advanced features.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Overview](#overview)
|
|
8
|
+
2. [Installation & Setup](#installation--setup)
|
|
9
|
+
3. [Quick Start](#quick-start)
|
|
10
|
+
4. [Strategy Types Guide](#strategy-types-guide)
|
|
11
|
+
5. [Advanced Usage](#advanced-usage)
|
|
12
|
+
6. [Error Handling](#error-handling)
|
|
13
|
+
7. [Best Practices](#best-practices)
|
|
14
|
+
8. [Troubleshooting](#troubleshooting)
|
|
15
|
+
|
|
16
|
+
## Overview
|
|
17
|
+
|
|
18
|
+
The `MemoryManager` class provides a comprehensive interface for managing AWS Bedrock AgentCore Memory resources. It handles all **Bedrock-agentcore-control operations** for creating, configuring, and managing memory resources that enable AI agents to retain and recall information across conversations and sessions. AgentCore Memory transforms stateless AI interactions into intelligent, context-aware experiences by automatically storing, organizing, and retrieving relevant information, allowing your agents to build relationships, remember preferences, and provide increasingly personalized responses over time.
|
|
19
|
+
|
|
20
|
+
### Key Features
|
|
21
|
+
|
|
22
|
+
- **Full Lifecycle Management**: Create, read, update, delete memories with automatic status polling
|
|
23
|
+
- **Strategy Management**: Add, modify, delete memory strategies of various types
|
|
24
|
+
- **Type Safety**: Support for strongly-typed strategy objects with validation
|
|
25
|
+
- **Backward Compatibility**: Works with existing dictionary-based strategy configurations
|
|
26
|
+
- **Advanced Polling**: Automatic waiting for resource state transitions
|
|
27
|
+
- **Error Handling**: Comprehensive error handling with detailed logging
|
|
28
|
+
|
|
29
|
+
### Supported Strategy Types
|
|
30
|
+
|
|
31
|
+
- **Semantic Memory Strategy**: Extract semantic information from conversations
|
|
32
|
+
- **Summary Memory Strategy**: Create summaries of conversation content
|
|
33
|
+
- **User Preference Strategy**: Store and manage user preferences
|
|
34
|
+
- **Custom Semantic Strategy**: Custom extraction and consolidation with specific prompts
|
|
35
|
+
- **Custom Summary Strategy**: Custom consolidation with specific prompts
|
|
36
|
+
- **Custom User Preference Strategy**: Custom extraction and consolidation with specific prompts
|
|
37
|
+
|
|
38
|
+
## Installation & Setup
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.manager import MemoryManager
|
|
42
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import (
|
|
43
|
+
SemanticStrategy, SummaryStrategy, UserPreferenceStrategy,
|
|
44
|
+
CustomSemanticStrategy, ExtractionConfig, ConsolidationConfig
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
# Initialize with default region
|
|
48
|
+
manager = MemoryManager()
|
|
49
|
+
|
|
50
|
+
# Initialize with specific region
|
|
51
|
+
manager = MemoryManager(region_name="us-east-1")
|
|
52
|
+
|
|
53
|
+
# Initialize with custom boto3 session
|
|
54
|
+
import boto3
|
|
55
|
+
session = boto3.Session(profile_name="my-profile")
|
|
56
|
+
manager = MemoryManager(boto3_session=session)
|
|
57
|
+
|
|
58
|
+
# Initialize with both (must match)
|
|
59
|
+
manager = MemoryManager(region_name="us-east-1", boto3_session=session)
|
|
60
|
+
|
|
61
|
+
# Initialize with custom boto client configuration
|
|
62
|
+
from botocore.config import Config as BotocoreConfig
|
|
63
|
+
|
|
64
|
+
# Custom retry and timeout configuration
|
|
65
|
+
custom_config = BotocoreConfig(
|
|
66
|
+
retries={'max_attempts': 5, 'mode': 'adaptive'},
|
|
67
|
+
read_timeout=60,
|
|
68
|
+
connect_timeout=30,
|
|
69
|
+
max_pool_connections=50
|
|
70
|
+
)
|
|
71
|
+
manager = MemoryManager(region_name="us-east-1", boto_client_config=custom_config)
|
|
72
|
+
|
|
73
|
+
# Custom configuration with existing user agent (will be preserved and extended)
|
|
74
|
+
custom_config_with_agent = BotocoreConfig(
|
|
75
|
+
user_agent_extra="my-application/1.0",
|
|
76
|
+
retries={'max_attempts': 3}
|
|
77
|
+
)
|
|
78
|
+
manager = MemoryManager(region_name="us-east-1", boto_client_config=custom_config_with_agent)
|
|
79
|
+
|
|
80
|
+
# Combine all initialization options
|
|
81
|
+
manager = MemoryManager(
|
|
82
|
+
region_name="us-east-1",
|
|
83
|
+
boto3_session=session,
|
|
84
|
+
boto_client_config=custom_config
|
|
85
|
+
)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Quick Start
|
|
89
|
+
|
|
90
|
+
### Basic Memory Creation
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
# Create a simple memory with semantic strategy
|
|
94
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import SemanticStrategy
|
|
95
|
+
|
|
96
|
+
manager = MemoryManager(region_name="us-east-1")
|
|
97
|
+
|
|
98
|
+
# Using typed strategy (recommended)
|
|
99
|
+
semantic_strategy = SemanticStrategy(
|
|
100
|
+
name="ConversationSemantics",
|
|
101
|
+
description="Extract semantic information from conversations",
|
|
102
|
+
namespaces=["semantics/{actorId}/{sessionId}"]
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
memory = manager.create_memory_and_wait(
|
|
106
|
+
name="MyMemory",
|
|
107
|
+
strategies=[semantic_strategy],
|
|
108
|
+
description="My first memory resource"
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
print(f"Created memory: {memory.id}")
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Get or Create Pattern
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
# Get existing memory or create if it doesn't exist
|
|
118
|
+
memory = manager.get_or_create_memory(
|
|
119
|
+
name="PersistentMemory",
|
|
120
|
+
strategies=[semantic_strategy],
|
|
121
|
+
description="Always available memory"
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### List and Manage Memories
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
# List all memories
|
|
129
|
+
memories = manager.list_memories()
|
|
130
|
+
for memory_summary in memories:
|
|
131
|
+
print(f"Memory: {memory_summary.id} - {memory_summary.name} ({memory_summary.status})")
|
|
132
|
+
|
|
133
|
+
# Get specific memory details
|
|
134
|
+
memory = manager.get_memory("mem-123")
|
|
135
|
+
print(f"Memory status: {memory.status}")
|
|
136
|
+
|
|
137
|
+
# Delete memory
|
|
138
|
+
manager.delete_memory_and_wait("mem-123")
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Create Memory and wait
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import (
|
|
145
|
+
SemanticStrategy, CustomSemanticStrategy, ExtractionConfig, ConsolidationConfig
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
# Create with typed strategies
|
|
149
|
+
semantic = SemanticStrategy(name="MySemanticStrategy")
|
|
150
|
+
custom = CustomSemanticStrategy(
|
|
151
|
+
name="MyCustomStrategy",
|
|
152
|
+
extraction_config=ExtractionConfig(
|
|
153
|
+
append_to_prompt="Extract insights",
|
|
154
|
+
model_id="anthropic.claude-3-sonnet-20240229-v1:0"
|
|
155
|
+
),
|
|
156
|
+
consolidation_config=ConsolidationConfig(
|
|
157
|
+
append_to_prompt="Consolidate insights",
|
|
158
|
+
model_id="anthropic.claude-3-haiku-20240307-v1:0"
|
|
159
|
+
)
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
memory = manager.create_memory_and_wait(
|
|
163
|
+
name="TypedMemory",
|
|
164
|
+
strategies=[semantic, custom],
|
|
165
|
+
description="Memory with typed strategies",
|
|
166
|
+
event_expiry_days=120,
|
|
167
|
+
memory_execution_role_arn="arn:aws:iam::123456789012:role/MemoryRole"
|
|
168
|
+
)
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### Get Memory
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
memory = manager.get_memory("mem-123")
|
|
175
|
+
print(f"Memory: {memory.name} - Status: {memory.status}")
|
|
176
|
+
print(f"Description: {memory.description}")
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### List Memories
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
# List all memories
|
|
183
|
+
memories = manager.list_memories()
|
|
184
|
+
for memory in memories:
|
|
185
|
+
print(f"ID: {memory.id}")
|
|
186
|
+
print(f"Name: {memory.name}")
|
|
187
|
+
print(f"Status: {memory.status}")
|
|
188
|
+
|
|
189
|
+
# List with limit
|
|
190
|
+
recent_memories = manager.list_memories(max_results=10)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
#### Delete Memory
|
|
194
|
+
```python
|
|
195
|
+
# Delete and wait for completion
|
|
196
|
+
response = manager.delete_memory_and_wait("mem-123")
|
|
197
|
+
print("Memory successfully deleted")
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
### Status and Information Methods
|
|
203
|
+
|
|
204
|
+
#### Get Memory Status
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
status = manager.get_memory_status("mem-123")
|
|
208
|
+
print(f"Memory status: {status}")
|
|
209
|
+
|
|
210
|
+
# Check if memory is ready
|
|
211
|
+
if status == "ACTIVE":
|
|
212
|
+
print("Memory is ready for use")
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### Get Memory Strategies
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
strategies = manager.get_memory_strategies("mem-123")
|
|
219
|
+
for strategy in strategies:
|
|
220
|
+
print(f"Strategy: {strategy.name} ({strategy.type})")
|
|
221
|
+
print(f"ID: {strategy.strategyId}")
|
|
222
|
+
print(f"Status: {strategy.get('status', 'N/A')}")
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
### Strategy Management Methods
|
|
228
|
+
|
|
229
|
+
```python
|
|
230
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import SummaryStrategy
|
|
231
|
+
|
|
232
|
+
# Add typed strategy
|
|
233
|
+
summary = SummaryStrategy(
|
|
234
|
+
name="ConversationSummary",
|
|
235
|
+
description="Summarize conversations",
|
|
236
|
+
namespaces=["summaries/{actorId}/{sessionId}"]
|
|
237
|
+
)
|
|
238
|
+
|
|
239
|
+
memory = manager.add_strategy_and_wait(
|
|
240
|
+
memory_id="mem-123",
|
|
241
|
+
strategy=summary
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
# Add custom strategy with configurations
|
|
245
|
+
custom = CustomSemanticStrategy(
|
|
246
|
+
name="CustomStrategy",
|
|
247
|
+
extraction_config=ExtractionConfig(
|
|
248
|
+
append_to_prompt="Extract key insights",
|
|
249
|
+
model_id="anthropic.claude-3-sonnet-20240229-v1:0"
|
|
250
|
+
),
|
|
251
|
+
consolidation_config=ConsolidationConfig(
|
|
252
|
+
append_to_prompt="Consolidate insights",
|
|
253
|
+
model_id="anthropic.claude-3-haiku-20240307-v1:0"
|
|
254
|
+
)
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
memory = manager.add_strategy_and_wait(
|
|
258
|
+
memory_id="mem-123",
|
|
259
|
+
strategy=custom
|
|
260
|
+
)
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
#### Update Memory Strategies
|
|
264
|
+
|
|
265
|
+
```python
|
|
266
|
+
# Add multiple strategies
|
|
267
|
+
new_strategies = [
|
|
268
|
+
SemanticStrategy(name="NewSemantic"),
|
|
269
|
+
SummaryStrategy(name="NewSummary")
|
|
270
|
+
]
|
|
271
|
+
|
|
272
|
+
memory = manager.update_memory_strategies_and_wait(
|
|
273
|
+
memory_id="mem-123",
|
|
274
|
+
add_strategies=new_strategies
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
# Modify existing strategy
|
|
278
|
+
modify_configs = [{
|
|
279
|
+
"strategyId": "strat-456",
|
|
280
|
+
"description": "Updated description",
|
|
281
|
+
"namespaces": ["updated/{actorId}"]
|
|
282
|
+
}]
|
|
283
|
+
|
|
284
|
+
memory = manager.update_memory_strategies_and_wait(
|
|
285
|
+
memory_id="mem-123",
|
|
286
|
+
modify_strategies=modify_configs
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
# Delete strategies
|
|
290
|
+
memory = manager.update_memory_strategies_and_wait(
|
|
291
|
+
memory_id="mem-123",
|
|
292
|
+
delete_strategy_ids=["strat-789", "strat-101"]
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
# Combined operations
|
|
296
|
+
memory = manager.update_memory_strategies_and_wait(
|
|
297
|
+
memory_id="mem-123",
|
|
298
|
+
add_strategies=[SemanticStrategy(name="NewStrategy")],
|
|
299
|
+
modify_strategies=[{"strategyId": "strat-456", "description": "Updated"}],
|
|
300
|
+
delete_strategy_ids=["strat-old"]
|
|
301
|
+
)
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### Modify Strategy
|
|
305
|
+
|
|
306
|
+
```python
|
|
307
|
+
# Update strategy description and namespaces
|
|
308
|
+
memory = manager.modify_strategy(
|
|
309
|
+
memory_id="mem-123",
|
|
310
|
+
strategy_id="strat-456",
|
|
311
|
+
description="Updated strategy description",
|
|
312
|
+
namespaces=["custom/{actorId}/{sessionId}"]
|
|
313
|
+
)
|
|
314
|
+
|
|
315
|
+
# Update strategy configuration
|
|
316
|
+
memory = manager.modify_strategy(
|
|
317
|
+
memory_id="mem-123",
|
|
318
|
+
strategy_id="strat-456",
|
|
319
|
+
configuration={
|
|
320
|
+
"extraction": {
|
|
321
|
+
"appendToPrompt": "New extraction prompt",
|
|
322
|
+
"modelId": "anthropic.claude-3-sonnet-20240229-v1:0"
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
)
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
#### Delete strategy
|
|
329
|
+
|
|
330
|
+
```python
|
|
331
|
+
memory = manager.delete_strategy(
|
|
332
|
+
memory_id="mem-123",
|
|
333
|
+
strategy_id="strat-456"
|
|
334
|
+
)
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
---
|
|
338
|
+
|
|
339
|
+
### Convenience Strategy Methods
|
|
340
|
+
|
|
341
|
+
```python
|
|
342
|
+
memory = manager.add_semantic_strategy_and_wait(
|
|
343
|
+
memory_id="mem-123",
|
|
344
|
+
name="ConversationSemantics",
|
|
345
|
+
description="Extract semantic information",
|
|
346
|
+
namespaces=["semantics/{actorId}/{sessionId}"]
|
|
347
|
+
)
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
```python
|
|
351
|
+
memory = manager.add_summary_strategy_and_wait(
|
|
352
|
+
memory_id="mem-123",
|
|
353
|
+
name="ConversationSummary",
|
|
354
|
+
description="Summarize conversations",
|
|
355
|
+
namespaces=["summaries/{actorId}/{sessionId}"]
|
|
356
|
+
)
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
```python
|
|
360
|
+
memory = manager.add_user_preference_strategy_and_wait(
|
|
361
|
+
memory_id="mem-123",
|
|
362
|
+
name="UserPreferences",
|
|
363
|
+
description="Store user preferences",
|
|
364
|
+
namespaces=["preferences/{actorId}"]
|
|
365
|
+
)
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
```python
|
|
369
|
+
extraction_config = {
|
|
370
|
+
"prompt": "Extract key business insights from the conversation",
|
|
371
|
+
"modelId": "anthropic.claude-3-sonnet-20240229-v1:0"
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
consolidation_config = {
|
|
375
|
+
"prompt": "Consolidate business insights into actionable summaries",
|
|
376
|
+
"modelId": "anthropic.claude-3-haiku-20240307-v1:0"
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
memory = manager.add_custom_semantic_strategy_and_wait(
|
|
380
|
+
memory_id="mem-123",
|
|
381
|
+
name="BusinessInsights",
|
|
382
|
+
extraction_config=extraction_config,
|
|
383
|
+
consolidation_config=consolidation_config,
|
|
384
|
+
description="Extract and consolidate business insights"
|
|
385
|
+
)
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
## Strategy Types Guide
|
|
389
|
+
|
|
390
|
+
```python
|
|
391
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import SemanticStrategy
|
|
392
|
+
|
|
393
|
+
# Basic semantic strategy
|
|
394
|
+
semantic = SemanticStrategy(
|
|
395
|
+
name="ConversationSemantics",
|
|
396
|
+
description="Extract semantic information from conversations"
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
# With custom namespaces
|
|
400
|
+
semantic = SemanticStrategy(
|
|
401
|
+
name="ConversationSemantics",
|
|
402
|
+
description="Extract semantic information from conversations",
|
|
403
|
+
namespaces=["semantics/{actorId}/{sessionId}"]
|
|
404
|
+
)
|
|
405
|
+
|
|
406
|
+
# Add to memory
|
|
407
|
+
memory = manager.add_strategy_and_wait(memory_id="mem-123", strategy=semantic)
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### Summary Strategy
|
|
411
|
+
|
|
412
|
+
```python
|
|
413
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import SummaryStrategy
|
|
414
|
+
|
|
415
|
+
# Basic summary strategy
|
|
416
|
+
summary = SummaryStrategy(
|
|
417
|
+
name="ConversationSummary",
|
|
418
|
+
description="Summarize conversation content"
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
# With custom namespaces
|
|
422
|
+
summary = SummaryStrategy(
|
|
423
|
+
name="ConversationSummary",
|
|
424
|
+
description="Summarize conversation content",
|
|
425
|
+
namespaces=["summaries/{actorId}/{sessionId}"]
|
|
426
|
+
)
|
|
427
|
+
|
|
428
|
+
# Add to memory
|
|
429
|
+
memory = manager.add_strategy_and_wait(memory_id="mem-123", strategy=summary)
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### User Preference Strategy
|
|
433
|
+
|
|
434
|
+
```python
|
|
435
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import UserPreferenceStrategy
|
|
436
|
+
|
|
437
|
+
# User preference strategy
|
|
438
|
+
user_pref = UserPreferenceStrategy(
|
|
439
|
+
name="UserPreferences",
|
|
440
|
+
description="Store user preferences and settings",
|
|
441
|
+
namespaces=["preferences/{actorId}"] # Note: typically per-actor, not per-session
|
|
442
|
+
)
|
|
443
|
+
|
|
444
|
+
# Add to memory
|
|
445
|
+
memory = manager.add_strategy_and_wait(memory_id="mem-123", strategy=user_pref)
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
### Custom Semantic Strategy
|
|
449
|
+
|
|
450
|
+
```python
|
|
451
|
+
from bedrock_agentcore_starter_toolkit.operations.memory.models import (
|
|
452
|
+
CustomSemanticStrategy, ExtractionConfig, ConsolidationConfig
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
# Create configuration objects
|
|
456
|
+
extraction_config = ExtractionConfig(
|
|
457
|
+
append_to_prompt="Extract key business insights and action items from the conversation",
|
|
458
|
+
model_id="anthropic.claude-3-sonnet-20240229-v1:0"
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
consolidation_config = ConsolidationConfig(
|
|
462
|
+
append_to_prompt="Consolidate business insights into actionable summaries with priorities",
|
|
463
|
+
model_id="anthropic.claude-3-haiku-20240307-v1:0"
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
# Create custom strategy
|
|
467
|
+
custom = CustomSemanticStrategy(
|
|
468
|
+
name="BusinessInsights",
|
|
469
|
+
description="Extract and consolidate business insights",
|
|
470
|
+
extraction_config=extraction_config,
|
|
471
|
+
consolidation_config=consolidation_config,
|
|
472
|
+
namespaces=["business/{actorId}/{sessionId}"]
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
# Add to memory
|
|
476
|
+
memory = manager.add_strategy_and_wait(memory_id="mem-123", strategy=custom)
|
|
477
|
+
```
|
|
478
|
+
|
|
479
|
+
### Dictionary Strategies
|
|
480
|
+
|
|
481
|
+
For backward compatibility, dictionary-based strategies are still supported:
|
|
482
|
+
|
|
483
|
+
```python
|
|
484
|
+
# Dictionary semantic strategy
|
|
485
|
+
semantic = {
|
|
486
|
+
"semanticMemoryStrategy": {
|
|
487
|
+
"name": "SemanticStrategy",
|
|
488
|
+
"description": "dictionary-based strategy",
|
|
489
|
+
"namespaces": ["business/{actorId}/{sessionId}"]
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
# Dictionary summary strategy
|
|
494
|
+
summary = {
|
|
495
|
+
"summaryMemoryStrategy": {
|
|
496
|
+
"name": "SummaryStrategy",
|
|
497
|
+
"description": "summary strategy"
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
# Mix typed and Dictionary strategies
|
|
502
|
+
mixed_strategies = [
|
|
503
|
+
SemanticStrategy(name="TypedStrategy"),
|
|
504
|
+
semantic, summary
|
|
505
|
+
]
|
|
506
|
+
|
|
507
|
+
memory = manager.create_memory_and_wait(
|
|
508
|
+
name="MixedMemory",
|
|
509
|
+
strategies=mixed_strategies
|
|
510
|
+
)
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
## Advanced Usage
|
|
514
|
+
|
|
515
|
+
### Namespace Patterns
|
|
516
|
+
|
|
517
|
+
Namespaces support template variables for dynamic organization:
|
|
518
|
+
|
|
519
|
+
```python
|
|
520
|
+
# Available template variables
|
|
521
|
+
namespaces = [
|
|
522
|
+
"global/shared", # Static namespace
|
|
523
|
+
"actor/{actorId}", # Per-actor namespace
|
|
524
|
+
"session/{actorId}/{sessionId}", # Per-session namespace
|
|
525
|
+
"strategy/{strategyId}", # Per-strategy namespace
|
|
526
|
+
"custom/{actorId}/{sessionId}" # Custom pattern
|
|
527
|
+
]
|
|
528
|
+
|
|
529
|
+
strategy = SemanticStrategy(
|
|
530
|
+
name="FlexibleStrategy",
|
|
531
|
+
namespaces=namespaces
|
|
532
|
+
)
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
### Batch Strategy Operations
|
|
536
|
+
|
|
537
|
+
```python
|
|
538
|
+
# Add multiple strategies at once
|
|
539
|
+
strategies_to_add = [
|
|
540
|
+
SemanticStrategy(name="Semantic1"),
|
|
541
|
+
SummaryStrategy(name="Summary1"),
|
|
542
|
+
UserPreferenceStrategy(name="UserPref1")
|
|
543
|
+
]
|
|
544
|
+
|
|
545
|
+
# Modify multiple strategies
|
|
546
|
+
strategies_to_modify = [
|
|
547
|
+
{"strategyId": "strat-1", "description": "Updated description 1"},
|
|
548
|
+
{"strategyId": "strat-2", "description": "Updated description 2"}
|
|
549
|
+
]
|
|
550
|
+
|
|
551
|
+
# Delete multiple strategies
|
|
552
|
+
strategy_ids_to_delete = ["strat-3", "strat-4"]
|
|
553
|
+
|
|
554
|
+
# Execute all operations in one call
|
|
555
|
+
memory = manager.update_memory_strategies_and_wait(
|
|
556
|
+
memory_id="mem-123",
|
|
557
|
+
add_strategies=strategies_to_add,
|
|
558
|
+
modify_strategies=strategies_to_modify,
|
|
559
|
+
delete_strategy_ids=strategy_ids_to_delete
|
|
560
|
+
)
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
### Custom Polling Configuration
|
|
564
|
+
|
|
565
|
+
```python
|
|
566
|
+
# Create memory with custom polling
|
|
567
|
+
memory = manager.create_memory_and_wait(
|
|
568
|
+
name="SlowMemory",
|
|
569
|
+
strategies=[SemanticStrategy(name="Strategy1")],
|
|
570
|
+
max_wait=600, # Wait up to 10 minutes
|
|
571
|
+
poll_interval=30 # Check every 30 seconds
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
# Add strategy with custom polling
|
|
575
|
+
memory = manager.add_strategy_and_wait(
|
|
576
|
+
memory_id="mem-123",
|
|
577
|
+
strategy=SummaryStrategy(name="SlowStrategy"),
|
|
578
|
+
max_wait=900, # Wait up to 15 minutes
|
|
579
|
+
poll_interval=60 # Check every minute
|
|
580
|
+
)
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
### Memory Configuration Options
|
|
584
|
+
|
|
585
|
+
```python
|
|
586
|
+
# Full memory configuration
|
|
587
|
+
memory = manager.create_memory_and_wait(
|
|
588
|
+
name="FullyConfiguredMemory",
|
|
589
|
+
strategies=[SemanticStrategy(name="Strategy1")],
|
|
590
|
+
description="A fully configured memory resource",
|
|
591
|
+
event_expiry_days=180, # Keep events for 6 months
|
|
592
|
+
memory_execution_role_arn="arn:aws:iam::123456789012:role/MemoryExecutionRole",
|
|
593
|
+
encryption_key_arn="arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012"
|
|
594
|
+
)
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
### Custom Boto Client Configuration
|
|
598
|
+
|
|
599
|
+
The `boto_client_config` parameter allows you to customize the underlying boto3 client behavior for advanced use cases:
|
|
600
|
+
|
|
601
|
+
```python
|
|
602
|
+
from botocore.config import Config as BotocoreConfig
|
|
603
|
+
|
|
604
|
+
# Production configuration with enhanced reliability
|
|
605
|
+
production_config = BotocoreConfig(
|
|
606
|
+
retries={
|
|
607
|
+
'max_attempts': 10,
|
|
608
|
+
'mode': 'adaptive' # Adaptive retry mode for better handling of throttling
|
|
609
|
+
},
|
|
610
|
+
read_timeout=120, # 2 minutes for read operations
|
|
611
|
+
connect_timeout=60, # 1 minute for connection establishment
|
|
612
|
+
max_pool_connections=50 # Higher connection pool for concurrent operations
|
|
613
|
+
)
|
|
614
|
+
|
|
615
|
+
manager = MemoryManager(
|
|
616
|
+
region_name="us-east-1",
|
|
617
|
+
boto_client_config=production_config
|
|
618
|
+
)
|
|
619
|
+
|
|
620
|
+
# Development configuration with faster timeouts
|
|
621
|
+
dev_config = BotocoreConfig(
|
|
622
|
+
retries={'max_attempts': 3},
|
|
623
|
+
read_timeout=30,
|
|
624
|
+
connect_timeout=10
|
|
625
|
+
)
|
|
626
|
+
|
|
627
|
+
dev_manager = MemoryManager(
|
|
628
|
+
region_name="us-east-1",
|
|
629
|
+
boto_client_config=dev_config
|
|
630
|
+
)
|
|
631
|
+
|
|
632
|
+
# Custom user agent for tracking
|
|
633
|
+
tracking_config = BotocoreConfig(
|
|
634
|
+
user_agent_extra="MyApp/2.1.0 Environment/Production",
|
|
635
|
+
retries={'max_attempts': 5}
|
|
636
|
+
)
|
|
637
|
+
|
|
638
|
+
# The final user agent will be: "MyApp/2.1.0 Environment/Production bedrock-agentcore-starter-toolkit"
|
|
639
|
+
tracking_manager = MemoryManager(
|
|
640
|
+
region_name="us-east-1",
|
|
641
|
+
boto_client_config=tracking_config
|
|
642
|
+
)
|
|
643
|
+
|
|
644
|
+
# Regional failover configuration
|
|
645
|
+
regional_config = BotocoreConfig(
|
|
646
|
+
retries={
|
|
647
|
+
'max_attempts': 8,
|
|
648
|
+
'mode': 'adaptive'
|
|
649
|
+
},
|
|
650
|
+
read_timeout=90,
|
|
651
|
+
)
|
|
652
|
+
|
|
653
|
+
regional_manager = MemoryManager(
|
|
654
|
+
region_name="us-west-2",
|
|
655
|
+
boto_client_config=regional_config
|
|
656
|
+
)
|
|
657
|
+
```
|
|
658
|
+
|
|
659
|
+
### Working with Memory Objects
|
|
660
|
+
|
|
661
|
+
```python
|
|
662
|
+
# Memory objects provide dict-like access
|
|
663
|
+
memory = manager.get_memory("mem-123")
|
|
664
|
+
|
|
665
|
+
# Access properties
|
|
666
|
+
print(f"ID: {memory.id}")
|
|
667
|
+
print(f"Name: {memory.name}")
|
|
668
|
+
print(f"Status: {memory.status}")
|
|
669
|
+
print(f"Description: {memory.description}")
|
|
670
|
+
|
|
671
|
+
# Dict-style access
|
|
672
|
+
print(f"ID: {memory['id']}")
|
|
673
|
+
print(f"Name: {memory['name']}")
|
|
674
|
+
|
|
675
|
+
# Safe access with defaults
|
|
676
|
+
creation_time = memory.get('creationTime', 'Unknown')
|
|
677
|
+
```
|
|
678
|
+
|
|
679
|
+
## Error Handling
|
|
680
|
+
|
|
681
|
+
### Common Error Patterns
|
|
682
|
+
|
|
683
|
+
```python
|
|
684
|
+
from botocore.exceptions import ClientError
|
|
685
|
+
|
|
686
|
+
try:
|
|
687
|
+
memory = manager.create_memory_and_wait(
|
|
688
|
+
name="TestMemory",
|
|
689
|
+
strategies=[SemanticStrategy(name="TestStrategy")]
|
|
690
|
+
)
|
|
691
|
+
except ClientError as e:
|
|
692
|
+
error_code = e.response['Error']['Code']
|
|
693
|
+
error_message = e.response['Error']['Message']
|
|
694
|
+
|
|
695
|
+
if error_code == 'ValidationException':
|
|
696
|
+
print(f"Invalid parameters: {error_message}")
|
|
697
|
+
elif error_code == 'ResourceNotFoundException':
|
|
698
|
+
print(f"Resource not found: {error_message}")
|
|
699
|
+
elif error_code == 'AccessDeniedException':
|
|
700
|
+
print(f"Access denied: {error_message}")
|
|
701
|
+
else:
|
|
702
|
+
print(f"AWS error ({error_code}): {error_message}")
|
|
703
|
+
|
|
704
|
+
except TimeoutError as e:
|
|
705
|
+
print(f"Operation timed out: {e}")
|
|
706
|
+
|
|
707
|
+
except RuntimeError as e:
|
|
708
|
+
print(f"Memory operation failed: {e}")
|
|
709
|
+
|
|
710
|
+
except Exception as e:
|
|
711
|
+
print(f"Unexpected error: {e}")
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
### Handling Memory State Transitions
|
|
715
|
+
|
|
716
|
+
```python
|
|
717
|
+
# Check memory status before operations
|
|
718
|
+
def safe_add_strategy(manager, memory_id, strategy):
|
|
719
|
+
"""Safely add a strategy, handling state transitions."""
|
|
720
|
+
try:
|
|
721
|
+
status = manager.get_memory_status(memory_id)
|
|
722
|
+
if status != "ACTIVE":
|
|
723
|
+
print(f"Memory is {status}, waiting for ACTIVE state...")
|
|
724
|
+
# Could implement custom waiting logic here
|
|
725
|
+
|
|
726
|
+
return manager.add_strategy_and_wait(memory_id, strategy)
|
|
727
|
+
|
|
728
|
+
except ClientError as e:
|
|
729
|
+
if e.response['Error']['Code'] == 'ConflictException':
|
|
730
|
+
print("Memory is being modified, retrying...")
|
|
731
|
+
# Could implement retry logic here
|
|
732
|
+
raise
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
### Strategy Validation
|
|
736
|
+
|
|
737
|
+
```python
|
|
738
|
+
# Validate strategy configuration before adding
|
|
739
|
+
def validate_and_add_strategy(manager, memory_id, strategy):
|
|
740
|
+
"""Validate strategy before adding to memory."""
|
|
741
|
+
if isinstance(strategy, BaseStrategy):
|
|
742
|
+
# Pydantic validation happens automatically
|
|
743
|
+
try:
|
|
744
|
+
strategy_dict = strategy.to_dict()
|
|
745
|
+
except ValidationError as e:
|
|
746
|
+
print(f"Strategy validation failed: {e}")
|
|
747
|
+
return None
|
|
748
|
+
|
|
749
|
+
return manager.add_strategy_and_wait(memory_id, strategy)
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
## Best Practices
|
|
753
|
+
|
|
754
|
+
### 1. Use Typed Strategies
|
|
755
|
+
|
|
756
|
+
```python
|
|
757
|
+
# ✅ Recommended: Use typed strategies
|
|
758
|
+
semantic = SemanticStrategy(
|
|
759
|
+
name="ConversationSemantics",
|
|
760
|
+
description="Extract semantic information"
|
|
761
|
+
)
|
|
762
|
+
|
|
763
|
+
# ❌ Avoid: Dictionary strategies (unless migrating)
|
|
764
|
+
semantic_dict = {
|
|
765
|
+
"semanticMemoryStrategy": {
|
|
766
|
+
"name": "ConversationSemantics",
|
|
767
|
+
"description": "Extract semantic information"
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
### 2. Always Use Wait Methods
|
|
773
|
+
|
|
774
|
+
```python
|
|
775
|
+
# ✅ Recommended: Use wait methods for reliability
|
|
776
|
+
memory = manager.create_memory_and_wait(name="MyMemory", strategies=[strategy])
|
|
777
|
+
memory = manager.add_strategy_and_wait(memory_id, new_strategy)
|
|
778
|
+
|
|
779
|
+
# ❌ Avoid: Non-wait methods (unless you handle state management)
|
|
780
|
+
memory = manager._create_memory(name="MyMemory", strategies=[strategy]) # Private method
|
|
781
|
+
memory = manager.add_strategy(memory_id, new_strategy) # May leave memory in CREATING state
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
### 3. Use Descriptive Names and Namespaces
|
|
785
|
+
|
|
786
|
+
```python
|
|
787
|
+
# ✅ Recommended: Clear, descriptive names
|
|
788
|
+
semantic = SemanticStrategy(
|
|
789
|
+
name="CustomerSupportSemantics",
|
|
790
|
+
description="Extract semantic information from customer support conversations",
|
|
791
|
+
namespaces=["support/semantics/{actorId}/{sessionId}"]
|
|
792
|
+
)
|
|
793
|
+
|
|
794
|
+
# ❌ Avoid: Generic names
|
|
795
|
+
semantic = SemanticStrategy(name="Strategy1")
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
### 4. Handle Errors Gracefully
|
|
799
|
+
|
|
800
|
+
```python
|
|
801
|
+
# ✅ Recommended: Comprehensive error handling
|
|
802
|
+
def create_memory_safely(manager, name, strategies):
|
|
803
|
+
"""Create memory with proper error handling."""
|
|
804
|
+
try:
|
|
805
|
+
return manager.create_memory_and_wait(name=name, strategies=strategies)
|
|
806
|
+
except TimeoutError:
|
|
807
|
+
print(f"Memory creation timed out for {name}")
|
|
808
|
+
# Could check status and decide whether to wait longer
|
|
809
|
+
return None
|
|
810
|
+
except ClientError as e:
|
|
811
|
+
print(f"Failed to create memory {name}: {e}")
|
|
812
|
+
return None
|
|
813
|
+
```
|
|
814
|
+
|
|
815
|
+
### 5. Use Get-or-Create Pattern
|
|
816
|
+
|
|
817
|
+
```python
|
|
818
|
+
# ✅ Recommended: Use get_or_create for idempotent operations
|
|
819
|
+
memory = manager.get_or_create_memory(
|
|
820
|
+
name="PersistentMemory",
|
|
821
|
+
strategies=[SemanticStrategy(name="DefaultStrategy")]
|
|
822
|
+
)
|
|
823
|
+
|
|
824
|
+
# This is safe to call multiple times
|
|
825
|
+
```
|
|
826
|
+
|
|
827
|
+
### 6. Organize Strategies by Purpose
|
|
828
|
+
|
|
829
|
+
```python
|
|
830
|
+
# ✅ Recommended: Group related strategies
|
|
831
|
+
conversation_strategies = [
|
|
832
|
+
SemanticStrategy(
|
|
833
|
+
name="ConversationSemantics",
|
|
834
|
+
namespaces=["conversation/semantics/{actorId}/{sessionId}"]
|
|
835
|
+
),
|
|
836
|
+
SummaryStrategy(
|
|
837
|
+
name="ConversationSummary",
|
|
838
|
+
namespaces=["conversation/summaries/{actorId}/{sessionId}"]
|
|
839
|
+
)
|
|
840
|
+
]
|
|
841
|
+
|
|
842
|
+
user_strategies = [
|
|
843
|
+
UserPreferenceStrategy(
|
|
844
|
+
name="UserPreferences",
|
|
845
|
+
namespaces=["user/preferences/{actorId}"]
|
|
846
|
+
)
|
|
847
|
+
]
|
|
848
|
+
|
|
849
|
+
# Create separate memories or combine them
|
|
850
|
+
memory = manager.create_memory_and_wait(
|
|
851
|
+
name="ConversationMemory",
|
|
852
|
+
strategies=conversation_strategies + user_strategies
|
|
853
|
+
)
|
|
854
|
+
```
|
|
855
|
+
|
|
856
|
+
## Troubleshooting
|
|
857
|
+
|
|
858
|
+
### Common Issues and Solutions
|
|
859
|
+
|
|
860
|
+
#### 1. Memory Stuck in CREATING State
|
|
861
|
+
|
|
862
|
+
**Problem**: Memory remains in CREATING state and never becomes ACTIVE.
|
|
863
|
+
|
|
864
|
+
**Possible Causes:**
|
|
865
|
+
- Invalid strategy configuration
|
|
866
|
+
- Insufficient IAM permissions
|
|
867
|
+
- Resource limits exceeded
|
|
868
|
+
|
|
869
|
+
**Solutions:**
|
|
870
|
+
```python
|
|
871
|
+
# Check memory status and failure reason
|
|
872
|
+
try:
|
|
873
|
+
memory = manager.get_memory("mem-123")
|
|
874
|
+
if memory.status == "FAILED":
|
|
875
|
+
failure_reason = memory.get("failureReason", "Unknown")
|
|
876
|
+
print(f"Memory creation failed: {failure_reason}")
|
|
877
|
+
except ClientError as e:
|
|
878
|
+
print(f"Error retrieving memory: {e}")
|
|
879
|
+
|
|
880
|
+
# Use longer timeout for complex configurations
|
|
881
|
+
memory = manager.create_memory_and_wait(
|
|
882
|
+
name="ComplexMemory",
|
|
883
|
+
strategies=complex_strategies,
|
|
884
|
+
max_wait=600, # 10 minutes instead of default 5
|
|
885
|
+
poll_interval=30 # Check every 30 seconds
|
|
886
|
+
)
|
|
887
|
+
```
|
|
888
|
+
|
|
889
|
+
#### 2. Strategy Addition Fails
|
|
890
|
+
|
|
891
|
+
**Problem**: Adding strategies to existing memory fails.
|
|
892
|
+
|
|
893
|
+
**Possible Causes:**
|
|
894
|
+
- Memory not in ACTIVE state
|
|
895
|
+
- Invalid strategy configuration
|
|
896
|
+
- Conflicting strategy names
|
|
897
|
+
|
|
898
|
+
**Solutions:**
|
|
899
|
+
```python
|
|
900
|
+
# Always check memory status first
|
|
901
|
+
status = manager.get_memory_status("mem-123")
|
|
902
|
+
if status != "ACTIVE":
|
|
903
|
+
print(f"Memory is {status}, cannot add strategies")
|
|
904
|
+
return
|
|
905
|
+
|
|
906
|
+
# Use wait methods to handle state transitions
|
|
907
|
+
try:
|
|
908
|
+
memory = manager.add_strategy_and_wait(
|
|
909
|
+
memory_id="mem-123",
|
|
910
|
+
strategy=new_strategy,
|
|
911
|
+
max_wait=300
|
|
912
|
+
)
|
|
913
|
+
except TimeoutError:
|
|
914
|
+
print("Strategy addition timed out")
|
|
915
|
+
except RuntimeError as e:
|
|
916
|
+
print(f"Strategy addition failed: {e}")
|
|
917
|
+
```
|
|
918
|
+
|
|
919
|
+
#### 3. Permission Errors
|
|
920
|
+
|
|
921
|
+
**Problem**: Access denied errors when managing memories.
|
|
922
|
+
|
|
923
|
+
**Required IAM Permissions:**
|
|
924
|
+
```json
|
|
925
|
+
{
|
|
926
|
+
"Version": "2012-10-17",
|
|
927
|
+
"Statement": [
|
|
928
|
+
{
|
|
929
|
+
"Effect": "Allow",
|
|
930
|
+
"Action": [
|
|
931
|
+
"bedrock-agentcore-control:CreateMemory",
|
|
932
|
+
"bedrock-agentcore-control:GetMemory",
|
|
933
|
+
"bedrock-agentcore-control:ListMemories",
|
|
934
|
+
"bedrock-agentcore-control:UpdateMemory",
|
|
935
|
+
"bedrock-agentcore-control:DeleteMemory"
|
|
936
|
+
],
|
|
937
|
+
"Resource": "*"
|
|
938
|
+
}
|
|
939
|
+
]
|
|
940
|
+
}
|
|
941
|
+
```
|
|
942
|
+
|
|
943
|
+
#### 4. Region Configuration Issues
|
|
944
|
+
|
|
945
|
+
**Problem**: Resources not found or region mismatch errors.
|
|
946
|
+
|
|
947
|
+
**Solutions:**
|
|
948
|
+
```python
|
|
949
|
+
# Ensure consistent region configuration
|
|
950
|
+
import boto3
|
|
951
|
+
|
|
952
|
+
# Option 1: Explicit region
|
|
953
|
+
manager = MemoryManager(region_name="us-east-1")
|
|
954
|
+
|
|
955
|
+
# Option 2: Use session with region
|
|
956
|
+
session = boto3.Session(region_name="us-east-1")
|
|
957
|
+
manager = MemoryManager(boto3_session=session)
|
|
958
|
+
|
|
959
|
+
# Option 3: Check current region
|
|
960
|
+
session = boto3.Session()
|
|
961
|
+
print(f"Current region: {session.region_name}")
|
|
962
|
+
manager = MemoryManager(boto3_session=session)
|
|
963
|
+
```
|
|
964
|
+
|
|
965
|
+
#### 5. Strategy Configuration Validation Errors
|
|
966
|
+
|
|
967
|
+
**Problem**: Pydantic validation errors when creating typed strategies.
|
|
968
|
+
|
|
969
|
+
**Solutions:**
|
|
970
|
+
```python
|
|
971
|
+
from pydantic import ValidationError
|
|
972
|
+
|
|
973
|
+
try:
|
|
974
|
+
strategy = CustomSemanticStrategy(
|
|
975
|
+
name="TestStrategy",
|
|
976
|
+
extraction_config=ExtractionConfig(
|
|
977
|
+
append_to_prompt="Extract insights",
|
|
978
|
+
model_id="invalid-model-id" # This might cause validation error
|
|
979
|
+
),
|
|
980
|
+
consolidation_config=ConsolidationConfig(
|
|
981
|
+
append_to_prompt="Consolidate insights",
|
|
982
|
+
model_id="anthropic.claude-3-haiku-20240307-v1:0"
|
|
983
|
+
)
|
|
984
|
+
)
|
|
985
|
+
except ValidationError as e:
|
|
986
|
+
print(f"Strategy validation failed: {e}")
|
|
987
|
+
# Fix the configuration and try again
|
|
988
|
+
```
|
|
989
|
+
|
|
990
|
+
### Debugging Tips
|
|
991
|
+
|
|
992
|
+
#### Enable Debug Logging
|
|
993
|
+
|
|
994
|
+
```python
|
|
995
|
+
import logging
|
|
996
|
+
|
|
997
|
+
# Enable debug logging for MemoryManager
|
|
998
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
999
|
+
logger = logging.getLogger('bedrock_agentcore_starter_toolkit.operations.memory.manager')
|
|
1000
|
+
logger.setLevel(logging.DEBUG)
|
|
1001
|
+
|
|
1002
|
+
# Now all MemoryManager operations will show debug information
|
|
1003
|
+
manager = MemoryManager(region_name="us-east-1")
|
|
1004
|
+
```
|
|
1005
|
+
|
|
1006
|
+
#### Check Resource States
|
|
1007
|
+
|
|
1008
|
+
```python
|
|
1009
|
+
def debug_memory_state(manager, memory_id):
|
|
1010
|
+
"""Debug helper to check memory and strategy states."""
|
|
1011
|
+
try:
|
|
1012
|
+
memory = manager.get_memory(memory_id)
|
|
1013
|
+
print(f"Memory Status: {memory.status}")
|
|
1014
|
+
|
|
1015
|
+
strategies = manager.get_memory_strategies(memory_id)
|
|
1016
|
+
print(f"Number of strategies: {len(strategies)}")
|
|
1017
|
+
|
|
1018
|
+
for strategy in strategies:
|
|
1019
|
+
print(f" Strategy: {strategy.name}")
|
|
1020
|
+
print(f" ID: {strategy.strategyId}")
|
|
1021
|
+
print(f" Type: {strategy.get('type', 'N/A')}")
|
|
1022
|
+
print(f" Status: {strategy.get('status', 'N/A')}")
|
|
1023
|
+
|
|
1024
|
+
except Exception as e:
|
|
1025
|
+
print(f"Error debugging memory {memory_id}: {e}")
|
|
1026
|
+
|
|
1027
|
+
# Usage
|
|
1028
|
+
debug_memory_state(manager, "mem-123")
|
|
1029
|
+
```
|
|
1030
|
+
|
|
1031
|
+
#### Validate Strategy Configurations
|
|
1032
|
+
|
|
1033
|
+
```python
|
|
1034
|
+
def validate_strategy_config(strategy):
|
|
1035
|
+
"""Validate strategy configuration before use."""
|
|
1036
|
+
if isinstance(strategy, BaseStrategy):
|
|
1037
|
+
try:
|
|
1038
|
+
# This will trigger Pydantic validation
|
|
1039
|
+
strategy_dict = strategy.to_dict()
|
|
1040
|
+
print(f"Strategy {strategy.name} is valid")
|
|
1041
|
+
return True
|
|
1042
|
+
except Exception as e:
|
|
1043
|
+
print(f"Strategy {strategy.name} validation failed: {e}")
|
|
1044
|
+
return False
|
|
1045
|
+
else:
|
|
1046
|
+
print("Dictionary strategy - manual validation needed")
|
|
1047
|
+
return True
|
|
1048
|
+
|
|
1049
|
+
# Usage
|
|
1050
|
+
for strategy in strategies:
|
|
1051
|
+
validate_strategy_config(strategy)
|
|
1052
|
+
```
|
|
1053
|
+
|
|
1054
|
+
### Performance Considerations
|
|
1055
|
+
|
|
1056
|
+
#### Batch Operations
|
|
1057
|
+
|
|
1058
|
+
```python
|
|
1059
|
+
# ✅ Efficient: Batch multiple strategy operations
|
|
1060
|
+
memory = manager.update_memory_strategies_and_wait(
|
|
1061
|
+
memory_id="mem-123",
|
|
1062
|
+
add_strategies=[strategy1, strategy2, strategy3],
|
|
1063
|
+
modify_strategies=[modify_config1, modify_config2],
|
|
1064
|
+
delete_strategy_ids=["old-strat-1", "old-strat-2"]
|
|
1065
|
+
)
|
|
1066
|
+
|
|
1067
|
+
# ❌ Inefficient: Multiple individual operations
|
|
1068
|
+
manager.add_strategy_and_wait(memory_id, strategy1)
|
|
1069
|
+
manager.add_strategy_and_wait(memory_id, strategy2)
|
|
1070
|
+
manager.add_strategy_and_wait(memory_id, strategy3)
|
|
1071
|
+
```
|
|
1072
|
+
|
|
1073
|
+
#### Polling Configuration
|
|
1074
|
+
|
|
1075
|
+
```python
|
|
1076
|
+
# For production environments, consider longer intervals
|
|
1077
|
+
memory = manager.create_memory_and_wait(
|
|
1078
|
+
name="ProductionMemory",
|
|
1079
|
+
strategies=strategies,
|
|
1080
|
+
max_wait=900, # 10 minutes to wait longer
|
|
1081
|
+
poll_interval=60 # Check every minute to reduce API calls
|
|
1082
|
+
)
|
|
1083
|
+
|
|
1084
|
+
# For development, use shorter intervals for faster feedback
|
|
1085
|
+
memory = manager.create_memory_and_wait(
|
|
1086
|
+
name="DevMemory",
|
|
1087
|
+
strategies=strategies,
|
|
1088
|
+
max_wait=300, # 5 minutes
|
|
1089
|
+
poll_interval=10 # Check every 10 seconds
|
|
1090
|
+
)
|
|
1091
|
+
```
|
|
1092
|
+
|
|
1093
|
+
### Memory Limits and Quotas
|
|
1094
|
+
|
|
1095
|
+
Be aware of AWS service limits: Check [AWS Bedrock AgentCore documentation](https://docs.aws.amazon.com/bedrock-agentcore/) for limits
|
|
1096
|
+
|
|
1097
|
+
### Getting Help
|
|
1098
|
+
|
|
1099
|
+
If you encounter issues not covered in this guide:
|
|
1100
|
+
|
|
1101
|
+
1. **Check AWS CloudWatch Logs**: Look for detailed error messages
|
|
1102
|
+
2. **Review IAM Permissions**: Ensure all required permissions are granted
|
|
1103
|
+
3. **Validate Configurations**: Use the debugging helpers provided above
|
|
1104
|
+
4. **Check AWS Service Health**: Verify no ongoing service issues
|
|
1105
|
+
5. **Consult AWS Documentation**: For the latest API changes and limits
|
|
1106
|
+
|
|
1107
|
+
---
|
|
1108
|
+
|
|
1109
|
+
This comprehensive guide covers all aspects of using the MemoryManager effectively. The documentation includes detailed method references, practical examples, error handling patterns, best practices, and troubleshooting guidance to help you successfully implement memory management in your applications.
|