"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.\n",
"\n",
"### What is a Vector Database\n",
"\n",
"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.\n",
"\n",
"### Why use a Vector Database\n",
"\n",
"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.\n",
"\n",
"\n",
"### Demo Flow\n",
"The demo flow is:\n",
"- **Setup**: Import packages and set any required variables\n",
"- **Load data**: Load a dataset and embed it using OpenAI embeddings\n",
@ -21,7 +31,7 @@
" - *Index Data*: We'll create an index with __title__ search vectors in it\n",
" - *Search Data*: We'll run a few searches to confirm it works\n",
"\n",
"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"
"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."
]
},
{
@ -31,12 +41,12 @@
"source": [
"## Setup\n",
"\n",
"Here we import the required libraries and set the embedding model that we'd like to use"
"Import the required libraries and set the embedding model that we'd like to use."
]
},
{
"cell_type": "code",
"execution_count": 98,
"execution_count": 1,
"id": "5be94df6",
"metadata": {},
"outputs": [],
@ -60,7 +70,7 @@
"import weaviate\n",
"\n",
"# I've set this to our new embeddings model, this can be changed to the embedding model of your choice\n",
"MODEL = \"text-embedding-ada-002\"\n",
"EMBEDDING_MODEL = \"text-embedding-ada-002\"\n",
"\n",
"# Ignore unclosed SSL socket warnings - optional in case you get these errors\n",
"import warnings\n",
@ -76,14 +86,12 @@
"source": [
"## Load data\n",
"\n",
"In this section we'll source the data for this task, embed it and format it for insertion into a vector database\n",
"\n",
"*Thanks to Ryan Greene for the template used for the batch ingestion"
"In this section we'll source the data for this task, embed it and format it for insertion into a vector database"
]
},
{
"cell_type": "code",
"execution_count": 116,
"execution_count": 6,
"id": "bd99e08e",
"metadata": {},
"outputs": [],
@ -92,7 +100,7 @@
"def get_embeddings(input: List):\n",
" response = openai.Embedding.create(\n",
" input=input,\n",
" model=MODEL,\n",
" model=EMBEDDING_MODEL,\n",
" )[\"data\"]\n",
" return [data[\"embedding\"] for data in response]\n",
"\n",
@ -102,7 +110,6 @@
" yield iterable[ndx : min(ndx + n, l)]\n",
"\n",
"# Function for batching and parallel processing the embeddings\n",
"First we need to create an index, which we'll call `wikipedia-articles`. Once we have an index, we can create multiple namespaces, which can make a single index searchable for various use cases. For more details, consult [this article](https://docs.pinecone.io/docs/namespaces#:~:text=Pinecone%20allows%20you%20to%20partition,different%20subsets%20of%20your%20index.)."
"First we need to create an index, which we'll call `wikipedia-articles`. Once we have an index, we can create multiple namespaces, which can make a single index searchable for various use cases. For more details, consult [Pinecone documentation](https://docs.pinecone.io/docs/namespaces#:~:text=Pinecone%20allows%20you%20to%20partition,different%20subsets%20of%20your%20index.).\n",
"\n",
"If you want to batch insert to your index in parallel to increase insertion speed then there is a great guide in the Pinecone documentation on [batch inserts in parallel](https://docs.pinecone.io/docs/insert-data#sending-upserts-in-parallel)."
]
},
{
"cell_type": "code",
"execution_count": 94,
"execution_count": 19,
"id": "0a71c575",
"metadata": {},
"outputs": [],
@ -429,7 +441,7 @@
},
{
"cell_type": "code",
"execution_count": 99,
"execution_count": 20,
"id": "7ea9ad46",
"metadata": {},
"outputs": [
@ -439,7 +451,7 @@
"['wikipedia-articles']"
]
},
"execution_count": 99,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@ -462,7 +474,7 @@
},
{
"cell_type": "code",
"execution_count": 100,
"execution_count": 22,
"id": "5daeba00",
"metadata": {},
"outputs": [
@ -476,7 +488,6 @@
],
"source": [
"# Upsert content vectors in content namespace\n",
"# NOTE: Using a thread pool here can accelerate this upsert operation\n",
"print(\"Uploading vectors to content namespace..\")\n",
"# NOTE: Using a thread pool here can accelerate this upsert operation\n",
"print(\"Uploading vectors to article schema..\")\n",
"\n",
"# Store a list of UUIDs in case we want to use to refer back to the initial dataframe\n",
"uuids = []\n",
"for articles in data_objects:\n",
" uuid = client.data_object.create(\n",
"\n",
"# Reuse our batcher from the Pinecone ingestion\n",
"for batch_df in df_batcher(article_df):\n",
" for k,v in batch_df.iterrows():\n",
" #print(articles)\n",
" uuid = client.data_object.create(\n",
" {\n",
" \"title\": articles[0],\n",
" \"content\": articles[1]\n",
" \"title\": v['title'],\n",
" \"content\": v['text']\n",
" },\n",
" \"Article\",\n",
" vector=articles[2]\n",
" vector=v['title_vector']\n",
" )\n",
" uuids.append(uuid)"
" uuids.append(uuid)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": 47,
"id": "3658693c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cave Story\n",
"is a freeware video game released in 2004 for PC. It was thought of and created over five years by Daisuke Amaya, known by his pseudonym, or art name, Pixel. The game is an action-adventure game, and is similar to the Castlevania and Metroid games. It was first made in Japanese, and was translated to English by the fan translating group, Aeon Genesis.\n",
"\n",
"References \n",
"\n",
"Notes\n",
"\n",
"2004 video games\n",
"Amiga games\n",
"Dreamcast games\n",
"Freeware games\n",
"Indie video games\n",
"Nintendo 3DS games\n",
"Nintendo Switch games\n",
"MacOS games\n",
"Platform games\n",
"Sega Genesis games\n",
"Video games developed in Japan\n",
"Wii games\n",
"Windows games\n"
]
},
{
"data": {
"text/plain": [
"{'content': 'Eddie Cantor (January 31, 1892 - October 10, 1964) was an American comedian, singer, actor, songwriter. Familiar to Broadway, radio and early television audiences, this \"Apostle of Pep\" was regarded almost as a family member by millions because his top-rated radio shows revealed intimate stories and amusing anecdotes about his wife Ida and five daughters. His eye-rolling song-and-dance routines eventually led to his nickname, Banjo Eyes, and in 1933, the artist Frederick J. Garner caricatured Cantor with large round and white eyes resembling the drum-like pot of a banjo. Cantor\\'s eyes became his trademark, often exaggerated in illustrations, and leading to his appearance on Broadway in the musical Banjo Eyes (1941). He was the original singer of 1929 hit song \"Makin\\' Whoopie\".\\n\\nReferences\\n\\nPresidents of the Screen Actors Guild\\nAmerican stage actors\\nComedians from New York City\\nAmerican Jews\\nActors from New York City\\nSingers from New York City\\nAmerican television actors\\nAmerican radio actors\\n1892 births\\n1964 deaths',\n",
"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"
"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."