Prompt Chain

Integrate Weaviate with OpenAI for Hybrid Search

OpenAI Cookbook notebook: run hybrid vector + BM25 search in Weaviate with automatic OpenAI vectorization.

Works with openaiweaviate

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

Add to Favorites

Why it matters

Leverage Weaviate's vector search engine and OpenAI's text embedding capabilities to implement hybrid search for your data. This asset guides you through setting up Weaviate, configuring data schemas with OpenAI modules, and performing hybrid queries.

Outcomes

What it gets done

01

Set up a Weaviate instance (SaaS or local Docker).

02

Configure data schema to utilize OpenAI's text2vec-openai module.

03

Import data into Weaviate, allowing automatic vectorization by OpenAI.

04

Execute hybrid search queries combining vector and BM25 search.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-hybrid-search-with-weaviate-and-openai | bash

Steps

Steps in the chain

01
Prerequisites Setup
02
Connect to Weaviate Instance
03
Schema Configuration
04
Import Data
05
Run Queries

Overview

Using Weaviate with OpenAI vectorize module for Hybrid Search

An OpenAI Cookbook notebook showing how to run hybrid vector + BM25 search in Weaviate using automatic text2vec-openai vectorization and the alpha weighting parameter. Use when your data isn't yet vectorized and you want ranked hybrid search results blending semantic and keyword matching, not a generated Q&A answer.

What it does

This OpenAI Cookbook notebook sets up hybrid search - combining dense vector similarity with BM25 keyword search in a single query - in Weaviate, using the text2vec-openai module so OpenAI embeddings are generated automatically at import and query time rather than computed manually. The demo flow: stand up a Weaviate instance, connect with an OpenAI API key, configure a schema (choosing the embedding model and which properties to vectorize), import a dataset (auto-vectorized), and run hybrid queries whose alpha parameter controls the balance between vector and BM25 scoring.

When to use - and when NOT to

Use this notebook when your data is not yet vectorized and you want search results that blend semantic (vector) relevance with exact keyword matching (BM25) rather than relying on pure vector search alone - hybrid search often performs better than either method alone for production use cases like chatbots and topic modeling, per Weaviate's own hybrid-search explainer linked in the notebook. It is a search-only setup, distinct from the companion Q&A notebook that adds the qna-openai module for generating natural-language answers - use this one when you want ranked search results, not a generated answer.

Inputs and outputs

