
    NgǤ                        d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ  ej        e          Zd
dddddZ eddd           G d de                      ZdS )    )annotationsN)AnyIterableListOptionalTupleUnion)uuid4)
deprecated)Document)
Embeddings)VectorStore)maximal_marginal_relevance	localhost19530 F)hostportuserpasswordsecurez0.2.0z1.0z"langchain_milvus.MilvusVectorStore)sinceremovalalternative_importc                     e Zd ZdZ	 	 	 	 	 	 	 	 	 dhddd	dddd
ddd	did*Zedjd,            Zdkd.Z	 	 	 	 	 dldmd3Z	 dndod5Z	dpd6Z
dqd7Zdpd8Zdpd9Z	 	 	 drdsd:Z	 	 	 dtdd<dudFZ	 	 	 	 dvdwdMZ	 	 	 	 dvdxdPZ	 	 	 	 dvdydRZ	 	 	 	 dvdzdSZ	 	 	 	 	 	 d{d|dYZ	 	 	 	 	 	 d{d}d[Z	 d~dd\Zeddeddddfdd<dd^            ZddaZddcZ	 	 d~ddgZdS )Milvusa  `Milvus` vector store.

    You need to install `pymilvus` and run Milvus.

    See the following documentation for how to run a Milvus instance:
    https://milvus.io/docs/install_standalone-docker.md

    If looking for a hosted Milvus, take a look at this documentation:
    https://zilliz.com/cloud and make use of the Zilliz vectorstore found in
    this project.

    IF USING L2/IP metric, IT IS HIGHLY SUGGESTED TO NORMALIZE YOUR DATA.

    Args:
        embedding_function (Embeddings): Function used to embed the text.
        collection_name (str): Which Milvus collection to use. Defaults to
            "LangChainCollection".
        collection_description (str): The description of the collection. Defaults to
            "".
        collection_properties (Optional[dict[str, any]]): The collection properties.
            Defaults to None.
            If set, will override collection existing properties.
            For example: {"collection.ttl.seconds": 60}.
        connection_args (Optional[dict[str, any]]): The connection args used for
            this class comes in the form of a dict.
        consistency_level (str): The consistency level to use for a collection.
            Defaults to "Session".
        index_params (Optional[dict]): Which index params to use. Defaults to
            HNSW/AUTOINDEX depending on service.
        search_params (Optional[dict]): Which search params to use. Defaults to
            default of index.
        drop_old (Optional[bool]): Whether to drop the current collection. Defaults
            to False.
        auto_id (bool): Whether to enable auto id for primary key. Defaults to False.
            If False, you needs to provide text ids (string less than 65535 bytes).
            If True, Milvus will generate unique integers as primary keys.
        primary_field (str): Name of the primary key field. Defaults to "pk".
        text_field (str): Name of the text field. Defaults to "text".
        vector_field (str): Name of the vector field. Defaults to "vector".
        metadata_field (str): Name of the metadata field. Defaults to None.
            When metadata_field is specified,
            the document's metadata will store as json.

    The connection args used for this class comes in the form of a dict,
    here are a few of the options:
        address (str): The actual address of Milvus
            instance. Example address: "localhost:19530"
        uri (str): The uri of Milvus instance. Example uri:
            "http://randomwebsite:19530",
            "tcp:foobarsite:19530",
            "https://ok.s3.south.com:19530".
        host (str): The host of Milvus instance. Default at "localhost",
            PyMilvus will fill in the default host if only port is provided.
        port (str/int): The port of Milvus instance. Default at 19530, PyMilvus
            will fill in the default port if only host is provided.
        user (str): Use which user to connect to Milvus instance. If user and
            password are provided, we will add related header in every RPC call.
        password (str): Required when user is provided. The password
            corresponding to the user.
        secure (bool): Default is false. If set to true, tls will be enabled.
        client_key_path (str): If use tls two-way authentication, need to
            write the client.key path.
        client_pem_path (str): If use tls two-way authentication, need to
            write the client.pem path.
        ca_pem_path (str): If use tls two-way authentication, need to write
            the ca.pem path.
        server_pem_path (str): If use tls one-way authentication, need to
            write the server.pem path.
        server_name (str): If use tls, need to write the common name.

    Example:
        .. code-block:: python

        from langchain_community.vectorstores import Milvus
        from langchain_community.embeddings import OpenAIEmbeddings

        embedding = OpenAIEmbeddings()
        # Connect to a milvus instance on localhost
        milvus_store = Milvus(
            embedding_function = Embeddings,
            collection_name = "LangChainCollection",
            drop_old = True,
            auto_id = True
        )

    Raises:
        ValueError: If the pymilvus python package is not installed.
    LangChainCollectionr   NSessionFpktextvector   )	primary_field
