Redis as a vector database quick start guide

Understand how to use Redis as a vector database

This quick start guide helps you to:

  1. Understand what a vector database is
  2. Create a Redis vector database
  3. Create vector embeddings and store vectors
  4. Query data and perform a vector search

Understand vector databases

Data is often unstructured, which means that it isn't described by a well-defined schema. Examples of unstructured data include text passages, images, videos, or audio. One approach to storing and searching through unstructured data is to use vector embeddings.

What are vectors? In machine learning and AI, vectors are sequences of numbers that represent data. They are the inputs and outputs of models, encapsulating underlying information in a numerical form. Vectors transform unstructured data, such as text, images, videos, and audio, into a format that machine learning models can process.

  • Why are they important? Vectors capture complex patterns and semantic meanings inherent in data, making them powerful tools for a variety of applications. They allow machine learning models to understand and manipulate unstructured data more effectively.
  • Enhancing traditional search. Traditional keyword or lexical search relies on exact matches of words or phrases, which can be limiting. In contrast, vector search, or semantic search, leverages the rich information captured in vector embeddings. By mapping data into a vector space, similar items are positioned near each other based on their meaning. This approach allows for more accurate and meaningful search results, as it considers the context and semantic content of the query rather than just the exact words used.

Create a Redis vector database

You can use Redis Stack as a vector database. It allows you to:

  • Store vectors and the associated metadata within hashes or JSON documents
  • Create and configure secondary indices for search
  • Perform vector searches
  • Update vectors and metadata
  • Delete and cleanup

The easiest way to get started is to use Redis Cloud:

  1. Create a free account.

  2. Follow the instructions to create a free database.

This free Redis Cloud database comes out of the box with all the Redis Stack features.

You can alternatively use the installation guides to install Redis Stack on your local machine.

You need to have the following features configured for your Redis server: JSON and Search and query.

Install the required Python packages

Create a Python virtual environment and install the following dependencies using pip:

  • redis: You can find further details about the redis-py client library in the clients section of this documentation site.
  • pandas: Pandas is a data analysis library.
  • sentence-transformers: You will use the SentenceTransformers framework to generate embeddings on full text.
  • tabulate: pandas uses tabulate to render Markdown.

You will also need the following imports in your Python code:

Connect

Connect to Redis. By default, Redis returns binary responses. To decode them, you pass the decode_responses parameter set to True:


Tip:
Instead of using a local Redis Stack server, you can copy and paste the connection details from the Redis Cloud database configuration page. Here is an example connection string of a Cloud database that is hosted in the AWS region us-east-1 and listens on port 16379: redis-16379.c283.us-east-1-4.ec2.cloud.redislabs.com:16379. The connection string has the format host:port. You must also copy and paste the username and password of your Cloud database. The line of code for connecting with the default user changes then to client = redis.Redis(host="redis-16379.c283.us-east-1-4.ec2.cloud.redislabs.com", port=16379, password="your_password_here", decode_responses=True).

Prepare the demo dataset

This quick start guide also uses the bikes dataset. Here is an example document from it:

{
  "model": "Jigger",
  "brand": "Velorim",
  "price": 270,
  "type": "Kids bikes",
  "specs": {
    "material": "aluminium",
    "weight": "10"
  },
  "description": "Small and powerful, the Jigger is the best ride for the smallest of tikes! ..."
}

The description field contains free-form text descriptions of bikes and will be used to create vector embeddings.

1. Fetch the demo data

You need to first fetch the demo dataset as a JSON array:

Inspect the structure of one of the bike JSON documents:

2. Store the demo data in Redis

Now iterate over the bikes array to store the data as JSON documents in Redis by using the JSON.SET command. The below code uses a pipeline to minimize the network round-trip times:

Once loaded, you can retrieve a specific attribute from one of the JSON documents in Redis using a JSONPath expression:

3. Select a text embedding model

HuggingFace has a large catalog of text embedding models that are locally servable through the SentenceTransformers framework. Here we use the MS MARCO model that is widely used in search engines, chatbots, and other AI applications.

from sentence_transformers import SentenceTransformer

embedder = SentenceTransformer('msmarco-distilbert-base-v4')

4. Generate text embeddings

Iterate over all the Redis keys with the prefix bikes::

