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]: Parent doc OOM fix (extends #5989) #6012

Merged
merged 3 commits into from
Jul 9, 2024
Merged
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
28 changes: 16 additions & 12 deletions langchain/src/retrievers/parent_document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ 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 +196,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 +210,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);
}
}
}
Loading