
    Ng?                         d dl mZ d dlZd dlmZmZmZmZ d dlm	Z	 d dl
mZ  ej        e          Z G d de          ZdS )    )annotationsN)AnyDictListOptional)
Embeddings)Milvusc                  J    e Zd ZdZd dZe	 	 	 	 	 	 	 d!ddd	d"d            ZdS )#Zilliza  `Zilliz` vector store.

    You need to have `pymilvus` installed and a
    running Zilliz database.

    See the following documentation for how to run a Zilliz instance:
    https://docs.zilliz.com/docs/create-cluster


    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 Zilliz collection to use. Defaults to
            "LangChainCollection".
        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.

    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 Zilliz
            instance. Example address: "localhost:19530"
        uri (str): The uri of Zilliz instance. Example uri:
            "https://in03-ba4234asae.api.gcp-us-west1.zillizcloud.com",
        host (str): The host of Zilliz instance. Default at "localhost",
            PyMilvus will fill in the default host if only port is provided.
        port (str/int): The port of Zilliz instance. Default at 19530, PyMilvus
            will fill in the default port if only host is provided.
        user (str): Use which user to connect to Zilliz 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.
        token (str): API key, for serverless clusters which can be used as
            replacements for user and password.
        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 Zilliz
        from langchain_community.embeddings import OpenAIEmbeddings

        embedding = OpenAIEmbeddings()
        # Connect to a Zilliz instance
        milvus_store = Milvus(
            embedding_function = embedding,
            collection_name = "LangChainCollection",
            connection_args = {
                "uri": "https://in03-ba4234asae.api.gcp-us-west1.zillizcloud.com",
                "user": "temp",
                "password": "temp",
                "token": "temp", # API key as replacements for user and password
                "secure": True
            }
            drop_old: True,
        )

    Raises:
        ValueError: If the pymilvus python package is not installed.
    returnNonec                   ddl m}m} t          | j        |          r|                                 	 | j        ddi d| _        	 | j                            | j        | j        | j	                   nE# |$ r= ddd	d
d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   )
CollectionMilvusExceptionNL2	AUTOINDEX)metric_type
index_typeparams)index_paramsusingHNSW   @   )MefConstructionz/Successfully created an index on collection: %sz+Failed to create an index on collection: %s)pymilvusr   r   
isinstancecol
_get_indexr   create_index_vector_fieldaliasloggerdebugcollection_nameerror)selfr   r   es       c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/zilliz.py_create_indexzZilliz._create_index_   s   88888888dh
++ '	0A0A0I&$,'+&1"$) )D%H))*%)%6"j *     '    (,&,()R"@"@) )D%
 H))*%)%6"j *      E(    
 #   A4CW   	G'	 '	0I0Is;   C ,A3 2C 3?B52C 4B55#C D"DDNLangChainCollectionSessionF)idsauto_idtexts	List[str]	embeddingr   	metadatasOptional[List[dict]]r&   strconnection_argsOptional[Dict[str, Any]]consistency_levelr   Optional[dict]search_paramsdrop_oldboolr.   Optional[List[str]]r/   kwargsr   c
               \     | d|||pi ||||	|d|}|                     |||
           |S )a  Create a Zilliz 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.
            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.

        Returns:
            Zilliz: Zilliz Vector Store
        )embedding_functionr&   r6   r8   r   r:   r;   r/   )r0   r3   r.    )	add_texts)clsr0   r2   r3   r&   r6   r8   r   r:   r;   r.   r/   r>   	vector_dbs                 r*   
from_textszZilliz.from_texts   sj    X C 

(++1r/%'

 

 

 

	 	%9#FFF    )r   r   )Nr,   Nr-   NNF)r0   r1   r2   r   r3   r4   r&   r5   r6   r7   r8   r5   r   r9   r:   r9   r;   r<   r.   r=   r/   r<   r>   r   r   r   )__name__
__module____qualname____doc__r+   classmethodrE   rA   rF   r*   r   r      s        O Ob+ + + +Z 
 +/448!*'+(,7 $(7 7 7 7 7 [7 7 7rF   r   )
__future__r   loggingtypingr   r   r   r   langchain_core.embeddingsr   'langchain_community.vectorstores.milvusr	   	getLoggerrG   r$   r   rA   rF   r*   <module>rR      s    " " " " " "  , , , , , , , , , , , , 0 0 0 0 0 0 : : : : : :		8	$	$w w w w wV w w w w wrF   