1 00:00:00,700 --> 00:00:04,990 In the last video, you learned how to work with PyCon indexes. 2 00:00:05,540 --> 00:00:10,530 Now, let's go ahead and add some random vectors to our PyCon index. 3 00:00:12,020 --> 00:00:20,490 I am generating a few random vectors with a dimension of 1536 using the random module. 4 00:00:21,500 --> 00:00:22,390 Import random. 5 00:00:24,730 --> 00:00:27,440 And I will use a list comprehension to 6 00:00:27,450 --> 00:00:28,460 generate the vectors. 7 00:00:28,470 --> 00:00:32,760 Vectors equals and the list comprehension. 8 00:00:33,630 --> 00:00:40,180 A vector is practically a list, so a list of vectors means a list of lists. 9 00:00:41,310 --> 00:00:59,100 Random .random for underlined in range of 1536, for V in range of 5. 10 00:01:00,030 --> 00:01:02,900 I am generating 5 vectors. 11 00:01:04,610 --> 00:01:05,980 These are the vectors. 12 00:01:12,290 --> 00:01:17,740 To insert a vector, we need the vector itself and its ID which is a string. 13 00:01:18,490 --> 00:01:24,360 Since there are 5 vectors, I am creating a list with 5 elements which represent 14 00:01:24,370 --> 00:01:25,220 the IDs. 15 00:01:26,770 --> 00:01:35,000 IDs equals list of A, B, C, D and E, the 16 00:01:35,010 --> 00:01:36,960 IDs of our vectors. 17 00:01:38,110 --> 00:01:41,140 The first step to insert vectors into an 18 00:01:41,150 --> 00:01:43,460 index is to select the index. 19 00:01:45,170 --> 00:01:47,460 I will use the same index, length shape. 20 00:01:51,270 --> 00:01:55,940 Index equals PC .index of index name. 21 00:01:59,750 --> 00:02:03,600 To insert new vectors into the index, use 22 00:02:03,610 --> 00:02:05,300 the upsert method. 23 00:02:09,740 --> 00:02:14,710 Note its upsert, not insert and the 24 00:02:14,720 --> 00:02:23,270 argument will be vectors equals zip of IDs and vectors. 25 00:02:25,250 --> 00:02:31,140 By calling the zip function, we connect the IDs and the vectors in a list of 26 00:02:31,150 --> 00:02:38,380 tuples and an upsert is a single operation that can be used to insert a 27 00:02:38,390 --> 00:02:43,000 new value or update an existing value if it already exists. 28 00:02:43,730 --> 00:02:49,300 This can be useful for situations where you are not sure whether the value 29 00:02:49,310 --> 00:02:50,260 already exists. 30 00:02:51,810 --> 00:02:52,820 I am running the code. 31 00:02:55,350 --> 00:03:02,080 The upsert method returned the number of vectors inserted, 5. 32 00:03:04,530 --> 00:03:06,740 This was inserting vectors. 33 00:03:07,950 --> 00:03:11,340 Let's take a look at updating the vectors. 34 00:03:12,610 --> 00:03:17,120 To update a vector, you can use the same upsert method. 35 00:03:17,290 --> 00:03:22,780 You will need to provide two arguments, the ID of the vector you want to update 36 00:03:22,790 --> 00:03:28,360 and the new value of the vector, index .upsert. 37 00:03:32,460 --> 00:03:36,330 I will update the vector with the ID c, 38 00:03:37,740 --> 00:03:48,210 vectors equals and the vector, c, the ID and the second argument, the vector. 39 00:03:49,180 --> 00:03:57,670 Let's say 0 .5 times 1536. 40 00:04:03,470 --> 00:04:05,980 One vector was affected. 41 00:04:09,540 --> 00:04:19,660 Let's go ahead and see how to fetch a vector by the select the index if it is 42 00:04:19,670 --> 00:04:20,880 not already selected. 43 00:04:22,150 --> 00:04:24,880 In this case, it is already selected. 44 00:04:25,310 --> 00:04:28,400 I am calling the fetch method. 45 00:04:29,570 --> 00:04:34,640 The argument is IDs equals and a list 46 00:04:34,650 --> 00:04:39,840 with the IDs of the vectors, let's say c and d. 47 00:04:40,510 --> 00:04:49,020 I am fetching the vectors with IDs c and d. Take a look here. 48 00:04:58,510 --> 00:04:59,620 This is vector d. 49 00:05:03,670 --> 00:05:05,900 Now, let's take a look at how to delete 50 00:05:05,910 --> 00:05:07,160 vectors by ID. 51 00:05:11,540 --> 00:05:14,190 You call the delete method of the index 52 00:05:14,200 --> 00:05:24,170 object with a list of vector IDs as an argument, index .delete of IDs equals b 53 00:05:24,180 --> 00:05:25,730 and c. 54 00:05:26,060 --> 00:05:28,270 I am deleting two vectors. 55 00:05:29,640 --> 00:05:34,270 I am calling index .describeIndexStacks again. 56 00:05:36,720 --> 00:05:39,530 We see that there are only three vectors 57 00:05:39,540 --> 00:05:43,430 in the index because two of them were deleted. 58 00:05:46,400 --> 00:05:49,190 If you fetch a vector that doesn't exist, 59 00:05:49,540 --> 00:05:59,820 you won't get an error, but an empty vector, index .fetch of IDs equals and x. 60 00:06:00,410 --> 00:06:06,940 I am fetching the vector with the ID x, which doesn't exist and I've got an empty vector. 61 00:06:11,410 --> 00:06:16,840 I have shown you how to delete vectors by ID, but if you want to delete all the 62 00:06:16,850 --> 00:06:21,440 vectors in the current index, delete the index and recreate it. 63 00:06:22,470 --> 00:06:24,460 Now, let's perform a query. 64 00:06:27,330 --> 00:06:29,720 I am adding the random vectors to the 65 00:06:29,730 --> 00:06:34,120 pine cone index again because I have just deleted some of them. 66 00:06:35,690 --> 00:06:42,340 I am deleting the index, creating it again and adding the five vectors again. 67 00:06:53,870 --> 00:06:56,080 We are starting from scratch. 68 00:06:56,810 --> 00:07:00,060 There are five vectors in the index. 69 00:07:02,120 --> 00:07:13,820 I am creating a query vector random .random for underscore in range of and 70 00:07:13,830 --> 00:07:15,040 the vector dimension. 71 00:07:20,850 --> 00:07:24,120 The query operation will retrieve the IDs 72 00:07:24,130 --> 00:07:32,120 of the most similar vectors in the index along with their similarity scores, index 73 00:07:32,130 --> 00:07:44,880 .query and the arguments are vector equals query vector, topK equals 3. 74 00:07:45,310 --> 00:07:52,960 It will return the top three most similar vectors and the include values equals false. 75 00:07:53,830 --> 00:07:57,660 I don't want to display the actual values of the vectors. 76 00:08:02,550 --> 00:08:08,780 This will return the top three most similar matches to our query vector. 77 00:08:09,450 --> 00:08:17,560 I am running it, C, A and E and this is the score. 78 00:08:18,110 --> 00:08:21,540 The closer to 1 is, the more similar is the vector. 79 00:08:23,370 --> 00:08:29,160 Great, in this video you learned how to work with vectors, you learned how to 80 00:08:29,170 --> 00:08:34,260 insert or update vectors into an index and how to fetch or delete vectors. 81 00:08:34,670 --> 00:08:40,880 You also learned to perform queries and find the most similar vectors to one 82 00:08:40,890 --> 00:08:41,460 query vector.