WEBVTT

00:00.240 --> 00:03.400
Let's define a variable called information.

00:03.400 --> 00:05.560
And let me put some placeholders here.

00:05.760 --> 00:09.440
And here we want to put here some text about Elon Musk.

00:10.880 --> 00:14.000
So let me go to Google and let me search Elon Musk.

00:14.520 --> 00:18.120
And let me take the information from Wikipedia.

00:18.400 --> 00:20.200
So let me just copy here.

00:20.760 --> 00:24.240
Let's copy here this text only the first part.

00:24.480 --> 00:25.680
And let me paste it here.

00:26.080 --> 00:29.280
So right here we have some information about Elon Musk.

00:29.280 --> 00:33.400
And this information here we want to eventually propagate to the LM.

00:33.840 --> 00:35.960
We want to do that through a prompt template.

00:36.160 --> 00:39.280
And for that let's write a template first.

00:39.560 --> 00:44.200
So it's going to be this template over here given the information.

00:44.200 --> 00:50.360
And right now we have curly brackets information about a person I want you to create a short summary

00:50.360 --> 00:52.920
into interesting facts about them.

00:53.320 --> 00:59.920
So from this string here you can probably guess that this information placeholder here, it's not going

00:59.920 --> 01:00.400
to stay.

01:00.400 --> 01:04.770
It's going to be substituted by the information variable.

01:04.770 --> 01:12.290
We have defined a couple of lines earlier with the Elon Musk information, but it's going to be filled

01:12.290 --> 01:13.250
in runtime.

01:13.810 --> 01:16.490
And if we want we can do this programmatically.

01:16.490 --> 01:21.650
And we're going to do this with the prompt template object of LinkedIn.

01:21.770 --> 01:24.970
And we initialize an object of prompt templates.

01:24.970 --> 01:28.410
And we initialize it with the template.

01:28.410 --> 01:31.930
So this is the summary template that we wrote before.

01:32.170 --> 01:39.970
And then we give it input variables which is going to be a list containing the keys of what we're going

01:39.970 --> 01:43.530
to be filling and plugging in in runtime.

01:43.730 --> 01:50.810
And this should match the placeholders between the curly brackets that we have in our template here.

01:51.290 --> 01:55.050
Now this is very much reminding us of an F string.

01:55.250 --> 01:57.650
And you might be thinking, why do we need all of this?

01:57.690 --> 02:00.290
And why don't we just use f strings instead?

02:00.810 --> 02:04.490
And this is because the prompt template is going to enforce that.

02:04.490 --> 02:12.330
We supply exactly the variables that we expect, and if we forget an input or misspell it, we get clean

02:12.330 --> 02:15.730
air instead sending us the broken prompt.

02:16.090 --> 02:23.010
It's also going to give us reusability and more clarity, because those prompts can become reusable

02:23.010 --> 02:27.010
in another link chain chain if we want to.

02:27.210 --> 02:29.730
And it's a first class citizen in link chain.

02:29.730 --> 02:33.370
So this means it's going to get logged, it's going to get traced.

02:33.370 --> 02:35.490
And we'll be showing this very very soon.

02:35.730 --> 02:40.610
And overall it's going to make our life debugging much more easier.

02:40.890 --> 02:46.570
And it's going to also be safer against prompt injection because it can enforce strict formatting and

02:46.570 --> 02:47.330
structure.

02:47.530 --> 02:51.850
And this is very powerful, especially when we're going to be using output parsers.

02:51.850 --> 02:55.850
And I know I'm talking a lot of things right now and a lot of buzzwords.

02:55.850 --> 02:59.050
And don't worry, you'll understand everything by the end of this course.

02:59.250 --> 03:05.980
But just to conclude, if strings tend to encourage just jamming the text here, which is fine, but

03:05.980 --> 03:10.180
if you want our code to be more reliable, we really want to use those prompt templates.

03:10.900 --> 03:15.060
All right, let's now create a OpenAI chat model.

03:15.060 --> 03:17.420
And I'm going to call it LLM.

03:18.340 --> 03:24.500
And this is the way we're going to interact with the OpenAI model GPT five.

03:24.660 --> 03:32.180
And this is what eventually is going to make the API calls to OpenAI to send the text and the prompts

03:32.180 --> 03:34.860
and to get back the response from Dlrm.

03:35.060 --> 03:37.700
And who's going to pay for all those API calls?

03:37.940 --> 03:44.980
And I remind you that in the previous video, we created an API key in OpenAI, which is connected to