Use the keys as input to the JSON.MGET command, along with the $.description field, to collect the descriptions in a list. Then, pass the list of descriptions to the .encode() method:

Insert the vectorized descriptions to the bike documents in Redis using the JSON.SET command. The following command inserts a new field into each of the documents under the JSONPath $.description_embeddings. Once again, do this using a pipeline to avoid unnecessary network round-trips:

Inspect one of the updated bike documents using the JSON.GET command:

Note:
When storing a vector embedding within a JSON document, the embedding is stored as a JSON array. In the example above, the array was shortened considerably for the sake of readability.

Create an index

1. Create an index with a vector field

You must create an index to query document metadata or to perform vector searches. Use the FT.CREATE command:

Here is a breakdown of the VECTOR field definition:

  • $.description_embeddings AS vector: The vector field's JSON path and its field alias vector.
  • FLAT: Specifies the indexing method, which is either a flat index or a hierarchical navigable small world graph (HNSW).
  • TYPE FLOAT32: Sets the float precision of a vector component, in this case a 32-bit floating point number.
  • DIM 768: The length or dimension of the embeddings, determined by the chosen embedding model.
  • DISTANCE_METRIC COSINE: The chosen distance function: cosine distance.

You can find further details about all these options in the vector reference documentation.

2. Check the state of the index

As soon as you execute the FT.CREATE command, the indexing process runs in the background. In a short time, all JSON documents should be indexed and ready to be queried. To validate that, you can use the FT.INFO command, which provides details and statistics about the index. Of particular interest are the number of documents successfully indexed and the number of failures:

Perform vector searches

This quick start guide focuses on vector search. However, you can learn more about how to query based on document metadata in the document database quick start guide.

1. Embed your queries

The following code snippet shows a list of text queries you will use to perform vector search in Redis:

First, encode each input query as a vector embedding using the same SentenceTransformers model:


Tip:
It is vital that you use the same embedding model to embed your queries as you did your documents. Using a different model will result in poor semantic search results or error.

The KNN algorithm calculates the distance between the query vector and each vector in Redis based on the chosen distance function. It then returns the top K items with the smallest distances to the query vector. These are the most semantically similar items.

Now construct a query to do just that:

query = (
    Query('(*)=>[KNN 3 @vector $query_vector AS vector_score]')
     .sort_by('vector_score')
     .return_fields('vector_score', 'id', 'brand', 'model', 'description')
     .dialect(2)
)

Let's break down the above query template:

  • The filter expression (*) means all. In other words, no filtering was applied. You could replace it with an expression that filters by additional metadata.
  • The KNN part of the query searches for the top 3 nearest neighbors.
  • The query vector must be passed in as the param query_vector.
  • The distance to the query vector is returned as vector_score.
  • The results are sorted by this vector_score.
  • Finally, it returns the fields vector_score, id, brand, model, and description for each result.
Note:
To utilize a vector query with the FT.SEARCH command, you must specify DIALECT 2 or greater.

You must pass the vectorized query as a byte array with the param name query_vector. The following code creates a Python NumPy array from the query vector and converts it into a compact, byte-level representation that can be passed as a parameter to the query:

client.ft('idx:bikes_vss').search(
    query,
    {
      'query_vector': np.array(encoded_query, dtype=np.float32).tobytes()
    }
).docs

With the template for the query in place, you can execute all queries in a loop. Notice that the script calculates the vector_score for each result as 1 - doc.vector_score. Because the cosine distance is used as the metric, the items with the smallest distance are closer and, therefore, more similar to the query.

Then, loop over the matched documents and create a list of results that can be converted into a Pandas table to visualize the results:

The query results show the individual queries' top three matches (our K parameter) along with the bike's id, brand, and model for each query.

For example, for the query "Best Mountain bikes for kids", the highest similarity score (0.54) and, therefore the closest match was the 'Nord' brand 'Chook air 5' bike model, described as:

The Chook Air 5 gives kids aged six years and older a durable and uberlight mountain bike for their first experience on tracks and easy cruising through forests and fields. The lower top tube makes it easy to mount and dismount in any situation, giving your kids greater safety on the trails. The Chook Air 5 is the perfect intro to mountain biking.

From the description, this bike is an excellent match for younger children, and the embeddings accurately captured the semantics of the description.