Prompt Chain

Query Redis with OpenAI Embeddings

Jupyter notebook demonstrating hybrid vector similarity search with Redis and OpenAI embeddings, combining VSS with lexical filtering on GEO, NUMERIC, TAG, and

Works with redisopenai

91
Spark score
out of 100
Updated 3 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Leverage Redis as a vector database with OpenAI embeddings to perform hybrid searches. Combine vector similarity with traditional filtering for more precise data retrieval.

Outcomes

What it gets done

01

Set up Redis with RediSearch for vector storage.

02

Generate OpenAI embeddings for data.

03

Create a search index in Redis.

04

Execute hybrid queries combining vector and keyword search.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-redis-hybrid-query-examples | bash

Steps

Steps in the chain

01
Start Redis

Start a Redis database with RediSearch using Docker Compose. Run `docker-compose up -d` to start the Redis Stack docker container, which includes RedisInsight GUI accessible at http://localhost:8001 for managing your Redis database.

02
Install Requirements

Install Redis-Py, the Python client for communicating with Redis. This library is used to communicate with the Redis-stack database.

03
Prepare your OpenAI API key

Obtain an OpenAI API key from https://beta.openai.com/account/api-keys. Add the key to your environment variables as OPENAI_API_KEY using the appropriate command for your system.

04
Load data

Load and clean an ecommerce dataset. Generate embeddings using OpenAI and prepare this data to create an index in Redis for searching similar vectors.

05
Connect to Redis

Connect to the running Redis database using the Redis-py client with the default host and port (localhost:6379).

06
Creating a Search Index in Redis

Create a search index in Redis by: 1) Setting constants for the index like distance metric and index name, 2) Defining the index schema with RediSearch fields, 3) Creating the index.

07
Generate OpenAI Embeddings and Load Documents

Get OpenAI embeddings for products in the dataset and load documents into the Redis index using the HASH data type to store documents.

08
Simple Vector Search Queries

Run vector search queries using OpenAI query embeddings. Create a function that runs search queries and returns results to demonstrate using Redis as a vector database.

09
Hybrid Queries with Redis

Combine vector search with other RediSearch fields for hybrid search. Demonstrate combining vector search with full text search and other filtering capabilities.

Overview

Running Hybrid VSS Queries with Redis and OpenAI

What it does

This Jupyter notebook teaches how to use Redis as a vector database with the RediSearch module to index and query OpenAI embeddings. It demonstrates creating a search index, generating embeddings for e-commerce product data, loading documents as Redis HASHes, and running hybrid queries that combine vector similarity search with traditional Redis filtering on GEO, NUMERIC, TAG, and TEXT fields. Includes setup via Redis Stack Docker container and RedisInsight GUI access.

How it connects

Use this when you need to combine semantic search (via OpenAI embeddings) with structured filters like location, price range, or tags in a single query-common in e-commerce (find visually similar products in a region and price band), content discovery, or recommendation systems. Ideal when you want Redis's real-time performance for both vector similarity and traditional search without maintaining separate systems.

Source README

Running Hybrid VSS Queries with Redis and OpenAI

This notebook provides an introduction to using Redis as a vector database with OpenAI embeddings and running hybrid queries that combine VSS and lexical search using Redis Query and Search capability. Redis is a scalable, real-time database that can be used as a vector database when using the RediSearch Module. The Redis Query and Search capability allows you to index and search for vectors in Redis. This notebook will show you how to use the Redis Query and Search to index and search for vectors created by using the OpenAI API and stored in Redis.

Hybrid queries combine vector similarity with traditional Redis Query and Search filtering capabilities on GEO, NUMERIC, TAG or TEXT data simplifying application code. A common example of a hybrid query in an e-commerce use case is to find items visually similar to a given query image limited to items available in a GEO location and within a price range.

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 and clean an ecommerce dataset. We'll generate embeddings using OpenAI and 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

Generate OpenAI Embeddings and Load Documents into the Index

Now that we have a search index, we can load documents into it. We will use the dataframe containing the styles dataset loaded previously. 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 cells below will show how to get OpenAI embeddings for the different products and 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 example below, we will combine vector search with full text search.

Step 1: Start Redis

Start a Redis database with RediSearch using Docker Compose. Run `docker-compose up -d` to start the Redis Stack docker container, which includes RedisInsight GUI accessible at http://localhost:8001 for managing your Redis database.

Step 2: Install Requirements

Install Redis-Py, the Python client for communicating with Redis. This library is used to communicate with the Redis-stack database.

Step 3: Prepare your OpenAI API key

Obtain an OpenAI API key from https://beta.openai.com/account/api-keys. Add the key to your environment variables as OPENAI_API_KEY using the appropriate command for your system.

Step 4: Load data

Load and clean an ecommerce dataset. Generate embeddings using OpenAI and prepare this data to create an index in Redis for searching similar vectors.

Step 5: Connect to Redis

Connect to the running Redis database using the Redis-py client with the default host and port (localhost:6379).

Step 6: Creating a Search Index in Redis

Create a search index in Redis by: 1) Setting constants for the index like distance metric and index name, 2) Defining the index schema with RediSearch fields, 3) Creating the index.

Step 7: Generate OpenAI Embeddings and Load Documents

Get OpenAI embeddings for products in the dataset and load documents into the Redis index using the HASH data type to store documents.

Step 8: Simple Vector Search Queries

Run vector search queries using OpenAI query embeddings. Create a function that runs search queries and returns results to demonstrate using Redis as a vector database.

Step 9: Hybrid Queries with Redis

Combine vector search with other RediSearch fields for hybrid search. Demonstrate combining vector search with full text search and other filtering capabilities.

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.