1 00:00:00,700 --> 00:00:08,370 In the last video, we developed a basic RAC system using Lankchain, OpenAI and Pinecon. 2 00:00:09,360 --> 00:00:14,710 RAC stands for Retrieval Augmented Generation and is a technique that 3 00:00:14,720 --> 00:00:18,590 combines an LLM with a way to search for information. 4 00:00:19,640 --> 00:00:22,770 This lets the model look at stuff like 5 00:00:22,780 --> 00:00:26,810 private documents or databases while its generating text. 6 00:00:28,890 --> 00:00:35,740 RAC helps overcome knowledge limits, makes answers more factual and lets the 7 00:00:35,750 --> 00:00:37,740 model handle complex questions. 8 00:00:38,930 --> 00:00:41,420 We used Pinecon which is a powerful 9 00:00:41,430 --> 00:00:44,740 vector store great for big systems and apps. 10 00:00:45,750 --> 00:00:47,920 In this video, I ll show you an 11 00:00:47,930 --> 00:00:50,100 alternative to Pinecon called Chroma. 12 00:00:50,810 --> 00:00:53,100 Chroma is an open source in -memory 13 00:00:53,110 --> 00:00:59,080 vector store making it a better fit for small to medium -sized projects, you 14 00:00:59,090 --> 00:01:04,360 might not need a separate server or hosting for it, plus you don t have to 15 00:01:04,370 --> 00:01:09,620 mess around with indexes and namespaces which saves time and effort. 16 00:01:10,810 --> 00:01:17,260 To install Chroma, just run pip install -q chroma -db. 17 00:01:22,700 --> 00:01:28,190 Finally, I cleaned up the code a bit by getting rid of stuff outside the 18 00:01:28,200 --> 00:01:31,550 functions I have defined, these functions. 19 00:01:35,000 --> 00:01:38,170 I am defining a new function that creates 20 00:01:38,180 --> 00:01:45,130 the embeddings using the OpenAIEmbeddings class, saves them in a Chroma database 21 00:01:45,140 --> 00:01:51,630 and returns the database, df createEmbeddings Chroma. 22 00:01:58,730 --> 00:02:05,960 The parameters are the chunks and a persistent directory used for storage, 23 00:02:09,670 --> 00:02:14,020 its default value will be chroma -db in the current directory. 24 00:02:24,040 --> 00:02:32,180 From langchain -vector -stores, I am importing Chroma and from langchain 25 00:02:32,190 --> 00:02:49,520 -OpenAI, I am importing OpenAIEmbeddings, I am instantiating an embedding model to 26 00:02:49,530 --> 00:02:52,220 convert text to numerical representations. 27 00:02:54,850 --> 00:03:01,460 Embeddings equals OpenAIEmbeddings of 28 00:03:01,470 --> 00:03:13,000 model equals and the embedding model, text -embedding -rismol and the 29 00:03:13,010 --> 00:03:19,860 dimensions equals 1536. 30 00:03:23,220 --> 00:03:25,830 Now I am creating a ChromaVectorStore 31 00:03:25,840 --> 00:03:30,830 object, storing the embeddings and linking them back to the original text, 32 00:03:31,480 --> 00:03:40,930 so vectorStore equals Chroma from documents and the arguments, chunks, 33 00:03:42,160 --> 00:03:50,930 embeddings and persistent directory equals persistent directory, the 34 00:03:50,940 --> 00:03:51,850 functions parameter. 35 00:03:54,040 --> 00:04:01,900 I am returning the vectorStore, that's it 36 00:04:01,910 --> 00:04:02,800 for this function. 37 00:04:02,810 --> 00:04:09,780 I am also defining a function, that loads 38 00:04:09,790 --> 00:04:18,120 the existing embeddings from disk, to a vectorStore object, def 39 00:04:18,130 --> 00:04:27,080 loadEmbeddingsChroma and the function will have one argument, the persistent directory. 40 00:04:30,670 --> 00:04:33,380 It will be by default, chroma -db. 41 00:04:37,930 --> 00:04:41,960 I am copy -pasting the first three lines 42 00:04:41,970 --> 00:04:44,580 of code from the previous function. 43 00:04:49,120 --> 00:04:51,810 I am loading the existing Chroma database 44 00:04:51,820 --> 00:04:58,150 from the persistent directory, specifying the embedding, so vectorStore equals 45 00:04:58,160 --> 00:05:09,130 Chroma of persistent directory equals persistent directory and the embedding 46 00:05:09,140 --> 00:05:20,260 function equals embeddings, the object, I have just defined one line of code above. 47 00:05:20,270 --> 00:05:35,640 I am returning the vectorStore, I am running the code, let's load a PDF file, 48 00:05:41,300 --> 00:05:46,970 this is about REX systems powered by Google search technology, it is worth 49 00:05:46,980 --> 00:05:53,970 reading the document and you will find it in the resources, let's get back to code. 50 00:05:54,580 --> 00:06:05,050 Data equals loadDocument of and the path to the files and the reg powered by 51 00:06:05,060 --> 00:06:06,450 Google search .pdf. 52 00:06:08,400 --> 00:06:10,790 I am splitting the document into chunks, 53 00:06:11,420 --> 00:06:21,840 chunks equals chunkData of data and the chunkSize equals 256. 54 00:06:21,850 --> 00:06:29,880 This is the default value, anyway, finally, I am creating the vectorStore, 55 00:06:30,330 --> 00:06:38,120 vectorStore equals createEmbeddingsChroma, I am calling this 56 00:06:38,130 --> 00:06:57,700 function and the functions argument are the chunks, I am running this code, I 57 00:06:57,710 --> 00:07:04,540 noticed a small mistake, the argument of the function from documents is persist, 58 00:07:04,770 --> 00:07:15,860 not persistent, so I will change everywhere to persist directory, I am 59 00:07:15,870 --> 00:07:27,260 running the code, the data from the PDF was loaded, chunked and embedded. 60 00:07:28,130 --> 00:07:42,800 Let's ask the first question, Q equals what is vertexAsearch and I will call the 61 00:07:42,810 --> 00:07:50,930 function askAndGetAnswer, the one I have defined in the previous lecture, answer 62 00:07:50,940 --> 00:07:58,510 equals askAndGetAnswer of vectorStore and Q, the question. 63 00:07:59,280 --> 00:08:07,760 And I am printing the answer, I am running the code, I got a good answer, 64 00:08:08,290 --> 00:08:17,720 that matched what was in the document, in the PDF document about regs powered by 65 00:08:17,730 --> 00:08:28,590 Google search, if I ask childGPT the same question, what is vertexAsearch, it will 66 00:08:28,600 --> 00:08:35,310 say something like this, as of my last update in January 2022, vertexAsearch is 67 00:08:35,320 --> 00:08:42,310 not a specific product or service provided by Google, that is how it is 68 00:08:42,320 --> 00:08:48,930 supposed to work, the application is pulling the answer from the PDF, not its 69 00:08:48,940 --> 00:08:49,710 general knowledge. 70 00:08:52,400 --> 00:08:55,380 Let's test the function that loads the 71 00:08:55,390 --> 00:09:00,660 embeddings from disk into a vectorStore object, the loadEmbeddingsChroma 72 00:09:00,670 --> 00:09:12,480 function, db equals loadEmbeddingsChroma, I will use the default value for the 73 00:09:12,490 --> 00:09:22,760 persist directory, ChromaDb in the current directory, this one, and the 74 00:09:22,770 --> 00:09:31,240 question, how many pairs of questions and answers had the StackOverflow dataset, I 75 00:09:31,250 --> 00:09:39,960 am calling the askAndGetAnswer function and printing the answer, I am running the 76 00:09:39,970 --> 00:09:47,780 code, alright, I got the good answer, the StackOverflow dataset had 8 million pairs 77 00:09:47,790 --> 00:09:59,980 of questions and answers, let's check the answer in the PDF, ok, the answer was 78 00:09:59,990 --> 00:10:06,540 correct, the StackOverflow dataset contained 8 million pairs of questions 79 00:10:06,550 --> 00:10:13,700 and answers, that is how it works with Chroma, however, there is a drawback, if 80 00:10:13,710 --> 00:10:18,840 I want to send a follow -up question, it will not have access to the previous chat 81 00:10:18,850 --> 00:10:27,470 history and will respond that it doesn't know the context, for example, Q equals, 82 00:10:28,440 --> 00:10:39,620 multiply that number by 2, the number is the one from the last question, 8 million 83 00:10:39,630 --> 00:10:54,790 pairs, it answered, I don't have a specific number to multiply by 2. 84 00:10:56,240 --> 00:11:01,990 Let's take a quick break and in the next video we will learn how to add memory to 85 00:11:02,000 --> 00:11:08,110 our rack system, this way it will remember our old questions and we can ask 86 00:11:08,120 --> 00:11:08,950 follow -ups.