Monitoring and Caching OpenAI Requests with Cloudflare AI Gateway
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.
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.
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.
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