Skip to main content
Weaviate is an open source vector database that stores both objects and vectors, allowing for combining vector search with structured filtering. LangChain connects to Weaviate via the weaviate-client package, the official Typescript client for Weaviate. This guide provides a quick overview for getting started with Weaviate vector stores. For detailed documentation of all WeaviateStore features and configurations head to the API reference.

Overview

Integration details

Setup

To use Weaviate vector stores, set up a Weaviate instance and install @langchain/weaviate, @langchain/core, and weaviate-client to connect to your deployment. This guide uses OpenAI embeddings as an example. You can use other supported embeddings models instead.
Run Weaviate locally or on a server. See the Weaviate documentation for more information.

Credentials

Set the following environment variables:
If you are using OpenAI embeddings for this guide, set your OpenAI key as well:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Instantiation

Connect a weaviate client

In most cases, you should use one of the connection helper functions to connect to your Weaviate instance:
  • connectToWeaviateCloud
  • connectToLocal
  • connectToCustom

Initiate the vectorStore

To create a collection, specify at least the collection name. If you don’t specify any properties, auto-schema creates them.
To use Weaviate’s named vectors, vectorizers, reranker, generative-models etc., use the schema property when enabling the vector store. The collection name and other properties in schema will take precedence when creating the vector store.

Manage vector store

Add items to vector store

Note: If you want to associate ids with your indexed documents, they must be UUIDs.

Delete items from vector store

You can delete by ID or by passing a filter parameter:

Query vector store

Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent. In weaviate’s v3, the client interacts with collections as the primary way to work with objects in the database. The collection object can be reused throughout the codebase

Query directly

Performing a simple similarity search can be done as follows. The Filter helper class makes it easier to use filters with conditions. The v3 client streamlines how you use Filter so your code is cleaner and more concise. See this page for more on Weaviate filter syntax.
If you want to execute a similarity search and receive the corresponding scores you can run:
In Weaviate, Hybrid search combines the results of a vector search and a keyword (BM25F) search by fusing the two result sets. To change the relative weights of the keyword and vector components, set the alpha value in your query. Check docs for the full list of hybrid search options.

Retrieval augmented generation (RAG)

Retrieval Augmented Generation (RAG) combines information retrieval with generative AI models. In Weaviate, a RAG query consists of two parts: a search query, and a prompt for the model. Weaviate first performs the search, then passes both the search results and your prompt to a generative AI model before returning the generated response.
  • @param query The query to search for.
  • @param options available options for performing the hybrid search
  • @param generate available options for the generation. Check docs for complete list

Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains.

Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

API reference

For detailed documentation of all WeaviateStore features and configurations head to the API reference.