- Newest
- Most votes
- Most comments
Based on your code and the error you're experiencing, there are a few issues that could be causing the problem:
-
API Usage: You're using
invoke_modelwith different request body formats, but the code might not be correctly formatted for each model. For Amazon Bedrock, it's recommended to use the newerconverseAPI for most models, especially when working with conversation-style interactions. -
Region Availability: You're using
eu-west-3for the bedrock-runtime client butus-east-1for listing inference profiles. Not all Bedrock models are available in all regions. Make sure the models you're trying to use are available ineu-west-3. -
Request Format: The request format for different models varies significantly. For example, the Anthropic Claude models require specific formatting with the
anthropic_versionparameter. -
Error Handling: Your code catches exceptions but doesn't provide detailed error information that might help diagnose the issue.
Here's a simplified approach using the recommended converse API for most modern models:
import boto3 from botocore.exceptions import ClientError # Create a Bedrock Runtime client client = boto3.client("bedrock-runtime", region_name="eu-west-3") # Set the model ID (try one of these models) model_id = "anthropic.claude-3-haiku-20240307-v1:0" # or another model ID # Define your question user_message = "Hello, how are you?" # Create conversation format conversation = [ { "role": "user", "content": [{"text": user_message}], } ] try: # Send the message using the converse API response = client.converse( modelId=model_id, messages=conversation, inferenceConfig={"maxTokens": 500, "temperature": 0.5}, ) # Extract and print the response text response_text = response["output"]["message"]["content"][0]["text"] print(response_text) except ClientError as e: print(f"ERROR: Can't invoke '{model_id}'. Reason: {e}")
If this still doesn't work, check:
- That your AWS account has access to the models you're trying to use
- That you have the necessary IAM permissions for Bedrock
- That the models are available in your chosen region
- The specific error messages to identify permission or configuration issues
Sources
Send and process a document with Llama on Amazon Bedrock - Amazon Bedrock
Invoke AI21 Labs Jurassic-2 on Amazon Bedrock using Bedrock's Converse API - Amazon Bedrock
answered a year ago
My requirements.txt:
torch==2.6.0 transformers==4.51.3 timm==1.0.15 PyMuPDF==1.25.5 Pillow==11.2.1 pytesseract opencv-python boto3>=1.34.0 botocore>=1.34.0 python-dotenv==1.1.0
answered a year ago
Relevant content
asked 4 months ago
asked 2 years ago
