
    dhh                        S SK Jr  S SKrS SKJrJrJrJrJrJ	r	J
r
  S SKJr  S SKJr  S SKJr  \(       a  S SKJr  SS	S
S
S.r\R(                  " \5      r " S S\5      rg)    )annotationsN)TYPE_CHECKINGAnyDictIterableListOptionalTuple)Document)
Embeddings)VectorStoreHippoClient	localhost7788admin)hostportusernamepasswordc                     \ rS rSrSr       S               SS jjrSS jr S     SS	 jjr S     SS
 jjrSS jr	SS jr
SS jr   S           SS jjr    S             S S jjr    S             S!S jjr    S             S"S jjr\SSS\SSS4                     S#S jj5       rSrg)$Hippo   a-  `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defaultNFc	                   X@l         XPl        Xl        X l        X0l        Xpl        SU l        SU l        SU l        / U l	        Uc  [        nU R                  U5      U l        S U l         U R                  R                  U R                  U R                  5      (       a7  U(       a0  U R                  R                  U R                  U R                  5         U R                  R                  U R                  U R                  5      (       a5  U R                  R'                  U R                  U R                  5      U l        U R)                  5         g ! [          a,  n	["        R$                  " SU R                   SU	 35        e S n	A	ff = f! [          a,  n	["        R$                  " SU R                   SU	 35        e S n	A	ff = f)Npktextvectorz+An error occurred while deleting the table z: z*An error occurred while getting the table )number_of_shardsnumber_of_replicasembedding_func
table_namedatabase_nameindex_params_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)
selfembedding_functionr#   r$   r    r!   connection_argsr%   drop_oldes
             ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/hippo.py__init__Hippo.__init__P   sx    !1"40$*( #!%!#"6O//@
	**4??D<N<NOO$$T__d6H6HI	ww))$//4;M;MNN77,,T__d>P>PQ 	!  	MM=doo=NbQRPST 		  	MM<T__<MRPQsS 		s2   (A,E A*F	 
F'FF	
F?'F::F?c                N    SSK Jn  UR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  S	S5      nUb[  UbX  S
U;   a7  UR	                  S
5      nS
R                  U V	s/ sH	  o SU 3PM     sn	5      n
O&[        U5      S-   [        U5      -   n
O[        S5      e [        R                  SU
 S35        U" U
/XgS9$ ! [         a  n[        S5      UeSnAff = fs  sn	f ! [         a  n[        R                  S5        UeSnAff = f)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   ,: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joinstr
ValueErrorloggerinfor0   r2   )r5   r7   r   r9   r   r   r   r   hostshgiven_addresss              r:   r+   Hippo._create_connection_alias   s8   	D $''5#''5'++J@'++J@  0d{

3 #)GACq-)G H #D	C#d) ;NOO	KK-m_A>?PP/  	+ 	 *H  	LL:;G	s5   C <C9: C> 
C6%C11C6>
D$DD$c                    [         R                  S5        Ub&  [         R                  S5        U R                  X5        U R                  5         U R	                  5         g )Nzinit ...zcreate collection)rJ   rK   _create_collection_extract_fields_create_index)r5   
embeddings	metadatass      r:   r4   Hippo._get_env   sI     	J!KK+,##J:    c           
        SSK Jn  SSKJn  [	        US   5      n[
        R                  SU 35        / nUR                  U" U R                  SUR                  5      5        UR                  U" U R                  SUR                  5      5        UR                  U" U R                  SUR                  SU0S95        U(       a  US   R                  5        Hn  u  px[        U[        5      (       a1  [	        U5      n	UR                  U" USUR                  SU	0S95        MK  UR                  U" USUR                  5      5        Mp     [
        R                  S	U 35        U R                   R#                  U R$                  SUU R&                  U R(                  U R*                  S
9  U R                   R-                  U R$                  U R&                  5      U l        [
        R1                  SU R$                   SU R&                   S35        g )Nr   )
HippoField)	HippoTypez[_create_collection] dim: TF	dimension)type_paramsz[_create_collection] fields: )nameauto_idr)   r$   r    r!   z$[_create_collection] : create table z in z successfully)rC   rY   transwarp_hippo_api.hippo_typerZ   lenrJ   debugappendr&   STRINGr'   r(   FLOAT_VECTORitems
isinstancelistr,   create_tabler#   r$   r    r!   r3   r-   rK   )
r5   rT   rU   rY   rZ   dimr)   keyvalue	value_dims
             r:   rQ   Hippo._create_collection   s    	@< *Q- 1#78 	j!4!4dI<L<LMN 	j!1!15):J:JKL 	""&&(#.		
 'l002
