From e772ca23e21bbe09e7263ac89dfc22d9cf628694 Mon Sep 17 00:00:00 2001
From: fuhui <hongen.phe@antgroup.com>
Date: Fri, 22 Nov 2024 20:24:34 +0800
Subject: [PATCH 1/2] fix regiter bug

---
 modelcache/manager/vector_data/faiss.py  | 11 +++++++++--
 modelcache/manager/vector_data/milvus.py |  8 ++++++++
 modelcache/manager/vector_data/redis.py  |  8 +++++---
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/modelcache/manager/vector_data/faiss.py b/modelcache/manager/vector_data/faiss.py
index 0f8445c..35c261e 100644
--- a/modelcache/manager/vector_data/faiss.py
+++ b/modelcache/manager/vector_data/faiss.py
@@ -14,8 +14,9 @@ def __init__(self, index_file_path, dimension, top_k):
         self._dimension = dimension
         self._index = faiss.index_factory(self._dimension, "IDMap,Flat", faiss.METRIC_L2)
         self._top_k = top_k
-        if os.path.isfile(index_file_path):
-            self._index = faiss.read_index(index_file_path)
+        self.index_file_path = index_file_path
+        if os.path.isfile(self.index_file_path):
+            self._index = faiss.read_index(self.index_file_path)
 
     def mul_add(self, datas: List[VectorData], model=None):
         data_array, id_array = map(list, zip(*((data.data, data.id) for data in datas)))
@@ -54,3 +55,9 @@ def close(self):
 
     def count(self):
         return self._index.ntotal
+
+    def create(self, model=None):
+        if os.path.isfile(self.index_file_path):
+            self._index = faiss.read_index(self.index_file_path)
+
+        return 'create_success'
diff --git a/modelcache/manager/vector_data/milvus.py b/modelcache/manager/vector_data/milvus.py
index 50d6ab1..2c07204 100644
--- a/modelcache/manager/vector_data/milvus.py
+++ b/modelcache/manager/vector_data/milvus.py
@@ -180,6 +180,14 @@ def rebuild_col(self, model):
     def rebuild(self, ids=None):  # pylint: disable=unused-argument
         self.col.compact()
 
+    def create(self, model=None):
+        collection_name_model = self.collection_name + '_' + model
+        if utility.has_collection(collection_name_model, using=self.alias):
+            return 'already_exists'
+        else:
+            self._create_collection(collection_name_model)
+            return 'create_success'
+
     def flush(self):
         self.col.flush(_async=True)
 
diff --git a/modelcache/manager/vector_data/redis.py b/modelcache/manager/vector_data/redis.py
index afa1088..83b701d 100644
--- a/modelcache/manager/vector_data/redis.py
+++ b/modelcache/manager/vector_data/redis.py
@@ -71,9 +71,11 @@ def create_index(self, index_name, index_prefix):
             definition = IndexDefinition(prefix=[index_prefix], index_type=IndexType.HASH)
 
             # create Index
-            self._client.ft(index_name).create_index(
-                fields=fields, definition=definition
-            )
+            print('self._client: {}'.format(self._client))
+            resp = self._client.ft(index_name).create_index(
+                    fields=fields, definition=definition
+                )
+            print('resp: {}'.format(resp))
             return 'create_success'
 
     def mul_add(self, datas: List[VectorData], model=None):

From 9c1807f89e1c554196541f8738cd1c89806c0861 Mon Sep 17 00:00:00 2001
From: fuhui <hongen.phe@antgroup.com>
Date: Mon, 23 Dec 2024 14:49:58 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E6=95=B4adapter=20query=E4=B8=AD?=
 =?UTF-8?q?=E7=9A=84use=5Freranker=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 modelcache/adapter/adapter_query.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/modelcache/adapter/adapter_query.py b/modelcache/adapter/adapter_query.py
index e3be30a..5c2afcc 100644
--- a/modelcache/adapter/adapter_query.py
+++ b/modelcache/adapter/adapter_query.py
@@ -7,11 +7,13 @@
 from modelcache.processor.pre import multi_analysis
 from FlagEmbedding import FlagReranker
 
-USE_RERANKER = True  # 如果为 True 则启用 reranker，否则使用原有逻辑
+# USE_RERANKER = True  # 如果为 True 则启用 reranker，否则使用原有逻辑
+
 
 def adapt_query(cache_data_convert, *args, **kwargs):
     chat_cache = kwargs.pop("cache_obj", cache)
     scope = kwargs.pop("scope", None)
+    use_reranker = kwargs.pop("use_reranker", False)
     model = scope['model']
     if not chat_cache.has_init:
         raise NotInitError()
@@ -76,7 +78,7 @@ def adapt_query(cache_data_convert, *args, **kwargs):
         if rank_pre < rank_threshold:
             return
 
-        if USE_RERANKER:
+        if use_reranker:
             reranker = FlagReranker('BAAI/bge-reranker-v2-m3', use_fp16=False)
             for cache_data in cache_data_list:
                 primary_id = cache_data[1]