03:45.020 --> 03:48.100
our account with our payment information configured.

03:48.300 --> 03:55.500
And the API key is stored in an environment variable, which is called OpenAI API key, which is loaded

03:55.500 --> 03:56.740
into the runtime.

03:56.780 --> 04:02.780
Now LinkedIn is going to search for this environment variable and is going to use this as credentials

04:02.780 --> 04:05.230
to talk with OpenAI API.

04:05.830 --> 04:07.750
And I give you an exercise.

04:07.750 --> 04:13.870
Maybe if you want, you can check out the source code of the chat OpenAI model and see where it happens

04:13.870 --> 04:14.270
there.

04:15.830 --> 04:19.830
And we initialize the chat model with temperature equals to zero.

04:20.230 --> 04:26.510
And temperature will control how random or creatives versus strict and deterministic.

04:26.550 --> 04:28.350
The model response is going to be.

04:28.750 --> 04:37.550
So low values between 0 and 0.3 is going to make the response deterministic, factual and probably repeatable.

04:37.910 --> 04:45.190
And it's good for summarization, for code, for instructions, for test and high values like above

04:45.230 --> 04:47.550
0.8 until one.

04:47.670 --> 04:50.350
It's going to get us very creative results.

04:50.350 --> 04:54.870
And it's good for poetry and fiction and out of the box ideas.

04:54.910 --> 05:00.630
And if you're interested about how temperature works, then feel free to check out the theoretical section

05:00.630 --> 05:02.190
where I explain this.

05:02.430 --> 05:02.830
All right.

05:02.830 --> 05:04.390
Let's go back to the code.

05:04.390 --> 05:09.840
And the second argument that we give is going to be model equals to GPT five.

05:10.400 --> 05:14.320
And now we're going to have a link chain chat.

05:14.320 --> 05:22.480
OpenAI object which eats under the hood, is going to be using the OpenAI SDK to make API calls to OpenAI

05:22.640 --> 05:23.800
with our API key.

05:24.120 --> 05:27.840
And now we can go and create our first chain.

05:28.400 --> 05:35.200
And we have the variable chain which is going to be summary prompt template the pipe operator.

05:35.200 --> 05:36.520
And then ln.

05:36.880 --> 05:39.400
So let me break down what's happening over here.

05:39.560 --> 05:44.880
And we're using something which is called the link chain expression language or LCL.

05:45.280 --> 05:54.760
And in this LCL syntax we create a chain by composing two components a prompt template and a large language

05:54.760 --> 05:55.280
model.

05:55.560 --> 06:00.000
Now the prompt template is going to come from the variable summary prompt template.

06:00.000 --> 06:02.800
And this is going to format input variables.

06:02.800 --> 06:10.520
And in this case information into a prompt string which will eventually be propagated to the LM, and

06:10.520 --> 06:19.680
the LM variable is a chat open AI object, which takes in an input a prompt string and generates a text

06:19.720 --> 06:20.400
response.

06:20.440 --> 06:27.920
Now, this pipe operator in an expression language is going to create a new runnable chain.

06:27.920 --> 06:35.560
So this is a new term runnable by connecting the output of the left component as an input to the right

06:35.560 --> 06:36.360
component.

06:36.640 --> 06:45.600
So summary prompt template pipe lm means that we first want to format the input using the prompt template,

06:45.800 --> 06:51.600
and then to pass the resulting prompt string into the LM to generate the response.

06:51.840 --> 06:56.440
So the resulting chain is called a runnable object.

06:56.440 --> 06:58.720
So this means we can invoke it.

06:58.760 --> 07:03.760
Use the invoke method with some input variables matching the prompt template.

07:03.760 --> 07:06.120
And here in this case it's going to be information.

07:06.640 --> 07:12.410
Now I know this is a lot to digest and to be honest, the LinkedIn expression language.

07:12.410 --> 07:15.650
I think this is the hardest thing to comprehend in LinkedIn.

07:15.650 --> 07:21.290
So if you don't understand what's happening here right from the first time, this is totally okay.

07:21.530 --> 07:25.970
We are going to dive deep into this and see the implementation itself, and we're going to have many

07:25.970 --> 07:27.250
examples during the course.

07:27.490 --> 07:32.410
And at this point of the course, I'm not expecting you to know every bit and byte of it.

07:32.450 --> 07:37.410
So we just need to know from a very high level that we read these from left to right.

07:37.410 --> 07:43.490
And here we are plugging in the result of formatting the prompt template into the LM.

