Contrast
< Back to Blog
Original link:

https://youtu.be/t2bSApmPzU4

2023-06-15 22:03:48

Prompt Engineering And LLM's With LangChain In One Shot-Generative AI

video content Image generated by Wilowrid

Hello , all my name is Krishna and welcome to my youtube channel .

So guys in this video , we are going to probably discuss about prompt engineering using chain .

Now , prompt engineering can be super important if you are probably working with L L M models because it will actually help you to design the input to the model that you are having a conversation with .

So obviously , uh in my previous video , we have discussed about LA and I also showed you that how we can actually create a simple search application just like a chatbot using Lain .

Now let's go ahead and let's discuss about prompt engineering .

So um in prompts uh engineering , we specifically design the prompts itself , right ?

So if I talk about LAIN , there is a topic which is called as prompts .

Uh Over here , you can see the new way of programming model is through prompts .

A prompt referred to the input to the model .

This input is often constructed from multiple components .

So here in LA , uh you will be able to create your own prompts using this prompt template , right ?

So it is responsible for construction of the inputs .

video content Image generated by Wilowrid

Lain provides several classes and functions to make constructing and working with prompt easy .

So obviously , whenever we use different tools I can use land chain , there are also other tools like chain let or there also the prompt template can be used from the LA chain itself , right ?

So let's go ahead and let's quickly start the coding over here .

And here , I'm going to give you a lot of examples with respect to this .

So um as usual , so over here , the first thing that you really need to do over here is that go ahead and use the open A I key , the API key , I will talk about it later on because uh let's say if you're probably creating a chatbot and you want to also use Google search and that we can actually use the Google search API .

So for that purpose , I would actually use that .

But initially , we just require this open API key .

I have already loaded it away and executed it .

But whatever API key you specifically have , you start with this .

OK .

As discussed earlier , we are going to specifically use prompt prompt template from the LA chain itself .

So now let's go ahead and import from LA chain .

OK ?

video content Image generated by Wilowrid

And many people who are asking what are the prerequisites prerequisite is nothing .

It's just Python .

If you know Python , everything will be working in this specific way .

You just have to keep on looking on the LAIN documentation with respect to the syntax and the parameters that we are going to use .

OK .

So from LA we are basically going to import the prompt template .

OK ?

And uh here from this prompt template , I'm just going to use template .

First of all , we need to design some kind of template .

So one example that I am probably going to probably take over here will be something like this .

So see over here , I will be designing my own prompt template over ac so I'm just saying that I want you to act as a financial advisor for the people in an easy way , explain the basics of whatever topic I really want to give right from the search engine itself .

Let's say uh I want to understand with respect to a financial concept , what is income tax ?

video content Image generated by Wilowrid

So that income tax word will get replaced over here .

So this is a generic template that I specifically need to use it .

You can change it anyhow , like whatever way you specifically want , like question answers , different , different ways you can basically do it .

I'll show you more examples as we go ahead right now .

Once this is done , once the template is basically decided , the next thing that you specifically need to do is that use this prompt template and create your entire , you take this specific template and insert it over here .

Now in the prompt template , the first thing is that you have to define your input variables .

Now what all input variables you are specifically providing over here .

That is nothing but financial concept , right ?

So I will be writing the financial concept over here and make sure that you always write this in cards .

So whatever parameters you're giving from your end , that parameters only you have to put it away .

You can if you are providing one parameter , just provide one parameter as the input or two parameters , provide two parameters as the input .

video content Image generated by Wilowrid

Then in the next statement that we specifically need to write over here after the input variables , right is nothing but your template right ?

Now , what template I'm basically using for designing my input is the same template , right ?

So here I can basically say uh demo template will be the name correct over here .

So I will search demo template and I will copy this over here and paste it over here .

OK ?

So oops just a second template is called to demo template .

Now this is done .

Now this is how your prompt template is designed .

OK ?

And I will save this in a variable called as prompt .

