A2LRS - P3 - Thumbnail
Dec 20, 2023 4 min read

Getting Started with Amazon Bedrock

Amazon Bedrock is Amazon’s fully managed service that allows developers to build and scale Generative AI applications. The general availability for this service was announced on September 28, 2023. So how does it work? Amazon Bedrock has a variety of foundational models from AI21 Labs, Amazon, Anthropic, Cohere, and Stability AI that are exposed via the Bedrock APIs. To consume them, we need to interact with the Bedrock APIs and provide the data as per schema of the model.

bedrock-101-part2.jpeg

To explain in simple terms, Amazon Bedrock is the playstation console and the CDs are the different foundational models that you can interact with.

Accessing the Bedrock Foundational Models

Currently Amazon Bedrock is available in N.Virginia (us-east-1), Oregon (us-west-2), Singapore (ap-southeast-1), Tokyo (ap-northeast-1), Frankfurt (eu-central-1). Login to the AWS Console and select any one of the regions mentioned to get started.

bedrock-101-part2.png

By default the foundational models are disabled, to enable the required models, click on Model access on the left bottom section and enable the access to the models. The access grant usually takes a couple of minutes.

Using the Bedrock Playground

Once the access is granted, click on any one of the items in the playground section. In the screenshot below I am showing the sample of Image playground. After selecting the playground, select the Model from the dropdown. If the dropdown does not have any model, then the models that are accessible cannot be used for the playground. For example, Stable Diffusion XL Model can only be used on an image playground and will not be available in other playgrounds.

Now, enter the prompt and click on Run to execute the prompt against the selected base model. To view the parameters of the API call, click on View API request on the right corner. So what API is being called? The bedrock’s invoke_model API. Next we will see how this API can be invoked from an AWS Lambda. bedrock-101-part3.png

Side note: Speaking about Shelby GT 500 check out Shelby SAM, a package designed to make it easier to work with SAM Projects and cloudformation.

AWS Lambda and Amazon Bedrock

AWS Lambda, the heart of serverless, is easy to integrate with the AWS Ecosystem, which also makes it easy to integrate with Amazon Bedrock. Make sure you use the latest version of AWS SDK to access the Bedrock APIs. The sample lambda handler code below shows how to use Amazon Bedrock from an AWS Lambda for a python runtime.

import json, boto3
client = boto3.client("bedrock-runtime")
def lambda_handler(event, context):
response_body = client.invoke_model(
	body=json.dumps({"prompt": event.body, "max_tokens": 400}),
	modelId="cohere.command-text-v14",
	accept="application/json",
	contentType="application/json",
)
return response_body["completions"][0]["data"]["text"]

Import boto3 and then Initialise the bedrock runtime. In the example above, we are assigning it to client variable. Then, in the main handler function named lambda_handler, we invoke the bedrock api invoke_model with the parameters required for the model. Note that every model varies a little in the input schema. Read more about it here. In case you want a streaming response from bedrock, you need to use the invoke_model_with_response_stream API. Note that the model also needs to support streaming.

Pricing

There are 2 types of pricing, on-demand and provisioned throughput pricing. The price for both varies between different models. On a high level, with on demand pricing, you pay for the number of input and output tokens consumed and with provisioned throughput, you have hourly pricing. Provisioned throughput is not supported in all the models. Check out this Amazon Bedrock pricing page to know all the details. A word of caution, the image generation using Stability AI can quickly turn out to be very expensive, if you are planning on using it, keep an eye on the billing.

Conclusion

  • Amazon Bedrock is a fully managed service that offers a variety of Foundation Models (FMs) exposed through API for building AI applications.
  • AWS Lambda can easily work with Amazon Bedrock.
  • To use Amazon Bedrock with AWS Lambda, the lambda needs to be updated with the latest AWS SDK.
  • Keep an eye out for your AWS bill while experimenting with Amazon Bedrock

Innovate faster, and go farther with serverless-native application development. Explore limitless possibilities with AntStack's serverless solutions. Empowering your business to achieve your most audacious goals. Build with us.

Author(s)

Topics

Share this blog

Your Digital Journey deserves a great story.

Build one with us.

Blog Recommendation

Serverless Expectations vs Reality

4 min read

Feb 14, 2024

Feb 14, 2024

4 min read

Feb 14, 2024

Is all cloud computing serverless?

7 min read

Feb 9, 2024

Feb 9, 2024

7 min read

Feb 9, 2024

7 min read

Feb 6, 2024

7 min read

Feb 2, 2024

7 min read

Jan 22, 2024

Serverless Predictions for 2024

5 min read

Jan 17, 2024

Jan 17, 2024

5 min read

Jan 17, 2024

4 min read

Dec 27, 2023

cookie

These cookies are used to collect information about how you interact with this website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors on this website.

If you decline, your information won’t be tracked when you visit this website. A single cookie will be used in your browser to remember your preference not to be tracked.

Build With Us