Prompt Chain

Index and Search Embeddings with Redis

OpenAI cookbook demo flow for embedding data and running vector plus hybrid search in Redis with RediSearch.

Works with redisopenai

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

Add to Favorites

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

01

Set up Redis with the RediSearch module for vector storage.

02

Index embedded data into Redis using Hash data types.

03

Perform hybrid search (vector + full-text) on indexed data.

04

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

01
Setup: Import packages and set variables
02
Load data: Embed dataset
03
Redis Setup: Configure Redis-Py client
04
Creating a Search Index
05
Load Documents into the Index
06
Running Search Queries
07
Hybrid Search: Vector + Full-Text

Overview

Using Redis for Embeddings Search

OpenAI cookbook demo flow embedding data with OpenAI embeddings and indexing/searching it in Redis via the RediSearch module, covering Docker-based Redis Stack setup, index schema creation, selective field returns, and hybrid vector-plus-filter search. Use as a first introduction to Redis as a vector database, especially when adding vector and hybrid search to an existing Redis-based stack.

What it does

This OpenAI cookbook notebook is a simple end-to-end flow for downloading data, embedding it with OpenAI embeddings, and indexing and searching it in Redis - framed as a common requirement for customers wanting to store and search embeddings alongside their own data in a secure environment for production use cases like chatbots and topic modelling. It first explains why vector databases matter: embeddings-based use cases often work at small scale but performance and security concerns block production deployment, and a vector database is the key component for solving that. The demo flow is setup, load and embed a dataset, then Redis-specific setup, index creation, and example search queries. For Redis specifically, it explains that the RediSearch module - long used by enterprises across cloud providers and on-premise deployments - recently gained vector storage and search capability in addition to its existing full-text, tag, geo, and numeric search features, and lists official client libraries across languages (Java's jedis, Python's redis-py, Node.js's node-redis, .NET's nredisstack, Go's redisearch-go, Rust's redisearch-api-rs). It deploys Redis Stack (Redis with RediSearch) via Docker with docker compose up -d, which also bundles the RedisInsight GUI at localhost:8001 for managing the database. It then creates a search index by setting constants like the distance metric and index name, defining the index schema with RediSearch fields, and creating the index; loads documents into that index using Redis's HASH data type (noting JSON via RedisJSON is also an option); and runs example search queries demonstrating two specific features - returning only a chosen subset of fields (like just title) without a separate document-retrieval call, and hybrid search, combining vector similarity with other RediSearch field types like full-text, tag, geo, or numeric filters in a single query.

When to use - and when NOT to

Use this notebook as a first, minimal introduction to setting up and querying Redis as a vector database - especially if your stack already uses Redis and you want to add vector and hybrid search without introducing a separate dedicated vector database. It is not a deep dive into Redis deployment options beyond Docker (the guide points to a separate redis directory in the repo for those), and for a more advanced e-commerce-style hybrid query example combining vector search with GEO/NUMERIC/TAG/TEXT filters, the companion Redis hybrid VSS queries notebook goes further.

Inputs and outputs

Input is a dataset to embed with OpenAI embeddings. Output is a searchable Redis index (via RediSearch) supporting plain vector similarity search, selective field returns, and hybrid queries that combine vector similarity with full-text, tag, geo, or numeric filtering.

Integrations

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

$ cd redis
$ docker compose up -d

Who it's for

Developers already using or considering Redis who want to add vector and hybrid search to an existing Redis-based stack without standing up a separate vector database.

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 Stars
redis-py Python MIT Redis Stars
node-redis Node.js MIT Redis Stars
nredisstack .NET MIT Redis Stars
redisearch-go Go BSD Redis redisearch-go-stars
redisearch-api-rs Rust BSD Redis redisearch-api-rs-stars

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

  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.

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.

  1. 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 title field in the search results.
  2. 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

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.