Inputs are a Weaviate instance (free Weaviate Cloud Service sandbox, recommended, or local Docker at http://localhost:8080 via docker-compose up -d), the weaviate-client, datasets, and apache-beam Python libraries, and an OPENAI_API_KEY environment variable. The notebook's example Articles schema has title, content, and url, vectorizing title and content (not url) with text-embedding-3-small, then imports the Simple Wikipedia dataset via Weaviate's batch import for efficiency. Output is a ranked list of matching articles per query, with the alpha parameter tuning how much weight vector similarity gets versus BM25 keyword matching in the final ranking.

docker-compose up -d
# Weaviate now available at http://localhost:8080

Integrations

Weaviate is an open-source vector search engine storing objects alongside their vectors, combining vector search with structured filtering via a KNN-based vector-optimized index; it offers Python, JavaScript, Java, and Go clients plus a REST API, deployable self-hosted (Docker), as SaaS (Weaviate Cloud Service), or hybrid-SaaS. The text2vec-openai module integrates directly with OpenAI's embeddings API so the calling application never invokes it separately.

Who it's for

Developers building search over unvectorized data who want the precision of keyword matching combined with the recall of semantic search, without manually blending two separate search systems - a common requirement for production chatbots, topic modeling, and other search use cases that need both to work well together.

Source README

Using Weaviate with OpenAI vectorize module for Hybrid Search

This notebook is prepared for a scenario where:

  • Your data is not vectorized
  • You want to run Hybrid Search (learn more) on your data
  • You want to use Weaviate with the OpenAI module (text2vec-openai), to generate vector embeddings for you.

This notebook takes you through a simple flow to set up a Weaviate instance, connect to it (with OpenAI API key), configure data schema, import data (which will automatically generate vector embeddings for your data), and run hybrid search (mixing of vector and BM25 search).

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 Weaviate

Weaviate is an open-source vector search engine that stores data objects together with their vectors. This allows for combining vector search with structured filtering.

Weaviate uses KNN algorithms to create an vector-optimized index, which allows your queries to run extremely fast. Learn more here.

Weaviate let you use your favorite ML-models, and scale seamlessly into billions of data objects.

Deployment options

Whatever your scenario or production setup, Weaviate has an option for you. You can deploy Weaviate in the following setups:

  • Self-hosted - you can deploy Weaviate with docker locally, or any server you want.
  • SaaS - you can use Weaviate Cloud Service (WCS) to host your Weaviate instances.
  • Hybrid-SaaS - you can deploy Weaviate in your own private Cloud Service

Programming languages

Weaviate offers four client libraries, which allow you to communicate from your apps:

Additionally, Weaviate has a REST layer. Basically you can call Weaviate from any language that supports REST requests.

Demo Flow

The demo flow is:

  • Prerequisites Setup: Create a Weaviate instance and install required libraries
  • Connect: Connect to your Weaviate instance
  • Schema Configuration: Configure the schema of your data
    • Note: Here we can define which OpenAI Embedding Model to use
    • Note: Here we can configure which properties to index
  • Import data: Load a demo dataset and import it into Weaviate
    • Note: The import process will automatically index your data - based on the configuration in the schema
    • Note: You don't need to explicitly vectorize your data, Weaviate will communicate with OpenAI to do it for you
  • Run Queries: Query
    • Note: You don't need to explicitly vectorize your queries, Weaviate will communicate with OpenAI to do it for you

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.

OpenAI Module in Weaviate

All Weaviate instances come equipped with the text2vec-openai module.

This module is responsible for handling vectorization during import (or any CRUD operations) and when you run a query.

No need to manually vectorize data

This is great news for you. With text2vec-openai you don't need to manually vectorize your data, as Weaviate will call OpenAI for you whenever necessary.

All you need to do is:

  1. provide your OpenAI API Key - when you connected to the Weaviate Client
  2. define which OpenAI vectorizer to use in your Schema

Prerequisites

Before we start this project, we need setup the following:

  • create a Weaviate instance
  • install libraries
    • weaviate-client
    • datasets
    • apache-beam
  • get your OpenAI API key

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

Create a Weaviate instance

To create a Weaviate instance we have 2 options:

  1. (Recommended path) Weaviate Cloud Service - to host your Weaviate instance in the cloud. The free sandbox should be more than enough for this cookbook.
  2. Install and run Weaviate locally with Docker.
Option 1 - WCS Installation Steps

Use Weaviate Cloud Service (WCS) to create a free Weaviate cluster.

  1. create a free account and/or login to WCS
  2. create a Weaviate Cluster with the following settings:
    • Sandbox: Sandbox Free
    • Weaviate Version: Use default (latest)
    • OIDC Authentication: Disabled
  3. your instance should be ready in a minute or two
  4. make a note of the Cluster Id. The link will take you to the full path of your cluster (you will need it later to connect to it). It should be something like: https://your-project-name.weaviate.network
Option 2 - local Weaviate instance with Docker

Install and run Weaviate locally with Docker.

  1. Download the ./docker-compose.yml file
  2. Then open your terminal, navigate to where your docker-compose.yml file is located, and start docker with: docker-compose up -d
  3. Once this is ready, your instance should be available at http://localhost:8080

Note. To shut down your docker instance you can call: docker-compose down

Learn more

To learn more, about using Weaviate with Docker see the installation documentation.

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

Install required libraries

Before running this project make sure to have the following libraries:

Weaviate Python client

The Weaviate Python client allows you to communicate with your Weaviate instance from your Python project.

datasets & apache-beam

To load sample data, you need the datasets library and its' dependency apache-beam.

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

Prepare your OpenAI API key

The OpenAI API key is used for vectorization of your data at import, and for running queries.

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.

Connect to your Weaviate instance

In this section, we will:

  1. test env variable OPENAI_API_KEY - make sure you completed the step in #Prepare-your-OpenAI-API-key
  2. connect to your Weaviate your OpenAI API Key
  3. and test the client connection

The client

After this step, the client object will be used to perform all Weaviate-related operations.

Schema

In this section, we will:

  1. configure the data schema for your data
  2. select OpenAI module

This is the second and final step, which requires OpenAI specific configuration.
After this step, the rest of instructions wlll only touch on Weaviate, as the OpenAI tasks will be handled automatically.

What is a schema

In Weaviate you create schemas to capture each of the entities you will be searching.

A schema is how you tell Weaviate:

  • what embedding model should be used to vectorize the data
  • what your data is made of (property names and types)
  • which properties should be vectorized and indexed

In this cookbook we will use a dataset for Articles, which contains:

  • title
  • content
  • url

We want to vectorize title and content, but not the url.

To vectorize and query the data, we will use text-embedding-3-small.

Import data

In this section we will:

  1. load the Simple Wikipedia dataset
  2. configure Weaviate Batch import (to make the import more efficient)
  3. import the data into Weaviate

Note:

Like mentioned before. We don't need to manually vectorize the data.

The text2vec-openai module will take care of that.

Search Data

As above, we'll fire some queries at our new Index and get back results based on the closeness to our existing vectors

Learn more about the alpha setting here

Thanks for following along, you're now equipped to set up your own vector databases and use embeddings to do all kinds of cool things - enjoy! For more complex use cases please continue to work through other cookbook examples in this repo.

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.