text_fieldvector_fieldmetadata_fieldpartition_key_fieldpartition_namesreplica_numbertimeout
num_shardsembedding_functionr   collection_namestrcollection_descriptioncollection_propertiesOptional[dict[str, Any]]connection_argsconsistency_levelindex_paramsOptional[dict]search_paramsdrop_oldOptional[bool]auto_idboolr#   r$   r%   r&   Optional[str]r'   r(   Optional[list]r)   intr*   Optional[float]r+   Optional[int]c       	           	 ddl m}m} n# t          $ r t          d          w xY wdddiddddiddddiddddiddddiddddiddddiddddd	ddd
diddd
diddi ddddddddddddiddddidd| _        || _        || _        || _        || _        || _	        || _
        || _        |
| _        || _        || _        || _        || _        || _        g | _        || _        || _        || _        || _        |t.          }|                     |          | _        d| _        |                    | j        | j                  rB || j        | j                  | _        | j        | j                            | j                   |	r5t;          | j        |          r | j                                         d| _        |                     |||           dS )z#Initialize the Milvus vector store.r   
CollectionutilityzXCould not import pymilvus python package. Please install it with `pip install pymilvus`.L2nprobe
   )metric_typeparamsef)rE   rI   search_k      )
itopk_sizesearch_widthmin_iterationsmax_iterations	team_size)IVF_FLATIVF_SQ8IVF_PQHNSW
RHNSW_FLATRHNSW_SQRHNSW_PQIVF_HNSWANNOYSCANN	AUTOINDEX	GPU_CAGRAGPU_IVF_FLAT
GPU_IVF_PQNusingr(   r)   r*   ) pymilvusrB   rC   ImportErrordefault_search_paramsembedding_funcr-   r/   r0   r4   r6   r3   r9   _primary_field_text_field_vector_field_metadata_field_partition_key_fieldfieldsr(   r)   r*   r+   DEFAULT_MILVUS_CONNECTION_create_connection_aliasaliascolhas_collectionset_properties
isinstancedrop_init)selfr,   r-   r/   r0   r2   r3   r4   r6   r7   r9   r#   r$   r%   r&   r'   r(   r)   r*   r+   rB   rC   s                         c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/milvus.py__init__zMilvus.__init__y   s   0	444444444 	 	 	A  	 )-2GG'+"~FF&*xnEE$(T2J??*.4*EE(,r
CC(,r
CC(,2R8P8PQQ%)j"5EFF%)j"5EFF)-<<#"%$%&'&'!" 	 	 -1XrNKK*.8R.II/&
 &
"4 1.&<#%:"(*!2 ,%)-$7!!#.,$ "7O22?CC
)- !!$"6dj!II 	D!z$j  DH )5''(BCCC 	
48Z88 	HMMOOODH 	

+) 	 	
 	
 	
 	
 	
s    %returnc                    | j         S N)rf   )rv   s    rw   
embeddingszMilvus.embeddings   s    ""    dictc                   ddl m}m} |                    dd          }|                    dd          }|                    dd          }|                    dd          }|                    dd          }|%|#t	          |          d	z   t	          |          z   }	n||                    d
          r|                    d
          d         }	n}|                    d          r|                    d          d         }	nLt                              d|           t          d|          ||}	nd}	t          
                    d           ||}
nd}
|	|                                D ]p}|                    |d                   }|d         rKd|v rG|d         |	k    r;d|v r7|d         |
k    r+t          
                    d|d                    |d         c S qt                      j        }	  |j        dd|i| t          
                    d|           |S # |$ r"}t                              d|           |d}~ww xY w)z+Create the connection to the Milvus server.r   )MilvusExceptionconnectionsr   Nr   addressurir   :zhttps://r"   zhttp://zInvalid Milvus URI: %sz/Missing standard address type for reuse attemptr   zUsing previous connection: %sro   z Created new connection using: %sz)Failed to create new connection using: %s )rc   r   r   getr.   
