Using Amazon Bedrock from Boto3

更新於 發佈於 閱讀時間約 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

留言
avatar-img
留言分享你的想法!
西尼亞ming-avatar-img
發文者
2023/09/30
Amazon Bedrock with Lambda Function提及了這篇文章,趕快過去看看吧!
西尼亞ming-avatar-img
發文者
2023/09/30
Amazon Bedrock - Build Generative AI service 提及了這篇文章,趕快過去看看吧!
avatar-img
西尼亞ming的沙龍
16會員
80內容數
西尼亞ming的沙龍的其他內容
2024/11/05
在ops-agent當中可以安裝 JVM 來收集 Java 參數,該工具主要收集 memory 以及 garbage collection 的參數,其中監控的選項有包含以下: jvm.classes.loaded jvm.gc.collections.count jvm.gc.collec
2024/11/05
在ops-agent當中可以安裝 JVM 來收集 Java 參數,該工具主要收集 memory 以及 garbage collection 的參數,其中監控的選項有包含以下: jvm.classes.loaded jvm.gc.collections.count jvm.gc.collec
2024/11/05
**Google Cloud 操作與 Audit Logs 說明** Google Cloud 作為純雲端服務提供商,所有操作均透過 HTTPS 網頁或 API 呼叫完成,並無「登入專案」的行為紀錄。不過,若使用 Google Workspace 管理使用者身份,則可以透過 Google
2024/11/05
**Google Cloud 操作與 Audit Logs 說明** Google Cloud 作為純雲端服務提供商,所有操作均透過 HTTPS 網頁或 API 呼叫完成,並無「登入專案」的行為紀錄。不過,若使用 Google Workspace 管理使用者身份,則可以透過 Google
2024/11/05
使用 Storage Transfer 服務是否能降低成本? Storage Transfer 的計價方式針對從 Cloud Storage 轉移至 Cloud Storage,除了一般費用外,Storage 移轉服務會使用 Cloud Storage rewrite 作業在 Cloud
2024/11/05
使用 Storage Transfer 服務是否能降低成本? Storage Transfer 的計價方式針對從 Cloud Storage 轉移至 Cloud Storage,除了一般費用外,Storage 移轉服務會使用 Cloud Storage rewrite 作業在 Cloud
看更多