From e9a111d9d37caab02296c09663d02cf58716fd90 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Wed, 23 Oct 2024 01:00:07 +0700
Subject: [PATCH] fix: VectorIndexRetrieverOptions typing (#1366)

Co-authored-by: Alex Yang <himself65@outlook.com>
---
 .changeset/calm-pandas-battle.md              |  5 +++
 .vscode/settings.json                         |  3 +-
 .../src/indices/vectorStore/index.ts          | 41 +++++++++++--------
 3 files changed, 32 insertions(+), 17 deletions(-)
 create mode 100644 .changeset/calm-pandas-battle.md

diff --git a/.changeset/calm-pandas-battle.md b/.changeset/calm-pandas-battle.md
new file mode 100644
index 000000000..eddce69ac
--- /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 2e30bf86b..fec54028e 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 09b357843..0b89d0dc4 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;
   }
 
   /**
-- 
GitLab