Skip to content
Snippets Groups Projects
Commit 9fa6d4a8 authored by Yi Ding's avatar Yi Ding
Browse files

make second arg in fromDocuments optional

parent c77dd18f
Branches
Tags llamaindex@0.0.7
No related merge requests found
---
"llamaindex": patch
---
Make second argument of fromDocuments optional again
...@@ -52,11 +52,13 @@ export class ListIndex extends BaseIndex<IndexList> { ...@@ -52,11 +52,13 @@ export class ListIndex extends BaseIndex<IndexList> {
const { docStore, indexStore } = storageContext; const { docStore, indexStore } = storageContext;
// Setup IndexStruct from storage // Setup IndexStruct from storage
let indexStructs = await indexStore.getIndexStructs() as IndexList[]; let indexStructs = (await indexStore.getIndexStructs()) as IndexList[];
let indexStruct: IndexList | null; let indexStruct: IndexList | null;
if (options.indexStruct && indexStructs.length > 0) { if (options.indexStruct && indexStructs.length > 0) {
throw new Error("Cannot initialize index with both indexStruct and indexStore"); throw new Error(
"Cannot initialize index with both indexStruct and indexStore"
);
} }
if (options.indexStruct) { if (options.indexStruct) {
...@@ -64,14 +66,18 @@ export class ListIndex extends BaseIndex<IndexList> { ...@@ -64,14 +66,18 @@ export class ListIndex extends BaseIndex<IndexList> {
} else if (indexStructs.length == 1) { } else if (indexStructs.length == 1) {
indexStruct = indexStructs[0]; indexStruct = indexStructs[0];
} else if (indexStructs.length > 1 && options.indexId) { } else if (indexStructs.length > 1 && options.indexId) {
indexStruct = await indexStore.getIndexStruct(options.indexId) as IndexList; indexStruct = (await indexStore.getIndexStruct(
options.indexId
)) as IndexList;
} else { } else {
indexStruct = null; indexStruct = null;
} }
// check indexStruct type // check indexStruct type
if (indexStruct && indexStruct.type !== IndexStructType.LIST) { if (indexStruct && indexStruct.type !== IndexStructType.LIST) {
throw new Error("Attempting to initialize ListIndex with non-list indexStruct"); throw new Error(
"Attempting to initialize ListIndex with non-list indexStruct"
);
} }
if (indexStruct) { if (indexStruct) {
...@@ -90,7 +96,7 @@ export class ListIndex extends BaseIndex<IndexList> { ...@@ -90,7 +96,7 @@ export class ListIndex extends BaseIndex<IndexList> {
options.nodes, options.nodes,
storageContext.docStore storageContext.docStore
); );
await indexStore.addIndexStruct(indexStruct); await indexStore.addIndexStruct(indexStruct);
} }
...@@ -108,7 +114,8 @@ export class ListIndex extends BaseIndex<IndexList> { ...@@ -108,7 +114,8 @@ export class ListIndex extends BaseIndex<IndexList> {
args: { args: {
storageContext?: StorageContext; storageContext?: StorageContext;
serviceContext?: ServiceContext; serviceContext?: ServiceContext;
}): Promise<ListIndex> { } = {}
): Promise<ListIndex> {
let { storageContext, serviceContext } = args; let { storageContext, serviceContext } = args;
storageContext = storageContext ?? (await storageContextFromDefaults({})); storageContext = storageContext ?? (await storageContextFromDefaults({}));
serviceContext = serviceContext ?? serviceContextFromDefaults({}); serviceContext = serviceContext ?? serviceContextFromDefaults({});
......
...@@ -54,11 +54,13 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { ...@@ -54,11 +54,13 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
const indexStore = storageContext.indexStore; const indexStore = storageContext.indexStore;
// Setup IndexStruct from storage // Setup IndexStruct from storage
let indexStructs = await indexStore.getIndexStructs() as IndexDict[]; let indexStructs = (await indexStore.getIndexStructs()) as IndexDict[];
let indexStruct: IndexDict | null; let indexStruct: IndexDict | null;
if (options.indexStruct && indexStructs.length > 0) { if (options.indexStruct && indexStructs.length > 0) {
throw new Error("Cannot initialize index with both indexStruct and indexStore"); throw new Error(
"Cannot initialize index with both indexStruct and indexStore"
);
} }
if (options.indexStruct) { if (options.indexStruct) {
...@@ -66,14 +68,18 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { ...@@ -66,14 +68,18 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
} else if (indexStructs.length == 1) { } else if (indexStructs.length == 1) {
indexStruct = indexStructs[0]; indexStruct = indexStructs[0];
} else if (indexStructs.length > 1 && options.indexId) { } else if (indexStructs.length > 1 && options.indexId) {
indexStruct = await indexStore.getIndexStruct(options.indexId) as IndexDict; indexStruct = (await indexStore.getIndexStruct(
options.indexId
)) as IndexDict;
} else { } else {
indexStruct = null; indexStruct = null;
} }
// check indexStruct type // check indexStruct type
if (indexStruct && indexStruct.type !== IndexStructType.SIMPLE_DICT) { if (indexStruct && indexStruct.type !== IndexStructType.SIMPLE_DICT) {
throw new Error("Attempting to initialize VectorStoreIndex with non-vector indexStruct"); throw new Error(
"Attempting to initialize VectorStoreIndex with non-vector indexStruct"
);
} }
if (indexStruct) { if (indexStruct) {
...@@ -94,7 +100,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { ...@@ -94,7 +100,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
vectorStore, vectorStore,
docStore docStore
); );
await indexStore.addIndexStruct(indexStruct); await indexStore.addIndexStruct(indexStruct);
} }
...@@ -182,7 +188,8 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { ...@@ -182,7 +188,8 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> {
args: { args: {
storageContext?: StorageContext; storageContext?: StorageContext;
serviceContext?: ServiceContext; serviceContext?: ServiceContext;
}): Promise<VectorStoreIndex> { } = {}
): Promise<VectorStoreIndex> {
let { storageContext, serviceContext } = args; let { storageContext, serviceContext } = args;
storageContext = storageContext ?? (await storageContextFromDefaults({})); storageContext = storageContext ?? (await storageContextFromDefaults({}));
serviceContext = serviceContext ?? serviceContextFromDefaults({}); serviceContext = serviceContext ?? serviceContextFromDefaults({});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment