Skip to content

Commit

Permalink
fix persistence bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yisding committed Aug 5, 2023
1 parent b3d659b commit 9214b06
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-boats-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---

Fix persistence bug (thanks @HenryHengZJ)
36 changes: 0 additions & 36 deletions apps/simple/persist.ts

This file was deleted.

26 changes: 19 additions & 7 deletions apps/simple/storageContext.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Document, VectorStoreIndex, storageContextFromDefaults } from "llamaindex";
import {
Document,
VectorStoreIndex,
storageContextFromDefaults,
} from "llamaindex";
import essay from "./essay";


async function main() {
// Create Document object with essay
const document = new Document({ text: essay });

// Split text and create embeddings. Store them in a VectorStoreIndex
// persist the vector store automatically with the storage context
const storageContext = await storageContextFromDefaults({ persistDir: "./storage" });
const index = await VectorStoreIndex.fromDocuments([document], { storageContext });
const storageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
const index = await VectorStoreIndex.fromDocuments([document], {
storageContext,
});

// Query the index
const queryEngine = index.asQueryEngine();
Expand All @@ -21,9 +28,14 @@ async function main() {
console.log(response.toString());

// load the index
const loadedIndex = await VectorStoreIndex.init({ storageContext });
const laodedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await laodedQueryEngine.query(
const secondStorageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
const loadedIndex = await VectorStoreIndex.init({
storageContext: secondStorageContext,
});
const loadedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await loadedQueryEngine.query(
"What did the author do growing up?"
);
console.log(loadedResponse.toString());
Expand Down
26 changes: 19 additions & 7 deletions examples/storageContext.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { Document, VectorStoreIndex, storageContextFromDefaults } from "llamaindex";
import {
Document,
VectorStoreIndex,
storageContextFromDefaults,
} from "llamaindex";
import essay from "./essay";


async function main() {
// Create Document object with essay
const document = new Document({ text: essay });

// Split text and create embeddings. Store them in a VectorStoreIndex
// persist the vector store automatically with the storage context
const storageContext = await storageContextFromDefaults({ persistDir: "./storage" });
const index = await VectorStoreIndex.fromDocuments([document], { storageContext });
const storageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
const index = await VectorStoreIndex.fromDocuments([document], {
storageContext,
});

// Query the index
const queryEngine = index.asQueryEngine();
Expand All @@ -21,9 +28,14 @@ async function main() {
console.log(response.toString());

// load the index
const loadedIndex = await VectorStoreIndex.init({ storageContext });
const laodedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await laodedQueryEngine.query(
const secondStorageContext = await storageContextFromDefaults({
persistDir: "./storage",
});
const loadedIndex = await VectorStoreIndex.init({
storageContext: secondStorageContext,
});
const loadedQueryEngine = loadedIndex.asQueryEngine();
const loadedResponse = await loadedQueryEngine.query(
"What did the author do growing up?"
);
console.log(loadedResponse.toString());
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ export abstract class BaseNode {
hash: this.hash,
};
}

/**
* Used with built in JSON.stringify
* @returns
*/
toJSON(): Record<string, any> {
return { ...this, type: this.getType() };
}
}

/**
Expand Down Expand Up @@ -232,6 +240,23 @@ export class Document extends TextNode {
}
}

export function jsonToNode(json: any) {
if (!json.type) {
throw new Error("Node type not found");
}

switch (json.type) {
case ObjectType.TEXT:
return new TextNode(json);
case ObjectType.INDEX:
return new IndexNode(json);
case ObjectType.DOCUMENT:
return new Document(json);
default:
throw new Error(`Invalid node type: ${json.type}`);
}
}

// export class ImageDocument extends Document {
// image?: string;
// }
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/indices/BaseIndex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Document, BaseNode } from "../Node";
import { Document, BaseNode, jsonToNode } from "../Node";
import { v4 as uuidv4 } from "uuid";
import { BaseRetriever } from "../Retriever";
import { ServiceContext } from "../ServiceContext";
Expand Down Expand Up @@ -74,7 +74,12 @@ export function jsonToIndexStruct(json: any): IndexStruct {
return indexList;
} else if (json.type === IndexStructType.SIMPLE_DICT) {
const indexDict = new IndexDict(json.indexId, json.summary);
indexDict.nodesDict = json.nodesDict;
indexDict.nodesDict = Object.entries(json.nodesDict).reduce<
Record<string, BaseNode>
>((acc, [key, value]) => {
acc[key] = jsonToNode(value);
return acc;
}, {});
return indexDict;
} else {
throw new Error(`Unknown index struct type: ${json.type}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/storage/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const DEFAULT_COLLECTION = "data";
export const DEFAULT_PERSIST_DIR = "./storage";
export const DEFAULT_INDEX_STORE_PERSIST_FILENAME = "index_store.json";
export const DEFAULT_DOC_STORE_PERSIST_FILENAME = "docstore.json";
export const DEFAULT_DOC_STORE_PERSIST_FILENAME = "doc_store.json";
export const DEFAULT_VECTOR_STORE_PERSIST_FILENAME = "vector_store.json";
export const DEFAULT_GRAPH_STORE_PERSIST_FILENAME = "graph_store.json";
export const DEFAULT_NAMESPACE = "docstore";
Expand Down

0 comments on commit 9214b06

Please sign in to comment.