To call an Amazon Bedrock-provided model like Jurassic-2 or Anthropic’s Claude using the AWS SDK or CLI, you’ll use the Bedrock Runtime service. First, ensure you have access to Bedrock in your AWS account and that the specific model is enabled. Bedrock provides a unified API for interacting with different foundation models, so the core steps are similar across models, though input/output formats may vary. You’ll need the AWS SDK installed (e.g., Boto3 for Python) or the AWS CLI configured with credentials, and you must target a region where Bedrock is available, such as us-east-1
.
Using the AWS SDK, start by initializing the Bedrock Runtime client. For example, in Python with Boto3, you’d create a client with boto3.client('bedrock-runtime')
. Next, construct a request body formatted for the target model. Claude requires a JSON payload with a prompt
field and parameters like max_tokens_to_sample
, while Jurassic-2 uses a promptText
field and parameters like temperature
. For instance, calling Claude might look like:
import boto3
client = boto3.client('bedrock-runtime', region_name='us-east-1')
response = client.invoke_model(
modelId='anthropic.claude-v2',
body=json.dumps({
"prompt": "Hello, how are you?",
"max_tokens_to_sample": 300
}),
contentType='application/json'
)
result = json.loads(response['body'].read())
print(result['completion'])
This sends a request to Claude and parses the generated text from the response.
For the AWS CLI, use the aws bedrock-runtime invoke-model
command. Specify the model ID, input file containing the JSON payload, and an output file. For example, to invoke Jurassic-2:
aws bedrock-runtime invoke-model \
--model-id 'ai21.j2-mid-v1' \
--body '{"promptText":"Explain quantum computing", "maxTokens":200}' \
--cli-binary-format raw-in-base64-out \
--region us-east-1 \
output.json
The response is saved to output.json
, which you can parse for the generated text. Note that CLI input must be properly escaped or loaded from a file. Always check the model’s documentation for required parameters and response formats, as they differ between providers. Proper error handling and retries are recommended for production use.
Zilliz Cloud is a managed vector database built on Milvus perfect for building GenAI applications.
Try FreeLike the article? Spread the word