Using Amazon Bedrock from Boto3

2023/09/30閱讀時間約 3 分鐘


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

raw-image

Python 3.9 or higher

raw-image

The model used is Jurassic-2 Mid.

I enquired what the longest motorway in the USA was.

raw-image
$ 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

13會員
57內容數
留言0
查看全部
發表第一個留言支持創作者!