Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Preparing a Chat Template

00:00 In the previous lesson, you learned about prompt templates and how they’re useful for creating dynamic and consistent prompts. Now in this lesson, you’ll look at a couple of other more specific templates that allow you to create system messages or human messages.

00:14 And the ChatPromptTemplate is an interesting one because you can use it to combine multiple message prompts into one that you can then send off together.

00:23 So let’s take a look at that right in the REPL, because there’s a lot of code to write for this.

00:28 I’ll start off by importing the different prompt templates from langchain.prompts, import, let me see, we need PromptTemplate SystemMessagePromptTemplate.

00:46 We can already guess which type of message this one’s going to produce. Then we have the HumanMessagePromptTemplate, and I will also import the ChatPromptTemplate.

01:00 And then I’m going to start that off with a review_system_template_str. We had a template string before, so I will call this one the review_system_template_str.

01:11 So that’ll produce a system message, and I got that one copied. “Your job is to use patient reviews to answer questions about their experience…” yada yada.

01:21 And let, before that we want some additional context. So I will say, here come the patient reviews

01:30 and then the input field, or I’ll put in the context and maybe add another new line in here and then close the string. Okay, so this is going to be the template for the system, prompt for the reviews.

01:45 And you can see it has one placeholder, which is the {context}. So this is where the patient reviews are going to go. And before that, it has some sort of system instructions.

01:54 Basically what at the beginning you sent with a system message, which before you put into the class system message. And this is also what SystemMessagePromptTemplate is going to produce in the end.

02:07 Alright, let’s go ahead and do that. So the review_system_prompt is going to be a system message that you create by setting up the SystemMessagePromptTemplate.

02:22 And it’ll take as an input, a PromptTemplate object that you instantiate with input variables.

02:32 And here you can pass the input variables that you need. So in this case, that will be the context.

02:40 And if you remember from before, you’ve seen input variables in the output of one of these system messages. They used to be empty in one of the previous lessons, but now you’re actually creating a prompt template and filling it and then passing this to the SystemMessagePromptTemplate, which will create a system message that then has that input variables filled.

03:01 Okay. And then you also need to pass it to template and that will be the review system, what did we call it? Template string. There you go.

03:16 Close all the parentheses we’ve opened here. And we should have a review_system_prompt that is now a SystemMessagePromptTemplate that has as a prompt, is PromptTemplate object, where you can now see the input variables is filled, right?

03:32 So it’s filled with context, which tells the class what is the replacement field in that template that it’s going to replace with something else when it gets the input passed.

03:42 Let’s make another one of those for the human message. This is just going to be a short question, so I don’t need to create a string beforehand. So we’ll just go straight into the review_human_prompt and set up a HumanMessage PromptTemplate object that again as its input to prompt, takes a PromptTemplate object

04:12 with input variables being the question. So you haven’t defined that yet. I’m about to. So the template, I’m just going to pass it directly in here. I’m not setting up a separate string for that, but I’m just creating the string object in here.

04:27 It’ll just basically consist of questions. So there’s nothing else around this template, this template is really just a question.

04:38 It’s a little more exciting with the previous one where we have this additional text around it. But you may also be creating ones like that. And I understand that this may look like doing a lot for something quite easy that you could get simpler, but you’ll see that when you start assembling these pieces together, all this functionality is going to be helpful.

04:58 Now, again, I’ll make a messages list and just put these two prompt templates in there. That was the review_system_prompt and review_human_prompt.

05:13 Oops, 'eeview_human_prompt' is not what I wanted to write. messages = review_system_prompt and review_human_prompt.

05:29 So both of these now sit together in the messages list. You can see two objects, the SystemMessage PromptTemplate object, and the HumanMessagePromptTemplate object.

05:40 Now what are we going to do with those?

05:45 What I’m going to do is I will create yet another prompt template that is the ChatPromptTemplate. So the review_prompt_template, it’s going to be a ChatPromptTemplate object that takes as input variables both, right?

06:04 Because this ChatPromptTemplate is an overarching object that’s going to hold the other ones as well. So here, it’ll need to have access to both the context and the question.

06:22 And messages are going to be the messages.

06:28 Okay, so the review_prompt_template now looks like this. You can see that it’s a ChatPromptTemplate object that has, again, input_variables filled with both context and question.

06:40 And then you can also see that the messages holds the system message from template and the human message from template that you defined before. Okay, a lot going on there.

06:51 Now, let’s use it so that you can actually also see what this is for. I’m going to now define a context.

07:01 “I had a great stay!” So this is an example user review that comes in from your database. And then here’s a question. This is a user of your chatbot that wants to know, “Did anyone have a positive experience?”

07:22 Alright, so now you have context and question. So now you can go ahead and fill this ChatPromptTemplate by passing the context and the question.

07:30 So let’s try it out. I’m going to call this one filled_prompt. And for this I will use my review_prompt_template and then call format_messages() on it and then pass in as context, the context and as question the question.

07:54 And now if you take a look at the filled_prompt, you can see that this is a system message.

08:03 It’s a list again that has as its contents a SystemMessage where, let me find that. Yeah, there it is. Patient review. So this used to be the placeholder, right?

08:14 And it got filled in with the context that you provided. And then it also has a HumanMessage. And again, this said, this got filled in with the question, “Did anyone have a positive experience?”

08:27 So this makes it dynamic and replaceable. These can change. This can be in the course of your program through user input, through queries, through your database.

08:35 These can be different, have different values, and you don’t need to change your code. You can just always fill them and make different API requests with different contents.

08:46 Let’s try it out. chat_model.invoke. I’m going to pass the filled_prompt. So like we did earlier

08:57 and as a response, you get an AI message that has the content: ‘Yes, one patient mentioned having a great stay, which indicates a positive experience.’ which is great, right?

09:06 This is the only review that we gave and correctly inferred that this was a positive one. But this is still a small example, right? But I hope you can see that this makes the prompts a lot more versatile than it would be if you’re just working with raw strings.

09:20 And as your application grows, and as it has different users and needs to make multiple requests to a database, then having this flexibility combined with the reusability of the prompts is very powerful and very useful, and adds a lot to the maintainability of the programs that you’ll write.

09:37 So this is one of the great strengths of LangChain and prompt templates. And in the next lesson, will move on to looking at the next concept, which is going to be the central piece of LangChain, the chains.

Become a Member to join the conversation.