Prompt Chain

Integrate Redis as a Vector Database with OpenAI

An intro OpenAI Cookbook notebook to Redis as a vector database - deployment options, index creation, and the FLAT vs HNSW tradeoff.

Works with redisopenai

81
Spark score
out of 100
Updated last month
Version 1.0.0
Models

Add to Favorites

Why it matters

Leverage Redis as a scalable vector database for your AI applications by integrating it with OpenAI embeddings. This asset demonstrates how to index, store, and query vector data efficiently, enabling powerful semantic search capabilities.

Outcomes

What it gets done

01

Set up Redis with the RediSearch module for vector storage.

02

Generate OpenAI embeddings for your data.

03

Index and load embedded data into Redis.

04

Perform vector similarity searches and hybrid queries.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-getting-started-with-redis-and-openai | bash

Steps

Steps in the chain

01
Start Redis
02
Install Requirements
03
Prepare your OpenAI API key
04
Load data
05
Connect to Redis
06
Creating a Search Index in Redis
07
Load Documents into the Index
08
Simple Vector Search Queries with OpenAI Query Embeddings
09
Hybrid Queries with Redis
10
HNSW Index

Overview

Using Redis as a Vector Database with OpenAI

This introductory notebook covers Redis as a vector database: what RediSearch is, deployment options from Docker to Redis Cloud/Enterprise, index creation, vector and hybrid search queries, and the FLAT versus HNSW index tradeoff. Use it as a first introduction to Redis vector search and deployment choices before diving into advanced embeddings-search material.

What it does

An OpenAI Cookbook introductory notebook for using Redis as a vector database with OpenAI embeddings via the RediSearch module. It explains what Redis is - an open-source key-value store usable as a cache, message broker, and database - and what Redis Modules add, including RedisJSON, RedisTimeSeries, RedisBloom, and RediSearch, the module providing secondary indexing, full-text search, and vector search. It surveys deployment options: the Redis Stack Docker container for local development, Redis Cloud for a fully managed production service, Redis Enterprise for self-hosted or Kubernetes deployment, and Redis Enterprise marketplace listings on AWS, Google Cloud, and Azure. Local setup starts the Redis Stack container via docker-compose:

docker-compose up -d

with the RedisInsight GUI available at localhost:8001 for managing the database.

When to use - and when NOT to

Use it as a first introduction to Redis as a vector database before moving to more advanced embeddings-search material - covering Redis and RediSearch fundamentals, deployment choices, and the FLAT-versus-HNSW index tradeoff. It is not a deep reference for hybrid query syntax or advanced RediSearch features - it's an entry point, one of several vector-database notebooks in the OpenAI Cookbook.

Inputs and outputs

Input is pre-embedded vector data plus natural-language queries embedded via the OpenAI API, using an API key set as the OPENAI_API_KEY environment variable. The walkthrough installs the redis-py client, connects to Redis on the default localhost:6379, and creates a search index by first setting constants such as the distance metric and index name, then defining the RediSearch field schema, then creating the index itself. Documents are loaded into the index as Redis records, either as the HASH data type used throughout the walkthrough or, if RedisJSON is enabled alongside RediSearch, as JSON documents instead. Output is a running RediSearch index, either FLAT or HNSW, plus ranked search results per query - including hybrid queries that combine vector search with full-text search fields. It closes by introducing the HNSW index as an alternative to the FLAT, or brute-force, index used throughout - a graph-based, hierarchical navigable small world index that is slower and more memory-hungry to build but faster to query, making it the better choice for large datasets needing approximate search.

Who it's for

Developers new to using Redis as a vector store who need to choose a deployment option and understand the FLAT-versus-HNSW indexing tradeoff before building a production embeddings search system.

Source README

Using Redis as a Vector Database with OpenAI

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.

What is 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.

In addition to the traditional uses of Redis. Redis also provides Redis Modules which are a way to extend Redis with new data types and commands. Example modules include RedisJSON, RedisTimeSeries, RedisBloom and RediSearch.

What is RediSearch?

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.

Deployment options

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.

Additionally, every major cloud provider (AWS Marketplace, Google Marketplace, or Azure Marketplace) offers Redis Enterprise in a marketplace offering.

Prerequisites

Before we start this project, we need to set up the following:

===========================================================

Start Redis

To keep this example simple, we will use the Redis Stack docker container which we can start as follows

$ 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.

Install Requirements

Redis-Py is the python client for communicating with Redis. We will use this to communicate with our Redis-stack database.

===========================================================

Prepare your OpenAI API key

The OpenAI API key is used for vectorization of query data.

If you don't have an OpenAI API key, you can get one from https://beta.openai.com/account/api-keys.

Once you get your key, please add it to your environment variables as OPENAI_API_KEY by using following command:

Load data

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.

Connect to Redis

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.

Creating a Search Index in Redis

The below cells will show how to specify and create a search index in Redis. We will:

  1. Set some constants for defining our index like the distance metric and the index name
  2. Define the index schema with RediSearch fields
  3. 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.

Simple Vector Search Queries with OpenAI Query Embeddings

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.

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.

HNSW Index

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.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.