forked from awslabs/amazon-bedrock-agentcore-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_basic_agent.py
More file actions
executable file
·190 lines (150 loc) · 5.28 KB
/
test_basic_agent.py
File metadata and controls
executable file
·190 lines (150 loc) · 5.28 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
#!/usr/bin/env python3
"""
Basic Agent Test Script
This script tests the Basic Agent with simple conversational prompts.
The basic agent has no additional tools - just core Q&A capabilities.
Usage:
python test_basic_agent.py <agent_arn>
agent_arn: ARN of the basic agent runtime (required)
Examples:
# Test basic agent
python test_basic_agent.py arn:aws:bedrock-agentcore:<region>:123456789012:runtime/basic-agent-id
# From Terraform outputs
python test_basic_agent.py $(terraform output -raw agent_runtime_arn)
"""
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, test_name, prompt):
"""Test the agent with a given prompt
Args:
client: boto3 bedrock-agentcore client
agent_arn: ARN of the agent runtime
test_name: Name of the test for display
prompt: The prompt to send to the agent
Returns:
bool: True if test passed, False otherwise
"""
print(f"\n{'=' * 80}")
print(f"TEST: {test_name}")
print(f"{'=' * 80}\n")
print(f"Prompt: '{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)
print(f"\n✅ Response:\n{response_content}")
except json.JSONDecodeError:
print(f"\n✅ Response:\n{response_text}")
else:
print("\n⚠️ No response content received")
return True
except Exception as e:
print(f"\n❌ Error: {str(e)}")
return False
def main():
"""Main test execution function"""
if len(sys.argv) < 2:
print("Error: Missing required agent ARN argument")
print("\nUsage:")
print(f" {sys.argv[0]} <agent_arn>")
print("\nExample:")
print(
f" {sys.argv[0]} arn:aws:bedrock-agentcore:<region>:123456789012:runtime/agent-id"
)
print("\nOr from Terraform:")
print(f" {sys.argv[0]} $(terraform output -raw agent_runtime_arn)")
sys.exit(1)
agent_arn = sys.argv[1]
# Extract region from ARN
try:
region = extract_region_from_arn(agent_arn)
except ValueError as e:
print(f"\n❌ ERROR: {e}\n")
sys.exit(1)
print("=" * 80)
print("BASIC AGENT TEST SUITE")
print("=" * 80)
print(f"\nAgent ARN: {agent_arn}")
print(f"Region: {region}\n")
# Initialize boto3 client with extracted region
client = boto3.client("bedrock-agentcore", region_name=region)
# Test cases for basic agent (no tools, just Q&A)
tests = [
{
"name": "Simple Greeting",
"prompt": "Hello! Can you introduce yourself?",
},
{
"name": "Reasoning Task",
"prompt": "Explain what cloud computing is in simple terms and list three key benefits.",
},
]
# Run all tests
results = []
for test in tests:
passed = test_agent(client, agent_arn, test["name"], test["prompt"])
results.append({"name": test["name"], "passed": passed})
# Print summary
print(f"\n{'=' * 80}")
print("TEST SUMMARY")
print("=" * 80)
for result in results:
status = "✅ PASSED" if result["passed"] else "❌ FAILED"
print(f"{status} - {result['name']}")
# Overall result
passed_count = sum(1 for r in results if r["passed"])
total_count = len(results)
print(f"\n{'=' * 80}")
if passed_count == total_count:
print("✅ ALL TESTS PASSED")
else:
print(f"⚠️ {passed_count}/{total_count} TESTS PASSED")
print("=" * 80 + "\n")
# Exit with appropriate code
sys.exit(0 if passed_count == total_count else 1)
if __name__ == "__main__":
main()