
    Ngh                        d dl mZ d dlZd dlmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZ d dlmZ erd dlmZ dd	d
d
dZ ej        e          Z G d de          ZdS )    )annotationsN)TYPE_CHECKINGAnyDictIterableListOptionalTuple)Document)
Embeddings)VectorStoreHippoClient	localhost7788admin)hostportusernamepasswordc                      e Zd ZdZ	 	 	 	 	 	 	 dBdCdZdDdZ	 dEdFdZ	 dGdHd!ZdId"ZdJd#Z	dId$Z
	 	 	 dKdLd.Z	 	 	 	 dMdNd6Z	 	 	 	 dMdOd8Z	 	 	 	 dMdPd;ZedddedddfdQdA            ZdS )RHippoa  `Hippo` vector store.

    You need to install `hippo-api` and run Hippo.

    Please visit our official website for how to run a Hippo instance:
    https://www.transwarp.cn/starwarp

    Args:
        embedding_function (Embeddings): Function used to embed the text.
        table_name (str): Which Hippo table to use. Defaults to
            "test".
        database_name (str): Which Hippo database to use. Defaults to
            "default".
        number_of_shards (int): The number of shards for the Hippo table.Defaults to
            1.
        number_of_replicas (int): The number of replicas for the Hippo table.Defaults to
            1.
        connection_args (Optional[dict[str, any]]): The connection args used for
            this class comes in the form of a dict.
        index_params (Optional[dict]): Which index params to use. Defaults to
            IVF_FLAT.
        drop_old (Optional[bool]): Whether to drop the current collection. Defaults
            to False.
        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".

    The connection args used for this class comes in the form of a dict,
    here are a few of the options:
        host (str): The host of Hippo instance. Default at "localhost".
        port (str/int): The port of Hippo instance. Default at 7788.
        user (str): Use which user to connect to Hippo 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.

    Example:
        .. code-block:: python

        from langchain_community.vectorstores import Hippo
        from langchain_community.embeddings import OpenAIEmbeddings

        embedding = OpenAIEmbeddings()
        # Connect to a hippo instance on localhost
        vector_store = Hippo.from_documents(
            docs,
            embedding=embeddings,
            table_name="langchain_test",
            connection_args=HIPPO_CONNECTION
        )

    Raises:
        ValueError: If the hippo-api python package is not installed.
    testdefault   NFembedding_functionr   
table_namestrdatabase_namenumber_of_shardsintnumber_of_replicasconnection_argsOptional[Dict[str, Any]]index_paramsOptional[dict]drop_oldOptional[bool]c	                   || _         || _        || _        || _        || _        || _        d| _        d| _        d| _        g | _	        |t          }|                     |          | _        d | _        	 | j                            | j        | j                  r'|r%| j                            | j        | j                   n2# t           $ r%}	t#          j        d| j         d|	             d }	~	ww xY w	 | j                            | j        | j                  r*| j                            | j        | j                  | _        n2# t           $ r%}	t#          j        d| j         d|	             d }	~	ww xY w|                                  d S )Npktextvectorz+An error occurred while deleting the table z: z*An error occurred while getting the table )r    r"   embedding_funcr   r   r%   _primary_field_text_field_vector_fieldfieldsDEFAULT_HIPPO_CONNECTION_create_connection_aliashccolcheck_table_existsdelete_table	Exceptionloggingerror	get_table_get_env)
selfr   r   r   r    r"   r#   r%   r'   es
             b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/hippo.py__init__zHippo.__init__P   s    !1"40$*( #!%!#"6O//@@
	**4?D<NOOJJ $$T_d6HIII 	 	 	MX$/XXUVXX   			w))$/4;MNN R7,,T_d>PQQ 	 	 	MWWWTUWW   		 	s2   2AB? ?
C.	 C))C.2AE 
E1 E,,E1dictreturnr   c                  	 	 ddl m} n"# t          $ r}t          d          |d}~ww xY w|                    dd          }|                    dd          	|                    dd          }|                    d	d          }|`	^d
|v r7|                    d
          }d
                    	fd|D                       }n2t          |          dz   t          	          z   }nt          d          	 t          	                    d| d            ||g||          S # t          $ r!}t                              d           |d}~ww xY w)z*Create the connection to the Hippo server.r   r   zQUnable to import transwarp_hipp_api, please install with `pip install hippo-api`.Nr   r   r   shivar   ,c                    g | ]	}| d  
