Hey, in this video you're going to learn how to use Lange chain chat models.
Chat models work by having a history of chat messages where you can talk to them over and over again.
As you'll see, they're the most common type of interface when working with a large language model.
Firstly, we're going to install a couple of packages inside of the notebook.
So I'm going to install Lange chain Lange chain open.
I will also install Lange Chain Community.
And we'll also upgrade these packages and put that on a quiet.
If you've already installed this you can skip this step.
The next thing that you'll need to do is import the OS package, and you'll need to set an API key.
I'm not going to do this step, because I've actually already got an OpenAI key set in my terminal window.
Then what you'll need to do is then we're going to import the chat model directly from Lange chain OpenAI.
And what's interesting about this is you can easily switch out chat models by installing a different
package and then importing a different chat model.
The next thing that you need to do once you have done this is you will then need to create a client.
So we'll call it chat OpenAI.
And then you can specifically pick whatever type of model you want.
I'm going to pick GPT four oh mini.
And you can also have different types of parameters.
So for example you could put the temperature in here etc..
Let's have a look at how we can then actually utilize that.
So I could do chat dot invoke and invoke is a function inside of lang chain, which allows us to operate
on a model or a chain.
So I could say what is the capital of Egypt?
And then what you can see here is then we get this returned AI message.
Also notice you can see there is a response metadata which can be quite useful for looking at token
usage.
So we'll look at the response metadata.
You can see we have the token usage, the model name that we used, a system fingerprint and a Finnish
region.
And the log probabilities.
You can access multiple different parts from the invoke and you could save those to your results.
So I could do for example result chat, invoke dot content and then the result now becomes a string.
So I can print the result.
And you can see that the capital of Egypt is Cairo.
Okay, great.
So that's one way that we can do that.
Another way that we'll want to invoke and call the chat model is by actually using messages.
There are multiple different types of messages.
For example system message, human message are the most common.
The system message is quite important and that is most respected inside of the large language model.
It's often where you'll put the instructions.
The human message is the message from a user.
You also do have the ability to add in AI messages as well.
We're then going to have some text and we'll say what would be a good company name for a company that
makes colourful socks?
And then we're going to have a messages Python list.
And inside that we're then going to put a system message which says you are a helpful assistant that
generates company names, and then we're going to have a human message.
And for that we're actually going to put in the text here.
So inside of this content we're going to put that.
And then what you'll see here is we're then following on with this pattern of chat dot invoke messages
content.
And then what we're then going to do is have a look and see what that gives us.
So we'll then print the result here.
And you can see what we're doing is we're now adding in a multiple messages into our chat model.
Right.
So it's got a system message.
We're then adding a human message and we're putting in the text directly inside of that.
Then we're invoking the chat model with that history of messages and getting the content out into the
result variable, and then printing that out.
There is one other way of doing this, which is a more legacy way of doing this.
So for example, we could have the original text.
We could then also just say, let's have a bunch of messages.
And in that we could say the system message is equal to the content of text.
And then we could also then have the chat model directly operate on those messages.
So you can see Lang chain is deprecating this.
So I wouldn't recommend using this.
But you might see this in the code somewhere.
In general you should favor the invoke function, and we'll see that there are lots of different types
of functions you can use in lang chain.
Dot invoke will only give you a single result, but in future videos you'll learn how to generate multiple
results.
Also, one of the other benefits that I just want to highlight about Lang Chain is that it's essentially
a piece of glue around the large language model, so you can easily switch the model out.
So for example, if we were to go to if we were to go to the Lang Chain community, click on the integrations.
What you'll see is there's a variety of providers that you can easily interact with.
So for example, if we wanted to interact with pinecone, that would be Lang chain Dash pinecone if
we wanted to interact with for example, a other language model.
So anthropic that would be lang chain anthropic.
And then we can then go and have a look at that on PyPI and see this is how the package would be imported
for a similar type of model.
So then you would basically pip install lang chain anthropic port from lang chain anthropic.
And I know it's chat anthropic.
And then we would then create our model and we could call this chat anthropic and pass in and pass in
a specific version of Claude.
Then we could have our messages which is equal to a human message.
And then we could also do what we had before where we're taking a chat model, invoking that with some
messages, getting the content out.
And you can see from here we're then easily able to swap the model out, right.
So we're getting our sock names just like we were before.
But the important point is that actually we've already got the messages defined so we could get rid
of this.
And essentially all we've done is we've just changed one piece of code here, the word model for the
variable earlier on which we had for chat.
So you can easily see that all of the rest of the code stays the same.
We're only changing out the chat model, and Lang chain has built a variety of providers that allows
you to easily switch models in and out via chat models.
In the next video, we'll learn how to easily set up prompt templates which allow you to inject dynamic
variables directly into your prompts using Lang Chain Chat prompt templates.