Skip to content

Commit

Permalink
Fix: Allow Redis addDocuments without overriding existing keys (#1555)
Browse files Browse the repository at this point in the history
* Fix: Allow Redis addDocuments without overriding existing keys

* Update redis.ts

* Revert "Update redis.ts"

This reverts commit 1d2c030.

* Revert "Revert "Update redis.ts""

This reverts commit 2ad93cf.

* Update fix

* Fix test

---------

Co-authored-by: jacoblee93 <jacoblee93@gmail.com>
  • Loading branch information
Haim763 and jacoblee93 authored Aug 29, 2023
1 parent a5cc301 commit 13c9e7c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
13 changes: 12 additions & 1 deletion langchain/src/vectorstores/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,15 @@ export class RedisVectorStore extends VectorStore {
// check if the index exists and create it if it doesn't
await this.createIndex(vectors[0].length);

const info = await this.redisClient.ft.info(this.indexName);
const lastKeyCount = parseInt(info.numDocs, 10) || 0;
const multi = this.redisClient.multi();

vectors.map(async (vector, idx) => {
const key = keys && keys.length ? keys[idx] : `${this.keyPrefix}${idx}`;
const key =
keys && keys.length
? keys[idx]
: `${this.keyPrefix}${idx + lastKeyCount}`;
const metadata =
documents[idx] && documents[idx].metadata
? documents[idx].metadata
Expand Down Expand Up @@ -284,6 +289,12 @@ export class RedisVectorStore extends VectorStore {
try {
await this.redisClient.ft.info(this.indexName);
} catch (err) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((err as any)?.message.includes("unknown command")) {
throw new Error(
"Failed to run FT.INFO command. Please ensure that you are running a RediSearch-capable Redis instance: https://js.langchain.com/docs/modules/data_connection/vectorstores/integrations/redis#setup"
);
}
// index doesn't exist
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions langchain/src/vectorstores/tests/redis.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import { OpenAIEmbeddings } from "../../embeddings/openai.js";
import { RedisVectorStore } from "../redis.js";
import { Document } from "../../document.js";

describe.skip("RedisVectorStore", () => {
describe("RedisVectorStore", () => {
let vectorStore: RedisVectorStore;

beforeEach(async () => {
expect(process.env.REDIS_URL).toBeDefined();

const client = createClient({ url: process.env.REDIS_URL });
await client.connect();

Expand Down
4 changes: 3 additions & 1 deletion langchain/src/vectorstores/tests/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const createRedisClientMockup = () => {

return {
ft: {
info: jest.fn(),
info: jest.fn<any>().mockResolvedValue({
numDocs: 0,
}),
create: jest.fn(),
search: jest.fn<any>().mockResolvedValue({
total: 0,
Expand Down
2 changes: 1 addition & 1 deletion test-int-deps-docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: '3.8'
services:
redis:
image: redis:6.2-alpine
image: redis/redis-stack-server:latest
restart: always
ports:
- '6379:6379'
Expand Down

1 comment on commit 13c9e7c

@vercel
Copy link

@vercel vercel bot commented on 13c9e7c Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.