1 00:00:00,830 --> 00:00:05,620 So far, you've learned how to ask questions over the content of custom 2 00:00:05,630 --> 00:00:07,360 documents using Langchain. 3 00:00:08,670 --> 00:00:11,700 This approach is called REG, Retrieval 4 00:00:11,710 --> 00:00:13,420 Augmented Generation. 5 00:00:13,970 --> 00:00:16,480 You've also learned how to add memory 6 00:00:16,490 --> 00:00:22,520 using the conversational retrieval chain, enabling users to ask follow -up questions. 7 00:00:23,490 --> 00:00:26,680 However, we've used the default prompt so far. 8 00:00:27,410 --> 00:00:29,680 Let's explore how to change the system 9 00:00:29,690 --> 00:00:35,720 prompt and use prompt engineering techniques to ask questions in specific ways. 10 00:00:40,290 --> 00:00:45,420 Note that we discussed prompt templates in the first section of the course, so 11 00:00:45,430 --> 00:00:47,780 you can take a look there if you feel the need. 12 00:00:48,930 --> 00:00:51,440 In a new cell, I will copy and paste the 13 00:00:51,450 --> 00:00:56,400 code from the previous video and change it to add the custom prompt. 14 00:01:02,850 --> 00:01:08,200 From Langchain prompts, I will import three classes, ChatPromptTemplate, 15 00:01:08,730 --> 00:01:12,360 SystemMessagePromptTemplate and HumanMessagePromptTemplate. 16 00:01:14,000 --> 00:01:16,800 I am defining the template for the system 17 00:01:16,810 --> 00:01:21,900 prompt, this sets the general behavior of the LLM. 18 00:01:24,390 --> 00:01:29,660 I am using a multi -line raw string because the string may contain both 19 00:01:29,670 --> 00:01:31,020 single and double quotes. 20 00:01:33,640 --> 00:01:40,430 Use the following pieces of context to 21 00:01:40,440 --> 00:01:42,650 answer the user's question. 22 00:01:49,020 --> 00:01:53,550 I am adding some dashes and the context 23 00:01:53,560 --> 00:01:57,790 colon and the context in curly braces. 24 00:01:58,720 --> 00:02:01,130 This is the dynamic part of the prompt. 25 00:02:01,780 --> 00:02:05,750 I will also enclose it in triple backticks. 26 00:02:08,810 --> 00:02:11,160 Now the template for the user prompt. 27 00:02:11,890 --> 00:02:22,600 UserTemplate equals and the multi -line string question colon and question in 28 00:02:22,610 --> 00:02:37,280 curly braces and triple backticks and the chat history colon chat history in curly braces. 29 00:02:38,190 --> 00:02:40,400 This is the key of the memory object. 30 00:02:46,750 --> 00:02:49,800 I am defining a list called messages. 31 00:02:53,850 --> 00:03:00,020 The first item in the list is an object of type SystemMessagePromptTemplate. 32 00:03:11,880 --> 00:03:16,670 Element in the list is an object of HumanMessagePromptTemplate. 33 00:03:19,350 --> 00:03:22,020 That from template of user template. 34 00:03:26,950 --> 00:03:32,000 And I am creating the question and answer prompt from the message. 35 00:03:33,390 --> 00:03:57,180 This is a chat prompt template to use the new prompt. 36 00:03:59,790 --> 00:04:00,500 CombineDocsChainKeyworks. 37 00:04:01,830 --> 00:04:02,900 It is a long name. 38 00:04:05,050 --> 00:04:16,420 Equals a dictionary, the key is prompt and the value is QAPrompt, the object of 39 00:04:16,430 --> 00:04:18,240 type ChatPromptTemplate. 40 00:04:21,190 --> 00:04:22,360 And that is it. 41 00:04:23,050 --> 00:04:24,340 I am running the code. 42 00:04:26,710 --> 00:04:29,580 I am also displaying the QAPrompt. 43 00:04:34,720 --> 00:04:35,230 Very well. 44 00:04:35,880 --> 00:04:37,430 Let us ask questions. 45 00:04:38,180 --> 00:04:42,350 I am loading the vector store from the directory on disk. 46 00:04:43,560 --> 00:04:46,790 I am calling loadEmbeddingsChroma. 47 00:04:48,060 --> 00:04:51,090 I am copy pasting the last question. 48 00:04:53,840 --> 00:04:58,650 How many pairs of questions and answers had the StackOverflow dataset? 49 00:04:59,200 --> 00:05:00,250 I am running it. 50 00:05:05,400 --> 00:05:06,910 And you see the prompt. 51 00:05:07,800 --> 00:05:08,850 You see the context. 52 00:05:10,620 --> 00:05:12,430 Let us ask something else. 53 00:05:18,790 --> 00:05:20,640 When was Elon Musk born? 54 00:05:30,770 --> 00:05:32,540 And this is the answer. 55 00:05:33,910 --> 00:05:39,580 Now, I am changing the system prompt so that it will respond in another language, 56 00:05:40,030 --> 00:05:40,960 let us say Spanish. 57 00:05:43,680 --> 00:05:47,270 Use the following pieces of context to 58 00:05:47,280 --> 00:05:50,470 answer the user s question in Spanish. 59 00:05:52,800 --> 00:05:55,330 I am running the code and I am sending 60 00:05:55,340 --> 00:05:57,090 the question to the LLM. 61 00:06:04,660 --> 00:06:05,270 Very well. 62 00:06:06,380 --> 00:06:10,610 The response is in Spanish as I requested it. 63 00:06:11,700 --> 00:06:14,570 In my experience, a common business 64 00:06:14,580 --> 00:06:18,050 request is to answer only from the provided documents. 65 00:06:18,800 --> 00:06:24,550 At this moment, the application tries to answer from the context it receives from 66 00:06:24,560 --> 00:06:30,690 the retriever and if it does not find the answer within the provided context, it 67 00:06:30,700 --> 00:06:32,870 will respond from its general knowledge base. 68 00:06:34,540 --> 00:06:37,650 Of course, the date of birth of Elon Musk 69 00:06:37,660 --> 00:06:40,770 is not specified in our PDF document. 70 00:06:41,350 --> 00:06:44,110 Let us add to the system prompt. 71 00:06:47,750 --> 00:06:54,080 If you do not find the answer in the provided context, just respond, I do not know. 72 00:06:56,380 --> 00:06:57,670 I am running the code. 73 00:06:58,900 --> 00:07:02,490 I am running the query again asking for 74 00:07:02,500 --> 00:07:05,250 Bill Gates date of birth this time. 75 00:07:07,810 --> 00:07:11,140 When was Bill Gates born? 76 00:07:14,730 --> 00:07:15,620 And I am running it. 77 00:07:20,250 --> 00:07:23,160 It responded, I do not know in Spanish, 78 00:07:23,810 --> 00:07:25,400 exactly what we wanted. 79 00:07:31,950 --> 00:07:32,860 We ve done it. 80 00:07:33,290 --> 00:07:39,340 This video showed how to use a custom prompt to tailor the LLM s answer to your 81 00:07:39,350 --> 00:07:40,120 specific needs. 82 00:07:40,490 --> 00:07:42,980 We created a powerful question answering 83 00:07:42,990 --> 00:07:47,900 application that can tackle any query about custom or private documents. 84 00:07:48,470 --> 00:07:54,800 It leverages OpenAI, Pinecon or Chroma and Langchain to quickly and efficiently 85 00:07:54,810 --> 00:07:57,320 process vast amounts of text data. 86 00:07:57,950 --> 00:07:59,960 Soon, we ll likely add a slick web 87 00:07:59,970 --> 00:08:04,720 interface, some other nice features and even deploy it in production. 88 00:08:05,290 --> 00:08:07,680 We might even market it to other businesses.