S ): ).0hr   s     r?   
<listcomp>z2Hippo._create_connection_alias.<locals>.<listcomp>   s#    )G)G)GAQ----)G)G)G    rG   z/Missing standard address type for reuse attemptzcreate HippoClient[])r   pwdzFailed to create new connection) transwarp_hippo_api.hippo_clientr   ImportErrorgetsplitjoinr   
ValueErrorloggerinfor8   r:   )
r=   r#   r   r>   r   r   r   hostsgiven_addressr   s
            @r?   r3   zHippo._create_connection_alias   s   	DDDDDDD 	 	 	+  	 $''55#''55'++J@@'++J@@  0d{{

3 #)G)G)G)G)G)G)G H H #D		C#d)) ;NOOO	KK>m>>>???;xPPPP 	 	 	LL:;;;G	s(   
 
)$)6,D# #
E-E		E
embeddingsOptional[list]	metadatasOptional[List[dict]]Nonec                    t                               d           |0t                               d           |                     ||           |                                  |                                  d S )Nzinit ...zcreate collection)rU   rV   _create_collection_extract_fields_create_index)r=   rY   r[   s      r?   r<   zHippo._get_env   so     	J!KK+,,,##J	:::rL   listc           
         ddl m} ddlm} t	          |d                   }t
                              d|            g }|                     || j        d|j	                             |                     || j
        d|j	                             |                     || j        d|j        d|i                     |r|d                                         D ]x\  }}t          |t                    r9t	          |          }	|                     ||d|j        d|	i                     S|                     ||d|j	                             yt
                              d	|            | j                            | j        d|| j        | j        | j        
           | j                            | j        | j                  | _        t
                              d| j         d| j         d           d S )Nr   )
HippoField)	HippoTypez[_create_collection] dim: TF	dimension)type_paramsz[_create_collection] fields: )nameauto_idr1   r   r    r"   z$[_create_collection] : create table z in z successfully)rO   rd   transwarp_hippo_api.hippo_typere   lenrU   debugappendr.   STRINGr/   r0   FLOAT_VECTORitems
isinstancerb   r4   create_tabler   r   r    r"   r;   r5   rV   )
r=   rY   r[   rd   re   dimr1   keyvalue	value_dims
             r?   r_   zHippo._create_collection   sp    	@?????<<<<<< *Q-  7#77888 	jj!4dI<LMMNNN 	jj!15):JKKLLL 	J"&(#.	  	
 	
 	
  	L'l0022 L L
UeT** L #E

IMM"
!%2)4i(@	      MM**S%9I"J"JKKKK=V==>>> 	,!2#6 	 	
 	
 	
 7$$T_d6HIIS OS S151CS S S	
 	
 	
 	
 	
rL   c                   ddl m} t          | j        |          rq| j        j        }t
                              d|            |D ]!}| j                            |j	                   "t
                              d| j                    dS dS )z,Grab the existing fields from the Collectionr   
