Skip to content

Commit

Permalink
Fix: ts upload file does not create index and document store (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj authored Nov 13, 2024
1 parent 80db5f7 commit 282eaa0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/big-turtles-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-llama": patch
---

Ensure that the index and document store are created when uploading a file with no available index.
8 changes: 7 additions & 1 deletion helpers/env-variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,13 @@ Otherwise, use CHROMA_HOST and CHROMA_PORT config above`,
},
];
default:
return [];
return [
{
name: "STORAGE_CACHE_DIR",
description: "The directory to store the local storage cache.",
value: ".cache",
},
];
}
};

Expand Down
14 changes: 12 additions & 2 deletions templates/components/llamaindex/typescript/documents/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IngestionPipeline,
Settings,
SimpleNodeParser,
storageContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";

Expand All @@ -28,11 +29,20 @@ export async function runPipeline(
return documents.map((document) => document.id_);
} else {
// Initialize a new index with the documents
const newIndex = await VectorStoreIndex.fromDocuments(documents);
newIndex.storageContext.docStore.persist();
console.log(
"Got empty index, created new index with the uploaded documents",
);
const persistDir = process.env.STORAGE_CACHE_DIR;
if (!persistDir) {
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
}
const storageContext = await storageContextFromDefaults({
persistDir,
});
const newIndex = await VectorStoreIndex.fromDocuments(documents, {
storageContext,
});
await newIndex.storageContext.docStore.persist();
return documents.map((document) => document.id_);
}
}
7 changes: 5 additions & 2 deletions templates/components/vectordbs/typescript/none/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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 file
dotenv.config();
Expand All @@ -20,9 +19,13 @@ async function getRuntime(func: any) {
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: STORAGE_CACHE_DIR,
persistDir,
});
const documents = await getDocuments();

Expand Down
7 changes: 5 additions & 2 deletions templates/components/vectordbs/typescript/none/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { SimpleDocumentStore, VectorStoreIndex } from "llamaindex";
import { storageContextFromDefaults } from "llamaindex/storage/StorageContext";
import { STORAGE_CACHE_DIR } from "./shared";

export async function getDataSource(params?: any) {
const persistDir = process.env.STORAGE_CACHE_DIR;
if (!persistDir) {
throw new Error("STORAGE_CACHE_DIR environment variable is required!");
}
const storageContext = await storageContextFromDefaults({
persistDir: `${STORAGE_CACHE_DIR}`,
persistDir,
});

const numberOfDocs = Object.keys(
Expand Down
1 change: 0 additions & 1 deletion templates/components/vectordbs/typescript/none/shared.ts

This file was deleted.

0 comments on commit 282eaa0

Please sign in to comment.