|
|
|
@ -8,6 +8,7 @@ from pymilvus import MilvusClient, MilvusException, connections
|
|
|
|
from core.rag.datasource.vdb.field import Field
|
|
|
|
from core.rag.datasource.vdb.field import Field
|
|
|
|
from core.rag.datasource.vdb.vector_base import BaseVector
|
|
|
|
from core.rag.datasource.vdb.vector_base import BaseVector
|
|
|
|
from core.rag.models.document import Document
|
|
|
|
from core.rag.models.document import Document
|
|
|
|
|
|
|
|
from extensions.ext_redis import redis_client
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
@ -61,16 +62,6 @@ class MilvusVector(BaseVector):
|
|
|
|
'params': {"M": 8, "efConstruction": 64}
|
|
|
|
'params': {"M": 8, "efConstruction": 64}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
metadatas = [d.metadata for d in texts]
|
|
|
|
metadatas = [d.metadata for d in texts]
|
|
|
|
|
|
|
|
|
|
|
|
# Grab the existing collection if it exists
|
|
|
|
|
|
|
|
from pymilvus import utility
|
|
|
|
|
|
|
|
alias = uuid4().hex
|
|
|
|
|
|
|
|
if self._client_config.secure:
|
|
|
|
|
|
|
|
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
|
|
|
|
|
|
|
|
connections.connect(alias=alias, uri=uri, user=self._client_config.user, password=self._client_config.password)
|
|
|
|
|
|
|
|
if not utility.has_collection(self._collection_name, using=alias):
|
|
|
|
|
|
|
|
self.create_collection(embeddings, metadatas, index_params)
|
|
|
|
self.create_collection(embeddings, metadatas, index_params)
|
|
|
|
self.add_texts(texts, embeddings)
|
|
|
|
self.add_texts(texts, embeddings)
|
|
|
|
|
|
|
|
|
|
|
|
@ -187,7 +178,22 @@ class MilvusVector(BaseVector):
|
|
|
|
|
|
|
|
|
|
|
|
def create_collection(
|
|
|
|
def create_collection(
|
|
|
|
self, embeddings: list, metadatas: Optional[list[dict]] = None, index_params: Optional[dict] = None
|
|
|
|
self, embeddings: list, metadatas: Optional[list[dict]] = None, index_params: Optional[dict] = None
|
|
|
|
) -> str:
|
|
|
|
):
|
|
|
|
|
|
|
|
lock_name = 'vector_indexing_lock_{}'.format(self._collection_name)
|
|
|
|
|
|
|
|
with redis_client.lock(lock_name, timeout=20):
|
|
|
|
|
|
|
|
collection_exist_cache_key = 'vector_indexing_{}'.format(self._collection_name)
|
|
|
|
|
|
|
|
if redis_client.get(collection_exist_cache_key):
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# Grab the existing collection if it exists
|
|
|
|
|
|
|
|
from pymilvus import utility
|
|
|
|
|
|
|
|
alias = uuid4().hex
|
|
|
|
|
|
|
|
if self._client_config.secure:
|
|
|
|
|
|
|
|
uri = "https://" + str(self._client_config.host) + ":" + str(self._client_config.port)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
uri = "http://" + str(self._client_config.host) + ":" + str(self._client_config.port)
|
|
|
|
|
|
|
|
connections.connect(alias=alias, uri=uri, user=self._client_config.user,
|
|
|
|
|
|
|
|
password=self._client_config.password)
|
|
|
|
|
|
|
|
if not utility.has_collection(self._collection_name, using=alias):
|
|
|
|
from pymilvus import CollectionSchema, DataType, FieldSchema
|
|
|
|
from pymilvus import CollectionSchema, DataType, FieldSchema
|
|
|
|
from pymilvus.orm.types import infer_dtype_bydata
|
|
|
|
from pymilvus.orm.types import infer_dtype_bydata
|
|
|
|
|
|
|
|
|
|
|
|
@ -225,8 +231,7 @@ class MilvusVector(BaseVector):
|
|
|
|
self._client.create_collection_with_schema(collection_name=collection_name,
|
|
|
|
self._client.create_collection_with_schema(collection_name=collection_name,
|
|
|
|
schema=schema, index_param=index_params,
|
|
|
|
schema=schema, index_param=index_params,
|
|
|
|
consistency_level=self._consistency_level)
|
|
|
|
consistency_level=self._consistency_level)
|
|
|
|
return collection_name
|
|
|
|
redis_client.set(collection_exist_cache_key, 1, ex=3600)
|
|
|
|
|
|
|
|
|
|
|
|
def _init_client(self, config) -> MilvusClient:
|
|
|
|
def _init_client(self, config) -> MilvusClient:
|
|
|
|
if config.secure:
|
|
|
|
if config.secure:
|
|
|
|
uri = "https://" + str(config.host) + ":" + str(config.port)
|
|
|
|
uri = "https://" + str(config.host) + ":" + str(config.port)
|
|
|
|
|