OK ?

And now what I'm going to basically do is that whenever I give an input , let's say my print prompt dot format I will be using .

And if I give my input over here as financial , just see this .

OK ?

Financial concept is equal to .

Now let's say my input is income tax .

OK ?

Now how the L L M model is going to take the entire text .

video content Image generated by Wilowrid

So if I probably just go ahead and execute it , it will take something like this .

I want you to act as an acting financial advisor for people in an easy way , explain the basics of income tax .

OK ?

So this is how the message is basically going to the L L M model .

Let's say if I'm using open A I uh or any L L M chains , right ?

So in , in that specific way , it is going to go .

Now , let's go and see that how this prompt template that we have probably created , we can pass this input to our L L M models , right ?

So for L L M models , obviously in my previous session , also , if you don't know , I will put that particular link in the pin comment of this particular video .

Uh What we'll do from LA chain , we will try to from la chain dot L L MS and there are a lot of L L MS model .

I've just started with open A I .

There is hugging phase , there are different , different kind of models .

Even for chat , there is different , different models we'll discuss about it as we go ahead .

But uh prompt engineering is the first basic things that you definitely need to know .

So I'm going to import open A I OK .

video content Image generated by Wilowrid

So open A I over here and then I will say from line chain dot chains .

Whenever we use prompt template , it is super important that we have to use chain for executing that particular prompt template .

OK ?

So I'm going to import L L M chain .

Now you may be thinking Chris from where do you get all these things ?

Just was this documentation , right ?

If you probably start with prompt templates over here getting started .

Here are some examples that has been given .

And if you really want to run this example , you have to probably use this uh L L M chain , any prompt template that you specifically want to define .

OK .

So here it is .

So here is my line chain dot chains .

I'm going to import L L M chain .

Now , the next thing is that I will initialize open A I and let's say the temperature variable that I'm actually going to define is 0.6 or 0.7 whatever by default is 0.7 over here .

And then I'm going to basically say L M chain , I'll initialize this .

Also the first parameter is nothing but the L L M model that I want to give .

And this is the L L M model .

video content Image generated by Wilowrid

Second parameter is basically my prompt that I specifically want to give so that it can give me the output and that prompt will be nothing but the same prompt , right ?

So this is I'll create this variable like this .

And let me just say that this is my chain one .

OK ?

And like this , I can create multiple chains which I had already shown you in my first tutorial , right ?

So once this is executed uh over here , I'll be having chain one dot run , right ?

When I say run all I have to give is my input over here , right ?

And that input will basically replace this particular input over here itself , right ?

Something like this .

So here I will say , OK , fine .

Uh My input is that I want to know about income tax .

Now , let's go ahead and execute this .

Now , what it is going to do as soon as we write change or run on income tax , it is going to take this prompt template inside this prompt template .

Wherever is my variable , it is going to replace that particular word over here .

It will take some time based on the API key that we specifically have .

video content Image generated by Wilowrid

And again , uh API key speed uh like how much cost you're paying for that , you can also make a free API key , but it will just give you $5 .

So this is what is the output that I'm getting income tax is a tax that is paid um that is paid to the government uh based on your income , your income job investment .

All this information is basically available over here .

See .

So this is what it is basically giving based on this kind of input , right ?

So this is how my prompt format is , it is taking this entire input and it is saying that OK .

With respect to the income tax , I'm going to get it .

Ok .

Uh let's say I want to probably understand about what is GDP , right ?

So it will try to give me the answer , ok , about GDP .

Over here again , it will take around 8 to 10 seconds and again , it depends on the type of API that you have .

So GDP stands for gross domestic product total .

This , this , this is that so you are able to get your own different kind of outputs , right ?

So uh this was it , right ?

video content Image generated by Wilowrid

And I can probably name this entire chat board application as a financial advisor , like whatever topics you specifically want to can put at .

OK .

Now let me show you one more example .

OK ?

And I'll give you entire materials over here .

