Source code for langchain_milvus.vectorstores.milvus
from__future__importannotationsimportloggingfromtypingimport(Any,Callable,Dict,Iterable,List,Literal,Optional,Tuple,TypeVar,Union,cast,)importnumpyasnpfromlangchain_core.documentsimportDocumentfromlangchain_core.embeddingsimportEmbeddingsfromlangchain_core.vectorstoresimportVectorStorefrompymilvusimport(AnnSearchRequest,Collection,CollectionSchema,DataType,FieldSchema,FunctionType,MilvusClient,MilvusException,RRFRanker,SearchResult,WeightedRanker,utility,)frompymilvus.client.typesimportLoadState# type: ignorefrompymilvus.orm.typesimportinfer_dtype_bydata# type: ignorefromlangchain_milvus.functionimportBaseMilvusBuiltInFunction,BM25BuiltInFunctionfromlangchain_milvus.utils.constantimportPRIMARY_FIELD,TEXT_FIELD,VECTOR_FIELDfromlangchain_milvus.utils.sparseimportBaseSparseEmbeddinglogger=logging.getLogger(__name__)# - If you only need a local vector database for small scale data or prototyping,# setting the uri as a local file, e.g.`./milvus.db`, is the most convenient method,# as it automatically utilizes [Milvus Lite](https://milvus.io/docs/milvus_lite.md)# to store all data in this file.## - If you have large scale of data, say more than a million vectors, you can set up a# more performant Milvus server on [Docker or Kubernetes](https://milvus.io/docs/quickstart.md).# In this setup, please use the server address and port as your uri, e.g.`http://localhost:19530`.# If you enable the authentication feature on Milvus, use# "<your_username>:<your_password>" as the token, otherwise don't set the token.## - If you use [Zilliz Cloud](https://zilliz.com/cloud), the fully managed cloud service# for Milvus, adjust the `uri` and `token`, which correspond to the# [Public Endpoint and API key](https://docs.zilliz.com/docs/on-zilliz-cloud-console#cluster-details)# in Zilliz Cloud.DEFAULT_MILVUS_CONNECTION={"uri":"http://localhost:19530",# "token": "",}Matrix=Union[List[List[float]],List[np.ndarray],np.ndarray]
[docs]defcosine_similarity(X:Matrix,Y:Matrix)->np.ndarray:"""Row-wise cosine similarity between two equal-width matrices."""iflen(X)==0orlen(Y)==0:returnnp.array([])X=np.array(X)Y=np.array(Y)ifX.shape[1]!=Y.shape[1]:raiseValueError(f"Number of columns in X and Y must be the same. X has shape {X.shape} "f"and Y has shape {Y.shape}.")try:importsimsimdassimdX=np.array(X,dtype=np.float32)Y=np.array(Y,dtype=np.float32)Z=1-np.array(simd.cdist(X,Y,metric="cosine"))returnZexceptImportError:logger.debug("Unable to import simsimd, defaulting to NumPy implementation. If you want ""to use simsimd please install with `pip install simsimd`.")X_norm=np.linalg.norm(X,axis=1)Y_norm=np.linalg.norm(Y,axis=1)# Ignore divide by zero errors run time warnings as those are handled below.withnp.errstate(divide="ignore",invalid="ignore"):similarity=np.dot(X,Y.T)/np.outer(X_norm,Y_norm)similarity[np.isnan(similarity)|np.isinf(similarity)]=0.0returnsimilarity
[docs]defmaximal_marginal_relevance(query_embedding:np.ndarray,embedding_list:list,lambda_mult:float=0.5,k:int=4,)->List[int]:"""Calculate maximal marginal relevance. Args: query_embedding: The query embedding. embedding_list: The list of embeddings. lambda_mult: The lambda multiplier. Defaults to 0.5. k: The number of results to return. Defaults to 4. Returns: List[int]: The list of indices. """ifmin(k,len(embedding_list))<=0:return[]ifquery_embedding.ndim==1:query_embedding=np.expand_dims(query_embedding,axis=0)similarity_to_query=cosine_similarity(query_embedding,embedding_list)[0]most_similar=int(np.argmax(similarity_to_query))idxs=[most_similar]selected=np.array([embedding_list[most_similar]])whilelen(idxs)<min(k,len(embedding_list)):best_score=-np.infidx_to_add=-1similarity_to_selected=cosine_similarity(embedding_list,selected)fori,query_scoreinenumerate(similarity_to_query):ifiinidxs:continueredundant_score=max(similarity_to_selected[i])equation_score=(lambda_mult*query_score-(1-lambda_mult)*redundant_score)ifequation_score>best_score:best_score=equation_scoreidx_to_add=iidxs.append(idx_to_add)selected=np.append(selected,[embedding_list[idx_to_add]],axis=0)returnidxs
[docs]def__init__(self,embedding_function:Optional[Union[EmbeddingType,List[EmbeddingType]]],collection_name:str="LangChainCollection",collection_description:str="",collection_properties:Optional[dict[str,Any]]=None,connection_args:Optional[dict[str,Any]]=None,consistency_level:str="Session",index_params:Optional[Union[dict,List[dict]]]=None,search_params:Optional[Union[dict,List[dict]]]=None,drop_old:Optional[bool]=False,auto_id:bool=False,*,primary_field:str=PRIMARY_FIELD,text_field:str=TEXT_FIELD,vector_field:Union[str,List[str]]=VECTOR_FIELD,enable_dynamic_field:bool=False,metadata_field:Optional[str]=None,partition_key_field:Optional[str]=None,partition_names:Optional[list]=None,replica_number:int=1,timeout:Optional[float]=None,num_shards:Optional[int]=None,vector_schema:Optional[Union[dict[str,Any],List[dict[str,Any]]]]=None,metadata_schema:Optional[dict[str,Any]]=None,builtin_function:Optional[Union[BaseMilvusBuiltInFunction,List[BaseMilvusBuiltInFunction]]]=None,):"""Initialize the Milvus vector store."""# Default search params when one is not provided.self.default_search_params={"FLAT":{"metric_type":"L2","params":{}},"IVF_FLAT":{"metric_type":"L2","params":{"nprobe":10}},"IVF_SQ8":{"metric_type":"L2","params":{"nprobe":10}},"IVF_PQ":{"metric_type":"L2","params":{"nprobe":10}},"HNSW":{"metric_type":"L2","params":{"ef":10}},"RHNSW_FLAT":{"metric_type":"L2","params":{"ef":10}},"RHNSW_SQ":{"metric_type":"L2","params":{"ef":10}},"RHNSW_PQ":{"metric_type":"L2","params":{"ef":10}},"IVF_HNSW":{"metric_type":"L2","params":{"nprobe":10,"ef":10}},"ANNOY":{"metric_type":"L2","params":{"search_k":10}},"SCANN":{"metric_type":"L2","params":{"search_k":10}},"AUTOINDEX":{"metric_type":"L2","params":{}},"GPU_CAGRA":{"metric_type":"L2","params":{"itopk_size":128,"search_width":4,"min_iterations":0,"max_iterations":0,"team_size":0,},},"GPU_IVF_FLAT":{"metric_type":"L2","params":{"nprobe":10}},"GPU_IVF_PQ":{"metric_type":"L2","params":{"nprobe":10}},"GPU_BRUTE_FORCE":{"metric_type":"L2","params":{"nprobe":10}},"SPARSE_INVERTED_INDEX":{"metric_type":"IP","params":{"drop_ratio_build":0.2},},"SPARSE_WAND":{"metric_type":"IP","params":{"drop_ratio_build":0.2}},}ifnotembedding_functionandnotbuiltin_function:raiseValueError("Either `embedding_function` or `builtin_function` should be provided.")self.embedding_func:Optional[Union[EmbeddingType,List[EmbeddingType]]]=self._from_list(embedding_function)self.builtin_func:Optional[Union[BaseMilvusBuiltInFunction,List[BaseMilvusBuiltInFunction]]]=self._from_list(builtin_function)self.collection_name=collection_nameself.collection_description=collection_descriptionself.collection_properties=collection_propertiesself.index_params=index_paramsself.search_params=search_paramsself.consistency_level=consistency_levelself.auto_id=auto_id# In order for a collection to be compatible, pk needs to be varcharself._primary_field=primary_field# In order for compatibility, the text field will need to be called "text"self._text_field=text_fieldself._check_vector_field(vector_field,vector_schema)ifmetadata_field:logger.warning("DeprecationWarning: `metadata_field` is about to be deprecated, ""please set `enable_dynamic_field`=True instead.")ifenable_dynamic_fieldandmetadata_field:metadata_field=Nonelogger.warning("When `enable_dynamic_field` is True, `metadata_field` is ignored.")self.enable_dynamic_field=enable_dynamic_fieldself._metadata_field=metadata_fieldself._partition_key_field=partition_key_fieldself.fields:list[str]=[]self.partition_names=partition_namesself.replica_number=replica_numberself.timeout=timeoutself.num_shards=num_shardsself.metadata_schema=metadata_schema# Create the connection to the serverifconnection_argsisNone:connection_args=DEFAULT_MILVUS_CONNECTIONself._milvus_client=MilvusClient(**connection_args,)self.alias=self.client._usingself.col:Optional[Collection]=None# Grab the existing collection if it existsifutility.has_collection(self.collection_name,using=self.alias):self.col=Collection(self.collection_name,using=self.alias,)ifself.collection_propertiesisnotNone:self.col.set_properties(self.collection_properties)# If need to drop old, drop itifdrop_oldandisinstance(self.col,Collection):self.col.drop()self.col=None# Initialize the vector storeself._init(partition_names=partition_names,replica_number=replica_number,timeout=timeout,)
def_check_vector_field(self,vector_field:Union[str,List[str]],vector_schema:Optional[Union[dict[str,Any],List[dict[str,Any]]]]=None,)->None:""" Check the validity of vector_field and vector_schema, as well as the relationships with embedding_func and builtin_func. """assertlen(self._as_list(vector_field))==len(set(self._as_list(vector_field))),"Vector field names should be unique."vector_fields_from_function=[]forbuiltin_functioninself._as_list(self.builtin_func):vector_fields_from_function.extend(self._as_list(builtin_function.output_field_names))# Check there are not overlapping fieldsassertlen(vector_fields_from_function)==len(set(vector_fields_from_function)),"When using builtin functions, there should be no overlapping fields."embedding_fields_expected=[]forfieldinself._as_list(vector_field):iffieldnotinvector_fields_from_function:embedding_fields_expected.append(field)# Number of customized fields <= number of embedding functionsiflen(embedding_fields_expected)<=len(self._as_list(self.embedding_func)):# type: ignore[arg-type]vector_fields_from_embedding=embedding_fields_expectedappending_fields=[]foriinrange(len(embedding_fields_expected),len(self._as_list(self.embedding_func)),# type: ignore[arg-type]):appending_fields.append(f"vector_{i+1}")vector_fields_from_embedding.extend(appending_fields)iflen(appending_fields)>0:logger.warning("When multiple embeddings function are used, one should provide ""matching `vector_field` names. ""Using generated vector names %s",appending_fields,)# Number of customized fields > number of embedding functionselse:raiseValueError(f"Too many custom fields: {embedding_fields_expected}."f" They cannot be mapped to a limited number of embedding functions,"f" nor do they belong to any build-in function.")assert(len(set(vector_fields_from_function)&set(vector_fields_from_embedding))==0),("Vector fields from embeddings and vector fields from builtin functions ""should not overlap.")all_vector_fields=vector_fields_from_embedding+vector_fields_from_function# For backward compatibility, the vector field needs to be called "vector",# and it can be either a list or a string.self._vector_field:Union[str,List[str]]=cast(Union[str,List[str]],self._from_list(all_vector_fields))self._vector_fields_from_embedding:List[str]=vector_fields_from_embeddingself._vector_fields_from_function:List[str]=vector_fields_from_function# Check vector schema and prepare vector schema mapself.vector_schema=vector_schemaself._vector_schema_map:Dict[str,dict]={}ifself.vector_schema:iflen(self._as_list(self.vector_schema))==1:assertlen(self._as_list(self._vector_field))==1,("When only one custom vector_schema is provided, ""it should keep the vector store has only one vector field.")vector_field_=cast(str,self._from_list(self._vector_field))vector_schema_=cast(dict,self._from_list(self.vector_schema))self._vector_schema_map[vector_field_]=vector_schema_else:ifself._is_embedding_onlyorself._is_function_only:assertlen(self._as_list(self._vector_field))==len(self._as_list(self.vector_schema)),("You should provide the same number of custom `vector_schema`s ""as the number of corresponding `vector_field`s.")else:# If both embedding and builtin functions are provided,# it must specify vector_schema for each vector field.assertlen(self._as_list(vector_field))==len(self._as_list(self.vector_schema)),("When multiple custom `vector_schema`s are provided, ""you should provide the same number of corresponding ""`vector_field`s.")forfield,vector_schemainzip(self._as_list(vector_field),self._as_list(self.vector_schema)):self._vector_schema_map[field]=vector_schemaelse:self._vector_schema_map={field:{}forfieldinself._as_list(self._vector_field)}# Check index param and prepare index param mapself._index_param_map:Dict[str,dict]={}ifself.index_params:iflen(self._as_list(self.index_params))==1:assertlen(self._as_list(self._vector_field))==1,("When only one custom index_params is provided, ""it should keep the vector store has only one vector field.")vector_field_=cast(str,self._from_list(self._vector_field))index_params_=cast(dict,self._from_list(self.index_params))self._index_param_map[vector_field_]=index_params_else:ifself._is_embedding_onlyorself._is_function_only:assertlen(self._as_list(self._vector_field))==len(self._as_list(self.index_params)),("You should provide the same number of custom `index_params`s ""as the number of corresponding `vector_field`s.")else:# If both embedding and builtin functions are provided,# it must specify index_params for each vector field.assertlen(self._as_list(vector_field))==len(self._as_list(self.index_params)),("When multiple custom `index_params`s are provided, ""you should provide the same number of corresponding ""`vector_field`s.")forfield,index_paramsinzip(self._as_list(vector_field),self._as_list(self.index_params)):self._index_param_map[field]=index_paramselse:self._index_param_map={field:{}forfieldinself._as_list(self._vector_field)}@propertydefembeddings(self)->Optional[Union[EmbeddingType,List[EmbeddingType]]]:# type: ignore"""Get embedding function(s)."""returnself.embedding_func@propertydefclient(self)->MilvusClient:"""Get client."""returnself._milvus_client@propertydefvector_fields(self)->List[str]:"""Get vector field(s)."""returnself._as_list(self._vector_field)@propertydef_is_multi_vector(self)->bool:"""Whether the sum of embedding functions and builtin functions is multi."""returnisinstance(self._vector_field,list)andlen(self._vector_field)>1@propertydef_is_multi_embedding(self)->bool:"""Whether there are multi embedding functions in this instance."""returnisinstance(self.embedding_func,list)andlen(self.embedding_func)>1@propertydef_is_multi_function(self)->bool:"""Whether there are multi builtin functions in this instance."""returnisinstance(self.builtin_func,list)andlen(self.builtin_func)>1@propertydef_is_embedding_only(self)->bool:"""Whether there are only embedding function(s) but no builtin function(s)."""return(len(self._as_list(self.embedding_func))>0# type: ignore[arg-type]andlen(self._as_list(self.builtin_func))==0)@propertydef_is_function_only(self)->bool:"""Whether there are only builtin function(s) but no embedding function(s)."""return(len(self._as_list(self.embedding_func))==0# type: ignore[arg-type]andlen(self._as_list(self.builtin_func))>0)@propertydef_is_sparse(self)->bool:"""Detect whether there is only one sparse embedding/builtin function"""ifself._is_embedding_only:embedding_func=self._as_list(self.embedding_func)# type: ignore[arg-type]iflen(embedding_func)==1andself._is_sparse_embedding(embedding_func[0]# type: ignore[arg-type]):returnTrueifself._is_function_only:builtin_func=self._as_list(self.builtin_func)iflen(builtin_func)==1andisinstance(builtin_func[0],BM25BuiltInFunction):returnTruereturnFalse@staticmethoddef_is_sparse_embedding(embeddings_function:EmbeddingType)->bool:returnisinstance(embeddings_function,BaseSparseEmbedding)def_init(self,embeddings:Optional[List[list]]=None,metadatas:Optional[list[dict]]=None,partition_names:Optional[list]=None,replica_number:int=1,timeout:Optional[float]=None,)->None:ifembeddingsisnotNone:self._create_collection(embeddings,metadatas)self._extract_fields()self._create_index()self._create_search_params()self._load(partition_names=partition_names,replica_number=replica_number,timeout=timeout,)def_create_collection(self,embeddings:List[list],metadatas:Optional[list[dict]]=None)->None:metadata_fields=self._prepare_metadata_fields(metadatas)text_fields=self._prepare_text_fields()primary_key_fields=self._prepare_primary_key_fields()vector_fields=self._prepare_vector_fields(embeddings)fields=text_fields+primary_key_fields+vector_fields+metadata_fields# Create the schema for the collectionschema=CollectionSchema(fields,description=self.collection_description,partition_key_field=self._partition_key_field,enable_dynamic_field=self.enable_dynamic_field,functions=[func.functionforfuncinself._as_list(self.builtin_func)],)# Create the collectiontry:ifself.num_shardsisnotNone:# Issue with defaults:# https://github.com/milvus-io/pymilvus/blob/59bf5e811ad56e20946559317fed855330758d9c/pymilvus/client/prepare.py#L82-L85self.col=Collection(name=self.collection_name,schema=schema,consistency_level=self.consistency_level,using=self.alias,num_shards=self.num_shards,)else:self.col=Collection(name=self.collection_name,schema=schema,consistency_level=self.consistency_level,using=self.alias,)# Set the collection properties if they existifself.collection_propertiesisnotNone:self.col.set_properties(self.collection_properties)exceptMilvusExceptionase:logger.error("Failed to create collection: %s error: %s",self.collection_name,e)raiseedef_prepare_metadata_fields(self,metadatas:Optional[list[dict]]=None)->List[FieldSchema]:fields=[]# If enable_dynamic_field, we don't need to create fields, and just pass it.ifself.enable_dynamic_field:# If both dynamic fields and partition key field are enabledifself._partition_key_fieldisnotNone:# create the partition fieldfields.append(FieldSchema(self._partition_key_field,DataType.VARCHAR,max_length=65_535))elifself._metadata_fieldisnotNone:fields.append(FieldSchema(self._metadata_field,DataType.JSON))else:# Determine metadata schemaifmetadatas:# Create FieldSchema for each entry in metadata.vector_fields:List[str]=self._as_list(self._vector_field)forkey,valueinmetadatas[0].items():# Check if the key is reservedif(keyin[self._primary_field,self._text_field,]+vector_fields):logger.error(("Failure to create collection, ""metadata key: %s is reserved."),key,)raiseValueError(f"Metadata key {key} is reserved.")# Infer the corresponding datatype of the metadataif(self.metadata_schemaandkeyinself.metadata_schema# type: ignoreand"dtype"inself.metadata_schema[key]# type: ignore):fields.append(self._get_field_schema_from_dict(key,self.metadata_schema[key]))else:dtype=infer_dtype_bydata(value)# Datatype isn't compatibleifdtype==DataType.UNKNOWNordtype==DataType.NONE:logger.error(("Failure to create collection, ""unrecognized dtype for key: %s"),key,)raiseValueError(f"Unrecognized datatype for {key}.")# Datatype is a string/varchar equivalentelifdtype==DataType.VARCHAR:kwargs={}forfunctioninself._as_list(self.builtin_func):ifisinstance(function,BM25BuiltInFunction):iffunction.input_field_names==self._text_field:kwargs=(function.get_input_field_schema_kwargs())breakfields.append(FieldSchema(key,DataType.VARCHAR,max_length=65_535,**kwargs))# infer_dtype_bydata currently can't recognize array type,# so this line can not be accessed.# This line may need to be modified in the future when# infer_dtype_bydata can recognize array type.# https://github.com/milvus-io/pymilvus/issues/2165elifdtype==DataType.ARRAY:kwargs=self.metadata_schema[key]["kwargs"]# type: ignorefields.append(FieldSchema(name=key,dtype=DataType.ARRAY,**kwargs))else:fields.append(FieldSchema(key,dtype))returnfieldsdef_prepare_text_fields(self)->List[FieldSchema]:fields=[]kwargs={}forfunctioninself._as_list(self.builtin_func):ifisinstance(function,BM25BuiltInFunction):ifself._from_list(function.input_field_names)==self._text_field:kwargs=function.get_input_field_schema_kwargs()breakfields.append(FieldSchema(self._text_field,DataType.VARCHAR,max_length=65_535,**kwargs))returnfieldsdef_prepare_primary_key_fields(self)->List[FieldSchema]:fields=[]ifself.auto_id:fields.append(FieldSchema(self._primary_field,DataType.INT64,is_primary=True,auto_id=True))else:fields.append(FieldSchema(self._primary_field,DataType.VARCHAR,is_primary=True,auto_id=False,max_length=65_535,))returnfieldsdef_prepare_vector_fields(self,embeddings:List[list])->List[FieldSchema]:fields=[]embeddings_functions:List[EmbeddingType]=self._as_list(self.embedding_func)assert(len(self._vector_fields_from_embedding)==len(embeddings_functions)==len(embeddings)),("The number of `self._vector_fields_from_embedding`, ""`embeddings_functions`, and `embeddings` should be the same."f"Got {len(self._vector_fields_from_embedding)}, "f"{len(embeddings_functions)}, and {len(embeddings)}.")# Loop through the embedding functionsforvector_field,embedding_func,embeddinginzip(self._vector_fields_from_embedding,embeddings_functions,embeddings):vector_schema=self._vector_schema_map.get(vector_field,None)ifvector_schemaand"dtype"invector_schema:fields.append(self._get_field_schema_from_dict(vector_field,vector_schema))else:ifself._is_sparse_embedding(embedding_func):fields.append(FieldSchema(vector_field,DataType.SPARSE_FLOAT_VECTOR))else:# Supports binary or float vectorsfields.append(FieldSchema(vector_field,infer_dtype_bydata(embedding[0]),dim=len(embedding[0]),))# Loop through the built-in functionsforvector_field,builtin_functioninzip(self._vector_fields_from_function,self._as_list(self.builtin_func)):vector_schema=self._vector_schema_map.get(vector_field,None)ifvector_schemaand"dtype"invector_schema:field=self._get_field_schema_from_dict(vector_field,vector_schema)elifisinstance(builtin_function,BM25BuiltInFunction):field=FieldSchema(vector_field,DataType.SPARSE_FLOAT_VECTOR)else:raiseValueError("Unsupported embedding function type: "f"{type(builtin_function)} for field: {vector_field}.")field.is_function_output=Truefields.append(field)returnfieldsdef_get_field_schema_from_dict(self,field_name:str,schema_dict:dict)->FieldSchema:assert"dtype"inschema_dict,(f"Please provide `dtype` in the schema dict. "f"Existing keys are: {schema_dict.keys()}")dtype=schema_dict.pop("dtype")kwargs=schema_dict.pop("kwargs",{})kwargs.update(schema_dict)returnFieldSchema(name=field_name,dtype=dtype,**kwargs)def_extract_fields(self)->None:"""Grab the existing fields from the Collection"""ifisinstance(self.col,Collection):schema=self.col.schemaforxinschema.fields:self.fields.append(x.name)def_get_index(self,field_name:Optional[str]=None)->Optional[dict[str,Any]]:"""Return the vector index information if it exists"""ifnotself._is_multi_vector:field_name:str=field_nameorself._vector_field# type: ignoreifisinstance(self.col,Collection):forxinself.col.indexes:ifx.field_name==field_name:returnx.to_dict()returnNonedef_get_indexes(self,field_names:Optional[List[str]]=None)->List[dict[str,Any]]:"""Return the list of vector index information"""index_list=[]ifnotfield_names:field_names=self._as_list(self._vector_field)forfield_nameinfield_names:index=self._get_index(field_name)ifindexisnotNone:index_list.append(index)returnindex_listdef_create_index(self)->None:"""Create an index on the collection"""ifisinstance(self.col,Collection):embeddings_functions:List[EmbeddingType]=self._as_list(self.embedding_func)default_index_params={"metric_type":"L2","index_type":"AUTOINDEX","params":{},}forvector_field,embeddings_funcinzip(self._vector_fields_from_embedding,embeddings_functions):ifnotself._get_index(vector_field):try:ifnotself._index_param_map.get(vector_field,None):ifself._is_sparse_embedding(embeddings_func):index_params={"metric_type":"IP","index_type":"SPARSE_INVERTED_INDEX","params":{"drop_ratio_build":0.2},}else:index_params=default_index_paramsself._index_param_map[vector_field]=index_paramselse:index_params=self._index_param_map[vector_field]self.col.create_index(vector_field,index_params=index_params,using=self.alias,)logger.debug("Successfully created an index""on %s field on collection: %s",vector_field,self.collection_name,)exceptMilvusExceptionase:logger.error("Failed to create an index on collection: %s",self.collection_name,)raiseeforvector_field,builtin_functioninzip(self._vector_fields_from_function,self._as_list(self.builtin_func)):ifnotself._get_index(vector_field):try:ifnotself._index_param_map.get(vector_field,None):ifbuiltin_function.type==FunctionType.BM25:index_params={"metric_type":"BM25","index_type":"AUTOINDEX","params":{},}else:raiseValueError("Unsupported built-in function type: "f"{builtin_function.type} for field: "f"{vector_field}.")self._index_param_map[vector_field]=index_paramselse:index_params=self._index_param_map[vector_field]self.col.create_index(vector_field,index_params=index_params,using=self.alias,)logger.debug("Successfully created an index""on %s field on collection: %s",vector_field,self.collection_name,)exceptMilvusExceptionase:logger.error("Failed to create an index on collection: %s",self.collection_name,)raiseeindex_params_list:List[dict]=[]forfieldinself._as_list(self._vector_field):index_params_list.append(self._index_param_map.get(field,{}))self.index_params=self._from_list(index_params_list)def_create_search_params(self)->None:"""Generate search params based on the current index type"""importcopyifisinstance(self.col,Collection)andself.search_paramsisNone:vector_fields:List[str]=self._as_list(self._vector_field)search_params_list:List[dict]=[]forvector_fieldinvector_fields:index=self._get_index(vector_field)ifindexisnotNone:index_type:str=index["index_param"]["index_type"]metric_type:str=index["index_param"]["metric_type"]search_params=copy.deepcopy(self.default_search_params[index_type])search_params["metric_type"]=metric_typesearch_params_list.append(search_params)self.search_params=self._from_list(search_params_list)def_load(self,partition_names:Optional[list]=None,replica_number:int=1,timeout:Optional[float]=None,)->None:"""Load the collection if available."""timeout=self.timeoutortimeoutif(isinstance(self.col,Collection)andself._get_indexes()andutility.load_state(self.collection_name,using=self.alias)==LoadState.NotLoad):self.col.load(partition_names=partition_names,replica_number=replica_number,timeout=timeout,)
[docs]defadd_texts(self,texts:Iterable[str],metadatas:Optional[List[dict]]=None,timeout:Optional[float]=None,batch_size:int=1000,*,ids:Optional[List[str]]=None,**kwargs:Any,)->List[str]:"""Insert text data into Milvus. Inserting data when the collection has not be made yet will result in creating a new Collection. The data of the first entity decides the schema of the new collection, the dim is extracted from the first embedding and the columns are decided by the first metadata dict. Metadata keys will need to be present for all inserted values. At the moment there is no None equivalent in Milvus. Args: texts (Iterable[str]): The texts to embed, it is assumed that they all fit in memory. metadatas (Optional[List[dict]]): Metadata dicts attached to each of the texts. Defaults to None. should be less than 65535 bytes. Required and work when auto_id is False. timeout (Optional[float]): Timeout for each batch insert. Defaults to None. batch_size (int, optional): Batch size to use for insertion. Defaults to 1000. ids (Optional[List[str]]): List of text ids. The length of each item Raises: MilvusException: Failure to add texts Returns: List[str]: The resulting keys for each inserted element. """texts=list(texts)ifnotself.auto_id:assertisinstance(ids,list),("A list of valid ids are required when auto_id is False. ""You can set `auto_id` to True in this Milvus instance to generate ""ids automatically, or specify string-type ids for each text.")assertlen(set(ids))==len(texts),"Different lengths of texts and unique ids are provided."assertall(isinstance(x,str)forxinids),"All ids should be strings."assertall(len(x.encode())<=65_535forxinids),"Each id should be a string less than 65535 bytes."else:ifidsisnotNone:logger.warning("The ids parameter is ignored when auto_id is True. ""The ids will be generated automatically.")embeddings_functions:List[EmbeddingType]=self._as_list(self.embedding_func)embeddings:List=[]forembedding_funcinembeddings_functions:try:embeddings.append(embedding_func.embed_documents(texts))exceptNotImplementedError:embeddings.append([embedding_func.embed_query(x)forxintexts])# Currently, it is field-wise# assuming [f1, f2] embeddings functions and [a, b, c] as texts:# embeddings = [# [f1(a), f1(b), f1(c)],# [f2(a), f2(b), f2(c)]# ]# or# embeddings = [# [f1(a), f1(b), f1(c)]# ]iflen(texts)==0:logger.debug("Nothing to insert, skipping.")return[]# Transpose it into row-wiseifself._is_multi_embedding:# transposed_embeddings = [# [f1(a), f2(a)],# [f1(b), f2(b)],# [f1(c), f2(c)]# ]transposed_embeddings=[[embeddings[j][i]forjinrange(len(embeddings))]foriinrange(len(embeddings[0]))]else:# transposed_embeddings = [# f1(a),# f1(b),# f1(c)# ]transposed_embeddings=embeddings[0]iflen(embeddings)>0else[]returnself.add_embeddings(texts=texts,embeddings=transposed_embeddings,metadatas=metadatas,timeout=timeout,batch_size=batch_size,ids=ids,**kwargs,)
[docs]defadd_embeddings(self,texts:List[str],embeddings:List[List[float]]|List[List[List[float]]],metadatas:Optional[List[dict]]=None,timeout:Optional[float]=None,batch_size:int=1000,*,ids:Optional[List[str]]=None,**kwargs:Any,)->List[str]:"""Insert text data with embeddings vectors into Milvus. This method inserts a batch of text embeddings into a Milvus collection. If the collection is not initialized, it will automatically initialize the collection based on the embeddings,metadatas, and other parameters. The embeddings are expected to be pre-generated using compatible embedding functions, and the metadata associated with each text is optional but must match the number of texts. Args: texts (List[str]): the texts to insert embeddings (List[List[float]] | List[List[List[float]]]): A vector embeddings for each text (in case of a single vector) or list of vectors for each text (in case of multi-vector) metadatas (Optional[List[dict]]): Metadata dicts attached to each of the texts. Defaults to None. should be less than 65535 bytes. Required and work when auto_id is False. timeout (Optional[float]): Timeout for each batch insert. Defaults to None. batch_size (int, optional): Batch size to use for insertion. Defaults to 1000. ids (Optional[List[str]]): List of text ids. The length of each item Raises: MilvusException: Failure to add texts and embeddings Returns: List[str]: The resulting keys for each inserted element. """ifembeddings:# row-wise -> field-wiseifnotself._is_multi_embedding:embeddings=[[embedding]forembeddinginembeddings]# type: ignore# transposed_embeddings = [# [f1(a), f2(a)],# [f1(b), f2(b)],# [f1(c), f2(c)]# ]# Transpose embeddings to make it a list of embeddings of each type.embeddings=[# type: ignore[embeddings[j][i]forjinrange(len(embeddings))]foriinrange(len(embeddings[0]))]# embeddings = [# [f1(a), f1(b), f1(c)],# [f2(a), f2(b), f2(c)]# ]# If the collection hasn't been initialized yet, perform all steps to do soifnotisinstance(self.col,Collection):kwargs={"embeddings":embeddings,"metadatas":metadatas}ifself.partition_names:kwargs["partition_names"]=self.partition_namesifself.replica_number:kwargs["replica_number"]=self.replica_numberifself.timeout:kwargs["timeout"]=self.timeoutself._init(**kwargs)insert_list:list[dict]=[]forvector_field_embeddingsinembeddings:assertlen(texts)==len(vector_field_embeddings),"Mismatched lengths of texts and embeddings."ifmetadatasisnotNone:assertlen(texts)==len(metadatas),"Mismatched lengths of texts and metadatas."fori,textinzip(range(len(texts)),texts):entity_dict={}metadata=metadatas[i]ifmetadataselse{}ifnotself.auto_id:entity_dict[self._primary_field]=ids[i]# type: ignore[index]entity_dict[self._text_field]=textforvector_field,vector_field_embeddingsinzip(# type: ignoreself._vector_fields_from_embedding,embeddings):entity_dict[vector_field]=vector_field_embeddings[i]ifself._metadata_fieldandnotself.enable_dynamic_field:entity_dict[self._metadata_field]=metadataelse:forkey,valueinmetadata.items():# if not enable_dynamic_field, skip fields not in the collection.ifnotself.enable_dynamic_fieldandkeynotinself.fields:continue# If enable_dynamic_field, all fields are allowed.entity_dict[key]=valueinsert_list.append(entity_dict)# Total insert counttotal_count=len(insert_list)pks:list[str]=[]assertisinstance(self.col,Collection)foriinrange(0,total_count,batch_size):# Grab end indexend=min(i+batch_size,total_count)batch_insert_list=insert_list[i:end]# Insert into the collection.try:res:Collectiontimeout=self.timeoutortimeoutres=self.col.insert(batch_insert_list,timeout=timeout,**kwargs)pks.extend(res.primary_keys)exceptMilvusExceptionase:logger.error("Failed to insert batch starting at entity: %s/%s",i,total_count)raiseereturnpks
def_collection_search(self,embedding_or_text:List[float]|Dict[int,float]|str,k:int=4,param:Optional[dict]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->Optional[SearchResult]:"""Perform a search on an embedding or a query text and return milvus search results. For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md Args: embedding_or_text (List[float] | Dict[int, float] | str): The embedding vector or query text being searched. k (int, optional): The amount of results to return. Defaults to 4. param (dict): The search params for the specified index. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (float, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() keyword arguments. Returns: pymilvus.client.abstract.SearchResult: Milvus search result. """ifself.colisNone:logger.debug("No existing collection to search.")returnNoneassertnotself._is_multi_vector,("_collection_search does not support multi-vector search. ""You can use _collection_hybrid_search instead.")ifparamisNone:assertlen(self._as_list(self.search_params))==1,("The number of search params is larger than 1, ""please check the search_params in this Milvus instance.")param=self._as_list(self.search_params)[0]ifself.enable_dynamic_field:output_fields=["*"]else:output_fields=self._remove_forbidden_fields(self.fields[:])col_search_res=self.col.search(data=[embedding_or_text],anns_field=self._vector_field,param=param,limit=k,expr=expr,output_fields=output_fields,timeout=self.timeoutortimeout,**kwargs,)returncol_search_resdef_collection_hybrid_search(self,query:str,k:int=4,param:Optional[dict|list[dict]]=None,expr:Optional[str]=None,fetch_k:Optional[int]=4,ranker_type:Optional[Literal["rrf","weighted"]]=None,ranker_params:Optional[dict]=None,timeout:Optional[float]=None,**kwargs:Any,)->Optional[SearchResult]:""" Perform a hybrid search on a query string and return milvus search results. For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/hybrid_search.md Args: query (str): The text being searched. k (int, optional): The amount of results to return. Defaults to 4. param (dict | list[dict], optional): The search params for the specified index. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. fetch_k (int, optional): The amount of pre-fetching results for each query. Defaults to 4. ranker_type (str, optional): The type of ranker to use. Defaults to None. ranker_params (dict, optional): The parameters for the ranker. Defaults to None. timeout (float, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.hybrid_search() keyword arguments. Returns: pymilvus.client.abstract.SearchResult: Milvus search result. """ifself.colisNone:logger.debug("No existing collection to search.")returnNonesearch_requests=[]reranker=self._create_ranker(ranker_type=ranker_type,ranker_params=ranker_paramsor{},)ifnotparam:param_list=self._as_list(self.search_params)else:assertlen(self._as_list(param))==len(self._as_list(self.search_params)),(f"The number of search params ({len(self._as_list(param))})"f" does not match the number of vector fields "f"({len(self._as_list(self._vector_field))})."f" All vector fields are: {(self._as_list(self._vector_field))},"" please provide a list of search params for each vector field.")param_list=self._as_list(param)forfield,param_dictinzip(self._vector_field,param_list):search_data:List[float]|Dict[int,float]|striffieldinself._vector_fields_from_embedding:embedding_func:EmbeddingType=self._as_list(self.embedding_func)[# type: ignoreself._vector_fields_from_embedding.index(field)]search_data=embedding_func.embed_query(query)else:search_data=queryrequest=AnnSearchRequest(data=[search_data],anns_field=field,param=param_dict,limit=fetch_k,expr=expr,)search_requests.append(request)ifself.enable_dynamic_field:output_fields=["*"]else:output_fields=self._remove_forbidden_fields(self.fields[:])col_search_res=self.col.hybrid_search(reqs=search_requests,rerank=reranker,limit=k,output_fields=output_fields,timeout=self.timeoutortimeout,**kwargs,)returncol_search_res
[docs]defsimilarity_search(self,query:str,k:int=4,param:Optional[dict|list[dict]]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->List[Document]:"""Perform a similarity search against the query string. Args: query (str): The text to search. k (int, optional): How many results to return. Defaults to 4. param (dict | list[dict], optional): The search params for the index type. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (int, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() keyword arguments. Returns: List[Document]: Document results for search. """ifself.colisNone:logger.debug("No existing collection to search.")return[]timeout=self.timeoutortimeoutres=self.similarity_search_with_score(query=query,k=k,param=param,expr=expr,timeout=timeout,**kwargs)return[docfordoc,_inres]
[docs]defsimilarity_search_by_vector(self,embedding:List[float],k:int=4,param:Optional[dict]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->List[Document]:"""Perform a similarity search against the query string. Args: embedding (List[float]): The embedding vector to search. k (int, optional): How many results to return. Defaults to 4. param (dict, optional): The search params for the index type. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (int, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() keyword arguments. Returns: List[Document]: Document results for search. """ifself.colisNone:logger.debug("No existing collection to search.")return[]timeout=self.timeoutortimeoutres=self.similarity_search_with_score_by_vector(embedding=embedding,k=k,param=param,expr=expr,timeout=timeout,**kwargs)return[docfordoc,_inres]
[docs]defsimilarity_search_with_score(self,query:str,k:int=4,param:Optional[dict|list[dict]]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->List[Tuple[Document,float]]:"""Perform a search on a query string and return results with score. For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md Args: query (str): The text being searched. k (int, optional): The amount of results to return. Defaults to 4. param (dict | list[dict], optional): The search params for the specified index. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (float, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() or hybrid_search() keyword arguments. Returns: List[Tuple[Document, float]]: List of result doc and score. """ifself.colisNone:logger.debug("No existing collection to search.")return[]ifself._is_multi_vector:col_search_res=self._collection_hybrid_search(query=query,k=k,param=param,expr=expr,timeout=timeout,**kwargs)else:assertlen(self._as_list(param))<=1,("When there is only one vector field, you can not provide multiple ""search param dicts.")param=cast(Optional[dict],self._from_list(param))if(len(self._as_list(self.embedding_func))==1# type: ignore[arg-type]andlen(self._as_list(self.builtin_func))==0):embedding=self._as_list(self.embedding_func)[0].embed_query(query)# type: ignorecol_search_res=self._collection_search(embedding_or_text=embedding,k=k,param=param,expr=expr,timeout=timeout,**kwargs,)elif(len(self._as_list(self.embedding_func))==0# type: ignore[arg-type]andlen(self._as_list(self.builtin_func))==1):col_search_res=self._collection_search(embedding_or_text=query,k=k,param=param,expr=expr,timeout=timeout,**kwargs,)else:raiseRuntimeError("Check either it's multi vectors or single vector with ""only one embedding/builtin function.")returnself._parse_documents_from_search_results(col_search_res)
[docs]defsimilarity_search_with_score_by_vector(self,embedding:List[float]|Dict[int,float],k:int=4,param:Optional[dict]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->List[Tuple[Document,float]]:"""Perform a search on an embedding and return results with score. For more information about the search parameters, take a look at the pymilvus documentation found here: https://milvus.io/api-reference/pymilvus/v2.5.x/ORM/Collection/search.md Args: embedding (List[float] | Dict[int, float]): The embedding vector being searched. k (int, optional): The amount of results to return. Defaults to 4. param (dict): The search params for the specified index. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (float, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() keyword arguments. Returns: List[Tuple[Document, float]]: Result doc and score. """col_search_res=self._collection_search(embedding_or_text=embedding,k=k,param=param,expr=expr,timeout=timeout,**kwargs,)returnself._parse_documents_from_search_results(col_search_res)
[docs]defmax_marginal_relevance_search(self,query:str,k:int=4,fetch_k:int=20,lambda_mult:float=0.5,param:Optional[dict]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->List[Document]:"""Perform a search and return results that are reordered by MMR. Args: query (str): The text being searched. k (int, optional): How many results to give. Defaults to 4. fetch_k (int, optional): Total results to select k from. Defaults to 20. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5 param (dict, optional): The search params for the specified index. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (float, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() keyword arguments. Returns: List[Document]: Document results for search. """ifself.colisNone:logger.debug("No existing collection to search.")return[]assert(len(self._as_list(self.embedding_func))==1# type: ignore[arg-type]),"You must set only one embedding function for MMR search."iflen(self._vector_fields_from_function)>0:logger.warning("MMR search will only use the embedding function, ""without the built-in functions.")embedding=self._as_list(self.embedding_func)[0].embed_query(query)# type: ignoretimeout=self.timeoutortimeoutreturnself.max_marginal_relevance_search_by_vector(embedding=embedding,k=k,fetch_k=fetch_k,lambda_mult=lambda_mult,param=param,expr=expr,timeout=timeout,**kwargs,)
[docs]defmax_marginal_relevance_search_by_vector(self,embedding:list[float]|dict[int,float],k:int=4,fetch_k:int=20,lambda_mult:float=0.5,param:Optional[dict]=None,expr:Optional[str]=None,timeout:Optional[float]=None,**kwargs:Any,)->List[Document]:"""Perform a search and return results that are reordered by MMR. Args: embedding (list[float] | dict[int, float]): The embedding vector being searched. k (int, optional): How many results to give. Defaults to 4. fetch_k (int, optional): Total results to select k from. Defaults to 20. lambda_mult: Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity. Defaults to 0.5 param (dict, optional): The search params for the specified index. Defaults to None. expr (str, optional): Filtering expression. Defaults to None. timeout (float, optional): How long to wait before timeout error. Defaults to None. kwargs: Collection.search() keyword arguments. Returns: List[Document]: Document results for search. """col_search_res=self._collection_search(embedding_or_text=embedding,k=fetch_k,param=param,expr=expr,timeout=timeout,**kwargs,)ifcol_search_resisNone:return[]ids=[]documents=[]scores=[]forresultincol_search_res[0]:data={x:result.entity.get(x)forxinresult.entity.fields}doc=self._parse_document(data)documents.append(doc)scores.append(result.score)ids.append(result.id)vectors=self.col.query(# type: ignore[union-attr]expr=f"{self._primary_field} in {ids}",output_fields=[self._primary_field,self._vector_field],timeout=timeout,)# Reorganize the results from query to match search order.vectors={x[self._primary_field]:x[self._vector_field]forxinvectors}ordered_result_embeddings=[vectors[x]forxinids]# Get the new order of results.new_ordering=maximal_marginal_relevance(np.array(embedding),ordered_result_embeddings,k=k,lambda_mult=lambda_mult)# Reorder the values and return.ret=[]forxinnew_ordering:# Function can return -1 indexifx==-1:breakelse:ret.append(documents[x])returnret
def_select_relevance_score_fn(self)->Callable[[float],float]:""" The 'correct' relevance function may differ depending on a few things, including: - the distance / similarity metric used by the VectorStore - the scale of your embeddings (OpenAI's are unit normed. Many others are not!) - embedding dimensionality - etc. """ifnotself.colornotself.col.indexes:raiseValueError("No index params provided. Could not determine relevance function.")ifself._is_multi_embeddingorself._is_multi_function:raiseValueError("No supported normalization function for multi vectors. ""Could not determine relevance function.")ifself._is_sparse:raiseValueError("No supported normalization function for sparse indexes. ""Could not determine relevance function.")def_map_l2_to_similarity(l2_distance:float)->float:"""Return a similarity score on a scale [0, 1]. It is recommended that the original vector is normalized, Milvus only calculates the value before applying square root. l2_distance range: (0 is most similar, 4 most dissimilar) See https://milvus.io/docs/metric.md?tab=floating#Euclidean-distance-L2 """return1-l2_distance/4.0def_map_ip_to_similarity(ip_score:float)->float:"""Return a similarity score on a scale [0, 1]. It is recommended that the original vector is normalized, ip_score range: (1 is most similar, -1 most dissimilar) See https://milvus.io/docs/metric.md?tab=floating#Inner-product-IP https://milvus.io/docs/metric.md?tab=floating#Cosine-Similarity """return(ip_score+1)/2.0ifnotself.index_params:logger.warning("No index params provided. Could not determine relevance function. ""Use L2 distance as default.")return_map_l2_to_similarityindexes_params=self._as_list(self.index_params)iflen(indexes_params)>1:raiseValueError("No supported normalization function for multi vectors. ""Could not determine relevance function.")# In the left case, the len of indexes_params is 1.metric_type=indexes_params[0]["metric_type"]ifmetric_type=="L2":return_map_l2_to_similarityelifmetric_typein["IP","COSINE"]:return_map_ip_to_similarityelse:raiseValueError("No supported normalization function"f" for metric type: {metric_type}.")
[docs]defdelete(# type: ignore[no-untyped-def]self,ids:Optional[List[str]]=None,expr:Optional[str]=None,**kwargs:str):"""Delete by vector ID or boolean expression. Refer to [Milvus documentation](https://milvus.io/docs/delete_data.md) for notes and examples of expressions. Args: ids: List of ids to delete. expr: Boolean expression that specifies the entities to delete. kwargs: Other parameters in Milvus delete api. """ifisinstance(ids,list)andlen(ids)>0:ifexprisnotNone:logger.warning("Both ids and expr are provided. ""Ignore expr and delete by ids.")expr=f"{self._primary_field} in {ids}"else:assertisinstance(expr,str),"Either ids list or expr string must be provided."returnself.col.delete(expr=expr,**kwargs)# type: ignore[union-attr]
[docs]@classmethoddeffrom_texts(cls,texts:List[str],embedding:Optional[Union[EmbeddingType,List[EmbeddingType]]],metadatas:Optional[List[dict]]=None,collection_name:str="LangChainCollection",connection_args:Optional[Dict[str,Any]]=None,consistency_level:str="Session",index_params:Optional[Union[dict,List[dict]]]=None,search_params:Optional[Union[dict,List[dict]]]=None,drop_old:bool=False,*,ids:Optional[List[str]]=None,auto_id:bool=False,builtin_function:Optional[Union[BaseMilvusBuiltInFunction,List[BaseMilvusBuiltInFunction]]]=None,**kwargs:Any,)->Milvus:"""Create a Milvus collection, indexes it with HNSW, and insert data. Args: texts (List[str]): Text data. embedding (Optional[Union[Embeddings, BaseSparseEmbedding]]): Embedding function. metadatas (Optional[List[dict]]): Metadata for each text if it exists. Defaults to None. collection_name (str, optional): Collection name to use. Defaults to "LangChainCollection". connection_args (dict[str, Any], optional): Connection args to use. Defaults to DEFAULT_MILVUS_CONNECTION. consistency_level (str, optional): Which consistency level to use. Defaults to "Session". index_params (Optional[dict], optional): Which index_params to use. Defaults to None. search_params (Optional[dict], optional): Which search params to use. Defaults to None. drop_old (Optional[bool], optional): Whether to drop the collection with that name if it exists. Defaults to False. ids (Optional[List[str]]): List of text ids. Defaults to None. auto_id (bool): Whether to enable auto id for primary key. Defaults to False. If False, you need to provide text ids (string less than 65535 bytes). If True, Milvus will generate unique integers as primary keys. builtin_function (Optional[Union[BaseMilvusBuiltInFunction, List[BaseMilvusBuiltInFunction]]]): Built-in function to use. Defaults to None. **kwargs: Other parameters in Milvus Collection. Returns: Milvus: Milvus Vector Store """ifisinstance(ids,list)andlen(ids)>0:ifauto_id:logger.warning("Both ids and auto_id are provided. ""Ignore auto_id and use ids.")auto_id=Falseelse:auto_id=Truevector_db=cls(embedding_function=embedding,collection_name=collection_name,connection_args=connection_args,consistency_level=consistency_level,index_params=index_params,search_params=search_params,drop_old=drop_old,auto_id=auto_id,builtin_function=builtin_function,**kwargs,)vector_db.add_texts(texts=texts,metadatas=metadatas,ids=ids)returnvector_db
[docs]defadd_documents(self,documents:List[Document],**kwargs:Any)->List[str]:"""Run more documents through the embeddings and add to the vectorstore. Args: documents: Documents to add to the vectorstore. Returns: List of IDs of the added texts. """# TODO: Handle the case where the user doesn't provide ids on the Collectiontexts=[doc.page_contentfordocindocuments]metadatas=[doc.metadatafordocindocuments]returnself.add_texts(texts,metadatas,**kwargs)
[docs]defget_pks(self,expr:str,**kwargs:Any)->List[int]|None:"""Get primary keys with expression Args: expr: Expression - E.g: "id in [1, 2]", or "title LIKE 'Abc%'" Returns: List[int]: List of IDs (Primary Keys) """ifself.colisNone:logger.debug("No existing collection to get pk.")returnNonetry:query_result=self.col.query(expr=expr,output_fields=[self._primary_field])exceptMilvusExceptionasexc:logger.error("Failed to get ids: %s error: %s",self.collection_name,exc)raiseexcpks=[item.get(self._primary_field)foriteminquery_result]returnpks
[docs]defupsert(# type: ignoreself,ids:Optional[List[str]]=None,documents:List[Document]|None=None,**kwargs:Any,)->List[str]|None:"""Update/Insert documents to the vectorstore. Args: ids: IDs to update - Let's call get_pks to get ids with expression \n documents (List[Document]): Documents to add to the vectorstore. Returns: List[str]: IDs of the added texts. """ifdocumentsisNoneorlen(documents)==0:logger.debug("No documents to upsert.")returnNoneifidsisnotNoneandlen(ids):try:self.delete(ids=ids)exceptMilvusException:passtry:returnself.add_documents(documents=documents,**kwargs)exceptMilvusExceptionasexc:logger.error("Failed to upsert entities: %s error: %s",self.collection_name,exc)raiseexc
@staticmethoddef_as_list(value:Optional[Union[T,List[T]]])->List[T]:"""Try to cast a value to a list"""ifnotvalue:return[]return[value]ifnotisinstance(value,list)elsevalue@staticmethoddef_from_list(value:Optional[Union[T,List[T]]])->Optional[Union[T,List[T]]]:"""Try to cast a list to a single value"""ifisinstance(value,list)andlen(value)==1:returnvalue[0]returnvaluedef_create_ranker(self,ranker_type:Optional[Literal["rrf","weighted"]],ranker_params:dict,)->Union[WeightedRanker,RRFRanker]:"""A Ranker factory method"""default_weights=[1.0]*len(self._as_list(self._vector_field))ifnotranker_type:returnWeightedRanker(*default_weights)ifranker_type=="weighted":weights=ranker_params.get("weights",default_weights)returnWeightedRanker(*weights)elifranker_type=="rrf":k=ranker_params.get("k",None)ifk:returnRRFRanker(k)returnRRFRanker()else:logger.error("Ranker %s does not exist. ""Please use on of the following rankers: %s, %s",ranker_type,"weighted","rrf",)raiseValueError("Unrecognized ranker of type %s",ranker_type)def_remove_forbidden_fields(self,fields:List[str])->List[str]:"""Bm25 function fields are not allowed as output fields in Milvus."""forbidden_fields=[]forbuiltin_functioninself._as_list(self.builtin_func):ifbuiltin_function.type==FunctionType.BM25:forbidden_fields.extend(self._as_list(builtin_function.output_field_names))return[fieldforfieldinfieldsiffieldnotinforbidden_fields]