Hello guys.
We are going to continue our generative AI on AWS Cloud series.
And in this specific video, we are going to develop an amazing end to end generative AI application,
uh, using various services that are present in AWS cloud.
I will give you a brief architecture, what we are going to implement.
And then step by step, I will be showing you how you can use various services that are present in AWS
and implement some amazing generative AI applications.
So what we are going to probably do in this specific use case, uh, this use case, I'm just going
to keep it as uh, block generation.
So here you can probably see block generation.
Is that uh, initially what we will do is that our main aim over here will be to create an API.
Okay.
So we will try to create an API.
So we are going to use postman to hit this specific API with some specific body.
Uh that body can include query.
Okay.
Query from the user.
Like what blocks we need to probably generate.
And as you know, in AWS, uh, in order to create APIs, we use Amazon API gateway.
So we are going to also use this.
As soon as we trigger this particular API, it is going to hit our Lambda function.
What exactly is the Lambda function will also understand about that.
This lambda function in turn will be interacting with Amazon bedrock.
Okay, now if you don't know about Amazon bedrock, here are all the foundation models will be available.
Right.
Foundation models will be available.
Foundational models will be available.
Like uh, Amazon has lot of different different foundation models like uh la la la la la to you know,
it also has paid from cloud anthropic specifically cloudy models and all are there.
We'll go ahead and see what are all these kind of fine ocean models and how we can specifically use
that.
And this lambda function will be responsible taking up the query, hitting this particular foundation
model and getting back the response once we get the response in this particular case will be our entire
blog, the 200 words or 300 blog blogs that we are probably going to create.
And finally, what we are going to do is that we are going to take that specific block and save it in
AWS, S3 in the form of text file or any kind of files.
Okay.
We can also convert that into a PDF file and save it in Amazon S3 with the recent timestamp.
Okay.
So this is the entire thing that we are going to implement and understand.
This kind of use cases is even available in AWS documentation.
Uh, and many, many companies are implementing the generative AI application in this way.
As we go ahead with respect to the series, in the future, we will also be developing some amazing
applications that are related to Rag.
You know, different document Q&A, how we can actually use vector databases.
Uh, I will be showing you all those things, and we will try to use all the ecosystem that is provided
by AWS cloud.
One of the important, uh, you can probably consider that in most of the companies.
They are specifically asking this in interviews.
They definitely want you to say that how you can probably implement anything in AWS cloud with respect
to the generative AI application.
So, uh, so many different things.
Uh, we are going to go ahead and see.
So first of all, uh, let's go back to your AWS account.
So initially you really need to have an AWS account and make sure that you have it.
Then only you'll be able to use all those services.
Now, first of all, what I'm actually going to do is that I'm going to go to AWS bedrock and just give
you an idea like how does all the foundation model looks like and what our foundation model is specifically
provided by Amazon bedrock.
Okay.
So first I will go ahead and click this.
And here you can probably see that my Amazon Bedrock will get loaded.
Once it is getting loaded you can probably go ahead and click on Get Started.
Now with respect to this you can see over here foundation models are specifically over there.
And you can actually do chat text image.
So uh it supports multiple models itself.
Now which all companies model is basically there in this foundation model.
Here you have a 1 to 1 labs that's from Jurassic to series.
Uh you have Titan by Amazon, you have cloud by anthropic, you have command by cohere, you have llama
three, you have Mistral, you have stable diffusion.
So you can actually work in images also.
And you can generate your own image as you want, right.
Let's say if I really want to use llama three.
So I will go ahead and see this here we can probably see all these things are there like llama 213 B,
llama two chat, 13 B, and even llama three is also there.
I will also show you that you can also go ahead and check it out in the playground, chat with it and
probably get it now.
The first step, uh, before we start all these things right, our first step is specifically to make
sure that we have to take the model access, understand with respect to different, different region.
Yeah.
You will be having the access of different different models.
So you have to request for it okay.
Here you can see that in my in my region that is I have selected us East one.
You'll be able to see that I have granted access to all these models.
Anthropic cloud three models are not given because for this you really need to provide some use case,
uh, details.
Uh, and then you have to submit it.
Then the anthropic will give you the access itself, like what kind of use cases you are basically doing.
But according to mine, I have already, you can probably see that I have got this access granted,
in order to get this access, you just click on Manage Model Access, select which all model, which
all models you specifically want as access.
And just go ahead and click on Save Changes.
Okay I've already done that.
So here you can also see that the Save Changes button is over here.
So here you can probably see save changes is there.
You can go ahead and probably click on this whatever models you specifically want.
Just go ahead and check it out.
And then in some time you will be getting the excess model okay.
The model excess will be granted.
So this is the first step that you really need to do okay.
Now once you specifically do it uh, then I will show you how you can go ahead and invoke all these
counter foundation models and how you can go ahead.
Okay.
So this was one of the services that we are going to use.
The next services is specifically lambda function.
What exactly is lambda function will try to understand.
So yeah here I will go ahead and search for AWS Lambda.
So here you can see what is Lambda console in AWS.
Uh quickly to test an event.
It is a serverless to manage application in.
Okay, I will just go ahead and search what is AWS Lambda?
Okay.
So if you probably go ahead and see the definition with respect to AWS Lambda, um, it's saying that
run code without thinking about servers or clusters.
See, instead of AWS Lambda, I can also use a EC2 instance in AWS and then probably set up all the
things, install all my requirements over there, set up all the libraries, develop my entire application,
and then probably with the help of API gateway, I can probably interact with that.
And within that EC2 instance, I will try to invoke my AWS bedrock that also you can probably do.
And in the going upcoming examples, I'll also show you that if you want.
But right now we are going to use AWS Lambda.
So the AWS Lambda is a compute service that runs your code in response to an event.
So suppose if I hit an API this is going to get triggered And then it will go ahead and call the AWS
bedrock.
So the benefits of Lambda is that no need of managing servers.
It is automatically scaling.
And you have to probably pay as you go like based on the number of requests.
And then performance optimization will also be taking place in the AWS Lambda itself.
Uh, many, many companies specifically use that.
If you have a smaller application, you can definitely develop this.
Um, so let's go ahead and let's try to see that how we can go ahead and work with AWS Lambda.
So here you can probably see AWS Lambda.
As soon as I click on AWS Lambda this will get opened.
Here you can create multiple functions.
I have already created one function.
I will show you, uh, how to create this.
So let's go ahead and create the function over here.
So as soon as I probably create the function here you can see I can select author from scratch.
Use a blueprint container, image, whatever things you specifically want here.
What I'm actually going to do is that I'm going to give my function name and my function name.
What I can do over here is let's say I'll give my AWS app okay with bedrock.
Okay.
So this will basically be my AWS function, uh, function name, uh lambda function name, uh runtime
I will go ahead and use Python 3.12, uh, just to make sure that I have the recent updated Python.
And along with that, whatever by default is there.
You can go ahead and select that.
But here I'll just keep it since we are developing it for the first time.
And I'll go ahead and click on create function.
As soon as I probably create the function here, you'll be able to see that my AWS app bedrock uh,
Lambda function will get created.
And again, uh, this takes time, some amount of time based on the internet speed and the kind of application
that you are developing.
So here you can see that it has got created.
It is bedrock.
I will show you.
Here are some options like layers add triggers, add destination.
Now see, my main aim is that as soon as I probably create a API gateway, right?
Because I need to hit the API, then only this, uh function should get triggered.
Right inside this function, I will write my entire code how to invoke the bedrock AWS Bedrock Foundation
models and.
All right, so if you go down here, you will be also able to see some amazing things like code test
monitor configuration analysis version.
So if I go ahead and click on configuration here you can see there is some timeout configuration that
you can probably give.
There are some environment variables.
If you want to probably give.
You can also give it over here directly.
Environment variables I hope I've shown you in VS code we create a dot env file.
We create a key like open a I api key and all here.
Also you can probably write it down and you can call that open API key in this particular code.
So one general configuration.
What I will do is that I'll keep this timeout to three minutes.
Because just to keep it in the safer side so that it does not get, um, hampered over there.
And here I'm going to write my entire code.
Okay.
Now before I write my entire code, uh, whatever code I'm going to write, I'll be writing over here.
But again, this does not look very comfortable writing the code because here I will not be able to
get much, uh, you know, suggestion with respect to the code, it is better that I write this entire
code in the, uh, in my VS code, and then I'll copy and paste it over here.
Okay.
So what code?
I'm going to write it down over here.
I'll be showing you that specific code with respect to this okay.
So let me just go ahead and open my VS code over here.
And here is one of my I will just open my terminal okay.
Oops.
Let me just go ahead and open my terminal.
And I'm here I'm going to write my code completely okay.
Uh command prompt.
So first of all I will just go ahead and create my environment.
So let me just go ahead and write conda create minus p v and v environment Python.
As you know I have taken 3.12 okay.
So this is the environment that I'm going to create because I'm going to write my entire code over here
with some requirement dot txt and all.
So let me just create this.
I'll create one file which is called as requirements dot txt.
So whatever is required uh, for my project and that I have to actually put it in my lambda function
all.
I will be writing it over here.
Okay so requirement dot txt.
Let's wait till this entire is getting created.
Uh, so here you can see a new version of this.
This and I will go just go ahead and click on yes.
And automatically my entire Conda environment will get created.
So there are two important things that I'll do.
First, let me go ahead and write the code for this.
What all things I actually require I'll be writing it.
Let me create a file also app.py okay and requirement dot txt.
The first library I actually want is nothing but boto3, so I'll keep this boto3 like this.
And whatever is the recent version of the Boto3 that is only supported by lambda function.
Okay, not the previous one because the recent version of boto three has lot of lots of functionalities
to invoke foundation models from AWS bedrock.
Okay, so you should know all these things.
Okay, so let me quickly go ahead and write my code now over here.
So first of all I will go ahead and write import Boto three.
And I'm just going to write import boto core dot config.
Okay so this is two important libraries that we specifically use.
Understand boto three is used to invoke the foundation model okay.
So here I will just go ahead and do one thing okay.
So let me just go ahead and see.
This is my Venv I will just go ahead and activate.
So conda activate V and V along with this what I'm going to do I'm going to just write pip install minus
r requirements.txt so that my boto3 gets installed at any point of time.
Okay.
So this installation will take place.
Let it take place.
Till then I will go ahead and start writing my code.
Okay.
Now what I'm actually going to do.
My main aim is to create a blog, right?
I really need to write a blog.
I need to create a blog.
And for that, uh Uh, what?
All things I will be doing, I'll be showing you step by step.
Uh, let me just do one thing.
I will keep my notepad over here.
The reason why I'm keeping the notepad so that, uh, uh, step by step, uh, I have from the documentation,
I've, I've taken all the steps, what is basically required.
And, uh, when we have to create what, what services needs to be created and all, I will be writing
all those things.
right?
So, uh, I will create a function which will basically be called as definition blog underscore generate
using bedrock.
Okay.
So this will basically be my function name.
And uh when I create this function I will also use this I will uh give my query or message or what blog
topic.
I can also basically say blog topic, blog topic and this will basically be a string.
And uh, this will also return a string.
Okay.
This will also return a string.
Right.
So this will basically be my function which will generate a blog here.
Uh, what kind of blog I'm going to use uh, that I will be discussing.
Sorry, what kind of foundation model that I will be using.
So first of all, let me just go ahead and create all my prompts since I'm going to create a multi line
character.
So I will go ahead and write this and create a blind blog okay.
Over here okay.
So sorry.
My prompt I'll write my prompt.
And before I write my prompt uh let's go and see which Amazon Bedrock uh model I will be specifically
using.
Llama two chat 13 b I can specifically go with this.
And here you can probably see this is my model name okay.
Model ID okay.
You you you really need to remember this because these are all the things that I really need to give.
The content type will be application, slash, JSON and body.
This is the most important thing.
What is the prompt that I am going to probably put over here?
What are the other information that you basically provide with respect to the body?
Right.
So this all information definitely you really need to provide.
Similarly if you go with llama two chat uh uh llama two chat uh, 70 billion parameters.
So here you can see this is where your, uh, this is where you place your input text.
Okay.
And similarly if you go ahead and explore llama 213 billion, you can also see other information with
respect to.
So each and every model how the API request should be.
You should be able to get it from the AWS bedrock itself.
Okay.
So all the information are specifically over here and you can basically check it out, right?
So whatever model you want to use okay.
You have to make sure that what body what API request is specifically there.
Based on that only you have to probably create everything.
Okay.
Now what I'm actually going to do is that I'm going to create my prompt.
Okay.
Now in this specific prompt I'm going to basically write.
an S character and let me just go ahead and provide like this s.
So the first thing is that I will be using this keyword inst.
As you all know, in llama two models we specifically use, I will go ahead and write human okay.
And human is basically giving this particular query okay, I'll say write a 200 words blog on the topic
on the topic, and the topic will specifically be my blog topic over here, which I will be giving from
my input right whenever I hit that particular API.
Okay.
And uh, after this, uh, what I can actually do is that as soon as I give this particular instruction
to, uh, as a human being over here, uh, the next thing is that I will have my assistant write,
and now I will go ahead and write about assistant.
And this will basically be closing the eye.
And this is the keyword that we specifically use for most of the llama models.
Right.
So very simple.
Write a 200 word, uh word blocks, uh, 200 word blogs on the topic blog topic.
And this will basically be my assistant.
Now as I said, I have to worry about the body.
Now my body has what?
All things right.
You really need to go ahead and check it out from here.
My body has a prompt message.
It has a max gen message, max gen length message.
It has a temperature message and it has a top underscore B.
So these are all parameters I really need to set it up so quickly.
I will go ahead and write it over here.
So first parameter will be nothing but prompt.
And this will basically be my prompt underscore text.
Okay.
So sorry.
Uh, I've not created my prompt yet.
Okay.
This will basically be my prompt.
Okay.
Now after I probably define my prompt, the next parameter that I have already written is nothing but
max underscore gen underscore length.
Okay.
And this will basically get assigned to five to L okay.
And then you can probably see this will basically be my temperature variable, which will be my third
parameter in my body that I will be sending.
And another one will be top underscore p, which is another another parameter that is specifically required
in my body.
So this basically becomes my body.
Now also what I am actually going to do is that, uh, with the help of Boto3, I'm going to call this
foundation model.
So that is my, uh, meta llama two uh, 13 billion chat model.
And then for the things I will try to go ahead and do it.
Okay.
Now before I call that particular model, let me just go ahead and create a try catch block so that
it is always good that we really need to have a try catch block so that whatever errors we specifically
get.
So I will go ahead and write bedrock and let me go ahead and write Boto three dot client.
And here I'm going to basically call bedrock underscore runtime.
And if you have already seen my bedrock videos I've already shown you how you can invoke the bedrock.
Uh, we have to specifically create the runtime, the region, uh, of all the foundation models that
I'm actually going to call will be nothing but US East one.
Okay, so this is the region that we are specifically going to use.
And the next thing that I am actually going to give is my config.
So inside my config uh oh.
Let me write it like this.
So this will basically be my config.
Uh inside my config I'm going to use my boto3 or sorry boto code dot config dot config.
And here I have to set up some of the parameters.
Right read timeout Okay.
Read timeout.
And here I'm going to probably take up 300 seconds.
And I'm also going to give one more parameter which is nothing but retries okay.
And this will basically be retries.
And this will be set to a variable which is called as max attempt.
Uh max underscore attempts will be nothing, but it will be three.
Okay.
So here I'm going to keep up all the values.
So these are the basic configuration that I am specifically giving uh with respect to my number of tries.
Uh, all the so in short, this variable will be used to call my foundation model.
Okay.
Now in order to call the foundation model I will write bedrock dot invoke underscore model.
And here I'm going to give my body.
The body is nothing but I'm just going to write JSON dot dumps okay.
JSON dot dumps.
And again I have not imported JSON.
So let me just go ahead and do that.
So I'm going to write import JSON okay.
So once I probably write import JSON.
So here a JSON dot dumps.
And here I'm going to probably give my entire body.
Or along with this I also need to call which model I'm basically calling.
So for that I have to mention my model ID and this time the model ID is nothing but it.
You have to refer it from here.
So this is nothing but meta 70 billion.
Now let's go with 30 billion because the request again I have to pay based on the number of requests.
That is probably going right.
So model ID here I'm going to just paste it and I'm going calling the 13 billion chat v1 model okay.
So once you do this then what we can do we will get our response underscore content.
And let me just go ahead and write response dot get body.
So for that response underscore content uh response dot get.
And here we are going to basically write body dot read okay.
Dot read.
So we are specifically going to read this specific response.
Uh, and one more thing uh that you can do write and understand.
One more thing guys.
Very simple.
Whatever response you are probably getting, I will just go ahead and import the response also because
I will be using this.
Okay.
So uh response dot get body dot read.
Once I read that particular data, what it will happen is that I will just go ahead and load this with
respect to my response.
Underscore content.
Okay.
Now here you should understand, right, that when I invoke this particular module and when I give my
body over here, I'm basically calling this particular model over here itself.
That is my llama 213 B uh, 13 million Chad B one.
And then with respect to this particular body, whatever body you are specifically getting will read
that particular response.
And let me do one thing.
Let me just make this particular spelling correctly.
And as soon as I load this particular response content, let me go ahead and store it in my variable.
Response Underscore data.
Okay.
Now, once I load this, uh, let me just go ahead and print it.
If I really want to see my response.
Underscore data response underscore data.
Okay.
If you want to probably see it, um, this response will be having a variable understand one thing that,
uh, whatever response we are basically calling it is giving the response from our entire model, the
model that we are using, the foundation model.
And now here I will go ahead and write.
This will basically be my blog details okay.
Blog details and this blog details, uh, blog details will be available inside response underscore
data with respect to my, uh, key variable, which is called as generation.
So this model, whatever response it is basically giving you, it will be saved in this particular key
variable within your response data which has been converted into JSON.
And when you write response dot get body dot read.
In short, you're basically getting the response from this.
Okay.
And finally I'm going to return the blog underscore details.
So this function here you can see.
And let me just go ahead and write my accept block also.
Also accept okay.
Accept exception I'm just going to create my exception exception as E.
And we are just going to print the errors and understand one thing guys whatever printing that we are
specifically doing AWS lambdas make sure that all the logs will be logged in the CloudWatch logs, right?
So that is also very good advantages for you so that you'll be able to see that.
So error generating the code uh okay.
Error generating the blog.
And I can probably display this particular error, which is basically over here.
And uh let me return it with a blank screen.
Okay.
So here we have basically done almost each and everything we have displayed the data.
This is the blog details that we are specifically getting.
And this is the function that is responsible in generating the blog.
Okay.
Now let me quickly go ahead and do one more thing.
Let me go ahead and uh, see, by default, if I go inside my lambda function, uh, it has a function
which is called as lambda handler.
Okay.
So let me just go ahead and define this lambda handler.
Because my API gateway, whenever it's say sends any Post event, uh, it will be captured from this
particular trigger and it will hit this particular function first.
So we have to capture this event along with the context.
And then we can probably, uh, retrieve whatever query we are specifically giving.
See here we are giving the blog topic right.
That blog top topic will be captured in this specific event.
Okay, that is what is my plan.
Okay, so here what I'm actually going to do, I'll just go ahead and create my event.
So event Jason Dot loads and I will take this event and let me do one thing.
Let me create a variable right which is basically called as body because whatever Post request we are
sending this will be available in the body key itself.
Okay?
You'll be able to understand it.
And then this will basically be my blog topic.
So I will let me go ahead and write my blog topic.
My blog topic will be nothing, but I will just go ahead and write event of.
I can keep a variable called as message, or I can also keep a key called as blog.
So you'll be able to understand.
Inside this body I will be giving in a JSON format where my blog topic will be in this particular variable
name.
Okay, so blog underscore topic.
Let's say here I go ahead and write blog underscore topic is equal to machine learning.
Then I should be able to get this.
Okay.
Um, then uh, once I probably get the blog topic, then I will go ahead and write generate underscore
blog.
And now I will go ahead and call my, uh, generate blog using bedrock.
Okay, so this will basically be my function.
And here I'm going to probably give my blog topic name, which will be nothing, but it will be your
blog topic that I've actually created.
So in short, uh, this function, when I'm calling it, it will be responsible in giving me the entire
blog by calling this particular, uh, model that is llama two.
Okay, so once I probably get this generate blog, block, now I'm going to go ahead and write if if
generate underscore block.
Okay.
Now as you know that I'm going to save this particular block in my S3 bucket okay.
Uh S3 bucket.
The reason why I'm saving it, I'll try to save it in a.
TXT file so that I will be able to access it whenever I really want.
So let me do one thing.
Let me go ahead and import from date time.
I'm going to import date time.
Okay.
So the reason why I'm importing this date time because I really want to create all the txt file in a
way where I consider the current time.
Okay.
So current underscore time is equal to date time dot now and here I'm going to use dot strf time in
the form of hours months and sorry hours minutes and seconds.
Okay.
And then I will go ahead and create my x three key key name okay S3 bucket key name.
So S3 underscore key how my file needs to be saved okay.
So I'm going to create an f string.
And let me just go ahead and go ahead and create something like this blog output folder.
So this will basically be my blog output folder.
And inside this I will go ahead and write current underscore time dot txt file.
Okay.
So this will basically be my txt file which will be saved in that particular S3 bucket.
Now let me give my S3 bucket name.
So S3 underscore bucket name.
And let me go ahead and write AWS bedrock Bedrock course one okay.
So let me create this particular bedrock name okay.
And now I've given all the information.
Now I can call a function okay.
And let's say uh first of all let me just close this if clause.
So here I'll say print.
No block was generated.
Right.
No block was generated.
Perfect.
Now the next step, what I'm actually going to do, uh, over here, is that once I get my key and bucket,
I should be able to call another function.
I'll create another function and let me go ahead and write this particular function.
And him go here I'm going to write slave uh, save block details okay.
In S3 okay.
So this will basically be my function.
And uh, with respect to three I require two information.
One is my S3 key S3 key.
And the second information is my S3 bucket.
Okay.
So in short, I'm taking up all this particular information.
And uh, here you can actually see that I'm, I'm saving it in the S3 bucket.
Now the further code I can probably write it over here again over here.
What I will do, I will go ahead and write Boto three dot client.
Okay.
So this will basically be my boto3.
Let me see.
I've imported boto three or not.
So this client will be nothing but my S3 bucket.
Okay.
And again here I will go ahead and create my try catch block.
And I will say that hey go ahead and create S3 three dot put, uh, object is nothing.
But with respect to my S3 bucket, uh, S3 key, uh, body, uh, body.
Also, I need to save it and the body will be nothing.
But, uh, let me just see over here.
Okay.
Uh, body will be nothing, but, uh, whatever is one generate block that I'm actually getting.
Okay, so here I will go ahead and give my generate block which will be assigned to this.
Okay.
So in short, I am putting I'm taking my entire information block that is generated in this particular
bucket with this particular file name.
So here I'm going to probably call my this function.
That is nothing but save block details.
And with respect to this particular save block details, as I said I require S3 underscore key.
The second parameter that I require S3 underscore bucket okay.
And the third parameter I require is nothing but my generated blog, which will be in the specific name.
Okay.
So done.
This is good enough.
Everything is probably given over here.
But again understand with respect to Lambda handler, uh, I should be returning something because once
the execution is done just for my verification, I will be returning something like a status code if
there is no error.
So I will set up a status code which will be nothing, but it will be equal to 200 and body will be
equal to JSON dot dumps.
And I'll say blog generation is completed.
Okay.
Just to get me a message okay.
Okay.
So done.
This is good enough.
Uh, I've got each and every thing over here.
Uh, it looks absolutely fine with respect to the functionalities.
Whatever is required.
I have actually given it over here, and this looks absolutely fine with respect to whatever information
I see over here.
Right.
And uh, again, uh, understand, uh, okay.
This response variable, I will save it over here.
Okay.
So so since I'm hitting this particular bedrock and this response, I can remove it.
Okay.
So that is those variables.
So let me quickly copy this and let me do one thing.
Let me paste it completely over here.
Right.
So here I'm just going to paste it.
And this will basically be my entire lambda function with respect to all the details and whatever things
I specifically require.
Again, to write it over here, it would have been difficult.
But now there you could see that how easy it is right now.
We'll go ahead and click on deploy okay.
So once this is probably deployed I can go ahead and create my API gateway and all.
Okay.
One more important thing.
Understand that, uh, this Lambda function requires a recent version of Boto3.
And if you probably go ahead and see what default Boto3 version is basically present in this lambda
is the older one.
Now, in order to do, create or have a new Boto3 library installed, what you really need to do.
Okay, so here you can see I'll be showing you one very important thing.
I have actually created a zip file.
Okay.
So you have to probably follow this step okay.
And this will basically be helpful for you okay.
Let me do one thing.
Let me just remove this.
Okay, so here you can probably see, uh, I have created a folder which is called as boto three underscore
layer.
Okay.
Now understand one thing.
What I am actually doing over here, this is important for you all to understand.
Okay.
Unless and until you don't understand this.
So I will go ahead and activate my V and V environment or okay V and V environment is not there.
Okay.
What I'm actually going to do.
See over here okay.
Conda conda deactivate okay.
Now understand one thing.
As I said, my lambda function requires a new version of the boto three installed.
See, whenever we open a Lambda function by default, the older version of Boto three is there now in
the older version of boto three, because at that point of time, uh, you did not had a Amazon bedrock.
All the foundation models, right?
So what we need to do is that we need to update the packages with respect to the Boto three.
Now how do you do that?
Okay.
How do you do that.
And this I would definitely explain you in a better way.
See, what I have done is that I've created a folder which is called as Python.
Okay.
Now inside this Python, what I have actually done, see it is very much important.
Now inside this python I have installed, I have installed a library which is called as Boto3.
So if you probably go ahead and see this right here, you'll be able to see Boto3 boto core.
All the information is specifically there right now.
This will be a task for you.
Just tell me, how can you install a library inside a folder?
Okay, just to give you pip install boto three minus t.
Right.
And if you go ahead and write this right, what it is basically going to do is that the entire Boto
three library will get installed inside this particular folder.
And once this folder is created, what I can do, I can make sure that I will go to this particular
folder and I will make a zip file.
You can compress this particular file and you make a zip file and you can name anything here I have
given boto3 underscore layer dot zip okay.
If you want to install any packages in Lambda, first of all you really need to create a Python folder.
Install all the packages that you want.
Let's say by default over here, if I go ahead and write import pandas, pandas may not be installed
in lambda.
So if you really want to install pandas, what you really need to do inside this Python folder, you
will go ahead and write pip install pandas minus t python folder slash automatically.
The installation will take place and then you convert it into a zip file.
The zip file name can be anything, but inside the zip file there should be a python folder with all
the packages.
Okay, so I'm going to specifically use this okay.
So I've created over here boto three dot layer dot --.
Now I'm going to go down and let me do one thing.
Uh over here you'll be able to see layers okay.
So I'm going to click on layer add a layer.
Now inside this particular layer this basically says that if you really want to add any kind of packages
you can add it in the form of layer.
Okay.
So here you can see this layer, choose Layer Custom Layers and all specify a layer.
So what I'm actually going to do I'll just go ahead and select my custom layer.
See over here you can also create your own layers also right.
You can create your own layer information.
So one of the layer you can probably see over here it is written as uh boto three underscore bedrock.
And this is the layer that I have already created okay.
Now in order to create the layers I can go ahead and click on this particular layer.
I'll go back I will click on Create Layer see what I have done.
So okay this is my lambda function.
Understand this thing okay.
Uh, if I probably show you my functions this is my function.
Okay.
Now I've already written the code.
I've deployed that particular code.
Now, what I need to do is that I need to create a layer right, so that I update all the packages.
Okay.
So in order to create the layer I will just go ahead and click on add a layer.
And over here what you do just go back to this particular layers and click on create a Layer.
And here I will go ahead and write Boto three updated layer okay.
And description I don't want I'll upload a zip file.
I hope everybody knows where the zip file is.
So this is my zip file.
So I will go ahead and click on upload okay.
And I will take this path over here and I'll paste it okay I will go ahead and write boto three underscore
layer dot zip okay.
And then uh there is also like which all version you really want to be compatible with.
So I will go ahead and write Python 3.12, Python 3.1, Python 3.10.
Okay, so this is all layers.
I really want to put that okay.
It is compatible with.
And now let's go ahead and click on create the layer okay.
So once this particular layer is basically added uh created then we can add this particular layer to
my lambda function.
And this is just to make sure that all your packages are up to date.
That is the main, main reason why we are specifically doing it.
So it is going to take some amount of time again based on your internet speed.
And based on that we will try to see that what all things we can actually do.
Okay, so let this thing get created and let me have a look with respect to this once it is getting
created okay.
Now this looks that it is created.
Now I'll go to my lambda function okay.
Uh, create a function.
Let me go back to my function.
This is my AWS bedrock.
And let me go ahead and now add a layer which I have already created it okay.
So now it should be my custom layer.
I will go ahead and select this boto three updated layer version.
I can probably give it as one and click on add.
Okay, so as I as soon as I probably give as add, you will be able to see that over here my layer will
get automatically added.
So one layer has got added.
So my code is ready.
My layer is ready.
Now all I have to do is that I have to probably go ahead and create my API gateway so that it will get
triggered or integrated with AWS or with my Lambda function, and I will be able to trigger things.
Okay, so now I will go ahead and call my API gateway.
So API gateway is over here.
This is how my API gateway looks like.
Now let me do one thing.
Let me quickly go ahead and integrate this API gateway with my Lambda function.
Okay, so now my API gateway is opened over here.
Now quickly let's see this.
Uh, as soon as I probably get this particular API gateway, I will go ahead and integrate it.
And then whenever I trigger this particular API, it should be hitting my lambda function, which is
in turn interacting it.
So I will go ahead and create my API.
And here you can see I will be using an http API.
Um, over here you will be able to see can should we add an integration, my API name and all are there.
Uh, let me just give my API name.
So here bedrock demo API I'm just going to write it down review and create.
Okay I will just go ahead and create it okay.
Now once I create it after that I will start adding routes.
So here you can probably see I'm getting routes over here.
So let me just go ahead and create some of the routes.
So here I will go ahead and write post request.
And here I can go ahead and write.
Blog generation will be my API.
And let me just go ahead and create over here.
Let me select this post here.
There are two option one authorizes protect your API against unauthorised requests.
So if I really want to keep some kind of key for authorisation purpose, I can do it.
I can basically attach it over here.
But right now I really want to show you that how the post request will basically get handled.
So here I'm going to basically attach integration right now.
So attach integration I will go ahead and click on.
And uh with respect to this attach integration I am going to attach to my lambda function.
Now inside my lambda function I will go ahead and choose my lambda function.
So here you can see AWS app bedrock.
This is the Lambda function that I have actually created.
And I let me just go ahead and click on create.
So once I probably create this here you'll be able to see that my lambda function is done.
Okay.
Now the most important thing okay.
Okay.
One thing is that here, this particular API, I can also deploy to multiple cloud platforms.
And sorry, in uh, C whenever we do a bigger project there is dev environment, there is Q environment,
there is UI environment, there is production environment.
So similar kind of environments also you can create.
So for that just go to the stages okay.
And by default this is basically the default stage.
But I will just go ahead and create my stage name.
So I'll write dev environment.
And uh I don't need to add the stage variable and all.
So I'll just go ahead and create okay.
Now inside this particular dev environment, uh, you'll be able to see that, um, here.
Right.
Uh, okay.
Let's see this.
So this will basically be my URL.
And along with this URL if I really want to call my API.
So this will basically be my URL.
So I'll just go ahead and deploy it.
So no stages found.
Let's see okay dev is there.
So I will just go ahead and deploy it okay.
So I'm deploying this entire URL.
And this is the API that I have to specifically use for interacting with my lambda function.
Okay.
This slash whatever will be my root name I can actually use it.
So this if I go ahead and I'll copy this also.
Okay.
Just a second.
I'll just copy this.
Um, okay.
It has gone to this okay.
No worries.
I'll go back to my API gateway.
And here now you'll be able to see all the API gateways that I have specifically created.
I have also integrated with the Lambda function that is available over there okay.
And this is how the entire interaction will take place.
See, it's more about writing a code, understanding about each and every services.
And that is why all these things are amazing, right?
So recent date is ninth.
So let me just click on this okay post over here.
I've already deployed this.
So let me do one thing.
So this is the slash blog generation.
If I probably go ahead and see with respect to stages, if I go ahead and click on dev.
So this will be my okay okay done.
Each and everything is done.
This is the integrated uh, this is in turn integrated to my lambda.
Now, one thing that I have to do is that I also have to create my S3 bucket.
Still, I have not created my S3 bucket because with respect to the code that I've actually given in
my AWS bedrock, my S3 bucket name should be, uh, let's see, what is the S3 bucket name that I have
to write?
So it should be AWS Bedrock course one.
So let me quickly go ahead and create my AWS bedrock A.W., AWS Bedrock Course one.
So I will just go ahead and create my bucket, and let me just go ahead and write my bucket name, okay.
And just let me go ahead and create the bucket okay.
So this is the new and understand one more thing guys.
This uh, AWS bedrock course, one that I'm basically writing.
Uh, it should the name should be unique.
Okay.
So this is the name that I'm actually selecting a AWS bedrock course one.
Okay.
And let me just go ahead and create the bucket because throughout the region it will be it has to be
unique.
Okay.
Now let me just go ahead and update in my name over here in my S3 bucket name with respect to this.
Okay.
Now I'll just go ahead and deploy it.
So my AWS S3 bucket is also created.
The code is written where we really need to save all the files and all everything is ready.
Now, uh, let's quickly go ahead and test it and I hope everything will work fine.
So I'll just go and open my postman.
Okay.
Create my new file.
It should be a Post request.
Let's see.
I don't know whether it should work.
Everything should work or not.
If I'm getting any errors, definitely, I'll let you know.
So let me do one more thing.
Uh, let me just go back to this.
Um, so this is the URL I've already seen.
I've already deployed it in it.
Uh, let me do APIs, block demo APIs.
So this should be slash block generation okay.
So this will be my URL.
I'll go back to my postman.
And you have to be patient.
Guys understand you have to be really be patient because there are multiple things that you will be
able to see.
Now let me just go ahead and set up my body.
My raw body will be with respect to one key.
And if you probably see the lambda function, what is the key name that I've actually created?
Let me go ahead and see it.
I'll open the lambda function.
And uh, once I open the lambda function with respect to my function name is bedrock.
Okay.
And here down, if you probably go ahead.
Come on, load it quicker.
Come on.
It's taking time to load.
I don't know why.
So code is getting loaded.
I've already deployed this code also.
You also have to deploy the code in order to use it.
Okay.
So Lambda function is getting loaded.
Let me see.
This is there.
Okay.
Over here you can see this will basically be my blog topic in the body.
So let me just go ahead and write blog topic blog topic.
And let me just go ahead and write.
The blog topic will be machine learning uh, and generative AI okay.
Okay.
So this will basically be my key name.
Uh, inside my this this entire thing will be a event.
It'll be in the event itself.
Inside that event, there will be a body variable.
And then we will be able to explore this blog underscore topic.
Okay.
So let me just go ahead and click on send Hope.
So it works fine.
So it is hitting hitting hitting till then uh message not found.
Uh why it is saying message not found because blog generation is there.
Let me see.
Uh, such a dirt block.
Block generation.
Let me see it.
Block generation.
Let me go back to my API gateway.
And for this also, what I can actually do is that go to my configuration, um, sorry, go to my monitor.
And inside that I can also see the logs and it will be available in the CloudWatch.
So here you can click on view CloudWatch logs.
And here is my entire CloudWatch logs that you will be able to see.
Okay.
But right now it is not working.
Let's see why it is not working.
Uh, we'll have the complete details that will be available over here.
Now it goes to my log groups inside that groups specifically where we are actually working with.
Again, I'm not going to delete this entire part of the video because I really also want you all to
see the logs.
Okay.
To specify log does not exist in this particular account.
Why?
Okay.
The logs has been not created.
Let me just hit it once again.
Blog underscore topic message not found status 404I think.
So there is some problem with my API gateway stages slash blog generation.
Is it correct?
Slash blog generation?
This looks fine.
My post request is this one.
Uh, the integration in the background is saved or request.
Let me just go ahead and see my stages.
My dev stage is this 1FPH.
Let me see whether I've deployed that or not.
Deploy successfully created deployment.
The deployment is active for dev.
This looks absolutely fine.
Now let me just go ahead and hit it for this one.
Message not found.
Still.
Message not found.
Let me see that.
What is the problem in this?
So guys one simple mistake that I've actually done over here is that, see, when I pasted this entire
thing right, there was an extra space that was added.
And postman does not resolve this particular space, and it it keeps the space like that.
So I'll just remove this space.
Now I'm going to search for the topic on generative AI.
So I will go ahead and write this 200 words block.
Now let me just go ahead and click on send.
And here you can probably see block generation is completed.
Let's see whether everything has worked fine or not.
I'm just going to reload this log groups and obviously you know how to go to this particular log groups
go to configuration okay.
Sorry.
Go to monitor and just click on view CloudWatch logs so that you'll be able to see the logs itself.
Right now let's see whether the logs uh will be able to see the buckets also or not.
I'll also go ahead and check the buckets.
So AWS bedrock.
So right now nothing has got created.
So I think there should be some kind of error over there.
But we'll have a look, uh, whether there is an error and we'll try to fix it.
Okay.
Uh, so all the things usually happens over here.
So here you can see AWS app bedrock and there is one log.
Uh, so with respect to this particular log let's see AWS bedrock some no block was generated.
Uh generating the blog.
An error occurred while when calling the invoke model uh is authorized to provoke invoke model and resource.
This because no identity based policies allows the bedrock invoke model action.
So guys from the error that you probably see over here, it is not provided, uh, it is basically not
providing you the access.
So definitely the role that we have actually created for a lambda Lambda function.
Right.
We really need to change over here.
Right.
So what we can do see initially it was in code.
Right now let's go back to the configuration and select the role name.
So if you select the role name over here okay let me just go ahead and select this.
And now within this particular role name we can provide further access to it okay.
So let's say over here only the a AWS Lambda function is basically there.
So what I can actually do is that I can provide more access, more, more permission policies, so that
I will also be able to access the AWS bedrock.
So to just to see like what kind of policies we can probably provide it, you know, and over here right
now what is basically provided only AWS Lambda.
So let's see that how we can still add more permissions.
So here what I'm going to basically write I'm going to basically select add uh attach policy.
And again based on like how your IAM console basically runs.
Right.
Uh I will basically provide it to administrator access.
Again.
If you are specifically working in the company, you can provide different, different access based
on the services that you are using.
But just to give a brief idea about it.
So what I'm actually going to do is that I'm going to add this particular administrator access so that
you'll be able to see that particular access over here.
Now, once that is added, uh, let me just go back over here and let's see whether it is updated or
not.
Uh, so I'm roll this is there.
Okay.
Perfect.
Everything works fine.
So let me just reload this.
And now I think we should be able to work out and see that.
Yes.
Now we'll be able to get the access.
So here you can see all access is basically given.
Now I'll go back to my postman and again hit the same request.
Now let's see.
See if it is taking more time.
Right.
That basically means my entire application is working fine and it sees we are able to see that yes,
it is taking more amount of time now I think our blog will be generated 200 words, blog will be generated
and we should be able to get or we should be able to be storing that entire.
TXT file in S3 bucket.
So let's see I will go again back over here.
Again reload my CloudWatch.
So let's see whether my CloudWatch has basically created or not.
Yes new cloud new log has been created.
So here is my entire block that has got generated generative AI the Future of Creativity and Innovation.
Here you can see everything is working fine.
Code save to S3 bucket.
I can also see in the logs.
Now it's time that.
Let's go into my S3 bucket and see whether I will be able to find it out or not.
So I will go back to my S3 bucket.
Let's go ahead and hit this, and then I will be able to see this AWS Bedrock course one.
And here is my blog output.
And here is my text file right.
If you want to probably see this particular txt file I'll just download it.
But usually when we are working with AWS services we can probably see that.
And here let me open this.
And here is my entire blog right.
And this specifically we are using the meta uh llama llama 213 million parameters generator I is a revolutionary.
All the blogs has been generated.
Again, the main aim over here was to just to make you understand the entire flow.
because in the upcoming videos, the kind of projects that we are going to deal with will be quite advanced,
will be quite better because there we are also going to use more things like vector databases and all.
But all in all, this is an amazing project and this all services are specifically used step by step.
If you are also following it, you will also be able to do it.
So I hope you like this particular video.
This was it for my side.
I'll see you in the next video.
Have a great day!
Thank you and take care.
Bye bye.