HippoTablez[_extract_fields] schema:z04 [_extract_fields] fields:N)
rO   ry   rq   r5   schemarU   rl   r1   rm   rh   )r=   ry   rz   xs       r?   r`   zHippo._extract_fields   s    ??????dh
++ 	GX_FLL=V==>>> + +""16****LLEEEFFFFF	G 	GrL   c                   ddl m} t          | j        |          r| j                            | j        | j                                      | j        i           }|                    dd          }|dS | j                            | j        | j                  | j                 d         D ]4}t          
                    d|            |d         | j        k    r|c S 5dS )z0Return the vector index information if it existsr   rx   embedding_indexesNz[_get_index] embedding_indexes column)rO   ry   rq   r5   r4   get_table_infor   r   rQ   rU   rl   r0   )r=   ry   
table_infor}   r{   s        r?   
_get_indexzHippo._get_index  s    ??????dh
++ 	!//!3 c$/2&&  !+/BD I I (t//ASTTO%' ! !A LL!VCT!V!VWWW{d&888  9trL   c                h
   ddl m} ddlm}m} t          | j        |          r	|                                 |                                 | j        d|j	        |j
        dd| _        | j                            | j        | j        d         | j        d	         | j        d
         | j        d                    t                              | j                            | j        d                              t                              d           dS |j
        |j        |j        |j        |j        d}|j        |j        |j	        |j	        d}|| j        d
                  | j        d
<   | j        d	         dk    r|| j        d	                  | j        d	<   | j                            | j        | j        d         | j        d	         | j        d
                    t                              | j                            | j        d                              dS | j        d	         dk    s| j        d	         dk    r|| j        d	                  | j        d	<   | j                            | j        | j        d         | j        d	         | j        d
         | j                            dd          | j                            dd                     t                              | j                            | j        d                              dS | j        d	         dk    r|| j        d	                  | j        d	<   | j                            | j        | j        d         | j        d	         | j        d
         | j                            dd          | j                            dd          | j                            dd          | j                            d                     t                              | j                            | j        d                              dS | j        d	         dk    r|| j        d	                  | j        d	<   | j                            | j        | j        d         | j        d	         | j        d
         | j                            d          | j                            d          | j                            d                     t                              | j                            | j        d                              dS t/          d          dS dS dS ) z Create a index on the collectionr   rx   )	IndexType
MetricTypeNlangchain_auto_create
   )
index_namemetric_type
index_typenlistr   r   r   r   )r   zcreate index successfully)IVF_FLATFLATIVF_SQIVF_PQHNSW)ipIPl2L2r   r   r   nprobe)r   r   r   nbits   m)r   r   r   r   r   Mef_construction	ef_search)r   r   r   zeIndex name does not match, please enter the correct index name. (FLAT, IVF_FLAT, IVF_PQ,IVF_SQ, HNSW))rO   ry   rj   r   r   rq   r5   r   r%   r   r   create_indexr0   rU   rl   activate_indexrV   r   r   r   r   r   rQ   rT   )r=   ry   r   r   
index_dictmetric_dicts         r?   ra   zHippo._create_index  sr   ??????HHHHHHHHdh
++ m	0A0A0I  ($,&='1}&/&8!#	) )D% H))*),7),7)-8"/8 *    LL//0A,0OPP   KK ;<<<<< %.$6 )"+"2"+"2 )" "J )m(m(m(m	# #K 8C)-88D%m4 (6&@@:D -l;;),7 -- . -l; -l; -m<	    H33D4El4STT     ),7:EE,\:hFF:D -l;;),7 -- . -l; -l; -m<"&"3"7"7"D"D#'#4#8#82#F#F .     H33D4El4STT     *<8HDD:D -l;;),7 -- . -l; -l; -m<"&"3"7"7"D"D#'#4#8#82#F#F"&"3"7"7"C"C"/33C88 . 	 	 	  H33D4El4STT     *<8FBB:D -l;;),7 -- . -l; -l; -m<"/33C88,0,=,A,ABS,T,T&*&7&;&;K&H&H .     H33D4El4STT     )D  Sm	 m	0I0I((rL     textsIterable[str]timeoutOptional[int]
batch_sizekwargsr   	List[str]c                    ddl m} |rt          d |D                       rt                              d           g S t          |          }t                              d|            	  j                            |          }n# t          $ r  fd|D             }Y nw xY wt          |          dk    rt                              d           g S t                              dt          |                      t           j        |          s                     ||            j        | j        |it                              d|            t                              d	 j                    |Q|D ]N}|                                D ]7\  }	}
|	 j        v r)                    |	g                               |
           8Ot                               j                             j                 }t          |          }d j        v r j                            d           t                              d|            t)          d||          D ]t+          |z   |          fd j        D             }	  j                            |          }t                              d|            c# t0          $ r#}t                              d|           |d
}~ww xY wdgS )a  
        Add text to the collection.

        Args:
            texts: An iterable that contains the text to be added.
            metadatas: An optional list of dictionaries,
            each dictionary contains the metadata associated with a text.
            timeout: Optional timeout, in seconds.
            batch_size: The number of texts inserted in each batch, defaults to 1000.
            **kwargs: Other optional parameters.

        Returns:
            A list of strings, containing the unique identifiers of the inserted texts.

        Note:
            If the collection has not yet been created,
            this method will create a new collection.
        r   rx   c              3  "   K   | ]
}|d k    V  dS ) NrH   )rI   ts     r?   	<genexpr>z"Hippo.add_texts.<locals>.<genexpr>  s&      33AG333333rL   zNothing to insert, skipping.z[add_texts] texts: c                D    g | ]}j                             |          S rH   )r-   embed_query)rI   r{   r=   s     r?   rK   z#Hippo.add_texts.<locals>.<listcomp>  s*    LLL$-99!<<LLLrL   z[add_texts] len_embeddings:z[add_texts] metadatas:z[add_texts] fields:Nr*   z[add_texts] total_count:c                0    g | ]}|                  S rH   rH   )rI   r{   endiinsert_dicts     r?   rK   z#Hippo.add_texts.<locals>.<listcomp>  s&    FFFQ;q>!C%0FFFrL   z05 [add_texts] insert z0Failed to insert batch starting at entity: %s/%sr   )rO   ry   allrU   rl   rb   r-   embed_documentsNotImplementedErrorrk   rq   r5   r<   r/   r0   r1   rp   
setdefaultrm   removerangemininsert_rowsrV   r8   r:   )r=   r   r[   r   r   r   ry   rY   drt   ru   vectorstotal_countinsert_listresr>   r   r   r   s   `               @@@r?   	add_textszHippo.add_texts  sU   4 	@????? 	33U33333 	LL7888IU2522333	M,<<UCCJJ" 	M 	M 	MLLLLeLLLJJJ	M z??aLL7888ID3z??DDEEE $(J// 	1MM*i000 e
(
 	9i99:::84;88999  F F"#'')) F FJCdk))#..sB77>>uEEEF 	[!12333 $D$67'll4;Kt$$$===>>>q+z22 	 	Aa*nk22CFFFFFF$+FFFKh**;77:S::;;;;   F;   	
 ts*   /B
 
B%$B%7K
K0K++K0   querykparamexprOptional[str]List[Document]c           	         | j         t                              d           g S  | j        d|||||d|}d |D             S )a  
        Perform a similarity search on the query string.

        Args:
            query (str): The text to search for.
            k (int, optional): The number of results to return. Default is 4.
            param (dict, optional): Specifies the search parameters for the index.
            Defaults to None.
            expr (str, optional): Filtering expression. Defaults to None.
            timeout (int, optional): Time to wait before a timeout error.
            Defaults to None.
            kwargs: Keyword arguments for Collection.search().

        Returns:
            List[Document]: The document results of the search.
        N!No existing collection to search.)r   r   r   r   r   c                    g | ]\  }}|S rH   rH   )rI   doc_s      r?   rK   z+Hippo.similarity_search.<locals>.<listcomp>  s    &&&Q&&&rL   rH   )r5   rU   rl   similarity_search_with_score)r=   r   r   r   r   r   r   r   s           r?   similarity_searchzHippo.similarity_search  sk    4 8LL<===I/d/ 