startswithsplitloggererror
ValueErrordebuglist_connectionsget_connection_addrr
   hexconnect)rv   r2   r   r   r   r   r   r   r   given_addresstmp_userconaddrro   es                  rw   rn   zMilvus._create_connection_alias   s   99999999 $''55 / 3 3FD A A&**9d;;"&&ud33""6400  0IIOc$ii7MM_~~j)) @ #		* 5 5a 8	** @ #		) 4 4Q 75s;;; !93??? #MM MLLJKKK HHH $"3355 
" 
""66s1v>>F""d**iM994f11LL!@#a&IIIq6MMM 	K??e????LL;UCCCL 	 	 	LLDeLLLG	s   0+H I!H>>Ir|   	metadatasOptional[list[dict]]Nonec                    ||                      ||           |                                  |                                  |                                  |                     |||           d S )Nrb   )_create_collection_extract_fields_create_index_create_search_params_load)rv   r|   r   r(   r)   r*   s         rw   ru   zMilvus._init#  s     !##J	:::""$$$

+) 	 	
 	
 	
 	
 	
r}   listc           	     N   ddl m}m}m}m}m} ddlm} t          |d                   }	g }
| j	        *|

                     || j	        |j                             n|r|d                                         D ]\  }} ||          }||j        k    s||j        k    r.t                              d|           t#          d| d          ||j        k    r'|

                     |||j        d                     |

                     |||                     |

                     || j        |j        d                     | j        r-|

                     || j        |j        d	d	
                     n-|

                     || j        |j        d	dd                     |

                     || j         ||d                   |	                      ||
| j        | j                  }	 | j        * || j        || j        | j        | j                  | _        n# || j        || j        | j                  | _        | j        !| j                             | j                   d S d S # |$ r(}t                              d| j        |           |d }~ww xY w)Nr   )rB   CollectionSchemaDataTypeFieldSchemar   )infer_dtype_bydataz<Failure to create collection, unrecognized dtype for key: %szUnrecognized datatype for .  )
max_lengthT)
is_primaryr9   F)r   r9   r   )dim)descriptionr'   )nameschemar3   ra   r+   )r   r   r3   ra   z)Failed to create collection: %s error: %s)!rc   rB   r   r   r   r   pymilvus.orm.typesr   lenrj   appendJSONitemsUNKNOWNNONEr   r   r   VARCHARrh   r9   rg   INT64ri   r/   rk   r+   r-   r3   ro   rp   r0   rr   )rv   r|   r   rB   r   r   r   r   r   r   rl   keyvaluedtyper   r   s                   rw   r   zMilvus._create_collection6  s   	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	:99999 *Q-  +MM++d&:HMJJKKKK  ?"+A,"4"4"6"6 ? ?JC..u55E 000EX]4J4J!A     ))Lc)L)L)LMMM("222'KX-=&QQQ    kk#u&=&=>>>> 	K((*:vNNN	
 	
 	
 < 	MM'DRV      MM'$#!%     	K*,>,>z!},M,MSVWWW	
 	
 	

 "!3 $ 9
 
 
	* &:-!&*&<*#   &:-!&*&<*	   )5''(BCCCCC 65 	 	 	LL;T=QST   G		s   9A:I7 7J$<#JJ$c                    ddl m} t          | j        |          r5| j        j        }|j        D ]#}| j                            |j                   "dS dS )z,Grab the existing fields from the Collectionr   rB   N)rc   rB   rs   rp   r   rl   r   r   )rv   rB   r   xs       rw   r   zMilvus._extract_fields  sq    ''''''dh
++ 	+X_F] + +""16****	+ 	++ +r}   c                    ddl m} t          | j        |          r5| j        j        D ](}|j        | j        k    r|                                c S )dS )z0Return the vector index information if it existsr   r   N)rc   rB   rs   rp   indexes
field_nameri   to_dict)rv   rB   r   s      rw   
_get_indexzMilvus._get_index  sj    ''''''dh
++ 	'X% ' '<4#55599;;&&& 6tr}   c                   ddl m}m} t          | j        |          r|                                 	 | j        dddddd	| _        	 | j                            | j        | j        | j	        
           nB# |$ r: ddi d	| _        | j                            | j        | j        | j	        
           Y nw xY wt                              d| j                   dS # |$ r'}t                              d| j                   |d}~ww xY wdS dS )z Create a index on the collectionr   rB   r   NrD   rU      @   )MefConstruction)rG   
