back to indexChat with OpenAI in LangChain - #5
Chapters
0:0 LangChain's new Chat modules
2:9 New LangChain chat in Python
3:14 Using LangChains ChatOpenAI object
4:36 Chat messages in LangChain
6:43 New chat prompt templates
9:5 LangChain human message prompt template
13:18 Using multiple chat prompt templates
17:42 F-strings vs. LangChain prompt templates
19:23 Where to use LangChain chat features?
00:00:00.000 |
With the introduction of OpenAI's new ChatGPT endpoint, 00:00:04.240 |
the LangChain library have very quickly, unsurprisingly, 00:00:13.000 |
unlike previous large language model endpoints, 00:00:16.080 |
the new ChatGPT endpoint is slightly different. 00:00:20.600 |
It takes multiple inputs, and therefore, with LangChain, 00:00:24.960 |
this new sort of approach to calling large language models 00:00:29.660 |
has been supported with its own set of objects and functions. 00:00:34.660 |
So the new ChatCompletion endpoint from OpenAI, 00:00:38.060 |
it differs in the typical large language model endpoints 00:00:41.720 |
in that you can essentially pass in three types of inputs 00:00:55.880 |
The system or system message acts as the initial prompt 00:01:06.220 |
So for example, with ChatGPT, what you would find, 00:01:13.220 |
OpenAI have already passed in a system message to ChatGPT, 00:01:24.060 |
So user messages is like what we write, okay? 00:01:27.200 |
So in ChatGPT, we write something that's a user message. 00:01:30.460 |
And then the other one is the assistant message. 00:01:32.600 |
Those are the responses that we get from ChatGPT, okay? 00:01:35.960 |
So the assistant is what ChatGPT is producing. 00:01:43.320 |
we're feeding in a history of previous interactions as well. 00:01:46.200 |
So we're always gonna have that system message at the top. 00:01:51.760 |
followed by an assistant message, followed by user message, 00:01:55.280 |
So there is some difference with this new endpoint. 00:01:59.160 |
And therefore, how we interact with ChatGPT via LangChain 00:02:19.040 |
okay, so this is the latest version of OpenAI and LangChain. 00:02:39.320 |
so you can follow along at the top of the video 00:02:42.500 |
But this will take us across to this page here. 00:03:03.920 |
If you're on Colab, it will appear just below the cell. 00:03:18.900 |
So for this, we're going to be using the chat GPT model. 00:03:23.900 |
Now, by using this, we're essentially going to default 00:03:31.780 |
So right now, the latest version is actually this here. 00:03:47.260 |
Basically, as they release new versions of this model, 00:03:58.180 |
that would make the completions fully deterministic 00:04:08.540 |
So the chats with chat GPT are kind of structured like this. 00:04:13.340 |
So we have system, user, assistant, user, assistant. 00:04:32.780 |
And the way that we format that is like this, okay? 00:04:36.820 |
In LineChain, they kind of mirror this format. 00:04:44.860 |
a human message object, and an AI message object. 00:04:49.100 |
So to create this up here, we would write this, okay? 00:04:56.900 |
In the order that they have been passed in the conversation. 00:05:32.780 |
Now, if we take a look up here at the initial response 00:05:40.300 |
come to the start and we can see that it's an AI message. 00:05:43.380 |
So it's the same type of object as this here. 00:05:46.820 |
So that means that we can actually just append 00:05:49.860 |
this AI message, our response, directly to messages here, 00:06:02.060 |
And then from there, we can just continue the conversation. 00:06:04.660 |
So we will create a new human message prompt, 00:06:09.860 |
and then we'll send all of those to chat GPT. 00:06:12.660 |
Okay, so now what was the next question I asked? 00:06:16.420 |
Why do physicists believe it can produce a unified theory? 00:06:25.580 |
that they believe that string theory has potential 00:06:28.060 |
to produce a unified theory, because so on and so on. 00:06:32.100 |
Now, that is, I suppose, a core functionality 00:06:46.500 |
So these new prompt templates, we have like a AI message, 00:06:51.020 |
human message, and system message prompt template. 00:06:57.340 |
of the original prompt templates in Lionchain. 00:07:00.780 |
But when you use them, you have a couple of functions 00:07:03.940 |
that will allow you to create your prompt template 00:07:07.540 |
and output it as a system message, AI message, 00:07:12.180 |
And you can also kind of like link them all together 00:07:14.980 |
to create a list of messages that you then just pass 00:07:20.380 |
Now, I'm not super aware of like a huge number of reasons 00:07:36.060 |
And if it seems like something that would actually help you 00:07:41.060 |
with whatever it is you're building, then that's great. 00:07:43.980 |
You now know how, or you will know how to use them. 00:07:53.580 |
So we're going to set up our first system message, 00:08:05.020 |
I'm saying I want the responses to be no more 00:08:07.780 |
than 100 characters long, including white space. 00:08:12.860 |
with a random name like robot or Barbara, okay? 00:08:18.940 |
to see how well it follows these instructions. 00:08:21.220 |
So run this, and now we make our first completion from this, 00:08:26.220 |
and let's see how it does with those instructions. 00:08:35.540 |
And it also didn't give us a sign off there as well. 00:08:49.780 |
It's not very good at following system messages, apparently. 00:08:52.780 |
It's kind of better to pass these instructions 00:08:56.540 |
But we might not want a user to have to specify these things. 00:09:06.740 |
What we're gonna do is for every human message, 00:09:19.180 |
And what I'm going to do is after the question, 00:09:21.900 |
I'm gonna say, can you keep the response to no more 00:09:38.380 |
And we also need to use this chat prompt template. 00:09:40.900 |
I feel like this is a little bit convoluted at the moment, 00:09:55.700 |
This is just like a typical prompt template in LangChain. 00:10:01.020 |
we need to pass it to this chat prompt template 00:10:18.900 |
like system message, human message, AI message, and so on, 00:10:22.220 |
which I found some way of kind of using that. 00:10:27.140 |
So, I mean, I think that's kind of interesting at least. 00:10:42.460 |
and it has messages, a list of messages in there. 00:10:48.860 |
hi AI, how are you, what is quantum physics, right? 00:10:54.380 |
to no more than 100 characters, including white space, 00:10:58.740 |
So that is our template that is being applied based on this. 00:11:35.100 |
or we can just create a string out of it, okay? 00:11:38.260 |
So this would, I suppose, be pretty much the same 00:11:57.100 |
Here, I'm just kind of throwing it all together. 00:12:12.740 |
which is essentially going to give us this human message. 00:12:32.460 |
All right, so you can see now it's listening. 00:12:35.060 |
So we said 100 characters here, didn't really work, 00:12:47.260 |
and we also have this signed off with bot route. 00:12:50.060 |
So that is working by adding those instructions 00:12:53.380 |
into the user message, we're getting better results. 00:13:03.820 |
and okay, so we've set temperature to zero here, 00:13:07.020 |
and because of that, we would expect the output 00:13:15.540 |
I think it's every time it's outputting the same. 00:13:17.940 |
Okay, cool, and then let's continue with this. 00:13:44.100 |
but maybe in that case, we might need to give some examples 00:13:53.900 |
and we're going to have this sign off inputs or variables. 00:13:58.540 |
we're just going to pass in the input there, right? 00:14:04.820 |
we're not going to pass in those instructions, 00:14:06.900 |
because we're actually going to create this human message, 00:14:09.380 |
and we're also going to create the following AI message 00:14:11.580 |
as an example to the chatbot as to how it should respond. 00:14:24.500 |
like know that we're using AI message prompt template, 00:14:28.300 |
and system message prompt template for each of those. 00:14:34.820 |
So it goes obviously the system message first, 00:14:36.780 |
the human message second, and the AI message third. 00:14:43.220 |
So what we then do is we take our chat prompt, 00:14:53.420 |
so half of what we had before, making it harder. 00:14:56.420 |
I'm going to say the sign off has to be this robot, 00:15:00.140 |
and the input is going to be the same as before. 00:15:04.180 |
And then we're giving an example response, right? 00:15:09.780 |
That example response is going to automatically 00:15:17.820 |
So system message, you are a helpful assistant. 00:15:21.060 |
You keep responses, no more than 50 characters long. 00:15:24.100 |
You sign off every message with robot McRobot, 00:15:26.740 |
so we can see where those are being added there. 00:15:30.220 |
Human message, hi AI, what is quantum physics? 00:15:35.500 |
because we're just passing the input in there. 00:15:39.340 |
Good, it's physics of small things, robot McRobot, okay? 00:15:52.540 |
So we run this, and we get atoms, electrons, photons, 00:16:16.300 |
that we used before where we add in the answer 00:16:18.860 |
in less than the character limit, including white space. 00:16:23.060 |
Okay, we're going to add that to our human message. 00:16:26.980 |
So we're going to create the human message like this. 00:16:29.980 |
So the chat prompt template and so on and so on. 00:16:42.540 |
answering less than 50 characters, including white space. 00:16:56.540 |
So I'm going to remove the most recent message in messages, 00:17:00.660 |
and I'm going to send it with this new human prompt value, 00:17:10.620 |
So let's have a look, make sure we have the right format. 00:17:16.700 |
That's the last correct response we got from the AI. 00:17:19.340 |
And now we have the new modified human message. 00:17:23.100 |
So it's like this, answering less than 50 characters. 00:17:26.260 |
And now we pass that through our chat system again, 00:17:37.260 |
we're telling it in the most recent query again, 00:17:40.500 |
but you need to answer in less than 50 characters. 00:17:45.300 |
is that maybe this is a little bit convoluted, 00:17:47.420 |
and that's not to say that there aren't use cases for this. 00:18:04.460 |
or simpler use cases, or something along those lines. 00:18:09.340 |
as to whether all of the above that we just did 00:18:31.660 |
which is set here, characters, including whitespace, right? 00:18:35.940 |
And the result of that is basically the same. 00:18:55.980 |
like what you're doing, how you prefer to write this, 00:19:22.780 |
Okay, so just wanted to make you aware of that. 00:19:30.660 |
of the new chat features within the Limechain, 00:19:38.980 |
Like the prompt templates, you can use of course, 00:19:44.300 |
if you have a simpler approach to doing these things. 00:19:47.500 |
But yeah, it's cool to see this being implemented 00:19:51.980 |
in Limechain, and although I haven't been through it yet, 00:19:54.860 |
I'm hoping that there will be good integrations 00:20:01.780 |
their conversation memory, their retrieval augmentation, 00:20:09.780 |
Right now, it's kind of like a simple wrapper 00:20:19.260 |
conversation memory, and retrieval augmentation components 00:20:22.140 |
that Limechain offers, we'll get a tight integration 00:20:24.820 |
between those, and that's where this will be useful. 00:20:31.460 |
I hope all of this has been useful and interesting. 00:20:33.900 |
But for now, thank you very much for watching,