Skip to main content
In this tutorial, we’ll demonstrate how to use Upstash Vector with LangChain to perform a similarity search. We will upload a document about global warming 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 following libraries via PyPI:

Code

We will load our environment variables and initialize the index from the environment variables (URL and token).
Next, we will upload the global_warming.txt document to the Upstash Vector index.
We will now insert the documents into the Upstash Vector index.
Finally, we will perform a semantic search.
Here’s the output:

Notes

  • You can also query with score using similarity_search_with_score method.
  • Namespaces can be used to separate different types of documents. You can specify a namespace when creating the UpstashVectorStore instance.
  • You can use OpenAI’s embeddings by setting embedding=OpenAIEmbeddings() in the UpstashVectorStore instance.
To learn more about LangChain and its integration with Upstash Vector, you can visit the LangChain documentation.