Skip to content
Snippets Groups Projects
Unverified Commit a8ad9c10 authored by yisding's avatar yisding Committed by GitHub
Browse files

Merge pull request #146 from run-llama/fix/allow-readonly-indexes

fix: allow readonly indexes
parents f1669224 2a270618
No related branches found
No related tags found
No related merge requests found
import { execSync } from "child_process";
import {
PDFReader,
serviceContextFromDefaults,
storageContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";
const STORAGE_DIR = "./cache";
async function main() {
// write the index to disk
const serviceContext = serviceContextFromDefaults({});
const storageContext = await storageContextFromDefaults({
persistDir: `${STORAGE_DIR}`,
});
const reader = new PDFReader();
const documents = await reader.loadData("data/brk-2022.pdf");
await VectorStoreIndex.fromDocuments(documents, {
storageContext,
serviceContext,
});
console.log("wrote index to disk - now trying to read it");
// make index dir read only
execSync(`chmod -R 555 ${STORAGE_DIR}`);
// reopen index
const readOnlyStorageContext = await storageContextFromDefaults({
persistDir: `${STORAGE_DIR}`,
});
await VectorStoreIndex.init({
storageContext: readOnlyStorageContext,
serviceContext,
});
console.log("read only index successfully opened");
}
main().catch(console.error);
...@@ -87,24 +87,23 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { ...@@ -87,24 +87,23 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
); );
} }
if (!indexStruct && !options.nodes) { if (options.nodes) {
// If nodes are passed in, then we need to update the index
indexStruct = await VectorStoreIndex.buildIndexFromNodes(
options.nodes,
serviceContext,
vectorStore,
docStore,
indexStruct,
);
await indexStore.addIndexStruct(indexStruct);
} else if (!indexStruct) {
throw new Error( throw new Error(
"Cannot initialize VectorStoreIndex without nodes or indexStruct", "Cannot initialize VectorStoreIndex without nodes or indexStruct",
); );
} }
const nodes = options.nodes ?? [];
indexStruct = await VectorStoreIndex.buildIndexFromNodes(
nodes,
serviceContext,
vectorStore,
docStore,
indexStruct,
);
await indexStore.addIndexStruct(indexStruct);
return new VectorStoreIndex({ return new VectorStoreIndex({
storageContext, storageContext,
serviceContext, serviceContext,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment