diff --git a/.changeset/calm-pandas-battle.md b/.changeset/calm-pandas-battle.md new file mode 100644 index 0000000000000000000000000000000000000000..eddce69ac0c2ad81194380c05a2716bf96e4f392 --- /dev/null +++ b/.changeset/calm-pandas-battle.md @@ -0,0 +1,5 @@ +--- +"llamaindex": patch +--- + +fix: VectorIndexRetrieverOptions typing diff --git a/.vscode/settings.json b/.vscode/settings.json index 2e30bf86b8481cc1c35f8974ef3f47424ce1063d..fec54028e2fa993f330852b3ce240d00c87de31a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,5 +13,6 @@ }, "[json]": { "editor.defaultFormatter": "esbenp.prettier-vscode" - } + }, + "prettier.prettierPath": "./node_modules/prettier" } diff --git a/packages/llamaindex/src/indices/vectorStore/index.ts b/packages/llamaindex/src/indices/vectorStore/index.ts index 09b3578438465dff0caaabccf70113ca65f3e424..0b89d0dc466e7f8c0d9ac95c0e39a6c813fd58b4 100644 --- a/packages/llamaindex/src/indices/vectorStore/index.ts +++ b/packages/llamaindex/src/indices/vectorStore/index.ts @@ -274,7 +274,7 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { } asRetriever( - options?: Omit<VectorIndexRetrieverOptions, "index">, + options?: OmitIndex<VectorIndexRetrieverOptions>, ): VectorIndexRetriever { return new VectorIndexRetriever({ index: this, ...options }); } @@ -382,12 +382,19 @@ export class VectorStoreIndex extends BaseIndex<IndexDict> { type TopKMap = { [P in ModalityType]: number }; +type OmitIndex<T> = T extends { index: any } ? Omit<T, "index"> : never; + export type VectorIndexRetrieverOptions = { index: VectorStoreIndex; - similarityTopK?: number | undefined; - topK?: TopKMap | undefined; filters?: MetadataFilters | undefined; -}; +} & ( + | { + topK?: TopKMap | undefined; + } + | { + similarityTopK?: number | undefined; + } +); export class VectorIndexRetriever extends BaseRetriever { index: VectorStoreIndex; @@ -396,20 +403,22 @@ export class VectorIndexRetriever extends BaseRetriever { serviceContext?: ServiceContext | undefined; filters?: MetadataFilters | undefined; - constructor({ - index, - similarityTopK, - topK, - filters, - }: VectorIndexRetrieverOptions) { + constructor(options: VectorIndexRetrieverOptions) { super(); - this.index = index; + this.index = options.index; this.serviceContext = this.index.serviceContext; - this.topK = topK ?? { - [ModalityType.TEXT]: similarityTopK ?? DEFAULT_SIMILARITY_TOP_K, - [ModalityType.IMAGE]: DEFAULT_SIMILARITY_TOP_K, - }; - this.filters = filters; + if ("topK" in options && options.topK) { + this.topK = options.topK; + } else { + this.topK = { + [ModalityType.TEXT]: + "similarityTopK" in options && options.similarityTopK + ? options.similarityTopK + : DEFAULT_SIMILARITY_TOP_K, + [ModalityType.IMAGE]: DEFAULT_SIMILARITY_TOP_K, + }; + } + this.filters = options.filters; } /**