WEBVTT

00:00.020 --> 00:02.450
You have a look at what a chat prompt template is.

00:02.480 --> 00:08.000
It is a template that allows you to create a list of messages that will form your chat history.

00:08.000 --> 00:10.790
And you can have these curly braces in here.

00:10.790 --> 00:14.900
So you can see we've got curly braces name or curly braces user input.

00:14.930 --> 00:21.260
When you invoke a template, essentially what's going to happen is for each individual curly braces

00:21.260 --> 00:27.470
with the name of that, you're going to inject a variable which will then output a chat prompt value.

00:27.500 --> 00:28.640
Look at the messages.

00:28.640 --> 00:29.120
You'll see.

00:29.150 --> 00:35.960
These then have become formatted strings, allowing you to inject variables at runtime into your prompts.

00:35.990 --> 00:39.110
Let's have a look at an implementation of this inside of Lang chain.

00:39.140 --> 00:45.140
Now we're going to import a new import which is Lang chain called prompts dot chat.

00:45.140 --> 00:48.560
And we're going to import the chat prompt template class.

00:48.590 --> 00:52.850
Then what we're going to do is we're just going to start by doing chat prompt template.

00:52.850 --> 00:55.190
And then we're going to make a template here.

00:55.190 --> 00:58.940
And then what we're going to do is we're going to have a chat prompt template.

00:58.940 --> 01:04.490
And we're going to do from messages, which is a function which takes a list of tuples.

01:04.490 --> 01:08.870
And in each tuple on the first index of that tuple you have the message type.

01:08.870 --> 01:13.010
So you can see the first message is a system type message that we learned in the previous video.

01:13.040 --> 01:17.220
We've told it that it's a helpful assistant that's capable of generating company names.

01:17.220 --> 01:21.810
And we also have a human message which talks about a text.

01:21.840 --> 01:23.730
Now, a text is a dynamic variable.

01:23.730 --> 01:29.430
So what we can then do is just like we can invoke in the chat model, we can also invoke a chat prompt

01:29.430 --> 01:29.910
template.

01:29.910 --> 01:33.120
So I'm going to invoke and then we pass a Python dictionary.

01:33.150 --> 01:38.430
You just have to look in each of your chat prompt template variables and see what are the dynamic curly

01:38.430 --> 01:39.930
braces that you're going to need to replace.

01:39.930 --> 01:41.040
So I have text.

01:41.040 --> 01:45.300
So I'm going to put here and I'm going to say what would be a good company name for a company that makes

01:45.300 --> 01:46.140
colorful socks.

01:46.140 --> 01:49.020
And then what you'll see is we actually get a result.

01:49.560 --> 01:51.330
And we're just going to have a look at that.

01:51.330 --> 01:53.040
So I'm going to print the result.

01:53.760 --> 02:01.260
And then we have a result which if we have a look at the type you can see this is a chat prompt value.

02:01.290 --> 02:05.190
We can pass a chat prompt value directly into a chat model.

02:05.190 --> 02:06.750
Let's have a look at how we could do that.

02:06.750 --> 02:08.670
Firstly we're going to set up a model.

02:08.670 --> 02:11.100
So I'm going to do model is equal to chat OpenAI.

02:11.100 --> 02:12.900
And we're using GPT four mini.

02:12.900 --> 02:16.140
And then what we're going to have is an AI LLM result variable.

02:16.140 --> 02:17.880
And we're going to do model dot invoke.

02:17.880 --> 02:24.090
And we're going to take the result, which is the result of formatting our prompt by invoking it.

02:24.090 --> 02:27.090
Then we're going to invoke based on the template.

02:27.110 --> 02:29.690
And we're going to store that in an AI LLM result.

02:29.690 --> 02:35.090
And then just like we did in the previous video, do print AI LLM result dot contents, we're going

02:35.090 --> 02:38.360
to use that dot content key to see what the actual value is.

02:38.360 --> 02:40.940
So again you can see how this is kind of flowing.

02:40.940 --> 02:45.410
We first have now made a prompt template which has a list of messages.

02:45.410 --> 02:49.490
The first message being a system message, the second message being a human message.

02:49.490 --> 02:53.300
You could even add in AI messages as well, like you're seeing in the suggestion.

02:53.300 --> 02:59.420
And as well as that, when we have curly braces for each curly brace, we have to replace the text.

02:59.420 --> 03:04.970
If I, for example, uncomment this code and I try to invoke this, and I try and do this without,

03:05.000 --> 03:11.150
it throws an error and it says that it actually is missing a variable called text expected text received.

03:11.150 --> 03:12.380
But we didn't get any.

03:12.380 --> 03:17.690
So what you need to know is that every time you have curly braces, you will have to add those into

03:17.690 --> 03:20.990
the dictionary, which will then invoke the prompt template.

03:20.990 --> 03:23.630
So again we're then getting the prompt template.

03:23.630 --> 03:28.520
We're formatting it into a list of messages which we're then setting up a chat model.

03:28.520 --> 03:31.460
And then with the chat model we are invoking the result.

03:31.460 --> 03:37.370
And then once we have the result back from the LLM, we're then doing the dot content on that to get

03:37.370 --> 03:38.720
the actual output.