Now let's say I want to probably build a language translation .

Now for this also my prompt template will be a little bit different .

OK ?

Now see this and here I will show you an example of giving two different inputs , right ?

So language translation over here you have .

So here also I'm using a prompt template .

The template will be that in an easy way translate the following sentence into target language .

So here I have to give two parameters .

First is my input sentence and what target language that I really want to give .

Uh since I'm giving two parameters here , also I'll use a chain .

But the way of providing these two parameters will change .

OK ?

Just see this .

Now this becomes my language prompt , my prompt template , my input variables will be again in a list sentence and target language and template will be this specific template .

OK ?

video content Image generated by Wilowrid

Now once I execute this and probably if I just like my language underscore prompt , right dot format , always whenever you want to see that how the input is basically going to go , you can basically write over .

So sentence is my first key .

Let's say I'm going to write , how are you ?

And I want to convert this into a target language .

OK .

This will be my target language .

I hope uh the spelling is correct .

OK .

So second parameter , how are you ?

And target language is called to , let's say I want to basically give us Hindi .

So I want to convert this sentence .

How are you into Hindi by using this L N M chain ?

So let me just go and see .

Now this is how the input is going to go when I , I provide my sentence , right ?

So in an easy way to translate the following sentence , how are you into Hindi ?

Something like this ?

OK .

Now this is what we are going to use over here .

And again , for this , uh we will use the same uh chain , uh we can probably create a new chain .

video content Image generated by Wilowrid

So let's say my chain two is L L M uh with this particular prompt .

OK .

Now , since I'm providing two parameters here one is sentence and one is uh language you cannot directly call like chain one dot run or chain two dot run .

OK .

So here what I will do , I will write chain two .

And whenever you have more than one parameters , uh you have to probably give it in the form of keys , right ?

So this will be my first keys .

Like this sentence will be my key .

And in the sign said inside this sentence , I can write my text something like this , right ?

So it will be like hello , hello , how are you ?

OK .

And uh this is what is my sentence or let me just remove this question mark .

And the second parameter is nothing but it is basically my target language .

OK ?

And the target language will be nothing but Hindi and you can change it , you can change it to French or whatever things you specifically want .

OK ?

So this is uh how you basically run it .

OK ?

video content Image generated by Wilowrid

Now if I execute it , you'll be able to see the output .

OK ?

I'm getting an error saying that financial concept , OK .

Financial concept was my OK ?

This chain too .

OK ?

Prompt .

I have to change it away .

I see uh this is a smaller mistake that I did because it is still referring to the previous prompt .

So here is my language prompt and here you have see sentence .

Hello .

How are you ?

Target language is Hindi ?

And the output text is when you get in this form of Jason right now , you can probably pick up whatever text you specifically want and you can give the output over there , right ?

At at least it is in the form of key value pairs .

So , right ?

And probably you can also convert this into French .

So French uh will come something like or comment Alice was I hope it is right .

Uh If you don't want this , let's say I want to give it in Canada , I don't know whether Canada is there or not .

Uh I'm not sure .

Let's see .

I hope so .

It should be there , right ?

Hello .

How are you target language ?

Canada , Canada is from India itself .

video content Image generated by Wilowrid

Uh Hello , something like that .

OK ?

So all those languages can be basically be used over here .

OK ?

So this is one example .

Now similarly see I did with two parameters , you do with three parameters , 400 parameters .

However , you want to make a sentence format , try to make it .

OK ?

So guys , now let us see one more example of a prompt template and this will be quite amazing .

So let's consider this .

OK ?

And I'm going to copy and paste one example in front of you .

And uh here you'll be able to see that I'm using one more class , which is called a few short prompt template .

And I'll tell you why I'm specifically using this .

But let's see this example .

In this example , I've given a format uh this is a template format .

And in this template format , what I'm doing is that uh I'll not say this is a template format , but I'm giving some examples in these examples .

I have some key value pads .

