Skip to content

Commit

Permalink
Support storing and retrieving Document id field in MemoryVectorStore
Browse files Browse the repository at this point in the history
  • Loading branch information
dschenkelman committed Aug 19, 2024
1 parent fc01973 commit c5f3974
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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 @@ -165,7 +166,7 @@ export class MemoryVectorStore extends VectorStore {
* @param documents Array of `Document` instances to be added to the store.
* @returns Promise that resolves when all documents have been added.
*/
async addDocuments(documents: Document[]): Promise<void> {
async addDocuments(documents: Document[]): Promise<vo > {
const texts = documents.map(({ pageContent }) => pageContent);
return this.addVectors(
await this.embeddings.embedDocuments(texts),
Expand All @@ -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 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

0 comments on commit c5f3974

Please sign in to comment.