-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcache.test.ts
30 lines (27 loc) · 1.08 KB
/
cache.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
import { test, expect } from "@jest/globals";
import { Embeddings } from "@langchain/core/embeddings";
import { CacheBackedEmbeddings } from "../cache_backed.js";
import { InMemoryStore } from "../../storage/in_memory.js";
class RandomEmbeddings extends Embeddings {
async embedDocuments(documents: string[]): Promise<number[][]> {
const quoteUnquoteEmbeddings = [];
for (const document of documents) {
quoteUnquoteEmbeddings.push(await this.embedQuery(document));
}
return quoteUnquoteEmbeddings;
}
async embedQuery(_document: string): Promise<number[]> {
return [Math.random(), Math.random()];
}
}
test("Basic embeddings cache", async () => {
const embeddingsCache = CacheBackedEmbeddings.fromBytesStore(
new RandomEmbeddings({}),
new InMemoryStore()
);
const documents = ["How are you?", "I am fine", "I am LangChain"];
const result = await embeddingsCache.embedDocuments(documents);
expect(result.findIndex((v) => v === undefined)).toEqual(-1);
const result2 = await embeddingsCache.embedDocuments(documents);
expect(result).toEqual(result2);
});