1 00:00:00,630 --> 00:00:06,180 Besides simple prompt templates, there are also chat prompt templates. 2 00:00:07,430 --> 00:00:12,960 Prompt templates are used for tasks that involve generating text, such as 3 00:00:12,970 --> 00:00:18,360 answering questions or completing sentences, and chat prompt templates are 4 00:00:18,370 --> 00:00:23,320 specifically designed for tasks that involve engaging in conversations. 5 00:00:24,110 --> 00:00:30,180 As we ve already discussed, the ChatCompletions API consists of a list of 6 00:00:30,190 --> 00:00:35,660 messages, SystemMessage, HumanMessage, or the prompt and a message. 7 00:00:36,850 --> 00:00:43,820 I will import some classes from the Langchain library, from LangchainPrompts, 8 00:00:44,530 --> 00:00:53,600 Import, ChatPromptTemplate and the HumanMessagePromptTemplate, and from 9 00:00:53,610 --> 00:01:03,880 LangchainCore, Messages, Import, SystemMessage. 10 00:01:07,680 --> 00:01:10,090 HumanMessagePromptTemplate is used to 11 00:01:10,100 --> 00:01:16,470 create dynamic prompts from the user s text and the SystemMessage is used to 12 00:01:16,480 --> 00:01:17,830 format the system message. 13 00:01:19,280 --> 00:01:22,570 Note that there is also a class called 14 00:01:24,600 --> 00:01:29,470 ChatSystemMessagePromptTemplate if you want to generate dynamic system messages. 15 00:01:33,270 --> 00:01:38,580 Now, I will create a new ChatPromptTemplate object from a list of messages. 16 00:01:39,510 --> 00:01:52,620 ChatTemplate equals ChatPromptTemplate .fromMessages and the argument will be a list. 17 00:01:53,330 --> 00:02:02,830 The first message is the SystemMessage and the second message is the 18 00:02:02,840 --> 00:02:07,730 HumanMessagePromptTemplate that represents the user s input. 19 00:02:11,990 --> 00:02:12,660 Now, the arguments. 20 00:02:14,130 --> 00:02:18,680 SystemMessage of content equals and the string. 21 00:02:20,840 --> 00:02:31,710 You respond only in the JSON format and the HumanMessagePromptTemplate 22 00:02:31,720 --> 00:02:45,480 .fromTemplate, I m calling the fromTemplate function of top n countries 23 00:02:45,490 --> 00:02:51,560 in area in curly braces by population. 24 00:02:53,030 --> 00:02:57,900 The placeholders n and area will be 25 00:02:57,910 --> 00:03:00,600 replaced with the actual input text. 26 00:03:02,250 --> 00:03:04,300 Next, I will format the 27 00:03:04,310 --> 00:03:11,060 ChatPromptTemplate object by replacing the placeholders with the provided value. 28 00:03:12,490 --> 00:03:28,780 So, messages equals ChatTemplate .formatMessage of n equals 10 as a string 29 00:03:28,790 --> 00:03:31,020 and area equals Europe. 30 00:03:34,410 --> 00:03:37,980 The resulting messages list contains the 31 00:03:37,990 --> 00:03:41,820 formatted SystemMessage and HumanMessage. 32 00:03:42,410 --> 00:03:45,580 Let s print it print messages and I 33 00:03:45,590 --> 00:03:46,120 running the code. 34 00:03:48,410 --> 00:03:50,460 This is the formatted prompt. 35 00:03:51,290 --> 00:03:54,580 It contains a SystemMessage and a HumanMessage. 36 00:03:57,800 --> 00:04:01,590 n in curly braces was replaced with pen 37 00:04:01,600 --> 00:04:05,970 and area in curly braces was replaced with Europe. 38 00:04:21,070 --> 00:04:35,560 From going with the defaults output equals llm .invoke of messages and I m 39 00:04:35,570 --> 00:04:39,560 printing the output printOutput .content. 40 00:04:42,480 --> 00:04:48,420 I m running the code very well. 41 00:04:49,210 --> 00:04:55,680 Notice how it responded in JSON according to the instructions in the SystemPrompt. 42 00:05:01,830 --> 00:05:05,420 You ve learned about prompt templates and chat prompt templates. 43 00:05:05,930 --> 00:05:10,320 They give you a lot of flexibility in the way you build your chat prompts. 44 00:05:11,250 --> 00:05:18,780 Some benefits of using prompt templates include reusability, scalability and accuracy. 45 00:05:19,670 --> 00:05:26,240 For example, if you want a response with the top 5 countries by population in the 46 00:05:26,250 --> 00:05:34,700 world, you can simply change the argument of chatTemplate .formatMessage, 5 and 1. 47 00:05:37,450 --> 00:05:39,380 You don t have to change the entire prompt. 48 00:05:43,140 --> 00:05:45,930 We are done with prompt templates for the 49 00:05:45,940 --> 00:05:51,470 moment, please take your time to fully understand them as they are a key 50 00:05:51,480 --> 00:05:53,030 component of Langchain.