You have a look at what a chat prompt template is.
It is a template that allows you to create a list of messages that will form your chat history.
And you can have these curly braces in here.
So you can see we've got curly braces name or curly braces user input.
When you invoke a template, essentially what's going to happen is for each individual curly braces
with the name of that, you're going to inject a variable which will then output a chat prompt value.
Look at the messages.
You'll see.
These then have become formatted strings, allowing you to inject variables at runtime into your prompts.
Let's have a look at an implementation of this inside of Lang chain.
Now we're going to import a new import which is Lang chain called prompts dot chat.
And we're going to import the chat prompt template class.
Then what we're going to do is we're just going to start by doing chat prompt template.
And then we're going to make a template here.
And then what we're going to do is we're going to have a chat prompt template.
And we're going to do from messages, which is a function which takes a list of tuples.
And in each tuple on the first index of that tuple you have the message type.
So you can see the first message is a system type message that we learned in the previous video.
We've told it that it's a helpful assistant that's capable of generating company names.
And we also have a human message which talks about a text.
Now, a text is a dynamic variable.
So what we can then do is just like we can invoke in the chat model, we can also invoke a chat prompt
template.
So I'm going to invoke and then we pass a Python dictionary.
You just have to look in each of your chat prompt template variables and see what are the dynamic curly
braces that you're going to need to replace.
So I have text.
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
colorful socks.
And then what you'll see is we actually get a result.
And we're just going to have a look at that.
So I'm going to print the result.
And then we have a result which if we have a look at the type you can see this is a chat prompt value.
We can pass a chat prompt value directly into a chat model.
Let's have a look at how we could do that.
Firstly we're going to set up a model.
So I'm going to do model is equal to chat OpenAI.
And we're using GPT four mini.
And then what we're going to have is an AI LLM result variable.
And we're going to do model dot invoke.
And we're going to take the result, which is the result of formatting our prompt by invoking it.
Then we're going to invoke based on the template.
And we're going to store that in an AI LLM result.
And then just like we did in the previous video, do print AI LLM result dot contents, we're going
to use that dot content key to see what the actual value is.
So again you can see how this is kind of flowing.
We first have now made a prompt template which has a list of messages.
The first message being a system message, the second message being a human message.
You could even add in AI messages as well, like you're seeing in the suggestion.
And as well as that, when we have curly braces for each curly brace, we have to replace the text.
If I, for example, uncomment this code and I try to invoke this, and I try and do this without,
it throws an error and it says that it actually is missing a variable called text expected text received.
But we didn't get any.
So what you need to know is that every time you have curly braces, you will have to add those into
the dictionary, which will then invoke the prompt template.
So again we're then getting the prompt template.
We're formatting it into a list of messages which we're then setting up a chat model.
And then with the chat model we are invoking the result.
And then once we have the result back from the LLM, we're then doing the dot content on that to get
the actual output.