Prompt Chain

Query Redis with OpenAI Embeddings

OpenAI cookbook running hybrid vector-plus-lexical search queries on Redis with an e-commerce OpenAI-embedded dataset.

Works with redisopenai

93
Spark score
out of 100
Updated 29 days 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
02
Install Requirements
03
Prepare your OpenAI API key
04
Load data
05
Connect to Redis
06
Creating a Search Index in Redis
07
Generate OpenAI Embeddings and Load Documents
08
Simple Vector Search Queries
09
Hybrid Queries with Redis

Overview

Running Hybrid VSS Queries with Redis and OpenAI

OpenAI cookbook running hybrid vector-plus-structured-filter queries in Redis via RediSearch, using an OpenAI-embedded e-commerce dataset to demonstrate combining vector similarity with GEO, price, tag, and text constraints in one query. Use when a search or recommendation feature needs vector similarity combined with structured filters like location, price, or category in a single query.

What it does

This OpenAI cookbook notebook introduces using Redis as a vector database with OpenAI embeddings, focused specifically on running hybrid queries that combine vector similarity search (VSS) with traditional Redis Query and Search filtering - on GEO, NUMERIC, TAG, or TEXT fields - via the RediSearch module. It frames the motivating use case as e-commerce: finding items visually similar to a query image, but limited to items available in a given GEO location and within a price range, all as a single hybrid query rather than separate application-side filtering steps. Setup starts Redis Stack (Redis with RediSearch) via a Docker Compose container, which also bundles the RedisInsight GUI for managing the database at localhost:8001, and installs redis-py as the Python client, connecting to the default localhost:6379 host and port. The OpenAI API key is set as the OPENAI_API_KEY environment variable for vectorizing query data. It loads and cleans an e-commerce styles dataset, then creates a Redis search index by setting constants for the distance metric and index name and defining the index schema with RediSearch fields. Documents are loaded into the index using the HASH data type (noting RedisJSON is an alternative), with OpenAI embeddings generated for each product before insertion. It then runs simple vector search queries using OpenAI-embedded query text against the indexed product vectors, and finally demonstrates hybrid queries that combine that vector similarity search with other RediSearch field types for full-text search, showing the pattern extends to GEO, NUMERIC, and TAG filters as well.

When to use - and when NOT to

Use this notebook when an application needs to filter vector similarity results by structured attributes in a single query - price range, location, category tags, or text matches - rather than post-filtering results in application code, as in e-commerce product search, geo-constrained recommendations, or attribute-filtered semantic search. It is not a general introduction to Redis as a vector database (see the companion Using Redis for Embeddings Search notebook for that broader setup) - this notebook assumes basic Redis vector search familiarity and focuses specifically on the hybrid-query pattern.

Inputs and outputs

Input is an e-commerce product dataset (styles) plus a search query with optional GEO/NUMERIC/TAG/TEXT constraints. Output is a ranked list of products matching both the vector-similarity criteria and the structured filter constraints, retrieved from a single RediSearch hybrid query.

Integrations

Built on Redis Stack (Redis plus RediSearch) deployed via Docker Compose, the redis-py Python client, RedisInsight for database management, and the OpenAI embeddings API for vectorizing products and queries.

$ docker-compose up -d

Who it's for

Developers building search or recommendation features - especially e-commerce - that need to combine semantic vector similarity with structured filters like price, location, or category in a single efficient query rather than filtering in application code.

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.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.