index_typerH   )r4   ra   r\   z/Successfully created an index on collection: %sz+Failed to create an index on collection: %s)rc   rB   r   rs   rp   r   r4   create_indexri   ro   r   r   r-   r   )rv   rB   r   r   s       rw   r   zMilvus._create_index  s   88888888dh
++ '	0A0A0I&$,'+&,()R"@"@) )D%H))*%)%6"j *     '    (,&1"$) )D%
 H))*%)%6"j *      E(    
 #   A4CW   	G'	 '	0I0Is;   C 	,A6 5C 6<B52C 4B55#C D"DDc                    ddl m} t          | j        |          rW| j        R|                                 }|>|d         d         }|d         d         }| j        |         | _        || j        d<   dS dS dS dS )z6Generate search params based on the current index typer   r   Nindex_paramr   rG   )rc   rB   rs   rp   r6   r   re   )rv   rB   indexr   rG   s        rw   r   zMilvus._create_search_params  s    ''''''dh
++ 	@0B0JOO%%E "'"6|"D
#(#7#F%)%?
%K"4?"=111	@ 	@0J0J  r}   c                    ddl m}m} ddlm} | j        p|}t          | j        |          r]|                                 K|	                    | j
        | j                  |j        k    r#| j                            |||           dS dS dS dS )z!Load the collection if available.r   rA   )	LoadStateNr`   rb   )rc   rB   rC   pymilvus.client.typesr   r*   rs   rp   r   
load_stater-   ro   NotLoadload)rv   r(   r)   r*   rB   rC   r   s          rw   r   zMilvus._load  s     	10000000333333,)'tx,,
	!!-""4#7tz"JJ ! ! HMM /-      
	 
	--! !r}     idstextsIterable[str]Optional[List[dict]]
batch_sizer   Optional[List[str]]kwargsr   	List[str]c                   ddl m}m} t          |          } j        syt          |t                    s
J d            t          t          |                    t          |          k    s
J d            t          d |D                       s
J d            	  j	        
                    |          }	n# t          $ r  fd|D             }	Y nw xY wt          |	          dk    rt                              d           g S t           j        |          sE|	|d	} j        r
 j        |d
<    j        r
 j        |d<    j        r
 j        |d<     j        di |  j        | j        |	i j        s
| j        <    j        4|D ]0}
                     j        g                               |
           1ny|w|D ]t}
|
                                D ]]\  }} j        r fd j        D             nd  j        D             }||v r)                    |g                               |           ^u j                 }t          |          }g }t           j        |          sJ t7          d||          D ]t9          |z   |          fd j        D             }	  j        p|}  j        j        |fd|i|}|                    |j                   d# |$ r#}t                               d|           |d}~ww xY w|S )a  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.
        r   r   z7A list of valid ids are required when auto_id is False.z7Different lengths of texts and unique ids are provided.c              3  `   K   | ])}t          |                                          d k    V  *dS )r   N)r   encode.0r   s     rw   	<genexpr>z#Milvus.add_texts.<locals>.<genexpr>/  sF        ./AHHJJ6)     r}   z1Each id should be a string less than 65535 bytes.c                D    g | ]}j                             |          S r   )rf   embed_queryr   r   rv   s     rw   
<listcomp>z$Milvus.add_texts.<locals>.<listcomp>6  s*    LLL$-99!<<LLLr}   zNothing to insert, skipping.)r|   r   r(   r)   r*   Nc                *    g | ]}|j         k    |S r   )rg   r   s     rw   r   z$Milvus.add_texts.<locals>.<listcomp>Y  s&    PPP1qD<O7O7OQ7O7O7Or}   c                    g | ]}|S r   r   r   s     rw   r   z$Milvus.add_texts.<locals>.<listcomp>[  s    !9!9!9!!9!9!9r}   c                8    g | ]}|v |                  S r   r   )r   r   endiinsert_dicts     rw   r   z$Milvus.add_texts.<locals>.<listcomp>k  s7       *+a;>N>NAqu%>N>N>Nr}   z0Failed to insert batch starting at entity: %s/%sr   )!rc   rB   r   r   r9   rs   r   setallrf   embed_documentsNotImplementedErrorr   r   rp   r(   r)   r*   ru   rh   ri   rg   rj   
setdefaultr   r   rl   rangemininsertextendprimary_keysr   )rv   r   r   r*   r   r   r   rB   r   r|   dr   r   keysvectorstotal_countpksinsert_listresr   r   r   r   s   `                   @@@rw   	add_textszMilvus.add_texts   sT   J 	98888888U| 		CT  I IHI I I s3xx==C% %   H     36     C CBC C C	M,<<UCCJJ" 	M 	M 	MLLLLeLLLJJJ	M z??aLL7888I $(J// 	!$.YGGF# A,0,@()" ?+/+>'(| 1$(Ly!DJ      e
(

 | 	3/2K+,+ K K&&t';R@@GGJJJJK $" J JA&'ggii J J
U  $|:PPPPPPPP!9!9T[!9!9!9 
 $;;'223;;BB5IIIJ $D$67'll$(J/////q+z22 	 	Aa*nk22C     /3{  K	,1'%dhokMM7MfMM

3+,,,,"   F;   	
 
s*   B8 8CC%8KL#LLrL   querykparamexprList[Document]c           	         | j         t                              d           g S | j        p|} | j        d|||||d|}d |D             S )an  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, 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.
        N!No existing collection to search.)r   r   r   r  r*   c                    g | ]\  }}|S r   r   r   doc_s      rw   r   z,Milvus.similarity_search.<locals>.<listcomp>      &&&Q&&&r}   r   )rp   r   r   r*   similarity_search_with_score)rv   r   r   r   r  r*   r   r   s           rw   similarity_searchzMilvus.similarity_search{  sw    0 8LL<===I,)'/d/ 
