-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathupdateEmbeddedContent.test.ts
525 lines (490 loc) · 15.1 KB
/
updateEmbeddedContent.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import {
updateEmbeddedContent,
updateEmbeddedContentForPage,
} from "./updateEmbeddedContent";
import {
makeMongoDbEmbeddedContentStore,
makeMongoDbPageStore,
MongoDbEmbeddedContentStore,
MongoDbPageStore,
persistPages,
updatePages,
} from ".";
import { makeMockPageStore } from "../test/MockPageStore";
import * as chunkPageModule from "../chunk/chunkPage";
import {
EmbeddedContentStore,
EmbeddedContent,
GetSourcesMatchParams,
} from "./EmbeddedContent";
import { Embedder } from "../embed";
import { Page, PersistedPage } from ".";
import { strict as assert } from "assert";
import { MongoMemoryReplSet } from "mongodb-memory-server";
import { DataSource } from "../dataSources";
import { MongoClient } from "mongodb";
export const makeMockEmbeddedContentStore = (): EmbeddedContentStore => {
const content: Map<string /* page url */, EmbeddedContent[]> = new Map();
return {
async deleteEmbeddedContent({ page }) {
if (page) {
content.set(page.url, []);
}
},
async findNearestNeighbors() {
return [];
},
async loadEmbeddedContent({ page }) {
return content.get(page.url) ?? [];
},
async updateEmbeddedContent({ embeddedContent, page }) {
content.set(page.url, [...embeddedContent]);
},
metadata: {
embeddingName: "test",
},
async getDataSources(matchQuery: GetSourcesMatchParams): Promise<string[]> {
return [];
},
};
};
const examplePage: Page = {
title: "Example",
body: "this is a test page. testy test test test test test test test test. more tokens!",
format: "md",
sourceName: "test",
metadata: {
tags: [],
},
url: "https://example.com/test",
};
const embedder = {
async embed() {
return { embedding: [1, 2, 3] };
},
};
describe("updateEmbeddedContent", () => {
it("deletes embedded content for deleted page", async () => {
const pageStore = makeMockPageStore();
await persistPages({
pages: [{ ...examplePage }],
store: pageStore,
sourceName: "test",
});
let pages = await pageStore.loadPages();
expect(pages).toHaveLength(1);
const embeddedContentStore = makeMockEmbeddedContentStore();
const since = new Date("2000-01-01");
await updateEmbeddedContent({
embedder,
embeddedContentStore,
pageStore,
since,
});
let embeddedContent = await embeddedContentStore.loadEmbeddedContent({
page: examplePage,
});
expect(embeddedContent).toHaveLength(1);
await persistPages({
pages: [],
store: pageStore,
sourceName: "test",
});
pages = await pageStore.loadPages();
expect(pages).toHaveLength(1);
expect(pages[0].action).toBe("deleted");
await updateEmbeddedContent({
embedder,
embeddedContentStore,
pageStore,
since,
});
embeddedContent = await embeddedContentStore.loadEmbeddedContent({
page: examplePage,
});
// Embedded content for page was deleted
expect(embeddedContent).toHaveLength(0);
});
it("updates page if chunk algorithm changes", async () => {
const pageStore = makeMockPageStore();
await persistPages({
pages: [{ ...examplePage }],
store: pageStore,
sourceName: "test",
});
const pages = await pageStore.loadPages();
expect(pages).toHaveLength(1);
const embeddedContentStore = makeMockEmbeddedContentStore();
const since = new Date("2000-01-01");
await updateEmbeddedContent({
embedder,
embeddedContentStore,
pageStore,
since,
});
const embeddedContent = await embeddedContentStore.loadEmbeddedContent({
page: examplePage,
});
expect(embeddedContent).toHaveLength(1);
expect(embeddedContent[0].chunkAlgoHash).toBe(
// You might need to update this expectation when the standard chunkPage
// function changes
"49d78a1d6b12ee6f433a2156060ed5ebfdefb8a90301f1c2fb04e4524944c5eb"
);
await updateEmbeddedContent({
embedder,
embeddedContentStore,
pageStore,
since,
chunkOptions: {
// Changing options impacts the chunkAlgoHash
chunkOverlap: 2,
},
});
const embeddedContent2 = await embeddedContentStore.loadEmbeddedContent({
page: examplePage,
});
expect(embeddedContent2).toHaveLength(1);
expect(embeddedContent2[0].chunkAlgoHash).toBe(
// You might need to update this expectation when the standard chunkPage
// function changes
"2cbfe9901657ca15260fe7f58c3132ac1ebd0d610896082ca1aaad0335f2e3f1"
);
});
describe("updateEmbeddedContent handles concurrency", () => {
const startTimes: number[] = [];
const endTimes: number[] = [];
const mockEmbedder: jest.Mocked<Embedder> = {
embed: jest.fn().mockImplementation(async (param) => {
const startTime = Date.now();
startTimes.push(startTime);
await new Promise((resolve) => setTimeout(resolve, 50));
const endTime = Date.now();
endTimes.push(endTime);
return { embedding: [1, 2, 3] };
}),
};
let chunkPageSpy: jest.SpyInstance;
beforeEach(() => {
chunkPageSpy = jest.spyOn(chunkPageModule, "chunkPage");
chunkPageSpy.mockResolvedValue([
{
text: "chunk1",
url: "",
sourceName: "",
tokenCount: 0,
},
{
text: "chunk2",
url: "",
sourceName: "",
tokenCount: 0,
},
{
text: "chunk3",
url: "",
sourceName: "",
tokenCount: 0,
},
]);
});
afterEach(() => {
jest.restoreAllMocks();
});
it("processes chunks concurrently within a page", async () => {
const embeddedContentStore = makeMockEmbeddedContentStore();
const page: PersistedPage = {
...examplePage,
updated: new Date(),
action: "updated",
};
await updateEmbeddedContentForPage({
embedder: mockEmbedder,
store: embeddedContentStore,
page,
concurrencyOptions: { createChunks: 2 },
chunkAlgoHash: "testchunkalgohash",
});
const embeddedContent = await embeddedContentStore.loadEmbeddedContent({
page: examplePage,
});
expect(embeddedContent).toHaveLength(3);
const executionPairs = startTimes.map((startTime, i) => ({
startTime,
endTime: endTimes[i],
}));
// Ensure some overlaps indicating concurrency
expect(
executionPairs.some((pair, i, pairs) =>
pairs.some(
(otherPair, j) =>
i !== j &&
pair.startTime < otherPair.endTime &&
otherPair.startTime < pair.endTime
)
)
).toBe(true);
});
it("processes pages concurrently", async () => {
const pageStore = makeMockPageStore();
const concurrentPages: Page[] = [
{ ...examplePage, url: "https://example.com/test1" },
{ ...examplePage, url: "https://example.com/test2" },
{ ...examplePage, url: "https://example.com/test3" },
];
await persistPages({
pages: concurrentPages,
store: pageStore,
sourceName: "test",
});
const embeddedContentStore = makeMockEmbeddedContentStore();
const since = new Date("2000-01-01");
await updateEmbeddedContent({
embedder: mockEmbedder,
embeddedContentStore,
pageStore,
since,
concurrencyOptions: { processPages: 2, createChunks: 2 },
});
const executionPairs = startTimes.map((startTime, i) => ({
startTime,
endTime: endTimes[i],
}));
// Ensure some overlaps indicating concurrency
expect(
executionPairs.some((pair, i, pairs) =>
pairs.some(
(otherPair, j) =>
i !== j &&
pair.startTime < otherPair.endTime &&
otherPair.startTime < pair.endTime
)
)
).toBe(true);
});
});
});
// These tests use "mongodb-memory-server", not mockEmbeddedContentStore
describe("updateEmbeddedContent", () => {
let mongod: MongoMemoryReplSet | undefined;
let pageStore: MongoDbPageStore;
let embedStore: MongoDbEmbeddedContentStore;
let uri: string;
let databaseName: string;
let mongoClient: MongoClient;
let page1Embedding: EmbeddedContent[], page2Embedding: EmbeddedContent[];
let pages: PersistedPage[] = [];
const embedder = {
async embed() {
return { embedding: [1, 2, 3] };
},
};
const mockDataSources: DataSource[] = [
{
name: "source1",
fetchPages: async () => [
{
url: "test1.com",
format: "html",
sourceName: "source1",
body: "hello source 1",
},
],
},
{
name: "source2",
fetchPages: async () => [
{
url: "test2.com",
format: "html",
sourceName: "source2",
body: "hello source 2",
},
],
},
];
const mockDataSourceNames = mockDataSources.map(
(dataSource) => dataSource.name
);
beforeAll(async () => {
mongod = await MongoMemoryReplSet.create();
uri = mongod.getUri();
mongoClient = new MongoClient(uri);
await mongoClient.connect();
});
beforeEach(async () => {
// setup mongo client, page store, and embedded content store
databaseName = "test-all-command";
embedStore = makeMongoDbEmbeddedContentStore({
connectionUri: uri,
databaseName,
searchIndex: { embeddingName: "test-embedding" },
});
pageStore = makeMongoDbPageStore({
connectionUri: uri,
databaseName,
});
// create pages and verify that they have been created
await updatePages({ sources: mockDataSources, pageStore });
pages = await pageStore.loadPages();
assert(pages.length == 2);
// create embeddings for the pages and verify that they have been created
await updateEmbeddedContent({
since: new Date(0),
embeddedContentStore: embedStore,
pageStore,
sourceNames: mockDataSourceNames,
embedder,
});
page1Embedding = await embedStore.loadEmbeddedContent({
page: pages[0],
});
page2Embedding = await embedStore.loadEmbeddedContent({
page: pages[1],
});
assert(page1Embedding.length);
assert(page2Embedding.length);
});
afterEach(async () => {
await pageStore?.drop();
await embedStore?.drop();
});
afterAll(async () => {
await pageStore?.close();
await embedStore?.close();
await mongoClient?.close();
await mongod?.stop();
});
it("updates embedded content for pages that have been updated after the 'since' date provided", async () => {
// Modify dates of pages and embedded content for testing
const sinceDate = new Date("2024-01-01");
const beforeSinceDate = new Date("2023-01-01");
const afterSinceDate = new Date("2025-01-01");
// set pages[0] to be last updated before sinceDate (should not be modified)
await mongoClient
.db(databaseName)
.collection("pages")
.updateOne({ ...pages[0] }, { $set: { updated: beforeSinceDate } });
await mongoClient
.db(databaseName)
.collection("embedded_content")
.updateOne(
{ sourceName: mockDataSourceNames[0] },
{ $set: { updated: beforeSinceDate } }
);
// set pages[1] to be last updated after sinceDate (should be re-chunked)
await mongoClient
.db(databaseName)
.collection("pages")
.updateOne({ ...pages[1] }, { $set: { updated: afterSinceDate } });
await mongoClient
.db(databaseName)
.collection("embedded_content")
.updateOne(
{ sourceName: mockDataSourceNames[1] },
{ $set: { updated: afterSinceDate } }
);
const originalPage1Embedding = await embedStore.loadEmbeddedContent({
page: pages[0],
});
const originalPage2Embedding = await embedStore.loadEmbeddedContent({
page: pages[1],
});
await updateEmbeddedContent({
since: sinceDate,
embeddedContentStore: embedStore,
pageStore,
sourceNames: mockDataSourceNames,
embedder,
});
const updatedPage1Embedding = await embedStore.loadEmbeddedContent({
page: pages[0],
});
const updatedPage2Embedding = await embedStore.loadEmbeddedContent({
page: pages[1],
});
assert(updatedPage1Embedding.length);
assert(updatedPage2Embedding.length);
expect(updatedPage1Embedding[0].updated.getTime()).toBe(
originalPage1Embedding[0].updated.getTime()
);
expect(updatedPage2Embedding[0].updated.getTime()).not.toBe(
originalPage2Embedding[0].updated.getTime()
);
});
it("updates embedded content when page has not changed, but chunk algo has, ignoring since date", async () => {
// change the chunking algo for the second page, but not the first
await updateEmbeddedContent({
since: new Date(),
embeddedContentStore: embedStore,
pageStore,
sourceNames: [mockDataSourceNames[0]],
embedder,
});
await updateEmbeddedContent({
since: new Date(),
embeddedContentStore: embedStore,
pageStore,
sourceNames: [mockDataSourceNames[1]],
embedder,
chunkOptions: { chunkOverlap: 2 },
});
const updatedPage1Embedding = await embedStore.loadEmbeddedContent({
page: pages[0],
});
const updatedPage2Embedding = await embedStore.loadEmbeddedContent({
page: pages[1],
});
assert(updatedPage1Embedding.length);
assert(updatedPage2Embedding.length);
expect(updatedPage1Embedding[0].chunkAlgoHash).toBe(
page1Embedding[0].chunkAlgoHash
);
expect(updatedPage2Embedding[0].chunkAlgoHash).not.toBe(
page2Embedding[0].chunkAlgoHash
);
});
it("use a new chunking algo on data sources, some of which have pages that have been updated", async () => {
// SETUP: Modify dates of pages and embedded content for this test case
const sinceDate = new Date("2024-01-01");
const afterSinceDate = new Date("2025-01-01");
await mongoClient
.db(databaseName)
.collection("pages")
.updateOne({ ...pages[0] }, { $set: { updated: afterSinceDate } });
await mongoClient
.db(databaseName)
.collection("embedded_content")
.updateOne(
{ sourceName: mockDataSourceNames[0] },
{ $set: { updated: afterSinceDate } }
);
const originalPage1Embedding = await embedStore.loadEmbeddedContent({
page: pages[0],
});
// END SETUP
await updateEmbeddedContent({
since: sinceDate,
embeddedContentStore: embedStore,
pageStore,
sourceNames: mockDataSourceNames,
embedder,
chunkOptions: { chunkOverlap: 2 },
});
const updatedPage1Embedding = await embedStore.loadEmbeddedContent({
page: pages[0],
});
const updatedPage2Embedding = await embedStore.loadEmbeddedContent({
page: pages[1],
});
assert(updatedPage1Embedding.length);
assert(updatedPage2Embedding.length);
// both pages should be updated
expect(updatedPage1Embedding[0].chunkAlgoHash).not.toBe(
originalPage1Embedding[0].chunkAlgoHash
);
expect(updatedPage2Embedding[0].chunkAlgoHash).not.toBe(
page2Embedding[0].chunkAlgoHash
);
});
});