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 Weaviate1.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 onhttp://localhost:8080
and port 50051 open for gRPC traffic. So, 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 av4
client API, which will create aweaviate.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 throughOpenAIEmbeddings
. 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 here 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 toWeaviate
by loading and chunking the contents of a long text file.
weaviate_client
object. For example, we can import the documents as shown below:
Step 2: Perform the search
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.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.
So, you can perform a pure keyword search by adding alpha=0
as shown below:
Persistence
Any data added throughlangchain-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 thetenant
parameter.
So when adding any data, provide the tenant
parameter as shown below.
tenant
parameter also.
Retriever options
Weaviate can also be used as a retrieverMaximal marginal relevance search (MMR)
In addition to using similaritysearch in the retriever object, you can also usemmr
.
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:- 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 theRetrievalQAWithSourcesChain
, 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.