WEBVTT

00:00.020 --> 00:03.800
Hey, in this video you're going to learn how to use Lange chain chat models.

00:03.830 --> 00:09.530
Chat models work by having a history of chat messages where you can talk to them over and over again.

00:09.530 --> 00:13.070
As you'll see, they're the most common type of interface when working with a large language model.

00:13.070 --> 00:16.790
Firstly, we're going to install a couple of packages inside of the notebook.

00:16.790 --> 00:18.920
So I'm going to install Lange chain Lange chain open.

00:18.920 --> 00:22.010
I will also install Lange Chain Community.

00:22.190 --> 00:25.520
And we'll also upgrade these packages and put that on a quiet.

00:25.760 --> 00:28.880
If you've already installed this you can skip this step.

00:28.880 --> 00:33.770
The next thing that you'll need to do is import the OS package, and you'll need to set an API key.

00:33.800 --> 00:39.260
I'm not going to do this step, because I've actually already got an OpenAI key set in my terminal window.

00:39.290 --> 00:45.260
Then what you'll need to do is then we're going to import the chat model directly from Lange chain OpenAI.

00:45.290 --> 00:50.150
And what's interesting about this is you can easily switch out chat models by installing a different

00:50.150 --> 00:52.550
package and then importing a different chat model.

00:52.580 --> 00:57.800
The next thing that you need to do once you have done this is you will then need to create a client.

00:57.800 --> 01:00.620
So we'll call it chat OpenAI.

01:00.650 --> 01:03.590
And then you can specifically pick whatever type of model you want.

01:03.620 --> 01:06.110
I'm going to pick GPT four oh mini.

01:06.140 --> 01:08.900
And you can also have different types of parameters.

01:08.900 --> 01:12.170
So for example you could put the temperature in here etc..

01:12.200 --> 01:14.870
Let's have a look at how we can then actually utilize that.

01:14.870 --> 01:20.870
So I could do chat dot invoke and invoke is a function inside of lang chain, which allows us to operate

01:20.900 --> 01:22.490
on a model or a chain.

01:22.490 --> 01:27.290
So I could say what is the capital of Egypt?

01:27.890 --> 01:32.300
And then what you can see here is then we get this returned AI message.

01:39.170 --> 01:44.390
Also notice you can see there is a response metadata which can be quite useful for looking at token

01:44.390 --> 01:44.870
usage.

01:44.870 --> 01:47.120
So we'll look at the response metadata.

01:47.150 --> 01:52.010
You can see we have the token usage, the model name that we used, a system fingerprint and a Finnish

01:52.010 --> 01:52.430
region.

01:52.430 --> 01:53.960
And the log probabilities.

01:53.960 --> 01:58.430
You can access multiple different parts from the invoke and you could save those to your results.

01:58.430 --> 02:04.700
So I could do for example result chat, invoke dot content and then the result now becomes a string.

02:04.700 --> 02:05.930
So I can print the result.

02:05.930 --> 02:09.690
And you can see that the capital of Egypt is Cairo.

02:09.720 --> 02:10.530
Okay, great.

02:10.530 --> 02:12.300
So that's one way that we can do that.

02:12.300 --> 02:18.450
Another way that we'll want to invoke and call the chat model is by actually using messages.

02:18.450 --> 02:20.340
There are multiple different types of messages.

02:20.340 --> 02:24.210
For example system message, human message are the most common.

02:24.240 --> 02:29.700
The system message is quite important and that is most respected inside of the large language model.

02:29.700 --> 02:31.590
It's often where you'll put the instructions.

02:31.590 --> 02:34.710
The human message is the message from a user.

02:34.710 --> 02:38.250
You also do have the ability to add in AI messages as well.

02:38.250 --> 02:46.260
We're then going to have some text and we'll say what would be a good company name for a company that

02:46.260 --> 02:47.520
makes colourful socks?

02:47.520 --> 02:50.910
And then we're going to have a messages Python list.

02:50.910 --> 02:56.010
And inside that we're then going to put a system message which says you are a helpful assistant that

02:56.010 --> 02:59.640
generates company names, and then we're going to have a human message.

02:59.640 --> 03:03.030
And for that we're actually going to put in the text here.

03:03.030 --> 03:05.460
So inside of this content we're going to put that.

