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

(EAI-152) check for change to chunkAlgoHash when updating embeddings #580

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
42 changes: 27 additions & 15 deletions packages/mongodb-rag-core/src/contentStore/updateEmbeddedContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ export interface EmbedConcurrencyOptions {
createChunks?: number;
}

const chunkAlgoHashes = new Map<string, string>();
yakubova92 marked this conversation as resolved.
Show resolved Hide resolved

const getHashForFunc = (f: ChunkFunc, o?: Partial<ChunkOptions>): string => {
const data = JSON.stringify(o ?? {}) + f.toString();
const existingHash = chunkAlgoHashes.get(data);
if (existingHash) {
return existingHash;
}
const hash = createHash("sha256");
hash.update(data);
const digest = hash.digest("hex");
chunkAlgoHashes.set(data, digest);
return digest;
};

/**
(Re-)embeddedContent the pages in the page store that have changed since the given date
and stores the embeddedContent in the embeddedContent store.
Expand Down Expand Up @@ -48,6 +63,18 @@ export const updateEmbeddedContent = async ({
sourceNames ? ` in sources: ${sourceNames.join(", ")}` : ""
}`
);
// find pages with changed chunkingHashes
const allPages = await pageStore.loadPages({sources: sourceNames});
const pagesWithChangedChunking = allPages.filter(async (page) => {
const existingContent = await embeddedContentStore.loadEmbeddedContent({
page,
});
const chunkAlgoHash = getHashForFunc(chunkPage, chunkOptions);
return existingContent[0].chunkAlgoHash !== chunkAlgoHash
});

changedPages.push(...pagesWithChangedChunking);
yakubova92 marked this conversation as resolved.
Show resolved Hide resolved

await PromisePool.withConcurrency(concurrencyOptions?.processPages ?? 1)
.for(changedPages)
.process(async (page, index, pool) => {
Expand All @@ -73,21 +100,6 @@ export const updateEmbeddedContent = async ({
});
};

const chunkAlgoHashes = new Map<string, string>();

const getHashForFunc = (f: ChunkFunc, o?: Partial<ChunkOptions>): string => {
const data = JSON.stringify(o ?? {}) + f.toString();
const existingHash = chunkAlgoHashes.get(data);
if (existingHash) {
return existingHash;
}
const hash = createHash("sha256");
hash.update(data);
const digest = hash.digest("hex");
chunkAlgoHashes.set(data, digest);
return digest;
};

export const updateEmbeddedContentForPage = async ({
page,
store,
Expand Down