1Eg
 
IO
 
 '&#&&&&rL   List[Tuple[Document, float]]c           	         | j         t                              d           g S | j                            |          } | j        d|||||d|}|S )a  
        Performs a search on the query string and returns results with scores.

        Args:
            query (str): The text being searched.
            k (int, optional): The number of results to return.
            Default is 4.
            param (dict): Specifies the search parameters for the index.
            Default is None.
            expr (str, optional): Filtering expression. Default is None.
            timeout (int, optional): The waiting time before a timeout error.
            Default is None.
            kwargs: Keyword arguments for Collection.search().

        Returns:
            List[float], List[Tuple[Document, any, any]]:
        Nr   )	embeddingr   r   r   r   rH   )r5   rU   rl   r-   r   &similarity_search_with_score_by_vector)	r=   r   r   r   r   r   r   r   rets	            r?   r   z"Hippo.similarity_search_with_score  st    6 8LL<===I '33E::	9d9 
1Eg
 
QW
 
 
rL   r   List[float]c                   | j         t                              d           g S | j        dd         }|                    | j                   t                              d| j                    t                              d|g            t                              d|            t                              d|            t                              d|            | j                             | j        |g|||          t                              d	            | j        d
z   }g }	d}
t          fd|D              D ]}d t          ||          D             }t          |
                    | j                  |          }t                              dd         |                     d         |         |
         }|
dz  }
|	                    ||f           |	S )a  
        Performs a search on the query string and returns results with scores.

        Args:
            embedding (List[float]): The embedding vector being searched.
            k (int, optional): The number of results to return.
            Default is 4.
            param (dict): Specifies the search parameters for the index.
            Default is None.
            expr (str, optional): Filtering expression. Default is None.
            timeout (int, optional): The waiting time before a timeout error.
            Default is None.
            kwargs: Keyword arguments for Collection.search().

        Returns:
            List[Tuple[Document, float]]: Resulting documents and scores.
        Nr   zsearch_field:zvectors:zoutput_fields:ztopk:zdsl:)search_fieldr   output_fieldstopkdslz-[similarity_search_with_score_by_vector] res:z%scoresr   c                ,    g | ]}d          |         S )r   rH   )rI   fieldr   s     r?   rK   z@Hippo.similarity_search_with_score_by_vector.<locals>.<listcomp>f  s!    DDDU3q6%=DDDrL   c                    i | ]\  }}||	S rH   rH   )rI   r   ru   s      r?   
