Use this file to discover all available pages before exploring further.
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.
To use Weaviate vector stores, you’ll need to set up a Weaviate instance and install the @langchain/weaviate integration package. You should also install the weaviate-client package to initialize a client to connect to your instance with, and the uuid package if you want to assign indexed documents ids.This guide will also use OpenAI embeddings, which require you to install the @langchain/openai integration package. You can also use other supported embeddings models if you wish.
Once you’ve set up your instance, set the following environment variables:
// If running locally, include port e.g. "localhost:8080"process.env.WEAVIATE_URL = "YOUR_WEAVIATE_URL";// Optional, for cloud deploymentsprocess.env.WEAVIATE_API_KEY = "YOUR_API_KEY";
If you are using OpenAI embeddings for this guide, you’ll need to set your OpenAI key as well:
To create a collection, specify at least the collection name. If you don’t specify any properties, auto-schema creates them.
const vectorStore = new WeaviateStore(embeddings, { client: weaviateClient, indexName: "Langchainjs_test",});
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.
const vectorStore = new WeaviateStore(embeddings, { client: weaviateClient, schema: { name: "Langchainjs_test", description: "A simple dataset", properties: [ { name: "title", dataType: dataType.TEXT, }, { name: "foo", dataType: dataType.TEXT, }, ], vectorizers: [ vectorizer.text2VecOpenAI({ name: "title", sourceProperties: ["title"], // (Optional) Set the source property(ies) // vectorIndexConfig: configure.vectorIndex.hnsw() // (Optional) Set the vector index configuration }), ], generative: weaviate.configure.generative.openAI(), reranker: weaviate.configure.reranker.cohere(), },});
Note: If you want to associate ids with your indexed documents, they must be UUIDs.
import type { Document } from "@langchain/core/documents";import { v4 as uuidv4 } from "uuid";const document1: Document = { pageContent: "The powerhouse of the cell is the mitochondria", metadata: { source: "https://example.com" }};const document2: Document = { pageContent: "Buildings are made out of brick", metadata: { source: "https://example.com" }};const document3: Document = { pageContent: "Mitochondria are made out of lipids", metadata: { source: "https://example.com" }};const document4: Document = { pageContent: "The 2024 Olympics are in Paris", metadata: { source: "https://example.com" }}const documents = [document1, document2, document3, document4];const uuids = [uuidv4(), uuidv4(), uuidv4(), uuidv4()];await vectorStore.addDocuments(documents, { ids: uuids });
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
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.
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]* Mitochondria are made out of lipids [{"source":"https://example.com"}]
If you want to execute a similarity search and receive the corresponding scores you can run:
* [SIM=0.835] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]* [SIM=0.852] Mitochondria are made out of lipids [{"source":"https://example.com"}]
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) 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
[ Document { pageContent: 'The powerhouse of the cell is the mitochondria', metadata: { source: 'https://example.com' }, id: undefined }, Document { pageContent: 'Mitochondria are made out of lipids', metadata: { source: 'https://example.com' }, id: undefined }]