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[patch]: Fix memory issue for large number of child chunks on large documents #5989

Closed
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
25 changes: 13 additions & 12 deletions langchain/src/retrievers/parent_document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ export class ParentDocumentRetriever extends MultiVectorRetriever {
return parentDocs.slice(0, this.parentK);
}

async _storeDocuments(parentDoc: Document, childDocs: Document[], addToDocstore: boolean) {
if (this.childDocumentRetriever) {
await this.childDocumentRetriever.addDocuments(childDocs);
}
else {
await this.vectorstore.addDocuments(childDocs);
}
if (addToDocstore) {
await this.docstore.mset(Object.entries(parentDoc));
}
}

/**
* Adds documents to the docstore and vectorstores.
* If a retriever is provided, it will be used to add documents instead of the vectorstore.
Expand Down Expand Up @@ -181,8 +193,6 @@ export class ParentDocumentRetriever extends MultiVectorRetriever {
`Got uneven list of documents and ids.\nIf "ids" is provided, should be same length as "documents".`
);
}
const embeddedDocs: Document[] = [];
const fullDocs: Record<string, Document> = {};
for (let i = 0; i < parentDocs.length; i += 1) {
const parentDoc = parentDocs[i];
const parentDocId = parentDocIds[i];
Expand All @@ -197,16 +207,7 @@ export class ParentDocumentRetriever extends MultiVectorRetriever {
metadata: { ...subDoc.metadata, [this.idKey]: parentDocId },
})
);
embeddedDocs.push(...taggedSubDocs);
fullDocs[parentDocId] = parentDoc;
}
if (this.childDocumentRetriever) {
await this.childDocumentRetriever.addDocuments(embeddedDocs);
} else {
await this.vectorstore.addDocuments(embeddedDocs);
}
if (addToDocstore) {
await this.docstore.mset(Object.entries(fullDocs));
await this._storeDocuments(parentDoc, taggedSubDocs, addToDocstore);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Flagging that this does make it a bit more likely that we'll get into half-finished states

}
}
}
Loading