This notebook provides an introduction to using Redis as a vector database with OpenAI embeddings. Redis is a scalable, real-time database that can be used as a vector database when using the RediSearch Module. The RediSearch module allows you to index and search for vectors in Redis. This notebook will show you how to use the RediSearch module to index and search for vectors created by using the OpenAI API and stored in Redis.
Most developers from a web services background are probably familiar with Redis. At it's core, Redis is an open-source key-value store that can be used as a cache, message broker, and database. Developers choice Redis because it is fast, has a large ecosystem of client libraries, and has been deployed by major enterprises for years.
RediSearch is a Redis module that provides querying, secondary indexing, full-text search and vector search for Redis. To use RediSearch, you first declare indexes on your Redis data. You can then use the RediSearch clients to query that data. For more information on the feature set of RediSearch, see the README or the RediSearch documentation.
There are a number of ways to deploy Redis. For local development, the quickest method is to use the Redis Stack docker container which we will use here. Redis Stack contains a number of Redis modules that can be used together to create a fast, multi-model data store and query engine.
For production use cases, The easiest way to get started is to use the Redis Cloud service. Redis Cloud is a fully managed Redis service. You can also deploy Redis on your own infrastructure using Redis Enterprise. Redis Enterprise is a fully managed Redis service that can be deployed in kubernetes, on-premises or in the cloud.
To keep this example simple, we will use the Redis Stack docker container which we can start as follows
$docker-composeup-d
This also includes the RedisInsight GUI for managing your Redis database which you can view at http://localhost:8001 once you start the docker container.
You're all set up and ready to go! Next, we import and create our client for communicating with the Redis database we just created.
Once you get your key, please add it to your environment variables as OPENAI_API_KEY by using following command:
! export OPENAI_API_KEY="your API key"
# Test that your OpenAI API key is correctly set as an environment variable# Note. if you run this notebook locally, you will need to reload your terminal and the notebook for the env variables to be live.import osimport openai# Note. alternatively you can set a temporary env variable like this:# os.environ["OPENAI_API_KEY"] = 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'if os.getenv("OPENAI_API_KEY") isnotNone: openai.api_key = os.getenv("OPENAI_API_KEY")print ("OPENAI_API_KEY is ready")else:print ("OPENAI_API_KEY environment variable not found")
In this section we'll load embedded data that has already been converted into vectors. We'll use this data to create an index in Redis and then search for similar vectors.
import sysimport numpy as npimport pandas as pdfrom typing import List# use helper function in nbutils.py to download and read the data# this should take from 5-10 min to runif os.getcwd() notin sys.path: sys.path.append(os.getcwd())import nbutilsnbutils.download_wikipedia_data()data = nbutils.read_wikipedia_data()data.head()
Now that we have our Redis database running, we can connect to it using the Redis-py client. We will use the default host and port for the Redis database which is localhost:6379.
The below cells will show how to specify and create a search index in Redis. We will:
Set some constants for defining our index like the distance metric and the index name
Define the index schema with RediSearch fields
Create the index
# ConstantsVECTOR_DIM=len(data['title_vector'][0]) # length of the vectorsVECTOR_NUMBER=len(data) # initial number of vectorsINDEX_NAME="embeddings-index"# name of the search indexPREFIX="doc"# prefix for the document keysDISTANCE_METRIC="COSINE"# distance metric for the vectors (ex. COSINE, IP, L2)
# Define RediSearch fields for each of the columns in the datasettitle = TextField(name="title")url = TextField(name="url")text = TextField(name="text")title_embedding = VectorField("title_vector","FLAT", {"TYPE": "FLOAT32","DIM": VECTOR_DIM,"DISTANCE_METRIC": DISTANCE_METRIC,"INITIAL_CAP": VECTOR_NUMBER, })text_embedding = VectorField("content_vector","FLAT", {"TYPE": "FLOAT32","DIM": VECTOR_DIM,"DISTANCE_METRIC": DISTANCE_METRIC,"INITIAL_CAP": VECTOR_NUMBER, })fields = [title, url, text, title_embedding, text_embedding]
# Check if index existstry: redis_client.ft(INDEX_NAME).info()print("Index already exists")except:# Create RediSearch Index redis_client.ft(INDEX_NAME).create_index(fields= fields,definition= IndexDefinition(prefix=[PREFIX], index_type=IndexType.HASH))
Now that we have a search index, we can load documents into it. We will use the same documents we used in the previous examples. In Redis, either the HASH or JSON (if using RedisJSON in addition to RediSearch) data types can be used to store documents. We will use the HASH data type in this example. The below cells will show how to load documents into the index.
defindex_documents(client: redis.Redis, prefix: str, documents: pd.DataFrame): records = documents.to_dict("records")for doc in records: key =f"{prefix}:{str(doc['id'])}"# create byte vectors for title and content title_embedding = np.array(doc["title_vector"], dtype=np.float32).tobytes() content_embedding = np.array(doc["content_vector"], dtype=np.float32).tobytes()# replace list of floats with byte vectors doc["title_vector"] = title_embedding doc["content_vector"] = content_embedding client.hset(key, mapping= doc)
index_documents(redis_client, PREFIX, data)print(f"Loaded {redis_client.info()['db0']['keys']} documents in Redis search index with name: {INDEX_NAME}")
Loaded 25000 documents in Redis search index with name: embeddings-index
Now that we have a search index and documents loaded into it, we can run search queries. Below we will provide a function that will run a search query and return the results. Using this function we run a few queries that will show how you can utilize Redis as a vector database.
# For using OpenAI to generate query embeddingresults = search_redis(redis_client, 'modern art in Europe', k=10)
0. Museum of Modern Art (Score: 0.875)
1. Western Europe (Score: 0.868)
2. Renaissance art (Score: 0.864)
3. Pop art (Score: 0.86)
4. Northern Europe (Score: 0.855)
5. Hellenistic art (Score: 0.853)
6. Modernist literature (Score: 0.847)
7. Art film (Score: 0.843)
8. Central Europe (Score: 0.843)
9. European (Score: 0.841)
results = search_redis(redis_client, 'Famous battles in Scottish history', vector_field='content_vector', k=10)
0. Battle of Bannockburn (Score: 0.869)
1. Wars of Scottish Independence (Score: 0.861)
2. 1651 (Score: 0.853)
3. First War of Scottish Independence (Score: 0.85)
4. Robert I of Scotland (Score: 0.846)
5. 841 (Score: 0.844)
6. 1716 (Score: 0.844)
7. 1314 (Score: 0.837)
8. 1263 (Score: 0.836)
9. William Wallace (Score: 0.835)
The previous examples showed how run vector search queries with RediSearch. In this section, we will show how to combine vector search with other RediSearch fields for hybrid search. In the below example, we will combine vector search with full text search.
defcreate_hybrid_field(field_name: str, value: str) -> str:returnf'@{field_name}:"{value}"'# search the content vector for articles about famous battles in Scottish history and only include results with Scottish in the titleresults = search_redis(redis_client,"Famous battles in Scottish history",vector_field="title_vector",k=5,hybrid_fields=create_hybrid_field("title", "Scottish") )
0. First War of Scottish Independence (Score: 0.892)
1. Wars of Scottish Independence (Score: 0.889)
2. Second War of Scottish Independence (Score: 0.879)
3. List of Scottish monarchs (Score: 0.873)
4. Scottish Borders (Score: 0.863)
# run a hybrid query for articles about Art in the title vector and only include results with the phrase "Leonardo da Vinci" in the textresults = search_redis(redis_client,"Art",vector_field="title_vector",k=5,hybrid_fields=create_hybrid_field("text", "Leonardo da Vinci") )# find specific mention of Leonardo da Vinci in the text that our full-text-search query returnedmention = [sentence for sentence in results[0].text.split("\n") if"Leonardo da Vinci"in sentence][0]mention
'In Europe, after the Middle Ages, there was a "Renaissance" which means "rebirth". People rediscovered science and artists were allowed to paint subjects other than religious subjects. People like Michelangelo and Leonardo da Vinci still painted religious pictures, but they also now could paint mythological pictures too. These artists also invented perspective where things in the distance look smaller in the picture. This was new because in the Middle Ages people would paint all the figures close up and just overlapping each other. These artists used nudity regularly in their art.'
Up until now, we've been using the FLAT or "brute-force" index to run our queries. Redis also supports the HNSW index which is a fast, approximate index. The HNSW index is a graph-based index that uses a hierarchical navigable small world graph to store vectors. The HNSW index is a good choice for large datasets where you want to run approximate queries.
HNSW will take longer to build and consume more memory for most cases than FLAT but will be faster to run queries on, especially for large datasets.
The following cells will show how to create an HNSW index and run queries with it using the same data as before.
import time# Check if index existsHNSW_INDEX_NAME=INDEX_NAME+"_HNSW"try: redis_client.ft(HNSW_INDEX_NAME).info()print("Index already exists")except:# Create RediSearch Index redis_client.ft(HNSW_INDEX_NAME).create_index(fields= fields,definition= IndexDefinition(prefix=[PREFIX], index_type=IndexType.HASH) )# since RediSearch creates the index in the background for existing documents, we will wait until# indexing is complete before running our queries. Although this is not necessary for the first query,# some queries may take longer to run if the index is not fully built. In general, Redis will perform# best when adding new documents to existing indices rather than new indices on existing documents.while redis_client.ft(HNSW_INDEX_NAME).info()["indexing"] =="1": time.sleep(5)
results = search_redis(redis_client, 'modern art in Europe', index_name=HNSW_INDEX_NAME, k=10)
0. Western Europe (Score: 0.868)
1. Northern Europe (Score: 0.855)
2. Central Europe (Score: 0.843)
3. European (Score: 0.841)
4. Eastern Europe (Score: 0.839)
5. Europe (Score: 0.839)
6. Western European Union (Score: 0.837)
7. Southern Europe (Score: 0.831)
8. Western civilization (Score: 0.83)
9. Council of Europe (Score: 0.827)
# compare the results of the HNSW index to the FLAT index and time both queriesdeftime_queries(iterations: int=10):print(" ----- Flat Index ----- ") t0 = time.time()for i inrange(iterations): results_flat = search_redis(redis_client, 'modern art in Europe', k=10, print_results=False) t0 = (time.time() - t0) / iterations results_flat = search_redis(redis_client, 'modern art in Europe', k=10, print_results=True)print(f"Flat index query time: {round(t0, 3)} seconds\n") time.sleep(1)print(" ----- HNSW Index ------ ") t1 = time.time()for i inrange(iterations): results_hnsw = search_redis(redis_client, 'modern art in Europe', index_name=HNSW_INDEX_NAME, k=10, print_results=False) t1 = (time.time() - t1) / iterations results_hnsw = search_redis(redis_client, 'modern art in Europe', index_name=HNSW_INDEX_NAME, k=10, print_results=True)print(f"HNSW index query time: {round(t1, 3)} seconds")print(" ------------------------ ")time_queries()
----- Flat Index -----
0. Museum of Modern Art (Score: 0.875)
1. Western Europe (Score: 0.867)
2. Renaissance art (Score: 0.864)
3. Pop art (Score: 0.861)
4. Northern Europe (Score: 0.855)
5. Hellenistic art (Score: 0.853)
6. Modernist literature (Score: 0.847)
7. Art film (Score: 0.843)
8. Central Europe (Score: 0.843)
9. Art (Score: 0.842)
Flat index query time: 0.263 seconds
----- HNSW Index ------
0. Western Europe (Score: 0.867)
1. Northern Europe (Score: 0.855)
2. Central Europe (Score: 0.843)
3. European (Score: 0.841)
4. Eastern Europe (Score: 0.839)
5. Europe (Score: 0.839)
6. Western European Union (Score: 0.837)
7. Southern Europe (Score: 0.831)
8. Western civilization (Score: 0.83)
9. Council of Europe (Score: 0.827)
HNSW index query time: 0.129 seconds
------------------------