From ed36d410247c73f2906f0dc66982a258dca512ed Mon Sep 17 00:00:00 2001 From: Logan Markewich <logan.markewich@live.com> Date: Sun, 23 Jul 2023 21:20:48 -0600 Subject: [PATCH] better type checking --- packages/core/src/indices/BaseIndex.ts | 5 +++-- packages/core/src/indices/list/ListIndex.ts | 17 ++++++++++++++--- .../indices/vectorStore/VectorStoreIndex.ts | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/packages/core/src/indices/BaseIndex.ts b/packages/core/src/indices/BaseIndex.ts index e618d69d6..63f29fe77 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 b3a41947c..74271d348 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 b5c9b8976..9b2c57bba 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( -- GitLab