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.
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.
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.
Setup: Import packages and set any required variables
Load data: Load a dataset and embed it using OpenAI embeddings
Qdrant
Setup: Here we'll set up the Python client for Qdrant. For more details go here
Index Data: We'll create a collection with vectors for titles and content
Search Data: We'll run a few searches to confirm it works
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.
Import the required libraries and set the embedding model that we'd like to use.
# We'll need to install Qdrant client!pip install qdrant-client
import openaiimport pandas as pdfrom ast import literal_evalimport qdrant_client # Qdrant's client library for Python# This can be changed to the embedding model of your choice. Make sure its the same model that is used for generating embeddingsEMBEDDING_MODEL="text-embedding-ada-002"# Ignore unclosed SSL socket warnings - optional in case you get these errorsimport warningswarnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)warnings.filterwarnings("ignore", category=DeprecationWarning)
In this section we'll load embedded data that we've prepared previous to this session.
import wgetembeddings_url ="https://cdn.openai.com/API/examples/data/vector_database_wikipedia_articles_embedded.zip"# The file is ~700 MB so this will take some timewget.download(embeddings_url)
# Read vectors from strings back into a listarticle_df['title_vector'] = article_df.title_vector.apply(literal_eval)article_df['content_vector'] = article_df.content_vector.apply(literal_eval)# Set vector_id to be a stringarticle_df['vector_id'] = article_df['vector_id'].apply(str)
Qdrant. is a high-performant vector search database written in Rust. It offers both on-premise and cloud version, but for the purposes of that example we're going to use the local deployment mode.
Setting everything up will require:
Spinning up a local instance of Qdrant
Configuring the collection and storing the data in it
For the local deployment, we are going to use Docker, according to the Qdrant documentation: https://qdrant.tech/documentation/quick_start/. Qdrant requires just a single container, but an example of the docker-compose.yaml file is available at ./qdrant/docker-compose.yaml in this repo.
You can start Qdrant instance locally by navigating to this directory and running docker-compose up -d
You might need to increase the memory limit for Docker to 8GB or more. Or Qdrant might fail to execute with an error message like 7 Killed.
Qdrant stores data in collections where each object is described by at least one vector and may contain an additional metadata called payload. Our collection will be called Articles and each object will be described by both title and content vectors.
We'll be using an official qdrant-client package that has all the utility methods already built-in.
from qdrant_client.http import models as rest
# Get the vector size from the first row to set up the collectionvector_size =len(article_df['content_vector'][0])# Set up the collection with the vector configuration. You need to declare the vector size and distance metric for the collection. Distance metric enables vector database to index and search vectors efficiently.qdrant.recreate_collection(collection_name='Articles',vectors_config={'title': rest.VectorParams(distance=rest.Distance.COSINE,size=vector_size, ),'content': rest.VectorParams(distance=rest.Distance.COSINE,size=vector_size, ), })
In addition to the vector configuration defined under vector, we can also define the payload configuration. Payload is an optional field that allows you to store additional metadata alongside the vectors. In our case, we'll store the id, title, and url of the articles. As we return the title of nearest articles in the search results from payload, we can also provide the user with the URL to the article (which is part of the meta-data).
from qdrant_client.models import PointStruct # Import the PointStruct to store the vector and payloadfrom tqdm import tqdm # Library to show the progress bar # Populate collection with vectors using tqdm to show progressfor k, v in tqdm(article_df.iterrows(), desc="Upserting articles", total=len(article_df)):try: qdrant.upsert(collection_name='Articles',points=[ PointStruct(id=k,vector={'title': v['title_vector'], 'content': v['content_vector']},payload={'id': v['id'],'title': v['title'],'url': v['url'] } ) ] )exceptExceptionas e:print(f"Failed to upsert row {k}: {v}")print(f"Exception: {e}")
Once the data is put into Qdrant we will start querying the collection for the closest vectors. We may provide an additional parameter vector_name to switch from title to content based search. Ensure you use the text-embedding-ada-002 model as the original embeddings in file were created with this model.
defquery_qdrant(query, collection_name, vector_name='title', top_k=20):# Creates embedding vector from user query embedded_query = openai.embeddings.create(input=query,model=EMBEDDING_MODEL, ).data[0].embedding # We take the first embedding from the list query_results = qdrant.search(collection_name=collection_name,query_vector=( vector_name, embedded_query ),limit=top_k, query_filter=None )return query_results
query_results = query_qdrant('modern art in Europe', 'Articles', 'title')for i, article inenumerate(query_results):print(f'{i +1}. {article.payload["title"]}, URL: {article.payload["url"]} (Score: {round(article.score, 3)})')