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

langchain[minor]: Support storing and retrieving Document id field in MemoryVectorStore #6572

Merged
merged 2 commits into from
Aug 22, 2024
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
6 changes: 6 additions & 0 deletions langchain/src/vectorstores/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface MemoryVector {
embedding: number[];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
metadata: Record<string, any>;
id?: string;
}

/**
Expand Down Expand Up @@ -186,6 +187,7 @@ export class MemoryVectorStore extends VectorStore {
content: documents[idx].pageContent,
embedding,
metadata: documents[idx].metadata,
id: documents[idx].id,
}));

this.memoryVectors = this.memoryVectors.concat(memoryVectors);
Expand All @@ -204,6 +206,7 @@ export class MemoryVectorStore extends VectorStore {
const doc = new Document({
metadata: memoryVector.metadata,
pageContent: memoryVector.content,
id: memoryVector.id,
});
return filter(doc);
};
Expand All @@ -215,6 +218,7 @@ export class MemoryVectorStore extends VectorStore {
metadata: vector.metadata,
content: vector.content,
embedding: vector.embedding,
id: vector.id,
}))
.sort((a, b) => (a.similarity > b.similarity ? -1 : 0))
.slice(0, k);
Expand All @@ -240,6 +244,7 @@ export class MemoryVectorStore extends VectorStore {
new Document({
metadata: search.metadata,
pageContent: search.content,
id: search.id,
}),
search.similarity,
]);
Expand Down Expand Up @@ -273,6 +278,7 @@ export class MemoryVectorStore extends VectorStore {
new Document({
metadata: searches[idx].metadata,
pageContent: searches[idx].content,
id: searches[idx].id,
})
);
}
Expand Down
38 changes: 38 additions & 0 deletions langchain/src/vectorstores/tests/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,44 @@ test("MemoryVectorStore with external ids", async () => {
]);
});

test("MemoryVectorStore stores and retrieves document IDs", async () => {
const embeddings = new SyntheticEmbeddings({
vectorSize: 1536,
});
const store = new MemoryVectorStore(embeddings);

const filterFunc = (doc: DocumentInterface): boolean => {
const { metadata } = doc;
if (metadata.namespace <= 2) {
return true;
}
return false;
};

const retriever = store.asRetriever({
k: 2,
filter: filterFunc,
});

expect(retriever).toBeDefined();

await retriever.addDocuments([
{ pageContent: "hello", metadata: { namespace: 1 }, id: "1" },
{ pageContent: "hello", metadata: { namespace: 2 }, id: "2" },
{ pageContent: "hello", metadata: { namespace: 3 }, id: "3" },
{ pageContent: "hello", metadata: { namespace: 4 }, id: "4" },
]);

const results = await retriever.getRelevantDocuments("hello");

expect(results).toHaveLength(2);

expect(results).toEqual([
new Document({ metadata: { namespace: 1 }, pageContent: "hello", id: "1" }),
new Document({ metadata: { namespace: 2 }, pageContent: "hello", id: "2" }),
]);
});

test("MemoryVectorStore as retriever can filter metadata", async () => {
const embeddings = new SyntheticEmbeddings({
vectorSize: 1536,
Expand Down
Loading