AristaDBA's Oracle Blog....

Similarity Search….

let’s take a look at basic queries and similarity search. Now before you can do queries against your vector data, you have to insert the data first. So I wanted to show you an example of inserting several rows of vectors. Now in this case, each vector represents some sort of data point. And in some cases, they have more dimensions than in others. So as an example, that first row of inserts, 1.1 comma 2.7 comma 3.14, that has three dimensions. Further down with the 7.3, that only has a single dimension. Now after you insert data, then you can select the data. And in this case, if we select star from the T1 table, it would look like this. You’ll notice that the data is returned in scientific notation. Bear in mind, you could use a function such as to number in order to return a more readable, friendly version of the value. Now let’s talk about some of the basic rules. You’re not allowed to use comparison operations between vectors. If you do, you’re going to get an error message back. And it’s going to tell you that you cannot use vector type as comparison key. We’ll show you another example. Here we’re using the less than or equal to sign. And again, this is going to return the following error. Now let’s take a look at similarity search. Now vector data is usually unevenly distributed and clustered. And clustered means that it is in groups. So you might have groupings of houses, for instance, in a neighborhood that are all around the same price. Now, with similarity searches, you might need to search for nearest neighbors, which is going to be an ordered list by rank. Searches can be approximate, or they can be specific. Now, in this example, you’ll notice that we have different types of animals. We have wolf, dog, puppy, cat, kitten, lion, and elephant. And if we’re talking about nearest neighbors, we see that a dog is more similar to a wolf and is less similar to a kitten. So if we’re looking for the nearest neighbors, we might want to look for dog, puppy, wolf, for instance. And that represents data that makes sense for something that’s relatable to a dog. Now with the exact similarity search, this is going to calculate the query vector distance to all other vectors. And exact similarity searches are also called flat searches. They do give you the most accurate results. But the trade off is that these take longer. Although the search quality is perfect. Now when you do vector distance queries, you use the vector distance function. And the vector distance function uses something that we call a metric as an optional parameter. Now, in this example, we’re going to show you Euclidean and Euclidean squared distances, cosine similarity, dot product similarity, Manhattan distance, Hamming similarity. Now think about it like this. If you’re talking about the distance between a couple of points, what if you’re walking in a city and there are buildings between the two points? You can’t do a straight line between those points. You have to maybe walk around a block or maybe walk around several blocks to get to the location. So that would be an example of Manhattan distance metric. And so you would want to use the metric that makes sense for your particular data set. So here, we have an exact similarity search. And we are searching for the three nearest neighbors to a particular query point. So the query point is vq. And we want to find the three nearest neighbors. This is called a k nearest neighbor search. And we’re highlighting where on this representation those closest neighbors lie to the query point. Now if we wanted to use the Euclidean metric, we would do a SELECT statement that looks something like this. Select the doc ID from the vector tab table. Order by vector distance, and there’s that vector distance function. Now we’re supplying a column called embedding, which is the column that has the embedding, or rather the vector data type. And then we have an input query vector. And then that final parameter is the Euclidean metric. Notice that we are fetching exact first 10 rows only. By the way, the exact keyword would be optional. Now, this is bringing back the 10 closest neighbors using the Euclidean metric. Now, if we leave off the Euclidean metric keyword, then it’s going to use Euclidean squared distance as the default. And so here is another example using the same column, the same table, the same fetch first 10 rows only. So we’re bringing back the 10 closest neighbors. But this time, we’re using the Euclidean squared distance metric. The Euclidean squared distance metric is going to be quicker because it doesn’t have to calculate the squared distance. Now, if you want to do approximate similarity search, that is going to involve the use of vector indexes. And in order to create vector indexes, you do have to enable the vector pool in the SGA. These can be more efficient because they usually operate quicker than exact similarity searches. They can be less accurate. As an example, if you’re doing an exact similarity search, it’s going to be perfect on the results. However, if you use approximate similarity search, it might be 80% accurate. Now you can also provide the parameter as far as your accuracy metric. So if you wanted it to be 95% accurate, you can provide that as input. And that is what I’m talking about here with the target accuracy. So let’s take a look at that in a visual example. So here we have the input, the vq, the query. And if we are looking at an exact similarity search, you’ll notice that it finds the five closest neighbors. And we can visually see that with the gray cloud that’s circling those five other black X’s. Now if we did an approximate search, then notice that we find four out of five of those nearest neighbors. And in this case, you see that that is 80% accurate. So as we mentioned, approximate similarity search uses vector indexes. We have two different indexes that are supported. We have Hierarchical Navigable Small World, or HNSW index. And we have Inverted File Flat, or IVF index. We’re going to show you an example of creating each one of these types of indexes. Here, we’re creating an HNSW index. Create vector index. And the name is going to be galaxies HNSW on the galaxies table, on the vector column, which is called embedding. Organization in memory. Neighbor graph distance cosine with target accuracy 95. And there’s that target accuracy metric that I talked about earlier. And then when we go in and do a select, then it’s going to use the index in order to return the result set. Now in this example, Iám going to show you how to create an IVF or inverted file flat index. Create vector index galaxies IVF IDX on galleries, or sorry, galaxies. Again, the embedding column, which is a vector type. Organization neighbor partitions distance cosine with target accuracy 95. And then when we do that SELECT statement there, it does return the first three rows or the three closest neighbors. And this will use the IVF index in this example.

Continue reading...

Vector Embedding Models….

vector embedding models quantify features or dimensions. As an example , we have the word LION. And we want to convert that into a vector to represent what that word means. So we use a vector embedding model to generate an embedding and store that as a vector in a vector type column. The models can be pretrained open source models. They can also be based on your own data set. Now, depending on the type of data, you can use different pretrained open source models as examples for textual data. There are sentence transformers which transform words, sentences or paragraphs into vector embeddings. For visual data, you can use residual network, also known as ResNet, to generate vector embeddings. For audio data, you can use the visual spectrogram representation of the audio data in order to fall back into the visual data case. Now, there are many different types of models. And depending on the model, they have different numbers of dimensions. As an example, Cohere’s embedding model has 1,024 dimensions. OpenAI’s embedding model, text-embedding-3-large, has 3,072 dimensions. And Hugging Face’s embedding model, all-MiniLM-L6-v2, has 384 dimensions. You can also create your own model that is trained with your own data set. Now we’re going to talk more on generating vector embeddings. You can do that either outside the Oracle database or within the Oracle database. In order to do that within the Oracle database, you can import a model, as long as it is in the ONNX format. Now ONNX stands for Open Neural Network Exchange. And it is a standard. Oracle Database implements an ONNX runtime directly within the database. This allows you to generate vector embeddings directly within the Oracle database using SQL.

Continue reading...