Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding bedrock-lambda-nodejs #1782

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bedrock-lambda-nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.aws-sam
samconfig.toml
1 change: 1 addition & 0 deletions bedrock-lambda-nodejs/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v16.20.2
114 changes: 114 additions & 0 deletions bedrock-lambda-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Amazon Bedrock integration in Lambda (NodeJS Runtime)

This pattern shows how to make use of Amazon Bedrock in a Lambda function with NodeJS runtime.

Learn more about this pattern at Serverless Land Patterns: [https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-lambda-nodejs](https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-lambda-nodejs)

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

## Requirements

* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
1. Change directory to the pattern directory:
```
cd bedrock-lambda-nodejs
```
1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:
```
sam build
sam deploy --guided
```
1. During the prompts:
* Enter a stack name
* Enter the desired AWS Region
* Allow SAM CLI to create IAM roles with the required permissions.

Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.

1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.

## How it works

This pattern illustrates how to use the Amazon Bedrock Client for NodeJs.
The business logic is in <code>./src/index.js</code>.
You can use sample payloads stored in <code>./events</code> to test the function.

The following is the prompt used in the function and sent to Bedrock.

```
provide a summary of the following text in 5 bulletpoints;
extract 5 tags to categorize the article in a CMS;
provide output as a json object with properties :
"summary" as a list of bulletpoints and "tags" as a list of tags;
<text> YOUR TEXT HERE</text>
```

### Expected Output

```json
{
"summary": [
"Amazon Bedrock has been announced in preview in April.",
"A few days ago, it has been made general available.",
"Bedrock allows you to use foundational models for generative AI, totally managed.",
"It's the serverless generative AI service.",
"Bedrock is available in two regions."
],
"tags": [
"Amazon",
"Bedrock",
"Generative AI",
"Serverless"
]
}
```

## Testing
Invoke the Lambda function with the following command
```bash
aws lambda invoke \
--region ${AWS_REGION} \
--function-name ${FUNCTION_NAME} \
--cli-binary-format raw-in-base64-out \
--payload file://events/event-${EVENT_NUMBER}.json \
response.json
```

It will write the function response object to `./response.json`

| Variable | Description | Sample Value |
| --- | --- | --- |
| `${AWS_REGION}` | AWS Region in which your stack is deployed. | `us-west-2` |
| `${FUNCTION_NAME}` | Name of your function. You can find it in the exports of the stack. | `MyFunctionName-123` |
| `${EVENT_NUMBER}` | A number between 1 and 6 inclusive. This will select one of the 6 test files available in this repository. | 3 |

Print the content of `response.json` in a pretty format by running the following
```bash
node -e "console.log(JSON.parse(require('./response').body))"
```

You can create your own test event as follows
```json
{
"text":"Insert your text here"
}
```
## Cleanup

