-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgenerate.ts
43 lines (36 loc) · 1.2 KB
/
generate.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
import { VectorStoreIndex } from "llamaindex";
import { storageContextFromDefaults } from "llamaindex/storage/StorageContext";
import * as dotenv from "dotenv";
import { getDocuments } from "./loader";
import { initSettings } from "./settings";
// Load environment variables from local .env file
dotenv.config();
async function getRuntime(func: any) {
const start = Date.now();
await func();
const end = Date.now();
return end - start;
}
async function generateDatasource() {
console.log(`Generating storage context...`);
// Split documents, create embeddings and store them in the storage context
const persistDir = process.env.STORAGE_CACHE_DIR;
if (!persistDir) {
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
}
const ms = await getRuntime(async () => {
const storageContext = await storageContextFromDefaults({
persistDir,
});
const documents = await getDocuments();
await VectorStoreIndex.fromDocuments(documents, {
storageContext,
});
});
console.log(`Storage context successfully generated in ${ms / 1000}s.`);
}
(async () => {
initSettings();
await generateDatasource();
console.log("Finished generating storage.");
})();