eT** #E
IMM"!%22)4i(@	 MM*S%9I9I"JK 3 	4VH=> 	,,!22#66 	 	
 77$$T__d6H6HI OO,D1C1C0DMS	
rW   c                D   SSK Jn  [        U R                  U5      (       a  U R                  R                  n[
        R                  SU 35        U H(  nU R                  R                  UR                  5        M*     [
        R                  SU R                   35        gg)z,Grab the existing fields from the Collectionr   
HippoTablez[_extract_fields] schema:z04 [_extract_fields] fields:N)
rC   rp   rf   r-   schemarJ   ra   r)   rb   r]   )r5   rp   rq   xs       r:   rR   Hippo._extract_fields   st    ?dhh
++XX__FLL4VH=>""166* LL7}EF ,rW   c                   SSK Jn  [        U R                  U5      (       a  U R                  R                  U R                  U R                  5      R                  U R                  0 5      nUR                  SS5      nUc  gU R                  R                  U R                  U R                  5      U R                     S    H2  n[        R                  SU 35        US   U R                  :X  d  M0  Us  $    g)z0Return the vector index information if it existsr   ro   embedding_indexesNz[_get_index] embedding_indexes column)rC   rp   rf   r-   r,   get_table_infor#   r$   rE   rJ   ra   r(   )r5   rp   
table_inforu   rr   s        r:   
_get_indexHippo._get_index  s    ?dhh
++//!3!3c$//2&  !+/BD I (//ASASTOO%'A LL#BCTBU!VW{d&8&88 ' rW   c                ^   SSK Jn  SSKJnJn  [        U R                  U5      (       Ga  U R                  5       Gcp  U R                  5       Gc]  U R                  c  SUR                  UR                  SS.U l        U R                  R                  U R                  U R                  S   U R                  S	   U R                  S
   U R                  S   S9  [        R                  U R                  R                  U R                  S   5      5        [        R!                  S5        gUR                  UR"                  UR$                  UR&                  UR(                  S.nUR*                  UR*                  UR                  UR                  S.nUU R                  S
      U R                  S
'   U R                  S	   S:X  a  UU R                  S	      U R                  S	'   U R                  R                  U R                  U R                  S   U R                  S	   U R                  S
   5        [        R                  U R                  R                  U R                  S   5      5        gU R                  S	   S:X  d  U R                  S	   S:X  a  UU R                  S	      U R                  S	'   U R                  R                  U R                  U R                  S   U R                  S	   U R                  S
   U R                  R-                  SS5      U R                  R-                  SS5      S9  [        R                  U R                  R                  U R                  S   5      5        gU R                  S	   S:X  Ga  UU R                  S	      U R                  S	'   U R                  R                  U R                  U R                  S   U R                  S	   U R                  S
   U R                  R-                  SS5      U R                  R-                  SS5      U R                  R-                  SS5      U R                  R-                  S5      S9  [        R                  U R                  R                  U R                  S   5      5        gU R                  S	   S:X  a  UU R                  S	      U R                  S	'   U R                  R                  U R                  U R                  S   U R                  S	   U R                  S
   U R                  R-                  S5      U R                  R-                  S5      U R                  R-                  S5      S9  [        R                  U R                  R                  U R                  S   5      5        g[/        S5      eggg) z Create a index on the collectionr   ro   )	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))rC   rp   r_   r|   r}   rf   r-   ry   r%   r   r   create_indexr(   rJ   ra   activate_indexrK   r   r   r   r   r   rE   rI   )r5   rp   r|   r}   
index_dictmetric_dicts         r:   rS   Hippo._create_index  s   ?Hdhh
++0A0I ($$,&='1}}&/&8&8!#	)D% HH))**)),7)),7))-8"//8 *  LL//0A0A,0OP KK ;< %.$6$6 )"+"2"2"+"2"2 )"J )mm(mm(mm(mm	#K 8C))-88D%%m4 ((6&@:D --l;;)),7 -- .. --l; --l; --m<	  HH33D4E4El4ST )),7:E,,\:hF:D --l;;)),7 -- .. --l; --l; --m<"&"3"3"7"7"D#'#4#4#8#82#F .   HH33D4E4El4ST **<8HD:D --l;;)),7 -- .. --l; --l; --m<"&"3"3"7"7"D#'#4#4#8#82#F"&"3"3"7"7"C"//33C8 . 	  HH33D4E4El4ST **<8FB:D --l;;)),7 -- .. --l; --l; --m<"//33C8,0,=,=,A,ABS,T&*&7&7&;&;K&H .   HH33D4E4El4ST )D Q ) 1J+rW   c                   SSK Jn  U(       a  [        S U 5       5      (       a  [        R	                  S5        / $ [        U5      n[        R	                  SU 35         U R                  R                  U5      n[        U5      S:X  a  [        R	                  S5        / $ [        R	                  S[        U5       35        [        U R                  U5      (       d  U R                  Xr5        U R                  UU R                  U0n	[        R	                  SU 35        [        R	                  SU R                    35        UbT  U HN  n
U
R#                  5        H7  u  pXR                   ;   d  M  U	R%                  U/ 5      R'                  U5        M9     MP     [        R	                  XR                     5        XR                     n[        U5      nS
U R                   ;   a  U R                   R)                  S
5        [        R	                  SU 35        [+        SX5       He  n[-        X-   U5      nU R                    Vs/ sH
  oU   UU PM     nn U R                  R/                  U5      n[        R1                  SU 35        Mg     S/$ ! [         a3    U Vs/ sH  oR                  R                  U5      PM     Os  snf nn GNOf = fs  snf ! [2         a  n[        R5                  SX5        UeS	nAff = f)a3  
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   ro   c              3  (   #    U H	  oS :H  v   M     g7f) N ).0ts     r:   	<genexpr>"Hippo.add_texts.<locals>.<genexpr>  s     3UGUs   zNothing to insert, skipping.z[add_texts] texts: z[add_texts] len_embeddings:z[add_texts] metadatas:z[add_texts] fields:Nr   z[add_texts] total_count:z05 [add_texts] insert z0Failed to insert batch starting at entity: %s/%sr   )rC   rp   allrJ   ra   rg   r"   embed_documentsNotImplementedErrorembed_queryr`   rf   r-   r4   r'   r(   r)   re   
setdefaultrb   removerangemininsert_rowsrK   r0   r2   )r5   textsrU   timeout
batch_sizekwargsrp   rT   rr   insert_dictdrj   rk   vectorstotal_countiendinsert_listresr9   s                       r:   	add_textsHippo.add_texts  s   4 	@3U333LL78IU*5'23	M,,<<UCJ z?aLL78I23z?2CDE $((J//MM*0 e
(
 	-i[9:*4;;-89 "#'')JCkk)#..sB7>>uE #, 
 	[!1!123 $$6$67'l4;;KKt$/}=>q+2Aank2C:>++F+Qq>!C0+KFhh**;74SE:; 3 tc # 	MFKLe--99!<eLJLJ	MP G  F 	s<    J K3KK#K ?K
K
K:K55K:c           	         U R                   c  [        R                  S5        / $ U R                  " SXX4US.UD6nU VV	s/ sH  u  pUPM	     sn	n$ s  sn	nf )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.
!No existing collection to search.)querykparamexprr   r   )r-   rJ   ra   similarity_search_with_score)
r5   r   r   r   r   r   r   r   doc_s
             r:   similarity_searchHippo.similarity_search  s`    4 88LL<=I// 
Eg
IO
 #&&##&&&s    Ac           	         U R                   c  [        R                  S5        / $ U R                  R	                  U5      nU R
                  " SXrX4US.UD6nU$ )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]]:
r   )	embeddingr   r   r   r   r   )r-   rJ   ra   r"   r   &similarity_search_with_score_by_vector)	r5   r   r   r   r   r   r   r   rets	            r:   r   "Hippo.similarity_search_with_score  sc    6 88LL<=I ''33E:	99 
Eg
QW
 
rW   c                   U R                   c  [        R                  S5        / $ U R                  SS nUR	                  U R
                  5        [        R                  SU R
                   35        [        R                  SU/ 35        [        R                  SU 35        [        R                  SU 35        [        R                  SU 35        U R                   R                  U R
                  U/UUUS9n[        R                  S	U 35        U R                  S
-   n	/ n
Sn[        U Vs/ sH
  oS   U   PM     sn6  H  n[        X}5       VVs0 sH  u  pX_M	     nnn[        UR                  U R                  5      US9n[        R                  SUS   U	    35        US   U	   U   nUS-  nU
R                  UU45        M     U
$ s  snf s  snnf )aP  
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   )page_contentmetadataz;[similarity_search_with_score_by_vector] res[0][score_col]:   )r-   rJ   ra   r)   r   r(   r   r'   zipr   poprb   )r5   r   r   r   r   r   r   r   r   	score_colr   countfieldre   rk   metar   scores                     r:   r   ,Hippo.similarity_search_with_score_by_vector.  s   4 88LL<=I AT//0 	}T%7%7$89:x}-.~m_56uQC[!tD6]#hhnn++K'  
 	DSEJK$$y0	mDmUq6%=mDEE585NO5N\UEL5NDO1A1A(BTRCLL%%(VI%6$79 F9%e,EQJEJJU|$ F 
 EOs   .GGc
           
         Uc  0 n[         R                  S5        U " SUUUUUU	S.U
D6n[         R                  SU 35        [         R                  SU 35        UR                  XS9  U$ )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.
z'00 [from_texts] init the class of Hippo)r6   r#   r$   r7   r%   r8   z[from_texts] texts:z[from_texts] metadatas:)r   rU   r   )rJ   rK   ra   r   )clsr   r   rU   r#   r$   r7   r%   search_paramsr8   r   	vector_dbs               r:   
from_textsHippo.from_textss  s    F  M=> 
(!'+%
 
	 	*5'23.yk:;%=rW   )r&   r'   r(   r-   r$   r"   r)   r,   r%   r!   r    r#   )r   r   r   r   NNF)r6   r   r#   rH   r$   rH   r    intr!   r   r7   Optional[Dict[str, Any]]r%   Optional[dict]r8   zOptional[bool])r7   dictreturnr   )NN)rT   zOptional[list]rU   Optional[List[dict]]r   None)N)rT   rg   rU   r   r   r   )r   r   )r   r   )NNi  )r   zIterable[str]rU   r   r   Optional[int]r   r   r   r   r   	List[str])   NNN)r   rH   r   r   r   r   r   Optional[str]r   r   r   r   r   zList[Document])r   rH   r   r   r   r   r   r   r   r   r   r   r   List[Tuple[Document, float]])r   zList[float]r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   rU   r   r#   rH   r$   rH   r7   zDict[str, Any]r%   zOptional[Dict[Any, Any]]r   r   r8   boolr   r   r   z'Hippo')__name__
__module____qualname____firstlineno____doc__r;   r+   r4   rQ   rR   ry   rS   r   r   r   r   classmethodr*   r   __static_attributes__r   rW   r:   r   r      s   5t !& !"#48'+#(7&7 7 	7
 7  7 27 %7 !7rD TX(<P	 CG?
?
+??
	?
B	G*rn +/!%VV (V 	V
 V V 
Vv  $"!% ' '  ' 	 '
  '  '  ' 
 'J  $"!%%% % 	%
 % % % 
&%T  $"!%CC C 	C
 C C C 
&CJ 
 +/ &*B152611 1 (	1
 1 1 (1 /1 01 1 1 
1 1rW   r   )
__future__r   r1   typingr   r   r   r   r   r	   r
   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   rC   r   r*   	getLoggerr   rJ   r   r   rW   r:   <module>r      sY    "  L L L - 0 3< 	  
		8	$M
K M
rW   