Prompt Chain

Integrate Weaviate with OpenAI for Vector Search

OpenAI cookbook using Weaviate's text2vec-openai module to auto-vectorize and semantically search unvectorized data.

Works with openaiweaviate

93
Spark score
out of 100
Updated 20 days ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Leverage Weaviate's vector search engine with OpenAI's text embedding models to efficiently store and query your data. This asset automates the vectorization process, enabling semantic search on your un-vectorized datasets.

Outcomes

What it gets done

01

Set up and connect to a Weaviate instance using your OpenAI API key.

02

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

03

Import data into Weaviate, allowing automatic vector embedding generation.

04

Perform semantic searches without manual query vectorization.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-getting-started-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 Embeddings Search

OpenAI cookbook using Weaviate's built-in text2vec-openai module to automatically vectorize unvectorized data with text-embedding-3-small at both import and query time, covering deployment options, schema configuration, and semantic search. Use when source data has no embeddings yet and you want Weaviate to handle vectorization automatically rather than embedding it yourself.

What it does

This OpenAI cookbook notebook is for the scenario where your data is not yet vectorized and you want Weaviate's built-in text2vec-openai module to generate embeddings for you automatically, rather than embedding data yourself before import. Weaviate is introduced as an open-source vector search engine that stores data objects together with their vectors (enabling vector search combined with structured filtering) and uses KNN algorithms to build a vector-optimized index for fast queries, scaling to billions of objects. It covers Weaviate's three deployment options - self-hosted via Docker, SaaS via Weaviate Cloud Service (WCS), or hybrid-SaaS in your own private cloud - and its four official client libraries (Python, JavaScript, Java, Go) plus a REST layer callable from any language. The demo flow is: set up a Weaviate instance and install required libraries (weaviate-client, datasets, apache-beam), connect using your OpenAI API key, configure a data schema selecting which OpenAI embedding model to use and which properties to vectorize and index, import a demo dataset (which is automatically vectorized during import based on that schema), and run queries (which are also automatically vectorized). For setup, it offers a recommended WCS path (create a free Sandbox cluster with OIDC authentication disabled) or a local Docker path (download the provided docker-compose.yml, run docker-compose up -d, and the instance becomes available at localhost:8080, shut down later with docker-compose down). The schema example defines an Articles entity with title, content, and url properties, configured so title and content are vectorized (but not url) using the text-embedding-3-small model. It then loads the Simple Wikipedia dataset, configures Weaviate's batch import for efficiency, and imports the data - with vectorization handled entirely by the text2vec-openai module rather than any manual embedding step - before running semantic search queries against the resulting index.

When to use - and when NOT to

Use this notebook when your source data has no embeddings yet and you want Weaviate to handle vectorization automatically at import and query time, using only your OpenAI API key and a schema configuration - the more common real-world starting point than already having vectors. It is not the notebook to use if you already have precomputed embeddings you want to bring directly into Weaviate (see the companion Using Weaviate for Embeddings Search notebook for that "bring your own vectors" path).

Inputs and outputs

Input is an unvectorized dataset (here, Simple Wikipedia articles with title, content, and url fields) plus natural-language search queries. Output is a populated Weaviate index with automatically generated text-embedding-3-small vectors on the vectorized fields, and semantic search results ranked by vector closeness, all without any manual embedding calls.

Integrations

Built on Weaviate (self-hosted, WCS SaaS, or hybrid-SaaS deployment; Python/JavaScript/Java/Go clients or REST), its built-in text2vec-openai vectorizer module using OpenAI's text-embedding-3-small model, and the Hugging Face datasets library (with apache-beam) for loading the Simple Wikipedia dataset.

docker-compose up -d

Who it's for

Developers with unvectorized data who want Weaviate to handle OpenAI embedding generation automatically at both import and query time, without writing manual embedding calls into their application code.

Source README

Using Weaviate with OpenAI vectorize module for Embeddings Search

This notebook is prepared for a scenario where:

  • Your data is not vectorized
  • You want to run Vector Search 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 semantic 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 the 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 with 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

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.