-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_bedrock.py
More file actions
72 lines (59 loc) · 2.12 KB
/
test_bedrock.py
File metadata and controls
72 lines (59 loc) · 2.12 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
#!/usr/bin/env python3
"""Simple test script to verify Bedrock connection."""
import os
import sys
import boto3
import json
from botocore.config import Config
# Set environment variables
os.environ['AWS_REGION'] = 'us-west-2'
os.environ['AWS_DEFAULT_REGION'] = 'us-west-2'
def test_bedrock_connection():
"""Test Bedrock connection with correct inference profile ID."""
try:
# Initialize Bedrock client
client = boto3.client(
'bedrock-runtime',
region_name='us-west-2',
config=Config(
retries={'max_attempts': 3},
connect_timeout=30,
)
)
print("✅ Bedrock client initialized successfully")
# Test model invocation with inference profile ID
model_id = "global.anthropic.claude-sonnet-4-5-20250929-v1:0"
# Simple test prompt
prompt = "Hello, please respond with 'Connection successful!'"
# Prepare request body for Claude 3.7 Sonnet
request_body = {
"messages": [
{
"role": "user",
"content": prompt
}
],
"max_tokens": 100,
"temperature": 0.1,
"anthropic_version": "bedrock-2023-05-31"
}
# Invoke model
response = client.invoke_model(
modelId=model_id,
body=json.dumps(request_body),
contentType='application/json',
accept='application/json'
)
# Parse response
response_body = json.loads(response['body'].read())
print("✅ Model invocation successful")
print(f"✅ Model ID: {model_id}")
print(f"✅ Response: {response_body.get('content', [{}])[0].get('text', 'No response text')}")
print("✅ Bedrock connection test passed!")
return True
except Exception as e:
print(f"❌ Bedrock connection test failed: {str(e)}")
return False
if __name__ == "__main__":
success = test_bedrock_connection()
sys.exit(0 if success else 1)