Prompt Chain

Query Weaviate with OpenAI for Answers

OpenAI Cookbook notebook: auto-vectorize data in Weaviate and answer questions over it via the qna-openai module.

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 and OpenAI's Q&A module to build a question-answering system on your un-vectorized data. This asset automates data ingestion, vectorization, and query processing.

Outcomes

What it gets done

01

Set up and connect to a Weaviate instance.

02

Configure data schema and integrate OpenAI for vectorization.

03

Import data, allowing Weaviate to automatically generate embeddings.

04

Run queries against your data and receive answers generated by OpenAI.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/oai-question-answering-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 Question Answering Queries

Overview

Question Answering in Weaviate with OpenAI Q&A module

An OpenAI Cookbook notebook showing how to auto-vectorize data in Weaviate and answer questions over it using the text2vec-openai and qna-openai modules. Use when your data isn't yet vectorized and you want Weaviate to handle OpenAI embedding and completion calls automatically for Q&A.

What it does

This OpenAI Cookbook notebook builds question answering over not-yet-vectorized data using Weaviate's two OpenAI-backed modules: text2vec-openai for automatic vectorization at import and query time, and qna-openai for automatically calling the OpenAI completions endpoint to answer questions against the closest-matching vectors. The demo flow is: stand up a Weaviate instance, connect with an OpenAI API key, configure a schema (choosing the embedding model and which properties get vectorized), import a dataset (Weaviate calls OpenAI to vectorize it automatically), and run Q&A queries (Weaviate again calls OpenAI automatically, this time for the completion).

When to use - and when NOT to

Use this notebook when your data is not already vectorized and you want Weaviate to handle both embedding and answer-generation calls to OpenAI automatically, rather than manually calling the OpenAI embeddings and completions APIs yourself. It's a good starting point for Weaviate plus OpenAI Q&A specifically; it is not needed if your data is already embedded elsewhere or if you want to control vectorization and completion calls directly instead of delegating them to Weaviate's built-in modules.

Inputs and outputs

Inputs are a Weaviate instance (via 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 used both for vectorization and completions. A schema defines which properties get vectorized - the notebook's example Articles dataset has title, content, and url, vectorizing title and content but not url - using text-embedding-3-small for embeddings and gpt-3.5-turbo-instruct for Q&A completions. Output is natural-language answers returned by querying the imported Simple Wikipedia dataset, ranked by vector closeness to the question.

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

Integrations

Weaviate is an open-source vector search engine that stores objects alongside their vectors, using a KNN-based vector-optimized index for fast queries combined with structured filtering; it offers Python, JavaScript, Java, and Go client libraries plus a REST API, and can be deployed self-hosted (Docker), as SaaS (Weaviate Cloud Service), or hybrid-SaaS. The text2vec-openai and qna-openai modules integrate directly with OpenAI's embeddings and completions APIs so the calling application never has to invoke them separately.

Who it's for

Developers building a question-answering system over unvectorized data who want Weaviate to manage both the embedding and completion calls to OpenAI automatically - a lower-friction starting point than wiring up embeddings and a separate LLM call by hand, and a template that scales from a free Weaviate sandbox to production self-hosted or cloud deployments.

Source README

Question Answering in Weaviate with OpenAI Q&A module

This notebook is prepared for a scenario where:

  • Your data is not vectorized
  • You want to run Q&A (learn more) on your data based on the OpenAI completions endpoint.
  • 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 question answering.

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
    • Note: The qna-openai module automatically communicates with the OpenAI completions endpoint

Once you've run through this notebook you should have a basic understanding of how to setup and use vector databases for question answering.

OpenAI Module in Weaviate

All Weaviate instances come equipped with the text2vec-openai and the qna-openai modules.

The first module is responsible for handling vectorization at import (or any CRUD operations) and when you run a search query. The second module communicates with the OpenAI completions endpoint.

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 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. For Q&A we will use gpt-3.5-turbo-instruct.

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.

Question Answering on the 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.