Skip to content

Commit 282eaa0

Browse files
authored
Fix: ts upload file does not create index and document store (#422)
1 parent 80db5f7 commit 282eaa0

File tree

6 files changed

+34
-8
lines changed

6 files changed

+34
-8
lines changed

.changeset/big-turtles-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-llama": patch
3+
---
4+
5+
Ensure that the index and document store are created when uploading a file with no available index.

helpers/env-variables.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,13 @@ Otherwise, use CHROMA_HOST and CHROMA_PORT config above`,
217217
},
218218
];
219219
default:
220-
return [];
220+
return [
221+
{
222+
name: "STORAGE_CACHE_DIR",
223+
description: "The directory to store the local storage cache.",
224+
value: ".cache",
225+
},
226+
];
221227
}
222228
};
223229

templates/components/llamaindex/typescript/documents/pipeline.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
IngestionPipeline,
44
Settings,
55
SimpleNodeParser,
6+
storageContextFromDefaults,
67
VectorStoreIndex,
78
} from "llamaindex";
89

@@ -28,11 +29,20 @@ export async function runPipeline(
2829
return documents.map((document) => document.id_);
2930
} else {
3031
// Initialize a new index with the documents
31-
const newIndex = await VectorStoreIndex.fromDocuments(documents);
32-
newIndex.storageContext.docStore.persist();
3332
console.log(
3433
"Got empty index, created new index with the uploaded documents",
3534
);
35+
const persistDir = process.env.STORAGE_CACHE_DIR;
36+
if (!persistDir) {
37+
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
38+
}
39+
const storageContext = await storageContextFromDefaults({
40+
persistDir,
41+
});
42+
const newIndex = await VectorStoreIndex.fromDocuments(documents, {
43+
storageContext,
44+
});
45+
await newIndex.storageContext.docStore.persist();
3646
return documents.map((document) => document.id_);
3747
}
3848
}

templates/components/vectordbs/typescript/none/generate.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as dotenv from "dotenv";
55

66
import { getDocuments } from "./loader";
77
import { initSettings } from "./settings";
8-
import { STORAGE_CACHE_DIR } from "./shared";
98

109
// Load environment variables from local .env file
1110
dotenv.config();
@@ -20,9 +19,13 @@ async function getRuntime(func: any) {
2019
async function generateDatasource() {
2120
console.log(`Generating storage context...`);
2221
// Split documents, create embeddings and store them in the storage context
22+
const persistDir = process.env.STORAGE_CACHE_DIR;
23+
if (!persistDir) {
24+
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
25+
}
2326
const ms = await getRuntime(async () => {
2427
const storageContext = await storageContextFromDefaults({
25-
persistDir: STORAGE_CACHE_DIR,
28+
persistDir,
2629
});
2730
const documents = await getDocuments();
2831

templates/components/vectordbs/typescript/none/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { SimpleDocumentStore, VectorStoreIndex } from "llamaindex";
22
import { storageContextFromDefaults } from "llamaindex/storage/StorageContext";
3-
import { STORAGE_CACHE_DIR } from "./shared";
43

54
export async function getDataSource(params?: any) {
5+
const persistDir = process.env.STORAGE_CACHE_DIR;
6+
if (!persistDir) {
7+
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
8+
}
69
const storageContext = await storageContextFromDefaults({
7-
persistDir: `${STORAGE_CACHE_DIR}`,
10+
persistDir,
811
});
912

1013
const numberOfDocs = Object.keys(

templates/components/vectordbs/typescript/none/shared.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)