Amazon Bedrock with Lambda Function

更新於 發佈於 閱讀時間約 6 分鐘

Continue from here

Using Amazon Bedrock from Boto3


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": "*"
}
]
}

The policy definition of the trust relationship is as follows:

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sts:AssumeRole"
],
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
}


Lambda

raw-image

Execution role

raw-image

First of all, you need to confirm the Boto3 version, otherwise you will encounter "errorMessage: "Unknown service: 'bedrock-runtime'".

raw-image

How to check

import json
import botocore
import boto3

def lambda_handler(event, context):

print('botocore vertion: {0}'.format(botocore.__version__))
print('boto3 vertion: {0}'.format(boto3.__version__))

The result is the old version

raw-image

What can I do?

Get the latest Boto3 library and create a zip file.

$ mkdir boto3
$ pip install -t ./boto3 boto3
$ mv ./boto3 ./python
$ zip -r boto3-1.28.57.zip ./python


Create Lambda Layer

raw-image

Upload a file and select a supported version

raw-image

Add Layer to Lambda Function

raw-image

Select the just created upload

raw-image

Retesting

raw-image


sample code

import boto3
import json

bedrock_runtime = boto3.client('bedrock-runtime')

def lambda_handler(event, context):

prompt = event.get('prompt')

modelId = 'ai21.j2-mid-v1'
accept = 'application/json'
contentType = 'application/json'

body = json.dumps({
"prompt": prompt,
"maxTokens": 100,
"temperature": 0.7,
"topP": 1,
})

response = bedrock_runtime.invoke_model(
modelId=modelId,
accept=accept,
contentType=contentType,
body=body
)

response_body = json.loads(response.get('body').read())

outputText = response_body.get('completions')[0].get('data').get('text')

print(outputText)

Enter the text you want to ask

raw-image
{
"prompt": "Highest mountain in the world?"
}


Results

raw-image

I got a good answer.


留言
avatar-img
留言分享你的想法!
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
看更多
你可能也想看
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
常常被朋友問「哪裡買的?」嗎?透過蝦皮分潤計畫,把日常購物的分享多加一個步驟,就能轉換成現金回饋。門檻低、申請簡單,特別適合學生與上班族,讓零碎時間也能創造小確幸。
Thumbnail
實驗目的:Lambda@Edge (L@E) 讓 CLoudFront (CF) 回源時,自動選擇較近的 S3 region 回源
Thumbnail
實驗目的:Lambda@Edge (L@E) 讓 CLoudFront (CF) 回源時,自動選擇較近的 S3 region 回源
Thumbnail
情境:想透過 IAM Role 的方式同時切換不同的帳號。 這邊以主帳號 "A" ,子帳號 "B" 為例。即在不重新登入的情況下,先登入A,然後利用 switch role的方式跳進B。
Thumbnail
情境:想透過 IAM Role 的方式同時切換不同的帳號。 這邊以主帳號 "A" ,子帳號 "B" 為例。即在不重新登入的情況下,先登入A,然後利用 switch role的方式跳進B。
Thumbnail
在沒有分環境之前,每一隻lambda只有一個code console給所有人一起編輯,開發好了就deploy,根據設定的trigger觸發執行。 現在我們希望能夠在code console開發,然後deploy到不同的stage,目標是不同stage的api gateway能夠調用該lambda的
Thumbnail
在沒有分環境之前,每一隻lambda只有一個code console給所有人一起編輯,開發好了就deploy,根據設定的trigger觸發執行。 現在我們希望能夠在code console開發,然後deploy到不同的stage,目標是不同stage的api gateway能夠調用該lambda的
Thumbnail
👨‍💻簡介 terraform在每次執行terraform plan或terraform apply時,是如何知道應該要管理哪些資源? 其實就是透過在每次執行terraform時,將建立或要變更的資源都記錄在terraform.state這份狀態檔,預設檔案使用JSON格式。
Thumbnail
👨‍💻簡介 terraform在每次執行terraform plan或terraform apply時,是如何知道應該要管理哪些資源? 其實就是透過在每次執行terraform時,將建立或要變更的資源都記錄在terraform.state這份狀態檔,預設檔案使用JSON格式。
Thumbnail
Prerequisites 建立 S3 bucket 設定 IAM role for OpenSearch (TheSnapshotRole) { "Version": "2012-10-17", "Statement": [{ "Action": [
Thumbnail
Prerequisites 建立 S3 bucket 設定 IAM role for OpenSearch (TheSnapshotRole) { "Version": "2012-10-17", "Statement": [{ "Action": [
Thumbnail
Continue from here Prerequisites Create an IAM role with access to Bedrock Create an "IAM Role" that defines the permissions needed to call Bed
Thumbnail
Continue from here Prerequisites Create an IAM role with access to Bedrock Create an "IAM Role" that defines the permissions needed to call Bed
Thumbnail
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.
Thumbnail
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.
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News