How to use Titan on AWS Bedrock
Quick answer
Use the
boto3 bedrock-runtime client to call the Titan model on AWS Bedrock by specifying modelId="amazon.titan-text-express-v1" in the converse or invoke_model method. Pass your chat messages as a list of role-content dictionaries in the request body to get Titan's responses.PREREQUISITES
Python 3.8+AWS account with Bedrock accessAWS CLI configured with credentialspip install boto3
Setup
Install the boto3 library and configure your AWS credentials with Bedrock permissions. Ensure your AWS CLI is set up with aws configure or environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Bedrock is available in specific AWS regions; set your region accordingly.
pip install boto3 Step by step
This example shows how to send a chat message to the Titan model amazon.titan-text-express-v1 using the boto3 Bedrock runtime client. It uses the converse method with a properly formatted message list.
import boto3
import json
# Initialize Bedrock runtime client
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# Define the Titan model ID
model_id = 'amazon.titan-text-express-v1'
# Prepare chat messages
messages = [
{"role": "user", "content": [{"type": "text", "text": "Hello Titan, how do I use you on AWS Bedrock?"}]}
]
# Call the converse API
response = client.converse(
modelId=model_id,
messages=messages,
maxTokens=100
)
# Extract and print the response text
output_message = response['messages'][0]['content'][0]['text']
print("Titan response:", output_message) output
Titan response: Hello! You can use me on AWS Bedrock by calling the Bedrock runtime API with modelId 'amazon.titan-text-express-v1' and sending your chat messages in the request.
Common variations
- Use
invoke_modelmethod with a JSON body for more control over parameters. - Adjust
maxTokens,temperature, and other parameters in the request body. - Use asynchronous calls with
aiobotocorefor concurrency. - Change region if your Bedrock deployment is in a different AWS region.
import boto3
import json
client = boto3.client('bedrock-runtime', region_name='us-east-1')
model_id = 'amazon.titan-text-express-v1'
body = json.dumps({
"max_tokens": 100,
"temperature": 0.7,
"messages": [
{"role": "user", "content": [{"type": "text", "text": "Explain AWS Bedrock Titan usage."}]}
]
})
response = client.invoke_model(
modelId=model_id,
body=body
)
response_body = json.loads(response['body'].read())
print("Titan response:", response_body['messages'][0]['content'][0]['text']) output
Titan response: AWS Bedrock allows you to access Titan by invoking the model with your chat messages and parameters via the Bedrock runtime API.
Troubleshooting
- If you get
AccessDeniedException, verify your IAM permissions include Bedrock access. - If
ModelNotFoundExceptionoccurs, confirm the model IDamazon.titan-text-express-v1is correct and available in your region. - For
InvalidParameterException, check your message format matches Bedrock API requirements. - Ensure your AWS region supports Bedrock; currently,
us-east-1is commonly supported.
Key Takeaways
- Use boto3's bedrock-runtime client with modelId 'amazon.titan-text-express-v1' to call Titan on AWS Bedrock.
- Pass chat messages as a list of role-content dicts with type 'text' in the request body.
- Configure AWS credentials and region properly to avoid access and region errors.