Skip to main content
This notebook covers how to get started with the Weaviate vector store in LangChain, using the langchain-weaviate package.
Weaviate is an open-source vector database. It allows you to store data objects and vector embeddings from your favorite ML-models, and scale seamlessly into billions of data objects.
To use this integration, you need to have a running Weaviate database instance.

Minimum versions

This module requires Weaviate 1.23.7 or higher. However, we recommend you use the latest version of Weaviate.

Connecting to weaviate

In this notebook, we assume that you have a local instance of Weaviate running on http://localhost:8080 and port 50051 open for gRPC traffic. We will connect to Weaviate with:

Other deployment options

Weaviate can be deployed in many different ways such as using Weaviate Cloud Services (WCS), Docker or Kubernetes. If your Weaviate instance is deployed in another way, read more here about different ways to connect to Weaviate. You can use different helper functions or create a custom instance.
Note that you require a v4 client API, which will create a weaviate.WeaviateClient object.

Authentication

Some Weaviate instances, such as those running on WCS, have authentication enabled, such as API key and/or username+password authentication. Read the client authentication guide for more information, as well as the in-depth authentication configuration page.

Connect to an existing collection (reuse an index)

If you already created a collection in your local Weaviate instance, you can connect to it directly:

Installation

Environment setup

This notebook uses the OpenAI API through OpenAIEmbeddings. We suggest obtaining an OpenAI API key and export it as an environment variable with the name OPENAI_API_KEY. Once this is done, your OpenAI API key will be read automatically. If you are new to environment variables, read more about them in the Python os.environ documentation or in this guide.

Usage

Find objects by similarity

Here is an example of how to find objects by similarity to a query, from data import to querying the Weaviate instance.

Step 1: Data import

First, we will create data to add to Weaviate by loading and chunking the contents of a long text file.
The langchain-community package is no longer maintained. Examples that import from langchain_community may be outdated or broken. Use with caution.
Now, we can import the data. To do so, connect to the Weaviate instance and use the resulting weaviate_client object. For example, we can import the documents as shown below:
We can now perform a similarity search. This will return the most similar documents to the query text, based on the embeddings stored in Weaviate and an equivalent embedding generated from the query text.
You can also add filters, which will either include or exclude results based on the filter conditions. (See more filter examples.)
It is also possible to provide k, which is the upper limit of the number of results to return.

Quantify result similarity

You can optionally retrieve a relevance “score”. This is a relative score that indicates how good the particular search results is, amongst the pool of search results. Note that this is relative score, meaning that it should not be used to determine thresholds for relevance. However, it can be used to compare the relevance of different search results within the entire search result set.

Search mechanism

similarity_search uses Weaviate’s hybrid search. A hybrid search combines a vector and a keyword search, with alpha as the weight of the vector search. The similarity_search function allows you to pass additional arguments as kwargs. See this reference doc for the available arguments. You can perform a pure keyword search by adding alpha=0 as shown below:

Persistence

Any data added through langchain-weaviate will persist in Weaviate according to its configuration. WCS instances, for example, are configured to persist data indefinitely, and Docker instances can be set up to persist data in a volume. Read more about Weaviate’s persistence.

Multi-tenancy

Multi-tenancy allows you to have a high number of isolated collections of data, with the same collection configuration, in a single Weaviate instance. This is great for multi-user environments such as building a SaaS app, where each end user will have their own isolated data collection. To use multi-tenancy, the vector store need to be aware of the tenant parameter. When adding any data, provide the tenant parameter as shown below.
And when performing queries, provide the tenant parameter also.

Retriever options

Weaviate can also be used as a retriever

Maximal marginal relevance search (MMR)

In addition to using similaritysearch in the retriever object, you can also use mmr.

Use with LangChain

A known limitation of large language models (LLMs) is that their training data can be outdated, or not include the specific domain knowledge that you require. Take a look at the example below:
Vector stores complement LLMs by providing a way to store and retrieve relevant information. This allow you to combine the strengths of LLMs and vector stores, by using LLM’s reasoning and linguistic capabilities with vector stores’ ability to retrieve relevant information. Two well-known applications for combining LLMs and vector stores are:
  • Question answering
  • Retrieval-augmented generation (RAG)

Question answering with sources

Question answering in langchain can be enhanced by the use of vector stores. Let’s see how this can be done. This section uses the RetrievalQAWithSourcesChain, which does the lookup of the documents from an Index. First, we will chunk the text again and import them into the Weaviate vector store.
Now we can construct the chain, with the retriever specified:
And run the chain, to ask the question:

Retrieval-Augmented generation

Another very popular application of combining LLMs and vector stores is retrieval-augmented generation (RAG). This is a technique that uses a retriever to find relevant information from a vector store, and then uses an LLM to provide an output based on the retrieved data and a prompt. We begin with a similar setup:
We need to construct a template for the RAG model so that the retrieved information will be populated in the template.
And running the cell, we get a very similar output.
Note that since the template is up to you to construct, you can customize it to your needs.

Wrap-up & resources

Weaviate is a scalable, production-ready vector store. This integration allows Weaviate to be used with LangChain to enhance the capabilities of large language models with a robust data store. Its scalability and production-readiness make it a great choice as a vector store for your LangChain applications, and it will reduce your time to production.