In the last video, you learned how to work with PyCon indexes.
Now, let's go ahead and add some random vectors to our PyCon index.
I am generating a few random vectors with a dimension of 1536 using the random module.
Import random.
And I will use a list comprehension to
generate the vectors.
Vectors equals and the list comprehension.
A vector is practically a list, so a list of vectors means a list of lists.
Random .random for underlined in range of 1536, for V in range of 5.
I am generating 5 vectors.
These are the vectors.
To insert a vector, we need the vector itself and its ID which is a string.
Since there are 5 vectors, I am creating a list with 5 elements which represent
the IDs.
IDs equals list of A, B, C, D and E, the
IDs of our vectors.
The first step to insert vectors into an
index is to select the index.
I will use the same index, length shape.
Index equals PC .index of index name.
To insert new vectors into the index, use
the upsert method.
Note its upsert, not insert and the
argument will be vectors equals zip of IDs and vectors.
By calling the zip function, we connect the IDs and the vectors in a list of
tuples and an upsert is a single operation that can be used to insert a
new value or update an existing value if it already exists.
This can be useful for situations where you are not sure whether the value
already exists.
I am running the code.
The upsert method returned the number of vectors inserted, 5.
This was inserting vectors.
Let's take a look at updating the vectors.
To update a vector, you can use the same upsert method.
You will need to provide two arguments, the ID of the vector you want to update
and the new value of the vector, index .upsert.
I will update the vector with the ID c,
vectors equals and the vector, c, the ID and the second argument, the vector.
Let's say 0 .5 times 1536.
One vector was affected.
Let's go ahead and see how to fetch a vector by the select the index if it is
not already selected.
In this case, it is already selected.
I am calling the fetch method.
The argument is IDs equals and a list
with the IDs of the vectors, let's say c and d.
I am fetching the vectors with IDs c and d. Take a look here.
This is vector d.
Now, let's take a look at how to delete
vectors by ID.
You call the delete method of the index
object with a list of vector IDs as an argument, index .delete of IDs equals b
and c.
I am deleting two vectors.
I am calling index .describeIndexStacks again.
We see that there are only three vectors
in the index because two of them were deleted.
If you fetch a vector that doesn't exist,
you won't get an error, but an empty vector, index .fetch of IDs equals and x.
I am fetching the vector with the ID x, which doesn't exist and I've got an empty vector.
I have shown you how to delete vectors by ID, but if you want to delete all the
vectors in the current index, delete the index and recreate it.
Now, let's perform a query.
I am adding the random vectors to the
pine cone index again because I have just deleted some of them.
I am deleting the index, creating it again and adding the five vectors again.
We are starting from scratch.
There are five vectors in the index.
I am creating a query vector random .random for underscore in range of and
the vector dimension.
The query operation will retrieve the IDs
of the most similar vectors in the index along with their similarity scores, index
.query and the arguments are vector equals query vector, topK equals 3.
It will return the top three most similar vectors and the include values equals false.
I don't want to display the actual values of the vectors.
This will return the top three most similar matches to our query vector.
I am running it, C, A and E and this is the score.
The closer to 1 is, the more similar is the vector.
Great, in this video you learned how to work with vectors, you learned how to
insert or update vectors into an index and how to fetch or delete vectors.
You also learned to perform queries and find the most similar vectors to one
query vector.