In this tutorial, we'll demonstrate how to use Upstash Vector for semantic search. We will upload several documents and perform a search query to find the most semantically similar documents using embeddings generated automatically by Upstash.
Installation and Setup#
First, we need to create a Vector Index in the Upstash Console. Once we have our index, we will copy the UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN and paste them to our .env file. To learn more about index creation, you can check out this page.
Add the following content to your .env file (replace with your actual URL and token):
We now need to install the upstash-vector library via PyPI. Additionally, we will install python-dotenv to load environment variables from the .env file.
Code#
Create a Python script (e.g., main.py) and add the following code to perform semantic search using Upstash Vector:
Running the Code#
To run the code, execute the following command in your terminal:
Here is an example output for the search query "What is Python?":
Code Breakdown#
-
Environment Setup: We use
python-dotenvto load our environment variables and use theIndex.from_env()method to initialize the index client. -
Document Insertion: We define a list of documents, each with a unique ID and text content. The
upsert()function inserts these documents into our index. These documents are automatically converted into embeddings. To learn more about Upstash Embedding Models, you can check out this page. -
Index Reset: Before inserting documents, the
reset()function clears any existing data in the index. -
Search Query: After inserting the documents, we perform semantic search. The
query()function returns thetop_kmost similar documents to the query along with their metadata ifinclude_metadatais set toTrue.