How to use Mistral on AWS Bedrock
Quick answer
Use the
boto3 bedrock-runtime client to call Mistral models on AWS Bedrock by specifying the Mistral model ID in the modelId parameter. Send your prompt as a message in the converse or invoke_model API call and parse the response to get the generated text.PREREQUISITES
Python 3.8+AWS account with Bedrock accessAWS CLI configured or AWS credentials setpip install boto3
Setup
Install the boto3 library and configure your AWS credentials with Bedrock permissions. Ensure your AWS CLI is configured or environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set.
pip install boto3 Step by step
This example demonstrates how to invoke a Mistral model on AWS Bedrock using boto3. Replace mistral-large-latest with the exact Bedrock model ID for Mistral if different.
import boto3
import json
# Initialize Bedrock runtime client
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# Define the Mistral model ID on Bedrock
model_id = 'amazon.mistral-large-latest' # Confirm exact model ID in your AWS Bedrock console
# Prepare the input message
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "Explain the benefits of using Mistral on AWS Bedrock."}
]
}
]
# Call the converse API
response = client.converse(
modelId=model_id,
messages=messages,
maxTokens=512
)
# Extract the generated text
output_message = response['messages'][-1]['content'][0]['text']
print("Mistral response:", output_message) output
Mistral response: Mistral models on AWS Bedrock provide scalable, low-latency access to advanced language capabilities, enabling seamless integration into your applications with secure and managed infrastructure.
Common variations
- Use
invoke_modelinstead ofconversefor raw JSON input/output. - Adjust
maxTokensand other parameters liketemperaturefor generation control. - Use async calls with
aiobotocorefor asynchronous integration. - Switch region if your Bedrock endpoint is in a different AWS region.
import boto3
import json
client = boto3.client('bedrock-runtime', region_name='us-east-1')
model_id = 'amazon.mistral-large-latest'
body = json.dumps({
"input": "Summarize the key features of AWS Bedrock.",
"max_tokens": 256,
"temperature": 0.7
})
response = client.invoke_model(
modelId=model_id,
body=body
)
result = json.loads(response['body'])
print("Summary:", result.get('generated_text', 'No text returned')) output
Summary: AWS Bedrock offers managed access to foundation models like Mistral, enabling developers to build AI-powered applications without managing infrastructure.
Troubleshooting
- If you get
AccessDeniedException, verify your IAM permissions include Bedrock access. ModelNotFoundExceptionmeans the model ID is incorrect or not available in your region.- Timeouts may require increasing client timeout or checking network connectivity.
- Check AWS region matches where Bedrock is enabled for your account.
Key Takeaways
- Use boto3's bedrock-runtime client to call Mistral models on AWS Bedrock.
- Specify the correct Mistral model ID and region for your Bedrock deployment.
- Handle responses by parsing the JSON messages array for generated text.
- Adjust generation parameters like maxTokens and temperature for customization.
- Ensure AWS credentials and IAM permissions allow Bedrock model invocation.