Like what is it called to happy ?

Then the opposite of this word will be nothing but antonym , right ?

video content Image generated by Wilowrid

So suppose , let's say I'm trying to find out the opposite of this word .

I'm giving this format some examples like an an antonym , you'll be finding the word as sad .

So happy word opposite is sad .

Similarly here , also you have a word tall and the opposite of this word tall is nothing but shot .

So this few examples , I want to give a hint to the L L M model .

OK ?

And considering this , I'm going to create my own prompt template .

So , so here I will be giving the word like this and my antonym will be looking something like this .

So this is my entire prompt template .

And before giving this prompt template , I also want to provide some examples over here .

OK ?

To the L L M model itself , it can be an open A I L L M model .

Now , this same template I'll be using inside this prompt template and you know that I'm giving two parameters , one is word and antonym .

So word and antonym is here and the template is basically this example format template .

Now this is perfectly fine .

Now , in order to give these examples first to my L N M model , we will specifically use this few short template .

video content Image generated by Wilowrid

OK ?

And I'm just going to give you the format over here .

And where did I get this format ?

It is from the documentation itself , right ?

So here I'm initializing free short template and I'm giving first of all some of the examples , right ?

So these are the examples we want to insert into the prompt .

The examples is nothing but word happy antonym , sad , something like this .

OK ?

And what is the uh prompt template that we are using ?

This is how the prompt template we are using over here , the prompt template is available .

Then the prefix I'm giving , give the antonym of every input that basically I'm saying the model to give the opposite of every input .

OK ?

And the suffix will be in this format and the input variable will be input for this .

OK ?

And the example separated , there's some parameters you can find out all the information over here .

But let me show you after running this OK ?

Which is , which is super important .

OK ?

Now let's say if I go ahead and print few short prompt , so few short prompt is nothing but the same thing .

OK ?

video content Image generated by Wilowrid

And here I want to probably write dot format and the input parameter that I'm giving to this is nothing but this input , right ?

So let's say if I give this input is equal to and let's say I'm going with the word big .

So this is how the entire uh uh the input will be given to the L N M models .

So given the antonym of every input , what is going to happen with some examples ?

This is my word .

Now , I'll be waiting for this specific answer .

OK .

Now the L L M models will be able to understand in a better way .

OK .

Now directly to run this all I have to do create a chain , OK ?

Create a chain over here .

And I am going to give this few short prompt over here .

So the same few short prompt over here and the chain will just run with input is called too big .

OK ?

I can either run like this or I can also say chain dot run , see this if I write chain dot run and I'll give my input as big , right ?

So I will be getting the output over here .

video content Image generated by Wilowrid

You'll be see big as small .

So if I remove do and probably keep the previous format , I will be able to give the entire things like what is my input is to big text is equal to small .

So in short , I'm first of all , this pro few short prompt is basically giving the entire examples to my L N M model .

And then finally , I'm able to get the output .

So these are some of the examples with respect to this .

Uh uh Here , I also explained you for short templates , uh short prompt template so that you can also give an example .

So quick summary about the prompt engineering guys .

Again , a very good thing , probably you want to create your own chatbot models on models .

At that time , you want to give your own input format .

You can definitely do it with the prompt template itself .

And I've heard like many places now prompt engineering job is also on fire .

So that basically means a lot of openings where people get highly paid .

But at the end of the day , my work is to basically teach you regarding that and I have done that .

Ok .

So yes , this was it from my side .

I will see you all in the next video .

Have a great day .

Partnership

Attention YouTube vloggers and media companies!
Are you looking for a way to reach a wider audience and get more views on your videos?
Our innovative video to text transcribing service can help you do just that.
We provide accurate transcriptions of your videos along with visual content that will help you attract new viewers and keep them engaged. Plus, our data analytics and ad campaign tools can help you monetize your content and maximize your revenue.
Let's partner up and take your video content to the next level!
Contact us today to learn more.