You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -126,13 +126,9 @@ Vectors can also be stored in databases in **binary formats** to save space. In
126
126
127
127
## Generating vectors
128
128
129
-
The [Hugging Face Transformers](https://huggingface.co/docs/transformers.js/index) library provides pre-trained models that can be fine-tuned or used out-of-the-box for various NLP (natural language processing) tasks. In our context, we're interested in generating sentence and image embeddings (vector).
129
+
In our context, we're interested in generating sentence (product description) and image (product image) embeddings/ vector.There are many AI model repositories (say like GitHub) where pre-trained AI models are stored, maintained, and shared with the public.
130
130
131
-
:::note
132
-
transformers.j library is essentially the JavaScript version of Hugging Face's popular Python library.
133
-
:::
134
-
135
-
### Sentence vector
131
+
Let's use one of model from [Hugging Face Model Hub](https://huggingface.co/models) for sentence embeddings and from [TensorFlow Hub](https://tfhub.dev/) for image embeddings for diversity.
136
132
137
133
:::tip GITHUB CODE
138
134
@@ -141,16 +137,31 @@ Below is a command to the clone the source code used in this tutorial
To obtain sentence embeddings, let's use a hugging face model called [Xenova/all-distilroberta-v1](https://huggingface.co/Xenova/all-distilroberta-v1) which is compatible version of [sentence-transformers/all-distilroberta-v1](https://huggingface.co/sentence-transformers/all-distilroberta-v1) for transformer.js with ONNX weights.
145
143
146
144
:::note
147
145
146
+
<u>
147
+
[Hugging Face Transformers](https://huggingface.co/docs/transformers.js/index)
148
+
</u> is a widely-used open-source library for Natural Language Processing (NLP) tasks.
149
+
It provides an accessible and straightforward way to use many state-of-the-art NLP
150
+
models
151
+
152
+
transformers.j library is essentially the JavaScript version of Hugging Face's popular Python library.
153
+
154
+
:::
155
+
156
+
:::note
157
+
148
158
<u>[ONNX (Open Neural Network eXchange)](https://onnx.ai) </u> is an open standard
149
159
that defines a common set of operators and a common file format to represent deep
150
160
learning models in a wide variety of frameworks, including PyTorch and TensorFlow
161
+
151
162
:::
152
163
153
-
Below is a Node.js code sample that showcases how to generate vector embeddings for the any sentence provided:
164
+
Below is a Node.js code sample that showcases how to generate vector embeddings for any sentence provided:
154
165
155
166
```sh
156
167
npm install @xenova/transformers
@@ -168,13 +179,15 @@ async function generateSentenceEmbeddings(_sentence): Promise<number[]> {
168
179
normalize:true,
169
180
});
170
181
171
-
const embeddings:number[] =Object.values(vectorOutput?.data);//object to array
Below is a Node.js code sample that showcases how to generate vector embeddings for any image provided:
199
209
200
210
```sh
201
211
npm i @tensorflow/tfjs @tensorflow/tfjs-node @tensorflow-models/mobilenet jpeg-js
@@ -253,7 +263,7 @@ async function generateImageEmbeddings(imagePath: string) {
253
263
We are using <u>[mobilenet model](https://github.com/tensorflow/tfjs-models/tree/master/mobilenet)</u> which is trained only on small <u>[set of image classes](https://github.com/tensorflow/tfjs-examples/blob/master/mobilenet/imagenet_classes.js)</u>. Selecting an image classification model depends on various factors, such as the dataset size, dataset diversity, computational resources, and the specific needs of the application. There are many image classification models like EfficientNet, ResNets, Vision Transformers (ViT)..etc which can be chosen from based on your requirements.
254
264
:::
255
265
256
-
Please find vector details for the sample watch image (11001.jpg)
266
+
Please find vector output for a sample watch image
Below implementation shows indexing different field types in Redis including vector fields like productDescriptionEmbeddings and productImageEmbeddings.
396
+
JSON fields must be indexed in Redis to perform search on them. Below implementation shows indexing different field types including vector fields like productDescriptionEmbeddings and productImageEmbeddings.
HNSW is a graph-based method for indexing high-dimensional data. For bigger datasets it becomes slower to compare with every single vector in the index, so a probabilistic approach through the HNSW algorithm provides very fast search results (but sacrifices some accuracy)
513
523
:::
514
524
515
-
## What is vector search by KNN?
525
+
:::note INITIAL_CAP and BLOCK_SIZE parameters
526
+
INITIAL_CAP and BLOCK_SIZE are configuration parameters related to how vectors are stored and indexed.
527
+
528
+
INITIAL_CAP defines the initial capacity of the vector index. It helps in pre-allocating space for the index.
529
+
530
+
BLOCK_SIZE defines the size of each block of the vector index. As more vectors are added, Redis will allocate memory in chunks, with each chunk being the size of the BLOCK_SIZE. It helps in optimizing the memory allocations during index growth.
531
+
:::
532
+
533
+
## What is vector KNN query?
516
534
517
535
KNN, or k-Nearest Neighbors, is an algorithm used in both classification and regression tasks, but when referring to "KNN Search," we're typically discussing the task of finding the "k" points in a dataset that are closest (most similar) to a given query point. In the context of vector search, this means identifying the "k" vectors in our database that are most similar to a given query vector, usually based on some distance metric like cosine similarity or Euclidean distance.
518
536
519
-
Redis provides support for vector search, allowing you to index and then search for vectors [using the KNN approach](https://redis.io/docs/stack/search/reference/vectors/#pure-knn-queries).
537
+
### KNN query with Redis
538
+
539
+
Redis allows you to index and then search for vectors [using the KNN approach](https://redis.io/docs/stack/search/reference/vectors/#pure-knn-queries).
520
540
521
-
### Vector KNN query with Redis
541
+
Below is a Node.js code sample that showcases how to perform a KNN query for any search term provided:
Note : Can combine KNN query with regular Redis search feature using [hybrid knn queries](https://redis.io/docs/interact/search-and-query/search/vectors/#hybrid-knn-queries)
642
+
643
+
## What is vector range query?
644
+
645
+
Range queries retrieve data that falls within a specified range of values.
646
+
For vectors, a "range query" typically refers to retrieving all vectors within a certain distance of a target vector. The "range" in this context is a radius in the vector space.
647
+
648
+
### Range query with Redis
649
+
650
+
Below is a Node.js code sample that showcases how to perform a vector range query for any radius (distance) range provided:
0 commit comments