To call an Amazon Bedrock-provided model like Jurassic-2 or Claude using the AWS SDK or CLI, you’ll use the InvokeModel
API operation. This requires configuring the model ID, specifying input parameters, and handling the response format. Here’s how to do it step-by-step.
1. Prerequisites and Setup
First, ensure you have AWS credentials configured (via IAM roles, CLI configuration, or environment variables) with permissions to call Bedrock’s InvokeModel
API. Install the AWS SDK for your language (e.g., Python’s boto3
) or the AWS CLI. For some models, you may need to request access via the AWS Management Console before using them. For example, Anthropic’s Claude requires explicit enablement in the Bedrock console.
2. Using the AWS SDK (Python Example)
Here’s a Python example using boto3
to call Anthropic’s Claude model:
import boto3
import json
client = boto3.client('bedrock-runtime', region_name='us-east-1')
response = client.invoke_model(
modelId='anthropic.claude-v2',
contentType='application/json',
body=json.dumps({
"prompt": "Explain quantum computing in one sentence.",
"max_tokens_to_sample": 200,
"temperature": 0.5
})
)
result = json.loads(response['body'].read())
print(result['completion'])
Replace modelId
with the target model (e.g., ai21.j2-mid-v1
for Jurassic-2). The body
structure varies by model: Jurassic-2 uses prompt
and maxTokens
, while Claude uses prompt
and max_tokens_to_sample
.
3. Using the AWS CLI
For the CLI, use the aws bedrock-runtime invoke-model
command:
aws bedrock-runtime invoke-model \
--model-id anthropic.claude-v2 \
--body '{"prompt":"Explain quantum computing in one sentence.", "max_tokens_to_sample":200}' \
--cli-binary-format raw-in-base64-out \
--region us-east-1 \
output.json
cat output.json
The response is saved to output.json
. For Jurassic-2, adjust the model-id
and body parameters:
{"prompt":"Your text", "maxTokens":200, "temperature":0.5}
Key Notes
- Model IDs are specific (e.g.,
anthropic.claude-v2
,ai21.j2-ultra-v1
). Check AWS documentation for exact IDs. - Input formats differ slightly between models. Always refer to the model provider’s API documentation for parameter names and structure.
- Responses are returned as a streamed payload in the
body
field, which you’ll need to parse.