mirror of
https://github.com/run-llama/chat-llamaindex.git
synced 2026-07-01 21:04:08 -04:00
5c16a343ea
--------- Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import { VectorStoreIndex } from "llamaindex";
|
|
import { storageContextFromDefaults } from "llamaindex/storage/StorageContext";
|
|
|
|
import * as dotenv from "dotenv";
|
|
|
|
import { getDocuments } from "./loader";
|
|
import { initSettings } from "./settings";
|
|
import { STORAGE_CACHE_DIR } from "./shared";
|
|
|
|
// Load environment variables from local .env.development.local file
|
|
dotenv.config({ path: ".env.development.local" });
|
|
|
|
async function getRuntime(func: any) {
|
|
const start = Date.now();
|
|
await func();
|
|
const end = Date.now();
|
|
return end - start;
|
|
}
|
|
|
|
async function generateDatasource() {
|
|
const datasource = process.argv[2];
|
|
if (!datasource) {
|
|
console.error("Please provide a datasource as an argument.");
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log(`Generating storage context for datasource '${datasource}'...`);
|
|
// Split documents, create embeddings and store them in the storage context
|
|
const ms = await getRuntime(async () => {
|
|
const storageContext = await storageContextFromDefaults({
|
|
persistDir: `${STORAGE_CACHE_DIR}/${datasource}`,
|
|
});
|
|
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.");
|
|
})();
|