03:05.460 --> 03:09.930
And then what you'll see here is we're then following on with this pattern of chat dot invoke messages

03:10.110 --> 03:10.890
content.

03:10.950 --> 03:14.940
And then what we're then going to do is have a look and see what that gives us.

03:14.970 --> 03:16.590
So we'll then print the result here.

03:18.150 --> 03:24.870
And you can see what we're doing is we're now adding in a multiple messages into our chat model.

03:24.900 --> 03:25.320
Right.

03:25.350 --> 03:26.640
So it's got a system message.

03:26.640 --> 03:30.810
We're then adding a human message and we're putting in the text directly inside of that.

03:30.810 --> 03:37.740
Then we're invoking the chat model with that history of messages and getting the content out into the

03:37.740 --> 03:40.740
result variable, and then printing that out.

03:41.190 --> 03:45.750
There is one other way of doing this, which is a more legacy way of doing this.

03:45.750 --> 03:48.150
So for example, we could have the original text.

03:48.180 --> 03:52.140
We could then also just say, let's have a bunch of messages.

03:52.140 --> 03:59.100
And in that we could say the system message is equal to the content of text.

03:59.370 --> 04:05.160
And then we could also then have the chat model directly operate on those messages.

04:05.160 --> 04:07.500
So you can see Lang chain is deprecating this.

04:07.500 --> 04:09.390
So I wouldn't recommend using this.

04:09.390 --> 04:11.460
But you might see this in the code somewhere.

04:11.490 --> 04:17.340
In general you should favor the invoke function, and we'll see that there are lots of different types

04:17.340 --> 04:19.380
of functions you can use in lang chain.

04:19.380 --> 04:24.930
Dot invoke will only give you a single result, but in future videos you'll learn how to generate multiple

04:24.930 --> 04:25.800
results.

04:26.010 --> 04:30.750
Also, one of the other benefits that I just want to highlight about Lang Chain is that it's essentially

04:30.750 --> 04:35.400
a piece of glue around the large language model, so you can easily switch the model out.

04:35.400 --> 04:43.710
So for example, if we were to go to if we were to go to the Lang Chain community, click on the integrations.

04:43.710 --> 04:48.090
What you'll see is there's a variety of providers that you can easily interact with.

04:48.090 --> 04:54.450
So for example, if we wanted to interact with pinecone, that would be Lang chain Dash pinecone if

04:54.450 --> 04:57.840
we wanted to interact with for example, a other language model.

04:57.840 --> 05:00.750
So anthropic that would be lang chain anthropic.

05:00.750 --> 05:06.420
And then we can then go and have a look at that on PyPI and see this is how the package would be imported

05:06.420 --> 05:08.250
for a similar type of model.

05:08.280 --> 05:14.220
So then you would basically pip install lang chain anthropic port from lang chain anthropic.

05:14.220 --> 05:16.300
And I know it's chat anthropic.

05:16.300 --> 05:21.820
And then we would then create our model and we could call this chat anthropic and pass in and pass in

05:21.820 --> 05:22.960
a specific version of Claude.

05:22.990 --> 05:27.820
Then we could have our messages which is equal to a human message.

05:27.820 --> 05:33.010
And then we could also do what we had before where we're taking a chat model, invoking that with some

05:33.010 --> 05:34.720
messages, getting the content out.

05:34.720 --> 05:39.100
And you can see from here we're then easily able to swap the model out, right.

05:39.130 --> 05:41.500
So we're getting our sock names just like we were before.

05:41.500 --> 05:46.540
But the important point is that actually we've already got the messages defined so we could get rid

05:46.540 --> 05:47.140
of this.

05:47.170 --> 05:52.810
And essentially all we've done is we've just changed one piece of code here, the word model for the

05:52.810 --> 05:55.120
variable earlier on which we had for chat.

05:55.120 --> 05:59.170
So you can easily see that all of the rest of the code stays the same.

05:59.200 --> 06:04.690
We're only changing out the chat model, and Lang chain has built a variety of providers that allows

06:04.690 --> 06:08.200
you to easily switch models in and out via chat models.

06:09.400 --> 06:14.710
In the next video, we'll learn how to easily set up prompt templates which allow you to inject dynamic

06:14.710 --> 06:19.180
variables directly into your prompts using Lang Chain Chat prompt templates.
