diff --git a/packages/core/src/indices/BaseIndex.ts b/packages/core/src/indices/BaseIndex.ts index e618d69d68feb94ca41742b48a31cb3219543fc0..63f29fe779db2161564cf3cc2f526415fccbde9a 100644 --- a/packages/core/src/indices/BaseIndex.ts +++ b/packages/core/src/indices/BaseIndex.ts @@ -40,6 +40,7 @@ export abstract class IndexStruct { export class IndexDict extends IndexStruct { nodesDict: Record<string, BaseNode> = {}; docStore: Record<string, Document> = {}; // FIXME: this should be implemented in storageContext + type: IndexStructType = IndexStructType.SIMPLE_DICT; getSummary(): string { if (this.summary === undefined) { @@ -57,7 +58,7 @@ export class IndexDict extends IndexStruct { return { ...super.toJson(), nodesDict: this.nodesDict, - type: IndexStructType.SIMPLE_DICT, + type: this.type, }; } } @@ -74,7 +75,7 @@ export class IndexList extends IndexStruct { return { ...super.toJson(), nodes: this.nodes, - type: IndexStructType.LIST, + type: this.type, }; } } diff --git a/packages/core/src/indices/list/ListIndex.ts b/packages/core/src/indices/list/ListIndex.ts index b3a41947c4fd83c863f9fbc21baabf540249e976..74271d3487145f98bfb9014f6168f304d2d6cd43 100644 --- a/packages/core/src/indices/list/ListIndex.ts +++ b/packages/core/src/indices/list/ListIndex.ts @@ -20,6 +20,7 @@ import { ResponseSynthesizer, CompactAndRefine, } from "../../ResponseSynthesizer"; +import { IndexStructType } from "../../dataStructs"; export enum ListRetrieverMode { DEFAULT = "default", @@ -53,16 +54,26 @@ export class ListIndex extends BaseIndex<IndexList> { // Setup IndexStruct from storage let indexStructs = await indexStore.getIndexStructs() as IndexList[]; let indexStruct: IndexList | null; - if (indexStructs.length == 1) { + + if (options.indexStruct && indexStructs.length > 0) { + throw new Error("Cannot initialize index with both indexStruct and indexStore"); + } + + if (options.indexStruct) { + indexStruct = options.indexStruct; + } else if (indexStructs.length == 1) { indexStruct = indexStructs[0]; } else if (indexStructs.length > 1 && options.indexID) { indexStruct = await indexStore.getIndexStruct(options.indexID) as IndexList; - } else if (options.indexStruct) { - indexStruct = options.indexStruct; } else { indexStruct = null; } + // check indexStruct type + if (indexStruct && indexStruct.type !== IndexStructType.LIST) { + throw new Error("Attempting to initialize ListIndex with non-list indexStruct"); + } + if (indexStruct) { if (options.nodes) { throw new Error( diff --git a/packages/core/src/indices/vectorStore/VectorStoreIndex.ts b/packages/core/src/indices/vectorStore/VectorStoreIndex.ts index b5c9b8976596e7bf6e96c964a1d7d5c0c38cecb1..9b2c57bba04283dfd05a3b61c80b52cfad976b78 100644 --- a/packages/core/src/indices/vectorStore/VectorStoreIndex.ts +++ b/packages/core/src/indices/vectorStore/VectorStoreIndex.ts @@ -24,6 +24,7 @@ import { import { BaseRetriever } from "../../Retriever"; import { ResponseSynthesizer } from "../../ResponseSynthesizer"; import { BaseDocumentStore } from "../../storage/docStore/types"; +import { IndexStructType } from "../../dataStructs"; /** * The VectorStoreIndex, an index that stores the nodes only according to their vector embedings. @@ -51,20 +52,30 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { const docStore = storageContext.docStore; const vectorStore = storageContext.vectorStore; const indexStore = storageContext.indexStore; - + // Setup IndexStruct from storage let indexStructs = await indexStore.getIndexStructs() as IndexDict[]; let indexStruct: IndexDict | null; - if (indexStructs.length == 1) { + + if (options.indexStruct && indexStructs.length > 0) { + throw new Error("Cannot initialize index with both indexStruct and indexStore"); + } + + if (options.indexStruct) { + indexStruct = options.indexStruct; + } else if (indexStructs.length == 1) { indexStruct = indexStructs[0]; } else if (indexStructs.length > 1 && options.indexID) { indexStruct = await indexStore.getIndexStruct(options.indexID) as IndexDict; - } else if (options.indexStruct) { - indexStruct = options.indexStruct; } else { indexStruct = null; } + // check indexStruct type + if (indexStruct && indexStruct.type !== IndexStructType.SIMPLE_DICT) { + throw new Error("Attempting to initialize VectorStoreIndex with non-vector indexStruct"); + } + if (indexStruct) { if (options.nodes) { throw new Error(