Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(redis): Add TTL support for redis vector store #7695

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions libs/langchain-redis/src/tests/vectorstores.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RedisVectorStore } from "../vectorstores.js";

const createRedisClientMockup = () => {
const hSetMock = jest.fn();
const expireMock = jest.fn();

return {
ft: {
Expand All @@ -20,9 +21,11 @@ const createRedisClientMockup = () => {
dropIndex: jest.fn(),
},
hSet: hSetMock,
expire: expireMock,
multi: jest.fn<any>().mockImplementation(() => ({
exec: jest.fn(),
hSet: hSetMock,
expire: expireMock,
})),
};
};
Expand Down Expand Up @@ -83,6 +86,25 @@ test("RedisVectorStore with generated keys", async () => {
expect(results).toHaveLength(0);
});

test("RedisVectorStore with TTL", async () => {
const client = createRedisClientMockup();
const embeddings = new FakeEmbeddings();
const ttl = 10;
const store = new RedisVectorStore(embeddings, {
redisClient: client as any,
indexName: "documents",
ttl,
});

expect(store).toBeDefined();

await store.addDocuments([{ pageContent: "hello", metadata: { a: 1 } }]);

expect(client.hSet).toHaveBeenCalledTimes(1);
expect(client.expire).toHaveBeenCalledTimes(1);
expect(client.expire).toHaveBeenCalledWith("doc:documents:0", ttl);
});

test("RedisVectorStore with filters", async () => {
const client = createRedisClientMockup();
const embeddings = new FakeEmbeddings();
Expand Down
10 changes: 9 additions & 1 deletion libs/langchain-redis/src/vectorstores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type RedisVectorStoreIndexOptions = Omit<
/**
* Interface for the configuration of the RedisVectorStore. It includes
* the Redis client, index name, index options, key prefix, content key,
* metadata key, vector key, and filter.
* metadata key, vector key, filter and ttl.
*/
export interface RedisVectorStoreConfig {
redisClient:
Expand All @@ -76,6 +76,7 @@ export interface RedisVectorStoreConfig {
metadataKey?: string;
vectorKey?: string;
filter?: RedisVectorStoreFilterType;
ttl?: number; // ttl in second
}

/**
Expand Down Expand Up @@ -123,6 +124,8 @@ export class RedisVectorStore extends VectorStore {

filter?: RedisVectorStoreFilterType;

ttl?: number;

_vectorstoreType(): string {
return "redis";
}
Expand All @@ -144,6 +147,7 @@ export class RedisVectorStore extends VectorStore {
this.metadataKey = _dbConfig.metadataKey ?? "metadata";
this.vectorKey = _dbConfig.vectorKey ?? "content_vector";
this.filter = _dbConfig.filter;
this.ttl = _dbConfig.ttl;
this.createIndexOptions = {
ON: "HASH",
PREFIX: this.keyPrefix,
Expand Down Expand Up @@ -208,6 +212,10 @@ export class RedisVectorStore extends VectorStore {
[this.metadataKey]: this.escapeSpecialChars(JSON.stringify(metadata)),
});

if (this.ttl) {
multi.expire(key, this.ttl);
}

// write batch
if (idx % batchSize === 0) {
await multi.exec();
Expand Down
Loading