In the previous post, I tried it immediately from the Bedrock console.
This time, I tried to use Bedrock from the programme, not from the console.Amazon Bedrock - Build Generative AI service
Prerequisites
Create an IAM role with access to Bedrock
Create an "IAM Role" that defines the permissions needed to call Bedrock API.
The policy for accessing permissions is defined as follows:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "bedrock:*",
"Resource": "*"
}
]
}
Use the Cloud9 IDE
Ensure the use of the latest Boto3

Python 3.9 or higher

The model used is Jurassic-2 Mid.
I enquired what the longest motorway in the USA was.

$ python
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> import json
>>> bedrock_runtime = boto3.client("bedrock-runtime", region_name="us-east-1")
>>>
>>> resp = bedrock_runtime.invoke_model(modelId="ai21.j2-mid-v1", body=json.dumps({"prompt":"What is America's longest highway?"}))
>>>
>>> json.loads(resp["body"].read())["completions"]
Running the model with Boto3 allows us to try all sorts of things!
Reference
https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html#api-using-sage
https://docs.aws.amazon.com/bedrock/latest/userguide/api-methods-list.html