Generative AI models are often showcased inside interactive web playgrounds, but real-world enterprise engineering requires consuming foundational models programmatically through production-grade cloud APIs.
In this guide, you will build an automated document summarization script using Python, the boto3 AWS SDK, and Amazon Bedrock.
Prerequisites
Before starting, ensure you have:
- An active AWS Account.
- Python 3.9 or higher installed on your local machine.
- AWS CLI installed and configured locally (
aws configure). - Model access granted in the AWS Bedrock console for Anthropic Claude 3 Haiku or Amazon Titan Text.
Step 1: Request Model Access in AWS Bedrock
- Log into the AWS Management Console and search for Amazon Bedrock.
- In the left navigation pane, select Model access.
- Click Modify model access, enable Anthropic (Claude 3 Haiku), and submit the request.
- Access is usually granted automatically within a few minutes.
Step 2: Set Up Your Python Virtual Environment
Initialize a project directory and install the required AWS SDK dependencies:
mkdir bedrock-summarizer && cd bedrock-summarizer
python3 -m venv venv
source venv/bin/activate
pip install boto3
Step 3: Implement the Bedrock Summarization Script
Create a file named summarize.py and insert the following code:
import json
import boto3
from botocore.exceptions import BotoCoreError, ClientError
def summarize_document(text_content: str) -> str:
"""
Sends raw text to Amazon Bedrock Converse API and returns a summary.
"""
client = boto3.client(
service_name="bedrock-runtime",
region_name="us-east-1"
)
model_id = "anthropic.claude-3-haiku-20240307-v1:0"
messages = [
{
"role": "user",
"content": [
{
"text": f"Please provide a concise, executive summary of the following document:\n\n{text_content}"
}
]
}
]
try:
response = client.converse(
modelId=model_id,
messages=messages,
inferenceConfig={
"maxTokens": 500,
"temperature": 0.3
}
)
summary = response["output"]["message"]["content"][0]["text"]
return summary
except (BotoCoreError, ClientError) as error:
print(f"Error calling Amazon Bedrock: {error}")
return ""
if __name__ == "__main__":
sample_document = """
Cloud computing security involves the technologies, policies, controls, and services
that protect cloud data, applications, and infrastructure from threats. As organizations
transition workloads to cloud providers like AWS and Azure, traditional perimeter-based
security models are replaced by modern Identity and Access Management (IAM), zero-trust
architectures, and continuous audit logging. Security remains a shared responsibility
between the cloud service provider and the customer.
"""
print("--- Generating Summary via Amazon Bedrock ---")
result = summarize_document(sample_document)
print("\nSummary Output:\n")
print(result)
python summarize.py
--- Generating Summary via Amazon Bedrock ---
Summary Output:
Cloud computing security relies on modern solutions like IAM and zero-trust architectures to protect cloud workloads, moving away from legacy perimeter security. Security operates under a shared responsibility model between the cloud vendor and customer.
Top comments (0)