Testing Amazon Bedrock Chat with Python
Python script to test Amazon Bedrock chat using boto3
Published Oct 31, 2024
I'll help you create a simple Python script to test out Amazon Bedrock chat using boto3. If you are not a fan of Python, no worries - the boto3.client can be updated to work with different code, more on that here https://docs.aws.amazon.com/code-library/latest/ug/bedrock-runtime_code_examples.html
To use the code below, you'll need to:
- Make sure you have the required permissions and credentials set up for AWS:
- Configure AWS credentials using
aws configure
- Ensure your IAM role/user has appropriate Bedrock permissions
- Install the required package:
pip install boto3
Some other key features of this example:
- Separate functions for client setup, message creation, and chat handling
- Error handling for API calls
- Configurable parameters like region and model ID
- Support for different models available on Bedrock
You may also test different models or adjust different parameters, and monitor the results!
- The
region_name
insetup_bedrock_client()
- The
model_id
increate_chat_message()
- Parameters like
temperature
andmax_tokens_to_sample
in the request body
import boto3
import json
def setup_bedrock_client():
"""Initialize the Bedrock client with appropriate configuration"""
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1' # Replace with your desired region
)
return bedrock_runtime
def create_chat_message(prompt, model_id="anthropic.claude-v2"):
"""Create a chat message request for Bedrock"""
body = {
"prompt": f"\n\nHuman: {prompt}\n\nAssistant:",
"max_tokens_to_sample": 300,
"temperature": 0.7,
"top_p": 1,
}
return {
"body": json.dumps(body),
"modelId": model_id,
"contentType": "application/json",
"accept": "application/json",
}
def chat_with_bedrock(client, prompt):
"""Send a chat message to Bedrock and get the response"""
try:
# Prepare the request
request = create_chat_message(prompt)
# Invoke the model
response = client.invoke_model(**request)
# Parse and return the response
response_body = json.loads(response.get('body').read())
return response_body.get('completion', '').strip()
except Exception as e:
print(f"Error during Bedrock chat: {str(e)}")
return None
def main():
# Initialize the client
bedrock_client = setup_bedrock_client()
# Example usage
prompt = "What is the capital of France?"
response = chat_with_bedrock(bedrock_client, prompt)
if response:
print(f"Prompt: {prompt}")
print(f"Response: {response}")
if __name__ == "__main__":
main()