forked from awslabs/amazon-bedrock-agentcore-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_multi_agent.py
More file actions
234 lines (189 loc) · 7.35 KB
/
test_multi_agent.py
File metadata and controls
234 lines (189 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env python3
"""
Multi-Agent System Test Script
This script tests a multi-agent system with Agent-to-Agent (A2A) communication.
It can work with agents deployed via any method (Terraform, CloudFormation, CDK, manual).
Usage:
python test_multi_agent.py <orchestrator_arn> [specialist_arn]
orchestrator_arn: ARN of the orchestrator agent (required)
specialist_arn: ARN of the specialist agent (optional, for independent testing)
Examples:
# Test orchestrator with A2A communication
python test_multi_agent.py arn:aws:bedrock-agentcore:<region>:123456789012:runtime/orchestrator-id
# Test both agents independently
python test_multi_agent.py \\
arn:aws:bedrock-agentcore:<region>:123456789012:runtime/orchestrator-id \\
arn:aws:bedrock-agentcore:<region>:123456789012:runtime/specialist-id
"""
import boto3
import json
import sys
def extract_region_from_arn(arn):
"""Extract AWS region from agent runtime ARN.
ARN format: arn:aws:bedrock-agentcore:REGION:account:runtime/id
Args:
arn: Agent runtime ARN string
Returns:
str: AWS region code
Raises:
ValueError: If ARN format is invalid or region cannot be extracted
"""
try:
parts = arn.split(":")
if len(parts) < 4:
raise ValueError(
f"Invalid ARN format: {arn}\n"
f"Expected format: arn:aws:bedrock-agentcore:REGION:account:runtime/id"
)
region = parts[3]
if not region:
raise ValueError(
f"Region not found in ARN: {arn}\n"
f"Expected format: arn:aws:bedrock-agentcore:REGION:account:runtime/id"
)
return region
except IndexError:
raise ValueError(
f"Invalid ARN format: {arn}\n"
f"Expected format: arn:aws:bedrock-agentcore:REGION:account:runtime/id"
)
def test_agent(client, agent_arn, agent_name, prompt):
"""Test a single agent with a given prompt
Args:
client: boto3 bedrock-agentcore client
agent_arn: ARN of the agent runtime
agent_name: Name for display purposes
prompt: Test prompt to send
"""
print(f"\nPrompt: '{prompt}'")
print("-" * 80)
try:
response = client.invoke_agent_runtime(
agentRuntimeArn=agent_arn,
qualifier="DEFAULT",
payload=json.dumps({"prompt": prompt}),
)
print(f"Status: {response['ResponseMetadata']['HTTPStatusCode']}")
print(f"Content Type: {response.get('contentType', 'N/A')}")
# Read the streaming response body
response_text = ""
if "response" in response:
response_body = response["response"].read()
response_text = response_body.decode("utf-8")
if response_text:
try:
result = json.loads(response_text)
response_content = result.get("response", response_text)
# Truncate long responses for readability
if len(response_content) > 500:
print(f"\n✅ Response:\n{response_content[:500]}...")
print("\n[Response truncated for display]")
else:
print(f"\n✅ Response:\n{response_content}")
except json.JSONDecodeError:
if len(response_text) > 500:
print(f"\n✅ Response:\n{response_text[:500]}...")
else:
print(f"\n✅ Response:\n{response_text}")
else:
print("\n⚠️ No response content received")
return True
except Exception as e:
print(f"\n❌ Error testing {agent_name}: {e}")
return False
def test_multi_agent(orchestrator_arn, specialist_arn=None):
"""Test the multi-agent system
Args:
orchestrator_arn: Orchestrator agent runtime ARN (required)
specialist_arn: Specialist agent runtime ARN (optional)
"""
# Extract region from orchestrator ARN
try:
region = extract_region_from_arn(orchestrator_arn)
except ValueError as e:
print(f"\n❌ ERROR: {e}\n")
sys.exit(1)
print("\n" + "=" * 80)
print("MULTI-AGENT SYSTEM TEST")
print("=" * 80)
print(f"\nOrchestrator Agent ARN: {orchestrator_arn}")
if specialist_arn:
print(f"Specialist Agent ARN: {specialist_arn}")
else:
print("Specialist Agent: Not provided (will test Orchestrator only)")
print(f"Region: {region}")
# Create bedrock-agentcore client with extracted region
agentcore_client = boto3.client("bedrock-agentcore", region_name=region)
test_results = []
# Test 1: Simple query to Orchestrator
print("\n" + "=" * 80)
print("TEST 1: Simple Query (Orchestrator)")
print("=" * 80)
result = test_agent(
agentcore_client,
orchestrator_arn,
"Orchestrator",
"Hello! Can you introduce yourself and your capabilities?",
)
test_results.append(("Simple Query", result))
# Test 2: Complex query triggering A2A communication
print("\n" + "=" * 80)
print("TEST 2: Complex Query with A2A Communication")
print("=" * 80)
result = test_agent(
agentcore_client,
orchestrator_arn,
"Orchestrator",
"I need expert analysis. Please coordinate with the specialist agent to provide a comprehensive explanation of cloud computing architectures and best practices.",
)
test_results.append(("A2A Communication Test", result))
# Summary
print("\n" + "=" * 80)
print("TEST SUMMARY")
print("=" * 80)
all_passed = True
for test_name, passed in test_results:
status = "✅ PASSED" if passed else "❌ FAILED"
print(f"{status} - {test_name}")
if not passed:
all_passed = False
print("\n" + "=" * 80)
if all_passed:
print("✅ ALL TESTS PASSED")
else:
print("⚠️ SOME TESTS FAILED")
print("=" * 80 + "\n")
return all_passed
def main():
"""Main entry point"""
if len(sys.argv) < 2:
print(__doc__)
print("\n❌ ERROR: Agent runtime ARN is required")
print("\nTo get your agent ARN:")
print(" - Terraform: terraform output orchestrator_runtime_arn")
print(
" - CloudFormation: aws cloudformation describe-stacks --stack-name <stack> --query 'Stacks[0].Outputs'"
)
print(" - CDK: cdk deploy --outputs-file outputs.json")
print(" - Console: Check Bedrock Agent Core console")
sys.exit(1)
orchestrator_arn = sys.argv[1]
specialist_arn = sys.argv[2] if len(sys.argv) > 2 else None
# Validate ARN format
if not orchestrator_arn.startswith("arn:aws:bedrock-agentcore:"):
print(f"\n❌ ERROR: Invalid ARN format for orchestrator: {orchestrator_arn}")
print(
"Expected format: arn:aws:bedrock-agentcore:region:account:runtime/runtime-id"
)
sys.exit(1)
if specialist_arn and not specialist_arn.startswith("arn:aws:bedrock-agentcore:"):
print(f"\n❌ ERROR: Invalid ARN format for specialist: {specialist_arn}")
print(
"Expected format: arn:aws:bedrock-agentcore:region:account:runtime/runtime-id"
)
sys.exit(1)
# Run tests
success = test_multi_agent(orchestrator_arn, specialist_arn)
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()