<dictcomp>z@Hippo.similarity_search_with_score_by_vector.<locals>.<dictcomp>g  s    OOO\UEE5OOOrL   )page_contentmetadataz;[similarity_search_with_score_by_vector] res[0][score_col]:r   )r5   rU   rl   r1   r   r0   r   r/   zipr   poprm   )r=   r   r   r   r   r   r   r   	score_colr   countrp   metar   scorer   s                  @r?   r   z,Hippo.similarity_search_with_score_by_vector.  s   4 8LL<===I AAAT/000 	9T%799:::---...5m55666[Q[[!!!]D]]###hnn+K'  
 
 	JSJJKKK$y0	DDDDmDDDE 		% 		%EOOS5N5NOOOD1A(B(BTRRRCLL9%(VI%69 9   F9%e,EQJEJJU|$$$$
rL   Dict[str, Any]Optional[Dict[Any, Any]]search_paramsbool'Hippo'c
           
        |i }t                               d            | d||||||	d|
}t                               d|            t                               d|            |                    ||           |S )a  
        Creates an instance of the VST class from the given texts.

        Args:
            texts (List[str]): List of texts to be added.
            embedding (Embeddings): Embedding model for the texts.
            metadatas (List[dict], optional):
            List of metadata dictionaries for each text.Defaults to None.
            table_name (str): Name of the table. Defaults to "test".
            database_name (str): Name of the database. Defaults to "default".
            connection_args (dict[str, Any]): Connection parameters.
            Defaults to DEFAULT_HIPPO_CONNECTION.
            index_params (dict): Indexing parameters. Defaults to None.
            search_params (dict): Search parameters. Defaults to an empty dictionary.
            drop_old (bool): Whether to drop the old collection. Defaults to False.
            kwargs: Other arguments.

        Returns:
            Hippo: An instance of the VST class.
        Nz'00 [from_texts] init the class of Hippo)r   r   r   r#   r%   r'   z[from_texts] texts:z[from_texts] metadatas:)r   r[   rH   )rU   rV   rl   r   )clsr   r   r[   r   r   r#   r%   r   r'   r   	vector_dbs               r?   
from_textszHippo.from_textss  s    F  M=>>>C 
(!'+%
 
 
 
	 	2522333:y::;;;%9===rL   )r   r   r   r   NNF)r   r   r   r   r   r   r    r!   r"   r!   r#   r$   r%   r&   r'   r(   )r#   rA   rB   r   )NN)rY   rZ   r[   r\   rB   r]   )N)rY   rb   r[   r\   rB   r]   )rB   r]   )rB   r$   )NNr   )r   r   r[   r\   r   r   r   r!   r   r   rB   r   )r   NNN)r   r   r   r!   r   r&   r   r   r   r   r   r   rB   r   )r   r   r   r!   r   r&   r   r   r   r   r   r   rB   r   )r   r   r   r!   r   r&   r   r   r   r   r   r   rB   r   )r   r   r   r   r[   r\   r   r   r   r   r#   r   r%   r   r   r$   r'   r   r   r   rB   r   )__name__
__module____qualname____doc__r@   r3   r<   r_   r`   r   ra   r   r   r   r   classmethodr2   r   rH   rL   r?   r   r      s       5 5t !& !"#48'+#(7 7 7 7 7r   D TX     CG?
 ?
 ?
 ?
 ?
B	G 	G 	G 	G   *r r r rn +/!%V V V V Vv  $"!% '  '  '  '  'J  $"!%% % % % %T  $"!%C C C C CJ 
 +/ &*B15261 1 1 1 [1 1 1rL   r   )
__future__r   r9   typingr   r   r   r   r   r	   r
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   rO   r   r2   	getLoggerr   rU   r   rH   rL   r?   <module>r      s   " " " " " "  L L L L L L L L L L L L L L L L L L - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3 =<<<<<< 	   
	8	$	$M
 M
 M
 M
 M
K M
 M
 M
 M
 M
rL   