Skip to main content

RedisVectorStore

Compatibility

Only available on Node.js.

Redis is a fast open source, in-memory data store. As part of the Redis Stack, RediSearch is the module that enables vector similarity semantic search, as well as many other types of searching.

This guide provides a quick overview for getting started with Redis vector stores. For detailed documentation of all RedisVectorStore features and configurations head to the API reference.

Overview

Integration details

ClassPackagePY supportPackage latest
RedisVectorStore@langchain/redisNPM - Version

Setup

To use Redis vector stores, you’ll need to set up a Redis instance and install the @langchain/redis integration package. You can also install the node-redis package to initialize the vector store with a specific client instance.

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.

yarn add @langchain/redis @langchain/core redis @langchain/openai

You can set up a Redis instance locally with Docker by following these instructions.

Credentials

Once you’ve set up an instance, set the REDIS_URL environment variable:

process.env.REDIS_URL = "your-redis-url";

If you are using OpenAI embeddings for this guide, you’ll need to set your OpenAI key as well:

process.env.OPENAI_API_KEY = "YOUR_API_KEY";

If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

// process.env.LANGCHAIN_TRACING_V2="true"
// process.env.LANGCHAIN_API_KEY="your-api-key"

Instantiation

import { RedisVectorStore } from "@langchain/redis";
import { OpenAIEmbeddings } from "@langchain/openai";

import { createClient } from "redis";

const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});

const client = createClient({
url: process.env.REDIS_URL ?? "redis://localhost:6379",
});
await client.connect();

const vectorStore = new RedisVectorStore(embeddings, {
redisClient: client,
indexName: "langchainjs-testing",
});

Manage vector store

Add items to vector store

import type { Document } from "@langchain/core/documents";

const document1: Document = {
pageContent: "The powerhouse of the cell is the mitochondria",
metadata: { type: "example" },
};

const document2: Document = {
pageContent: "Buildings are made out of brick",
metadata: { type: "example" },
};

const document3: Document = {
pageContent: "Mitochondria are made out of lipids",
metadata: { type: "example" },
};

const document4: Document = {
pageContent: "The 2024 Olympics are in Paris",
metadata: { type: "example" },
};

const documents = [document1, document2, document3, document4];

await vectorStore.addDocuments(documents);

Top-level document ids and deletion are currently not supported.

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.

Query directly

Performing a simple similarity search can be done as follows:

const similaritySearchResults = await vectorStore.similaritySearch(
"biology",
2
);

for (const doc of similaritySearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}

Filtering will currently look for any metadata key containing the provided string.

If you want to execute a similarity search and receive the corresponding scores you can run:

const similaritySearchWithScoreResults =
await vectorStore.similaritySearchWithScore("biology", 2);

for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(
`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(
doc.metadata
)}]`
);
}
* [SIM=0.835] The powerhouse of the cell is the mitochondria [{"type":"example"}]
* [SIM=0.852] Mitochondria are made out of lipids [{"type":"example"}]

Query by turning into retriever

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

const retriever = vectorStore.asRetriever({
k: 2,
});
await retriever.invoke("biology");
[
Document {
pageContent: 'The powerhouse of the cell is the mitochondria',
metadata: { type: 'example' },
id: undefined
},
Document {
pageContent: 'Mitochondria are made out of lipids',
metadata: { type: 'example' },
id: undefined
}
]

Usage for retrieval-augmented generation

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

Deleting an index

You can delete an entire index with the following command:

await vectorStore.delete({ deleteAll: true });

Closing connections

Make sure you close the client connection when you are finished to avoid excessive resource consumption:

await client.disconnect();

API reference

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


Was this page helpful?


You can also leave detailed feedback on GitHub.