-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
index.ts
72 lines (63 loc) 路 1.85 KB
/
index.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
import assert from "assert";
import { OpenAI } from "@langchain/openai";
import { LLMChain } from "langchain/chains";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { HNSWLib } from "@langchain/community/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "@langchain/openai";
import { Document } from "@langchain/core/documents";
import { CSVLoader } from "langchain/document_loaders/fs/csv";
async function test(useAzure: boolean = false) {
// Test exports
assert(typeof OpenAI === "function");
assert(typeof LLMChain === "function");
assert(typeof ChatPromptTemplate === "function");
assert(typeof HNSWLib === "function");
// Test dynamic imports of peer dependencies
const { HierarchicalNSW } = await HNSWLib.imports();
const openAIParameters = useAzure
? {
azureOpenAIApiKey: "sk-XXXX",
azureOpenAIApiInstanceName: "XXXX",
azureOpenAIApiDeploymentName: "XXXX",
azureOpenAIApiVersion: "XXXX",
}
: {
openAIApiKey: "sk-XXXX",
};
const vs = new HNSWLib(new OpenAIEmbeddings(openAIParameters), {
space: "ip",
numDimensions: 3,
index: new HierarchicalNSW("ip", 3),
});
await vs.addVectors(
[
[0, 1, 0],
[0, 0, 1],
],
[
new Document({
pageContent: "a",
}),
new Document({
pageContent: "b",
}),
]
);
assert((await vs.similaritySearchVectorWithScore([0, 0, 1], 1)).length === 1);
// Test CSVLoader
const loader = new CSVLoader(new Blob(["a,b,c\n1,2,3\n4,5,6"]));
const docs = await loader.load();
assert(docs.length === 2);
}
test(false)
.then(() => console.log("openAI Api success"))
.catch((e) => {
console.error(e);
process.exit(1);
});
test(true)
.then(() => console.log("Azure openAI Api success"))
.catch((e) => {
console.error(e);
process.exit(1);
});