Skip to content

Commit a3c1c55

Browse files
committed
allow upload file with empty index store
1 parent e0921fe commit a3c1c55

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "llamaindex";
88

99
export async function runPipeline(
10-
currentIndex: VectorStoreIndex,
10+
currentIndex: VectorStoreIndex | null,
1111
documents: Document[],
1212
) {
1313
// Use ingestion pipeline to process the documents into nodes and add them to the vector store
@@ -21,8 +21,18 @@ export async function runPipeline(
2121
],
2222
});
2323
const nodes = await pipeline.run({ documents });
24-
await currentIndex.insertNodes(nodes);
25-
currentIndex.storageContext.docStore.persist();
26-
console.log("Added nodes to the vector store.");
27-
return documents.map((document) => document.id_);
24+
if (currentIndex) {
25+
await currentIndex.insertNodes(nodes);
26+
currentIndex.storageContext.docStore.persist();
27+
console.log("Added nodes to the vector store.");
28+
return documents.map((document) => document.id_);
29+
} else {
30+
// Initialize a new index with the documents
31+
const newIndex = await VectorStoreIndex.fromDocuments(documents);
32+
newIndex.storageContext.docStore.persist();
33+
console.log(
34+
"Got empty index, created new index with the uploaded documents",
35+
);
36+
return documents.map((document) => document.id_);
37+
}
2838
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FileMetadata, parseFile, storeFile } from "./helper";
66
import { runPipeline } from "./pipeline";
77

88
export async function uploadDocument(
9-
index: VectorStoreIndex | LlamaCloudIndex,
9+
index: VectorStoreIndex | LlamaCloudIndex | null,
1010
filename: string,
1111
raw: string,
1212
): Promise<FileMetadata> {

templates/types/streaming/nextjs/app/api/chat/upload/route.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ export async function POST(request: NextRequest) {
2323
);
2424
}
2525
const index = await getDataSource(params);
26-
if (!index) {
27-
throw new Error(
28-
`StorageContext is empty - call 'npm run generate' to generate the storage first`,
29-
);
30-
}
3126
return NextResponse.json(await uploadDocument(index, filename, base64));
3227
} catch (error) {
3328
console.error("[Upload API]", error);

0 commit comments

Comments
 (0)