import boto3
from botocore.exceptions import ClientError
# 1. Initialize the bedrock-runtime client
# This establishes the secure link to the inference engines
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1' # Or your preferred Bedrock region
)
# 2. Define the model you want to use (e.g., Anthropic Claude 3.5 Sonnet)
model_id = "anthropic.claude-3-5-sonnet-20240620-v1:0"
# 3. Format the conversation structure
messages = [
{
"role": "user",
"content": [{"text": "What is the best way for a startup to scale database reads?"}]
}
]
try:
# 4. Invoke the model using the Converse API
response = bedrock_runtime.converse(
modelId=model_id,
messages=messages,
inferenceConfig={"maxTokens": 500, "temperature": 0.5}
)
# 5. Extract the text answer returned by the model
output_text = response['output']['message']['content'][0]['text']
print(output_text)
except ClientError as e:
print(f"ERROR: Can't invoke {model_id}. Reason: {e}")
No comments:
Post a Comment