![](https://crypto4nerd.com/wp-content/uploads/2024/01/1sHFhzQmIkNJpJOqtT1wpPA-1024x990.png)
Introduction:
Amazon Bedrock stands as a fully managed service, offering access to high-performing foundation models (FMs) developed by leading AI startups and Amazon. Through a unified API, these models provide a sturdy foundation for deploying machine learning at scale in the cloud. This post explores the seamless integration of Lambda functions, enabling efficient model invocation and establishing a flexible and cost-effective avenue for data processing and predictions. The following concise steps will guide you through using a Lambda function to invoke an AWS Bedrock Foundation Model (FM).
AWS Bedrock provides several powerful foundation models as below:
In today’s post, we are using FM “Jurassic-2 Ultra”.
- Navigate to the Bedrock console.
- Click “Model access” > “Manage model access.”
- Click “Save changes.”
- Wait a few minutes and refresh the page to see “Access granted.”
At here, you can go to playgrounds (Chat, text, image) checking out each model. I did play around for a while asking a “2-day trip to Guangzhou with an Aussie friend”.
- Ensure Lambda has necessary permissions by configuring IAM roles and policies.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"bedrock:InvokeModel",
"bedrock:ListFoundationModels"
],
"Resource": "*"
}
]
}
In the AWS Lambda console, create a new Lambda function with step 2 “bedrock-lambda-role”.
Below are my code snippet with lambda runtime python 3.9:
import json
import boto3# Bedrock client used to interact with APIs around models
bedrock = boto3.client(
service_name='bedrock',
region_name='us-east-1'
)
# Bedrock Runtime client used to invoke and question the models
bedrock_runtime = boto3.client(
service_name='bedrock-runtime',
region_name='us-east-1'
)
def lambda_handler(event, context):
# Just shows an example of how to retrieve information about available models
foundation_models = bedrock.list_foundation_models()
matching_model = next((model for model in foundation_models["modelSummaries"] if model.get("modelName") == "Jurassic-2 Ultra"), None)
prompt = json.loads(event.get("body")).get("input").get("question")
# The payload to be provided to Bedrock
body = json.dumps({
"prompt": prompt,
"maxTokens": 200,
"temperature": 0.7,
"topP": 1,
})
# The actual call to retrieve an answer from the model
response = bedrock_runtime.invoke_model(
body=body,
modelId=matching_model["modelId"],
accept='application/json',
contentType='application/json'
)
response_body = json.loads(response.get('body').read())
# The response from the model now mapped to the answer
answer = response_body.get('completions')[0].get('data').get('text')
return {
'statusCode': 200,
'body': json.dumps({"Answer": answer})
}
At here, if you run testing, you will see some errors like “Unknown service: bedrock runtime”.
This is because boto3 SDK is released prior to Bedrock service, so it doesn’t recognize this.
So we will need to update boto3 library as a lambda layer.
- Create a Lambda layer with the latest boto3 library.
- Zip the layer and upload it to Lambda for compatibility with Bedrock services.
mkdir ./bedrock-layer
cd ./bedrock-layer/# Create a python directory
mkdir ./python
# install boto3 library to target folder
pip3 install -t ./python/ boto3
# validate all library files
ls -l ./python/
# Zip all files name as bedrock-layer.zip
zip -r bedrock-layer.zip .
# validate
ls -ls
# use aws cli to publish this zip file
aws lambda publish-layer-version --layer-name bedrock-layer --zip-file fileb://bedrock-layer.zip
Then you will see the outputs similar to below:
Next we go to console to confirm:
Then we add this layer to our lambda function:
Now when we run test with example prompt “explain what is machine learning?”, I get results below:
The synergy between AWS Bedrock Models and Lambda functions offers a scalable and efficient solution for deploying machine learning models in a serverless environment. Follow these steps to seamlessly integrate your AWS Bedrock Models with Lambda functions, unlocking the potential for flexible and cost-effective machine learning workflows in the cloud.
References: