-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
hnswlib.test.ts
64 lines (56 loc) · 1.71 KB
/
hnswlib.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
import { test, expect } from "@jest/globals";
import { Document } from "@langchain/core/documents";
import { FakeEmbeddings } from "@langchain/core/utils/testing";
import { HNSWLib } from "../hnswlib.js";
test("Test HNSWLib.fromTexts + addVectors", async () => {
const vectorStore = await HNSWLib.fromTexts(
["Hello world"],
[{ id: 2 }],
new FakeEmbeddings()
);
expect(vectorStore.index?.getMaxElements()).toBe(1);
expect(vectorStore.index?.getCurrentCount()).toBe(1);
await vectorStore.addVectors(
[
[0, 1, 0, 0],
[1, 0, 0, 0],
[0.5, 0.5, 0.5, 0.5],
],
[
new Document({
pageContent: "hello bye",
metadata: { id: 5 },
}),
new Document({
pageContent: "hello worlddwkldnsk",
metadata: { id: 4 },
}),
new Document({
pageContent: "hello you",
metadata: { id: 6 },
}),
]
);
expect(vectorStore.index?.getMaxElements()).toBe(4);
const resultTwo = await vectorStore.similaritySearchVectorWithScore(
[1, 0, 0, 0],
3
);
const resultTwoMetadatas = resultTwo.map(([{ metadata }]) => metadata);
expect(resultTwoMetadatas).toEqual([{ id: 4 }, { id: 6 }, { id: 2 }]);
});
test("Test HNSWLib metadata filtering", async () => {
const pageContent = "Hello world";
const vectorStore = await HNSWLib.fromTexts(
[pageContent, pageContent, pageContent],
[{ id: 2 }, { id: 3 }, { id: 4 }],
new FakeEmbeddings()
);
// If the filter wasn't working, we'd get all 3 documents back
const results = await vectorStore.similaritySearch(
pageContent,
3,
(document) => document.metadata.id === 3
);
expect(results).toEqual([new Document({ metadata: { id: 3 }, pageContent })]);
});