1Eg
 
IO
 
 '&#&&&&r}   	embeddingList[float]c           	         | j         t                              d           g S | j        p|} | j        d|||||d|}d |D             S )a  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.
        Nr  r  r   r   r  r*   c                    g | ]\  }}|S r   r   r  s      rw   r   z6Milvus.similarity_search_by_vector.<locals>.<listcomp>  r	  r}   r   )rp   r   r   r*   &similarity_search_with_score_by_vector)rv   r  r   r   r  r*   r   r   s           rw   similarity_search_by_vectorz"Milvus.similarity_search_by_vector  sw    0 8LL<===I,)'9d9 
1Eg
 
QW
 
 '&#&&&&r}   List[Tuple[Document, float]]c           	         | j         t                              d           g S | j                            |          }| j        p|} | j        d|||||d|}|S )aJ  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.2.6/Collection/search().md

        Args:
            query (str): The 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:
            List[float], List[Tuple[Document, any, any]]:
        Nr  r  r   )rp   r   r   rf   r   r*   r  )	rv   r   r   r   r  r*   r   r  r   s	            rw   r
  z#Milvus.similarity_search_with_score  s    8 8LL<===I '33E::	,)'9d9 
1Eg
 
QW
 
 
r}   c                   | j         t                              d           g S || j        }| j        dd         }|                    | j                   | j        p|} | j         j        d|g| j        |||||d|}g }	|d         D ]Cfd|D             }
| 	                    |
          }|j
        f}|	                    |           D|	S )ah  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.2.6/Collection/search().md

        Args:
            embedding (List[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.
        Nr  data
anns_fieldr   limitr  output_fieldsr*   r   c                F    i | ]}|j                             |          S r   entityr   r   r   results     rw   
<dictcomp>zAMilvus.similarity_search_with_score_by_vector.<locals>.<dictcomp>  +    CCCAv}((++CCCr}   r   )rp   r   r   r6   rl   removeri   r*   search_parse_documentscorer   )rv   r  r   r   r  r*   r   r  r   retr  r  pairr  s                @rw   r  z-Milvus.similarity_search_with_score_by_vector  s   8 8LL<===I=&E AAAT/000,)'dho 	
)'	
 	
 	
 	
 !f 	 	FCCCC]CCCD&&t,,C&DJJt
r}            ?fetch_klambda_multfloatc                    | j         t                              d           g S | j                            |          }	| j        p|} | j        d|	||||||d|S )a  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.
        Nr  )r  r   r*  r+  r   r  r*   r   )rp   r   r   rf   r   r*   'max_marginal_relevance_search_by_vector)
