Index and Search Embeddings with Redis
Index and search embeddings using Redis and the RediSearch module.
Why it matters
Leverage Redis as a vector database to securely store, index, and search embeddings generated from your data. This enables powerful applications like chatbots and topic modeling by facilitating semantic search over unstructured content.
Outcomes
What it gets done
Set up Redis with the RediSearch module for vector storage.
Index embedded data into Redis using Hash data types.
Perform hybrid search (vector + full-text) on indexed data.
Retrieve specific fields from search results.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/oai-usingredisforembeddingssearch | bash Steps
Steps in the chain
Import the required libraries and set the embedding model that we'd like to use.
In this section we'll load embedded data that we've prepared previous to this session.
Set up the Redis-Py client. Start a version of Redis with RediSearch (Redis Stack) by running docker compose up -d in the redis directory. This includes RedisInsight GUI accessible at http://localhost:8001. Import and create a client for communicating with the Redis database.
Set some constants for defining the index like the distance metric and index name. Define the index schema with RediSearch fields. Create the index.
Load documents into the search index using the same documents from previous examples. Use the HASH data type to store documents in Redis. Show how to load documents into the index.
Run search queries against the indexed documents. Provide a function that runs a search query and returns results. Demonstrate features like specifying return fields to only return a subset of fields in documents.
Combine vector search with other RediSearch fields for hybrid search such as full text search, tag, geo, and numeric. Demonstrate combining vector search with full text search in example queries.
Overview
Using Redis for Embeddings Search
What it does
This notebook demonstrates a flow for downloading data, embedding it, and then indexing and searching it using Redis with the RediSearch module.
How it connects
Use this when you want to store and search embeddings with your own data in a secure environment to support production use cases such as chatbots and topic modeling. This is a common requirement for customers. Do not use this if you only need basic key-value storage without vector search capabilities, or if you are not working with embedding vectors.
Source README
Using Redis for Embeddings Search
This notebook takes you through a simple flow to download some data, embed it, and then index and search it using a selection of vector databases. This is a common requirement for customers who want to store and search our embeddings with their own data in a secure environment to support production use cases such as chatbots, topic modelling and more.
What is a Vector Database
A vector database is a database made to store, manage and search embedding vectors. The use of embeddings to encode unstructured data (text, audio, video and more) as vectors for consumption by machine-learning models has exploded in recent years, due to the increasing effectiveness of AI in solving use cases involving natural language, image recognition and other unstructured forms of data. Vector databases have emerged as an effective solution for enterprises to deliver and scale these use cases.
Why use a Vector Database
Vector databases enable enterprises to take many of the embeddings use cases we've shared in this repo (question and answering, chatbot and recommendation services, for example), and make use of them in a secure, scalable environment. Many of our customers make embeddings solve their problems at small scale but performance and security hold them back from going into production - we see vector databases as a key component in solving that, and in this guide we'll walk through the basics of embedding text data, storing it in a vector database and using it for semantic search.
Demo Flow
The demo flow is:
- Setup: Import packages and set any required variables
- Load data: Load a dataset and embed it using OpenAI embeddings
- Redis
- Setup: Set up the Redis-Py client. For more details go here
- Index Data: Create the search index for vector search and hybrid search (vector + full-text search) on all available fields.
- Search Data: Run a few example queries with various goals in mind.
Once you've run through this notebook you should have a basic understanding of how to setup and use vector databases, and can move on to more complex use cases making use of our embeddings.
Setup
Import the required libraries and set the embedding model that we'd like to use.
Load data
In this section we'll load embedded data that we've prepared previous to this session.
Redis
The next vector database covered in this tutorial is Redis. You most likely already know Redis. What you might not be aware of is the RediSearch module. Enterprises have been using Redis with the RediSearch module for years now across all major cloud providers, Redis Cloud, and on premise. Recently, the Redis team added vector storage and search capability to this module in addition to the features RediSearch already had.
Given the large ecosystem around Redis, there are most likely client libraries in the language you need. You can use any standard Redis client library to run RediSearch commands, but it's easiest to use a library that wraps the RediSearch API. Below are a few examples, but you can find more client libraries here.
| Project | Language | License | Author | Stars |
|---|---|---|---|---|
| jedis | Java | MIT | Redis | |
| redis-py | Python | MIT | Redis | |
| node-redis | Node.js | MIT | Redis | |
| nredisstack | .NET | MIT | Redis | |
| redisearch-go | Go | BSD | Redis | |
| redisearch-api-rs | Rust | BSD | Redis |
In the below cells, we will walk you through using Redis as a vector database. Since many of you are likely already used to the Redis API, this should be familiar to most.
Setup
There are many ways to deploy Redis with RediSearch. The easiest way to get started is to use Docker, but there are are many potential options for deployment. For other deployment options, see the redis directory in this repo.
For this tutorial, we will use Redis Stack on Docker.
Start a version of Redis with RediSearch (Redis Stack) by running the following docker command
$ cd redis
$ docker compose up -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.
Creating a Search Index
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
Load Documents into the Index
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.
Running Search Queries
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. Each example will demonstrate specific features to keep in mind when developing your search application with Redis.
- Return Fields: You can specify which fields you want to return in the search results. This is useful if you only want to return a subset of the fields in your documents and doesn't require a separate call to retrieve documents. In the below example, we will only return the
titlefield in the search results. - Hybrid Search: You can combine vector search with any of the other RediSearch fields for hybrid search such as full text search, tag, geo, and numeric. In the below example, we will combine vector search with full text search.
Hybrid Queries with Redis
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.
For more example with Redis as a vector database, see the README and examples within the vector_databases/redis directory of this repository
Step 1: Setup
Import the required libraries and set the embedding model that we'd like to use.
Step 2: Load data
In this section we'll load embedded data that we've prepared previous to this session.
Step 3: Redis Setup
Set up the Redis-Py client. Start a version of Redis with RediSearch (Redis Stack) by running docker compose up -d in the redis directory. This includes RedisInsight GUI accessible at http://localhost:8001. Import and create a client for communicating with the Redis database.
Step 4: Creating a Search Index
Set some constants for defining the index like the distance metric and index name. Define the index schema with RediSearch fields. Create the index.
Step 5: Load Documents into the Index
Load documents into the search index using the same documents from previous examples. Use the HASH data type to store documents in Redis. Show how to load documents into the index.
Step 6: Running Search Queries
Run search queries against the indexed documents. Provide a function that runs a search query and returns results. Demonstrate features like specifying return fields to only return a subset of fields in documents.
Step 7: Hybrid Search
Combine vector search with other RediSearch fields for hybrid search such as full text search, tag, geo, and numeric. Demonstrate combining vector search with full text search in example queries.
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.