```bash
sam delete
```
----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
3 changes: 3 additions & 0 deletions bedrock-lambda-nodejs/events/event-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"text": " Hello world! And welcome to another episode of FooBar. I'm super excited. Finally, Bedrock is generally available. We can play with it. In this video, we are going to see what is Bedrock, very on top, because there is so many videos about it. But the interesting part that I want to show you is how you can connect Bedrock to your Lambda functions. So let's get started. So let's start by what is Bedrock. Amazon Bedrock has been announced in preview in April. And a few days ago, it has been made general available. That means that everybody can use it. However, it's only available in a few regions and with some limitations, we'll talk about those later. But now you can start building your applications. So the idea of Bedrock is that allows you to use foundational models for generative AI, totally managed. So it's the serverless generative AI service, and I'm so excited about it. I'm not a machine learning expert. So for me, SageMaker is cool, but it's too complicated. Bedrock is that thing I was looking for months. Now, We can build our applications and use Bedrock from an API perspective and get all the magic of generative AI in our applications. There is a few foundational models that are coming with Bedrock. So you can work with text and with images and more will be added in the future. So this is just the beginning. So let's go to the console and see a little bit of Bedrock in action. As I said, Bedrock is available in. Two regions is in North Virginia, us-east-1 and in it seems that now it's in Ohio, Oregon, Singapore, and nothing else. So let's go with Virginia because I'm there already and when you open from the console, you type Bedrock, it will take you here. So we can get started, but to get started with Bedrock, the first thing you need to do is to get access to the foundational models. So you are going to go to model access, and then here, you're going to select all the ones that you want to have access to, edit and boom, you select them all. So now I have Claude available and you will see that more models get available all the time. This means that now your account has access to those models based on IAM permission. So you just save the changes and that will now grant me access to cloud that I didn't have it before. You can see here, what is the different modality, text, embedding or images, and you can see from the different companies that are providing the models, what exact models you have available. So that's something to get started. After we get started, if you want, if you like to play with these, totally recommended, you can go to the chat and you can pick a model, for example, we can pick any model, and then you can start saying for example what is the color of the sky? I don't know. And it will answer something. The sky is typically blue. And why is the sky that color? And you have been using these chats forever. You know how this works. Then we have the image one, and I like this one a lot. It's using stable diffusion. And you can say give me an image that portrays a cat in the space, in space, wearing a bikini. I don't know. Let's see what it comes. And you can see that there is some inference configuration here in the side that you can tune. There is no bikini in this cat, but it's in space. And you can see how you can learn by clicking here. How you can tune. For example, if we want the input to have the prompt to have more strength, maybe this increasing this will get a better result of the bikini that the cat is wearing.Oh, now it's a kind of. I don't know if YouTube will like this image. So let's jump to the next one. And again, here you can see all the different models. You can see here that you can fine tune your own models, but I'm not getting there. That's out of my reach, but you can use any of the base models.So I will pick one of the models from here and I will say Something like, can you make a story about a cat in space wearing a bikini and its adventures With aliens. And again, here we have different configurations. These makes the randomness and the length of the response. So here you can see that the response is a trim. So we can increase these 2000 tokens. And this is important because different models have different tokens that they can handle. And you can see, for example, here that says Jumbo, Grande and Large support this amount of tokens and other models, this amount of tokens. So this is something you need to have in mind when working with these models as an API. You can see now that the story is a little bit longer. Once upon a time in a distant galaxy there was a god named Luna blah blah blah and it tells me all the story. You know how this works. Bedrock is very cool, but if you're watching this, you want to know how to use it. So Bedrock, as I said, it comes with support for embedding it into your applications as an API. So when you go to the documentation of Bedrock, you can see the different APIs operations that there are available for you. You can list all the models. You can get information about the models. You can run an inference. So this is the cool part. And there is two flavors of running the inference. The first one is to literally invoke the model in our request response manner. Or then you can invoke the model and get the response as a response stream. So if the model takes a while to return, you can start showing the response as it comes through. So I want to show you how you can invoke a model from a lambda function. Because that's what you're here for. So let's go to the code. And again, the code is available in GitHub for you to check it out if you want. We are going to use SAM but you could use CDK. You can use anything you want. Important things here. First, we are defining a function. This function, I will invoke it directly from the CLI. You can connect it to a API gateway if you want or whatever, but I want to show you the Bedrock integration. Second thing, until now, a Bedrock it's only available from boto3 so this means that applications using Python can use AWS SDK that in Python word is called boto3, don't ask me why. So for the first time in this channel, we are going to create a Python Lambda function. You asked for it and here it comes. So that's the first thing. The second thing is that you need to give permissions to your lambda function to access bedrock. So that's something you can do, you can be more specific and say here that you want to invoke the model, but that's important. The third consideration is up to the time that I'm recording the version of AWS SDK that is inside the Lambda function. It's not yet the one that has support for Bedrock. So it's important that you add in a requirements. txt file and you put this version, you should check in your maybe you're watching this in the future. If you're a Lambda function already has a Boto version over this number, then you don't need to do this step. But because by the time I'm recording, the version that is in Lambda is 1. 27 something and doesn't include Bedrock. So this will fail. So that's the other consideration. Good. So now we have defined our function. Very simple with access to bedrock, we have make it with Python, and then we have created the we have imported the dependency of the boto on version that we want. The next step is to write the function. And the cool thing here is that this body that you see here is something that the Bedrock Playground will give you. So when you're working with an API in the Bedrock Playroom, you can. View API request. And you can see here, all these parameters that are here that you put to test that call in the playground, and then you can make that exactly same API request. To the model from your application. So when you're working with this, it's very easy to configure. So if you have select the amount of tokens or things like that, these all comes in these in these prompts. So you can see here, for example the tokens, you can see here the temperature, you can see here and. All the configurations for the penalty of repetitions and so on and so forth. You can see here the prompt. You can see here the name of the model that you will need here. So all of these is very important. And you can see that the four parts, body, model ID, accept, and content type that we have here are exactly as well in the call to the API. So this is really good because you don't need to start digging in the documentation on how to call this particular model, because that's another caveat. Every model has a little different way of making the body and you need to be aware of that. So even though we are using bedrock as a centralized place to connect to multiple models, every model is a tiny bit different and has tiny different parameters and names, and this. Will help you to make sure that you are calling the the model in the right way. So you can see here that this is the inference configuration, but if we go to to, let's see if I have another one here, there is different names. So it's temperature, PK, maximum tokens, top sequence. So the name is a little bit different and the API request in the body will be a little different. So that's something very important. When you're working with Bedrock, you cannot just change the model from A21 to Cohere or to Antropofit because it just doesn't work. So be careful. So in this function what we are going to do, the first thing is to get that client for Bedrock and here if you don't have the right version, It will fail. So how to know if you're using the latest version, you can just make a function that prints the version in the screen, and then you can add all the things if you're using a version over the 1. 2857, then you don't need the requirements file. If not, then you need it. And what this simple function does it has a prompt that says to write a compelling YouTube description for a video, maximum two paragraph with good SEO that don't focus on the person. I will basically pass in the prompt at a whole transcribe for a video. And then it should have the description in the same language as the video it is. So I can pass my English and my Spanish videos, and then I need to have five titles and 20 tags for this video. So everything is comma separated values, but then everything is formatted in JSON. It's very specific prompt, but that's the type of prompt we want to do in our application. So then I can use this response. Later on for doing something else. So this is how I build my prompt. And then I pass this text. That comes from the event object, and this is the transcribe from a video. I build a body and then I make an invoke to that particular model. I pass the body, I pass the model ID, I pass the accept and the content time. And then I get something back. And then basically I print it in screen and return it to whoever wants to hear about it. So it's a very simple function, but this will enable you to do whatever you want. So let's invoke it. I have the invoke here, so let's bring it and you can see here I invoke it in the region with the function name and then make sure that you have this so it doesn't complain about base 64. I'm passing an event. json in the payload and this is just basically transcript of a video. So you can see that is quite a long thing, but that's my event file. And then I'm getting a response back basically. Let's see what happens. This takes a little bit to run and now it has finished running. And we can see in the response file, just not well parsed. I'm not a proficient in Python, but you can see here that this is JSON, it has description, API gateway is a fully managed service that makes it simple for developers to create, publish and maintain APIs at scale. In this video, I cover API gateway, what it does, and when you should use it, let's get started. That get started is extra. Titles, introduction to API gateway, how API gateway works. So I asked for five titles, API gateway for developers, build your first API, blah, blah, blah. And then tags, I asked for 20, Amazon API gateway API development, serverless, blah, blah, blah, blah, blah. And everything is kind of JSON. It just looks funny, but I'm pretty sure that's my fault on not understanding how to parse JSON with Python. And then you can check the logs. The logs are, pretty simple. We just call the function. We check the version of Boto. You can see it here. And then this is the input text that is very long, and then this is the response and you can add as many logs and print logs as you want. So one important thing, if you're using the requirements, you need to do sam build and then sam deploy, if you don't build, then you're going to use the version of AWS SDK that is in the Lambda function. So always sam build and then sam deploy to get this working. So that's another thing you have to have in mind. And yeah, that's it. You can find the code in GitHub. So that was the video for me today. I hope you like this short video. I want to show you this feature. It's a hot out of the oven and I just made this video. I hope I can show it to you with Node. js as always. As soon as it's available, I will make another video and more content related to Gen AI is coming in the future. Now that Bedrock is available, I have a lot of content. In the cooking machine. So I'm very excited. If you like this kind of Gen AI videos, I recommend you to check the video on Code Whisperer. That was the first Gen AI service that we announced that was generally available. It's a companion that will help you to improve your programming experience. And you can check here in this playlist all the announcements that we have in 2023 that are related to serverless that you might be interested. So I see you in another episode."
}
Loading
Loading