rv   r   r   r*  r+  r   r  r*   r   r  s
             rw   max_marginal_relevance_searchz$Milvus.max_marginal_relevance_search!  s    B 8LL<===I'33E::	,)';t; 	
#	
 	
 	
 	
 		
r}   list[float]c                >     j         t                              d           g S | j        } j        dd         }	|	                     j                    j        p|}  j         j        d|g j        ||||	|d|}
g }g }g }|
d         D ]nfd|	D             } 	                    |          }|
                    |           |
                    j                   |
                    j                   o j                              j         d|  j         j        g|           fdD             fd	|D             }t          t!          j        |          |||
          }g }|D ]%}|dk    r n|
                    ||                    &|S )a  Perform a search and return results that are reordered by MMR.

        Args:
            embedding (str): 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.
        Nr  r  r   c                F    i | ]}|j                             |          S r   r  r  s     rw   r   zBMilvus.max_marginal_relevance_search_by_vector.<locals>.<dictcomp>  r!  r}    in )r  r  r*   c                B    i | ]}|j                  |j                 S r   )rg   ri   r   s     rw   r   zBMilvus.max_marginal_relevance_search_by_vector.<locals>.<dictcomp>  s+    RRRQ1T()1T-?+@RRRr}   c                     g | ]
}|         S r   r   )r   r   r   s     rw   r   zBMilvus.max_marginal_relevance_search_by_vector.<locals>.<listcomp>  s    $=$=$=AWQZ$=$=$=r}   )r   r+  r   )rp   r   r   r6   rl   r"  ri   r*   r#  r$  r   r%  idr   rg   r   nparray)rv   r  r   r*  r+  r   r  r*   r   r  r   r   	documentsscoresr  r  ordered_result_embeddingsnew_orderingr&  r   r  r   s   `                   @@rw   r.  z.Milvus.max_marginal_relevance_search_by_vectorS  s   @ 8LL<===I=&E AAAT/000,)'dho 	
)'	
 	
 	
 	
 	!f 	" 	"FCCCC]CCCD&&t,,CS!!!MM&,'''JJvy!!!!(..'22S22.0BC ! 
 
 SRRR'RRR$=$=$=$=$=$=$=! 2HY!:a[
 
 

  	) 	)ABww

9Q<((((
r}   c                
   t          |t                    r<t          |          dk    r)|t                              d           | j         d| }nt          |t                    s
J d             | j        j        dd|i|S )ao  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.
        r   Nz>Both ids and expr are provided. Ignore expr and delete by ids.r3  z0Either ids list or expr string must be provided.r  r   )	rs   r   r   r   warningrg   r.   rp   delete)rv   r   r  r   s       rw   r@  zMilvus.delete  s     c4   		BSXX\\W   )44s44DDc  B BAB B B tx33D3F333r}   dict[str, Any]c
                   t          |
t                    rt          |
          dk    rd}nd} | d|||||||	|d|}|                    |||
           |S )a  Create a Milvus collection, indexes it with HNSW, and insert data.

        Args:
            texts (List[str]): Text data.
            embedding (Embeddings): 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.

        Returns:
            Milvus: Milvus Vector Store
        r   FT)r,   r-   r2   r3   r4   r6   r7   r9   )r   r   r   r   )rs   r   r   r   )clsr   r  r   r-   r2   r3   r4   r6   r7   r   r   r9   	vector_dbs                 rw   
from_textszMilvus.from_texts  s    P c4   	SXX\\GGGC 

(++/%'

 

 

 

	 	%9#FFFr}   r  r   c                    t          |                    | j                  | j        r|                    | j                  n|          S )N)page_contentmetadata)r   poprh   rj   )rv   r  s     rw   r$  zMilvus._parse_document   sK    $"2337;7KUTXXd2333QU
 
 
 	
r}   List[int] | Nonec                    ddl m}  j        t                              d           dS 	  j                            | j        g          }n0# |$ r(}t                              d j        |           |d}~ww xY w fd|D             }|S )zGet 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)
        r   r   Nz!No existing collection to get pk.)r  r  zFailed to get ids: %s error: %sc                D    g | ]}|                     j                  S r   )r   rg   )r   itemrv   s     rw   r   z"Milvus.get_pks.<locals>.<listcomp>  s(    FFFtxx+,,FFFr}   )	rc   r   rp   r   r   r   rg   r   r-   )rv   r  r   r   query_resultexcr   s   `      rw   get_pkszMilvus.get_pks  s     	-,,,,,8LL<===4	8>>$*=)> *  LL  	 	 	LL:D<PRUVVVI	 GFFFFFF
s   "A A<#A77A<r:  List[Document] | NoneList[str] | Nonec                b   ddl m} |t          |          dk    rt                              d           dS |7t          |          r(||d<   	 |                     |           n# |$ r Y nw xY w	  | j        d	d|i|S # |$ r(}t                              d| j        |           |d}~ww xY w)
a"  Update/Insert documents to the vectorstore.

        Args:
            ids: IDs to update - Let's call get_pks to get ids with expression 

            documents (List[Document]): Documents to add to the vectorstore.

        Returns:
            List[str]: IDs of the added texts.
        r   rL  NzNo documents to upsert.r   r   r:  z'Failed to upsert entities: %s error: %sr   )	rc   r   r   r   r   r@  add_documentsr   r-   )rv   r   r:  r   r   rP  s         rw   upsertzMilvus.upsert   s     	-,,,,,I! 3 3LL23334?s3xx?F5M$$$$"   	%4%DD	DVDDD 	 	 	LL94;OQT   I		s*   A& &A.-A.2B B.#B))B.)	r   r   NNr   NNFF)&r,   r   r-   r.   r/   r.   r0   r1   r2   r1   r3   r.   r4   r5   r6   r5   r7   r8   r9   r:   r#   r.   r$   r.   r%   r.   r&   r;   r'   r;   r(   r<   r)   r=   r*   r>   r+   r?   )ry   r   )r2   r~   ry   r.   )NNNr"   N)r|   r<   r   r   r(   r<   r)   r=   r*   r>   ry   r   r{   )r|   r   r   r   ry   r   )ry   r   )ry   r1   )Nr"   N)r(   r<   r)   r=   r*   r>   ry   r   )NNr   )r   r   r   r   r*   r>   r   r=   r   r   r   r   ry   r   )rL   NNN)r   r.   r   r=   r   r5   r  r;   r*   r>   r   r   ry   r  )r  r  r   r=   r   r5   r  r;   r*   r>   r   r   ry   r  )r   r.   r   r=   r   r5   r  r;   r*   r>   r   r   ry   r  )r  r  r   r=   r   r5   r  r;   r*   r>   r   r   ry   r  )rL   r(  r)  NNN)r   r.   r   r=   r*  r=   r+  r,  r   r5   r  r;   r*   r>   r   r   ry   r  )r  r0  r   r=   r*  r=   r+  r,  r   r5   r  r;   r*   r>   r   r   ry   r  )NN)r   r   r  r;   r   r.   )r   r   r  r   r   r   r-   r.   r2   rA  r3   r.   r4   r5   r6   r5   r7   r:   r   r   r   r   ry   r   )r  r~   ry   r   )r  r.   r   r   ry   rJ  )r   r   r:  rR  r   r   ry   rS  )__name__
__module____qualname____doc__rx   propertyr|   rn   ru   r   r   r   r   r   r   r   r  r  r
  r  r/  r.  r@  classmethodrm   rE  r$  rQ  rV  r   r}   rw   r   r      su       W Wx  5&(:>48!*'+(,#(j
 " $(,-1*.#'$(+j
 j
 j
 j
 j
 j
X # # # X#8 8 8 8x &**.*.#'
 
 
 
 
( CGe e e e eN+ + + +   + + + +Z
@ 
@ 
@ 
@ +/#'	    4 +/#'y $(y y y y y y|  $"#'' ' ' ' 'H  $"#'' ' ' ' 'H  $"#'& & & & &V  $"#': : : : :~   $"#'0
 0
 0
 0
 0
j   $"#'X X X X Xv FJ4 4 4 4 40 
 +/4*C!*'+(,8 $(8 8 8 8 8 [8t
 
 
 
   8 $(+/" " " " " " "r}   r   )
__future__r   loggingtypingr   r   r   r   r   r	   uuidr
   numpyr8  langchain_core._api.deprecationr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   	getLoggerrW  r   rm   r   r   r}   rw   <module>rh     s_   " " " " " "  > > > > > > > > > > > > > > > >           6 6 6 6 6 6 - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3 M M M M M M		8	$	$    
;  
c c c c c[ c c 
c c cr}   