Continue from here
Using Amazon Bedrock from Boto3
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"
]
}
}
]
}
Execution role
First of all, you need to confirm the Boto3 version, otherwise you will encounter "errorMessage: "Unknown service: 'bedrock-runtime'".
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
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
Upload a file and select a supported version
Add Layer to Lambda Function
Select the just created upload
Retesting
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
{
"prompt": "Highest mountain in the world?"
}
Results
I got a good answer.