07:43.530 --> 07:45.050
So when do we run this.

07:45.210 --> 07:50.010
So we do this using the invoke method that the runnable interface has.

07:50.010 --> 07:57.690
So what's going to happen here is that when we run change dot invoke we actually going to invoke the

07:57.730 --> 08:03.490
invoke method of the summary prompt template which is a class of prompt template.

08:03.690 --> 08:10.140
And when we invoke it, we're going to give it the input of the information key to be holding the information

08:10.140 --> 08:10.700
value.

08:10.700 --> 08:14.660
And in this case it's going to be the Elon Musk information.

08:15.140 --> 08:23.700
So once we do that then we're going to create a prompt value, which is sort of like a fancy word of

08:23.700 --> 08:24.540
saying a string.

08:24.740 --> 08:28.700
And that string is going to be piped to the LM method.

08:28.820 --> 08:37.180
And this pipe is actually some very clever way to go and call the LM invoke method, which it has with

08:37.180 --> 08:40.660
the input to be the prompt value.

08:40.660 --> 08:43.540
So the final prompt that we want to send to the LM.

08:43.700 --> 08:49.340
So this is just a clever way to chain everything up together and hence the name of link chain.

08:49.500 --> 08:55.820
So eventually when we're going to run this code we are going to take this string that you see right

08:55.820 --> 08:56.660
over here.

08:56.660 --> 08:59.620
We're going to plug in the information of Elon Musk.

08:59.780 --> 09:03.740
And this string is what is going to be sent eventually to the LM.

09:03.780 --> 09:04.220
Right.

09:04.260 --> 09:07.620
So I know it sounded at the beginning very, very complicated.

09:07.620 --> 09:10.350
But actually what it's doing is quite simple.

09:10.350 --> 09:15.870
And in the next video we're also going to be tracing this and see the exact objects and debugging it.

09:15.870 --> 09:18.350
So don't worry, you will understand this.

09:18.390 --> 09:25.030
And I think this is the hardest concept to comprehend in length chain and the length chain expression

09:25.030 --> 09:25.590
language.

09:25.750 --> 09:29.390
And it is totally okay that you do not understand it by now.

09:29.390 --> 09:35.550
And just a fun fact, even implementing and understanding how agents work is going to be much easier

09:35.550 --> 09:39.950
than understanding this piping operator in the chain expression language.

09:39.950 --> 09:45.750
All right, so just to summarize the code here, all you need to understand is that we're taking the

09:46.110 --> 09:47.750
variable of information.

09:47.750 --> 09:51.150
We're plugging it in to this string over here.

09:51.150 --> 09:56.750
And we're dynamically going to put the value of the information about Elon Musk.

09:56.950 --> 10:01.870
And this is the string that we're going to be sending eventually to the LLM and get a response to it.

10:01.910 --> 10:02.230
Right.

10:02.270 --> 10:04.750
So this is everything you need to know from this video.

10:04.790 --> 10:06.870
You can forget about gene expression language.

10:06.870 --> 10:08.150
You can forget about everything.

10:08.150 --> 10:11.070
As long as you understand this, this is okay.

10:11.190 --> 10:12.430
And we can continue.

10:12.950 --> 10:13.510
All right.

10:13.510 --> 10:18.950
So let me go now and finally run this code and let's see what results do we get.

10:18.950 --> 10:23.910
And I'm going to fast forward it a bit because we're going to use GPT four.

10:24.110 --> 10:30.350
And GPT five is a bit slow LLM it's very capable but it's very slow.

10:30.910 --> 10:32.790
So let me go fast forward it.

10:33.630 --> 10:39.830
And at the end we're going to print response dot content, which is going to be the final string that

10:39.830 --> 10:41.070
the LLM returns.

10:41.230 --> 10:45.270
So we'll be debugging and we'll be tracing all of this in the next video.

10:45.390 --> 10:47.350
So let's just wait to see the output.

10:47.830 --> 10:50.190
All right so we can see now the short summary.

10:50.190 --> 10:54.430
And we can see now two interesting facts on Elon Musk.

10:55.110 --> 10:57.270
So this looks great.

10:57.510 --> 11:00.670
And now you can read it in your own time.

11:00.670 --> 11:03.750
And in the next video we're going to be debugging this.

11:03.790 --> 11:06.030
We're going to be examining the objects.

11:06.030 --> 11:09.430
And we are going to be tracing it with Lindsmith.

11:09.430 --> 11:12.350
And it should clarify everything that we discussed now.
