Hello guys.
So we are going to continue the Langton series.
Uh, in our previous video, we have already done the setup of Lang Chain and Lang Smith.
We have also seen how to properly extract the Lang Chain API key.
Along with that, we also saw how to get the OpenAI API key.
And we have set up that entire thing in our environment variable.
Now let's go ahead and probably start this particular coding where we will be using some basics and
common components of lang chain, uh, like uh, prompt templates and all.
But before that, let me do one thing.
Let me clearly show you that how you can go ahead and export all these environment variables that we
have.
Right.
We really need to have that first.
So first of all, what I'm actually going to do, I'm going to go ahead and write import OS.
Right.
OS is one of the library in Python.
Uh, which will be um, you which we will be specifically using to set up our environment variable.
Then if you remember, we had also imported from dot env import load underscore dot env.
Okay so this load underscore dot env will be specifically used to, you know, import all the environment
variables that are available in this dot env file.
And already you know that we have imported this Python dot env.
And from here only this particular library is getting imported.
Okay.
Now the first thing what I'm actually going to do, I'm just going to write OS dot environ.
And here I'm going to set up my open ai API key open underscore AI.
Sorry apo open AI underscore API underscore key okay.
And in order to call that particular environment variable I will just go ahead and write OS dot get
env.
And here we are again going to use the open AI underscore API underscore key okay.
So from the environment variable, uh, the key name that I've actually created is OpenAI underscore
API key that will get uh, set up with this specific environment.
Uh, this was the basic setup.
Okay.
Again, uh, if you, if you probably go ahead and see that we also had created something like Lang
chain API key.
Right.
So that also we'll go ahead and do the setup.
So here I'm going to write OS dot environment environ okay.
And here we are basically going to set up our lang chain underscore API underscore key okay.
And this will be nothing but OS dot get env again with the same name lang chain underscore API underscore
key.
Because this is what we have actually created in our environment variable along with this uh, and this
is specifically used for Lang Smith tracking.
Okay.
We'll discuss about it as we go ahead.
More, uh, and, uh, so this is nothing but Lang Smith tracking.
Okay, uh, along with this, what I am actually going to do, uh, I'm also going to make sure that
I'll go ahead and write OS dot environ.
Okay.
And here no, we will be also setting up our lang chain tracing okay.
So we have to make sure that we set up this particular value.
Also long chain tracing underscore v2.
That is the requirement for long chain to true okay.
So this will basically be set up to true okay.
So till here everything looks perfect.
Uh along with this uh there is one more very much important thing is that is with respect to the, uh,
long chain project.
So here what I'm actually going to do again, we will go ahead and set up OS dot environment.
Okay.
This is just for Lang Smith tracking that is required.
And here I'll just go ahead and call my OS dot get env again.
The name will be nothing but Lang chan project.
Okay so this are my three important keys that I'll be able to set up over here.
And by default, uh, here, you will be able to see that.
what all things I am actually doing.
Right.
So this is my OpenAI API key.
This is my OS dot environment.
Uh, with respect to lang chain API key lang chain tracing needs to be enabled.
So it is set to true.
And then finally we have this uh, lang chain underscore project.
Okay.
So all this, uh, setup is specifically done.
We have also done the installation of, uh, Lam Lang chain.
Now, uh, since we are using luncheon with OpenAI.
So there is one very important library that we have to probably go ahead and do the installation.
That is nothing but lang chain dash OpenAI.
Okay, so open AI.
The reason we have to do this because all the inbuilt function that is required with the integration
of OpenAI, we really need to use this particular library.
So let me quickly go ahead and open my terminal okay.
And here I will just go ahead and run add pip install minus our requirement dot txt.
So the installation will happen.
And now it will also start the installation for Langshan dash OpenAI.
Right.
So it is going to take some time based on the requirements.
So here you can see the installation has basically happened.
Perfect.
So this is good enough.
Uh we are good to go okay.
So I'm just going to close this up.
And now uh, the next step is that how do I probably, um, you know, create or work with chat open
API.
Okay.
Now, since I want to use open AI API key along with that.
So there is a library which is called as Chat open API.
So I will go ahead and write from long chain underscore okay open API.
I'm just going to import something called as open AI.
Right.
So this is this will definitely be there.
And this open OpenAI will specifically be responsible in, you know, creating your object.
But I'll not use OpenAI directly over here.
Instead, let's go ahead and use chat OpenAI because I really want to create a chat bot.
Okay.
Now I will go ahead and create my LLM okay.
And here I'll say, hey, use this chat open API.
And here I'm going to set my API key to nothing.
But it will be the same value that we are going to set it up right.
C API key is nothing but OpenAI underscore API key.
But since I have already made sure that I have created my environment variable, I don't even have to
specify the API key.
Okay, so if I go ahead and execute this and let's go ahead and print my LLM right.
So this will be my LLM model okay.
So here you can see uh install kernel change kernel okay let's do this one thing.
So here you can actually see that I have not installed, uh, IP kernel.
So let me just go ahead and do that pip install IP kernel okay.
Because I really require IP kernel for this.
And I've not even executed this.
So let me quickly install this okay.
So here you can see uh pip install Ipykernel Okay.
Run cells with this.
Uh, let me do one thing.
I'll just go ahead and update over here.
Ipykernel.
Okay.
Ipykernel.
Because for the first time, I think we have to do it from here itself, from the terminal.
So I will just go ahead and open my terminal pip install r requirements.txt quickly, do the installation
of ipykernel, and then I will go ahead and select my kernel over here.
So no need to go ahead and execute it again.
Okay.
Right now it is getting installed.
Let's wait for some time.
Okay.
And then you will be able to see that this all errors will probably go but you have to install pip install
ipykernel.
Okay.
So uh, this error that you are seeing I will keep it like that.
I'll not cut it because the simple reason that I really need to show you that where all things you will
be specifically getting an error.
Okay, so Ipykernel is basically getting installed and then only you will be able to attach this particular
kernel.
Okay.
So let's wait for some time.
And uh, it is again, depending on your internet connection it will take how much time to install a
package.
So now the installation is done.
Uh, now let's go ahead and execute this.
So here you can see connecting to a kernel.
And this has got executed okay.
Ipykernel is definitely required.
Now let me just go ahead and execute this LM now here you will be able to see that I have actually got
this particular lm, uh, model, uh, over here.
This is my OpenAI secret key.
Everything is basically there.
Okay, perfect.
This is good.
Uh, now, again, uh, with respect to if whenever we specifically work with, uh, LM models, uh,
one thing that you really need to worry about is, like, what?
All different kind of models are definitely there, right?
And, uh, in in open AI right now, the most famous model that you will probably be seeing is GPT four.
Right.
And how to call that particular model, we'll be seeing how how simply, easily you can basically call
it.
So here what I will do inside this chat open AI.
And for our entire course we'll use the best model instead of using variant different variants model
that is GPT four right.
So here you just need to write model equal to GPT four.
Right.
And now I will go ahead and print my LLM.
So we will be using this particular model because this is the most efficient model that is available
in OpenAI API.
Okay.
So perfect till here you have actually done this.
Everything as such.
So this is how you can load your LLM model okay.
Now with respect to LLM if I want to ask any question.
So let's provide some input and get response from LM okay.
And again, as I said, uh, the reason I'm able to connect it because I have my open API key, uh,
loaded in my environment variable.
Okay.
Sorry.
In my, uh, in as an environment variable.
I have loaded it over here.
Now, in order to give any input to this LM, I will just go ahead and call LM invoke.
And here we will go ahead and write, let's say, uh, how uh, what is generative AI.
So this is my question that I'm asking this LM model okay.
So once I probably go ahead and execute it.
So here you will be able to see that I will be able to get the response, uh, through the API key.
Okay.
Again, uh, this depends on how much time it is going to take.
Okay.
And the best thing why it is taking so much of time, right.
That also you really need to understand.
So here you can see I have actually got this AI message content uh is equal to generative AI, something
like this.
Right.
So let me do one thing.
Let me store this in my result variable.
And now I will just go ahead and print my result okay.
Result okay.
So let's go ahead and print this result.
So it is going to take some time.
The reason it is taking time um why eight seconds has basically taken time.
Because because of this with the help of this long chain API key, we are going to record all these
things, all these events that are specifically happening in the length.
Smith okay.
So if I go ahead and print the result.
So this is what I'm actually getting to get the content, I can just go ahead and write result of content.
Okay.
So here is my output okay.
Uh I message is not susceptible.
Let's see what is the error result.
Uh content is equal to generative AI.
So here is my entire output that I am able to get.
Okay.
Uh, let's see, uh, we will try to I'll also say why we are getting this error, but just in some
time, because I really need to show you some very, very important thing.
So let me do one thing.
Let me just go back to my browser.
Uh, let me show you this.
Okay.
Just a second.
I will just go to by browser over here.
Okay.
Now, inside this browser, let me just go to my langsamt.
Okay.
Now if I go ahead and click on the project.
So here you can see j'en ai app with open AI right.
And here if you go ahead and see there are two chat open AI request.
Right.
What other question I asked what is generative AI?
So see how beautifully.
As soon as I probably hit the LM model, everything is basically getting tracked in this particular
Lang Smith.
Now.
I hope everybody is able to understand why Lang Smith is super amazing, right?
Because if you see the entire ecosystem, uh, Lang Smith is a uniform DevOps platform for developing,
collaborating, testing, deploying and monitoring LLM application.
That is the most amazing thing.
And right now here you can see right.
And now let's go ahead and do one thing.
Let's click on this request.
So here you will be able to see what is the human feedback that was given.
Then what is the output that we got right.
Everything is basically getting displayed.
How much delay is basically happening?
Right.
Uh, how much delay?
Latency.
Right.
Tokens.
And it also probably suggest you how much cost it is basically taking based on the token size.
Right.
So 364 token, 494 token.
This much cost has basically incurred in my OpenAI.
Right.
And this is how beautifully we are able to develop this entire thing right now.
Coming to the next step over here, you can see that I have actually displayed the results.
Uh, and this is my entire results over here.
Okay, now, uh, let me work along with chat prompt template.
Now, see, uh, there is something called as prompt template.
And uh, again prompt template is like how you want your LM model to behave or what kind of response
or what kind of role you really want to give your LM model.
I can say, hey, act as an AI engineer and provide me suggestion how to develop an amazing AI project.
Right?
So that is a kind of rule.
I'm setting it to my LM app itself, ai lm model itself.
Hey, you have to behave in this specific way and give me the answer, right?
So similarly, um, Lang Chain also supports, uh, different types of chat prompt template also.
Right.
So that is what I'm actually going to show you.
So now we are going to discuss about chat prompt template.
So I have just shown you one way of calling this particular model we'll be discussing as we go ahead.
Right now, uh, what I'm actually going to do over here, I'll just go ahead and write from Lang Chen.
Okay.
Lang Chen underscore core okay.
Dot prompts okay.
Dot prompts I'm just going to import chat prompt template okay.
Now what is this chat prompt template we'll discuss about okay.
Now chat prompt template.
Uh it it is just like I can tell my LM model.
Hey, you you have to behave something like this.
Okay, so let's say I will just go ahead and create my custom prompt.
So here I will go ahead and write prompt is equal to chat prompt template.
Now here uh with respect to this prompt template I will be using one very important function which is
called as from messages.
And from this particular messages we will define our entire like what my LM model needs to behave like.
Right.
So here I can probably go ahead and create my list.
Now inside this list I will be giving in the form of tuples my key value pairs.
So first thing when I go ahead and write hey system I'm assigning some role to the system.
System basically means my LM model.
Let's say hey you are an expert AI engineer okay.
Provide me answers based on the question.
Okay.
Something like this.
So this is a very simple role that I'm giving to my system, my LM model saying that, hey, you are
an expert AI engineer okay.
So please go ahead and provide me some answers based on the questions that I have.
Right.
So this is one very important thing.
Now the this is for the system, but what with respect to the users.
Right.
So here I'll go ahead and write user comma.
And here I'll say hey user will provide you some inputs okay.
This input is just like a placeholder okay.
This input is just like a placeholder like user is asking you any question.
Then the system the LLM model is ah, basically an AI engineer.
It should provide me the answer based on the question provided by the user or based on the questions.
Okay.
So the way uh, this is what is a very simple mechanisms.
Okay.
And, uh, this is my entire prompt template that I have actually created.
Okay.
Now if I go ahead and just execute this okay.
And show you.
So this becomes my child prompt template, my input variables is input.
And here you can see system prompt template input variables.
Template is like you are an expert AI engineer.
Provide me the answer based on the questions.
Right.
And it has also said that hey uh, in the template also you have to consider input as your input variable
itself, right.
So all this information is there in my prompt template.
This is very much good to start right now.
What I can actually do.
Right, uh, is that use this particular prompt template along with my LM model.
Right.
So what I will do, I will first of all go ahead and create a chain.
And it is important because see my LM model should know like what it has to behave like.
And that instruction is probably coming from here, from this particular chart from template.
So we will go ahead and create a chain.
So prompt slash LM model.
Right.
So when we do or like this this pipe sign right.
It is basically saying that hey we need to combine this prompt along with my LM and that actually becomes
a chain.
Now whenever I give any input, first of all it will go through the prompt and then it will go to the
LM.
And finally we'll get the response right now in order to call this right chain.
So I'll just go ahead and write chain dot invoke.
Now you know what is my input.
Right.
So this is my key that I really need to pass because that is what I have actually set it over here in
my prompt template.
Right.
I will go ahead and say, hey, uh, uh, can you tell me, let's say can you tell me about locksmith?
Okay.
What does Lang Smith is useful for?
Like what is Lange Smith useful for?
So I'm just asking a simple question over here.
Let's see what kind of answer we will be able to get.
And let me do one thing.
Let me just go ahead and provide a variable which is called as response.
And then I will just go ahead and print this response, display this response or print this response,
whatever thing I really want.
So I'll just go ahead and print this response.
So if I go ahead and execute it.
So here you'll be able to see that, um, again, uh, the best thing will be that it will, that the
entire thing will also be going towards, uh, the uh, you'll be able to see that every, every thing
is basically getting tracked in the Langsamt portal.
Okay.
So here you can see certainly Lang Center tool designed to enhance the development.
All the answers you are specifically getting, logging, tracing everything, all the answers you are
actually getting over here based on the content that I am actually getting a response as okay, now
this is one basic example of probably creating chain.
So chain basically means you can combine multiple things.
Chain like prompt you can combine LM, you can create your own custom prompt.
And based on that, uh, and it just gives you an idea like how once the input is given by the user,
how the flow is, you know, initially it will go to the prompt template, then it will go to the LM.
And then that is how you will be able to see it.
If I go ahead and see the type of response, you'll be able to see that it is a kind of AI message.
That basically means this response is basically coming from the AI message itself.
Right?
So I hope you are able to understand it very much clearly.
Okay.
Now, uh, let's do one more thing.
See, right now, by default, I'm getting some kind of message.
Okay?
And, uh, I really want to make sure that I probably can use some kind of output parser also to display
my response.
So that is where, uh, one type of output parser, the default output parser that we'll be using is
something called a string output parser.
Okay.
Now string output parser.
Uh, if I really want to give you an example, you know, uh, and you'll be seeing in further sessions,
you know, we will also making sure that our output parser is just like getting the message from the
LLM and how you really want to display it.
Okay.
By default, uh, there is a string output parser class.
Uh, but you can also create your own custom output parser.
Okay.
And so for that, uh, we'll be seeing that how we can basically create this output parser.
So first of all what I will be doing I will be going ahead and importing from Lang chain.
So underscore core okay dot output parsers.
Okay.
And then we are going to import str output parser.
Okay.
Now this output parser will be responsible in displaying my entire output.
So here I'm just going to go ahead and write output parser is equal to str output parser.
And here I'm initializing it.
Now again I have to go ahead and create my chain this time.
First thing that I really want to pass is my prompt.
Then I will pass it to my LM.
Then whatever response LM is basically giving, it will just go through this particular output parser,
which is nothing, but it is my output parser variable that I've actually defined.
Okay, now see the magic?
What will specifically happen once I create this chain?
So here I will go ahead and print my response.
Let us go ahead okay.
So I'm writing same thing chain dot invoke input.
Can you tell me about blacksmith print response.
So let's go ahead and execute it.
And this time you'll be able to see my output parser.
I'm able to get directly I don't have content.
So that is the reason I was showing you right that time.
We are getting an error when I was able to access this particular variable content by default.
String output parser.
Whatever message you are specifically getting that is only going to get displayed right.
So it is always handy to use this string output parser, because based on your likes and dislikes,
you can actually display the output result in the upcoming videos.
We'll also be customizing this entire thing will be, uh, creating this particular class and we'll
be doing more things.
Okay.
So, uh, in short, uh, in this video, we have used the most common and, uh, most basic and common
components of link chain prompt templates, models and output parser.
Now in my next video, we will create, a.
We'll build a simple application with the lamp chain in the same notebook, so that we'll be able to
track things.
Right now, uh, let me just go ahead and see, like how much information has basically been tracked
over here.
So here you can see so many different information has basically been tracked.
And all this information is probably available over here.
Right.
So this was my chat prompt template.
See see chat prompt.
Can you tell me about the blacksmith.
So this is my entire chat prompt template that you'll be seeing I'm setting.
You are an expert engineer based on this.
And then we hit the chat open AI request.
And based on that I got the entire response right.
So step by step you'll be able to explore everything.
See let's let's consider in this case okay.
So this is my runnable sequence.
So first uh my chat prompt template was there.
Then I gave it to my chat open AI.
Then finally I got my string output parser which is probably displaying the entire content that same
chain.
You are able to do it, and this in turn helps us to monitor each and every thing.
And here you can see the token based on a token cost.
You are able to see each and every cost that is there, right?
So, uh, yeah, I hope you were able to understand this particular video.
I hope you liked it.
In the next video, we will try to create a simple generative AI application again with the use of OpenAI.
Right.
So yes, this was it from my side.
I will see you all in the next video.
Thank you.