
    3fiVQ                         d dl Z d dl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mZ d dlmZ  ej                   e      Z G d de      Zy)    N)AnyIterableListOptionalSequenceTuple)Document)
Embeddings)VectorStorec            "       8   e Zd ZdZdddddddddddd	ded
ee   dee   dee   dee   dee   dee   dee   dee   dedee   deee	      fdZ
d2dZd2dZd2dZdededdfdZdededee   deddf
dZdeddfdZdedefd Z	 	 d3d!ee   d"ed#ee	   d$e	deeeef      f
d%Z	 	 d4d&ee   d'eee      d(eee      d$e	dee   f
d)Zd5d(eee      d$e	dee   fd*Zd(ee   dee   fd+Z	 	 d6d,ed"ed#ee	   d$e	dee   f
d-Z	 	 d6d,ed"ed#ee	   d.e	d$e	deeeef      fd/Z	 	 d6dee   d"ed#ee	   d$e	dee   f
d0Ze 	 	 	 	 	 	 	 	 	 	 	 	 d7d&ee   ded'eee      d
ee   dee   dee   dee   dee   dee   dee   dee   dedee   deee	      d$e	dd f d1       Z!y)8TablestoreVectorStorea;  `Tablestore` vector store.

    To use, you should have the ``tablestore`` python package installed.

    Example:
        .. code-block:: python

            import os

            from langchain_openai import OpenAIEmbeddings
            from langchain_community.vectorstores import TablestoreVectorStore
            import tablestore

            embeddings = OpenAIEmbeddings()
            store = TablestoreVectorStore(
                embeddings,
                endpoint=os.getenv("end_point"),
                instance_name=os.getenv("instance_name"),
                access_key_id=os.getenv("access_key_id"),
                access_key_secret=os.getenv("access_key_secret"),
                vector_dimension=512,
                # metadata mapping is used to filter non-vector fields.
                metadata_mappings=[
                    tablestore.FieldSchema(
                        "type",
                        tablestore.FieldType.KEYWORD,
                        index=True,
                        enable_sort_and_agg=True
                    ),
                    tablestore.FieldSchema(
                        "time",
                        tablestore.FieldType.LONG,
                        index=True,
                        enable_sort_and_agg=True
                    ),
                ]
            )
    Nlangchain_vector_store_ots_v1#langchain_vector_store_ots_index_v1content	embedding   cosine)endpointinstance_nameaccess_key_idaccess_key_secret
table_name
index_name
text_fieldvector_fieldvector_dimensionvector_metric_typemetadata_mappingsr   r   r   r   r   r   r   r   r   r   r   c          
         	 dd l }|| _        |j                  |||||j	                               | _        || _        || _        |
| _        |	| _	        || _
        |dk(  r|j                  j                  | _        nP|dk(  r|j                  j                  | _        n/|dk(  r|j                  j                  | _        nt!        d|       |j#                  | j                  |j$                  j&                  dd	d	|j(                  j*                  
      |j#                  | j                  |j$                  j,                  |j/                  |j0                  j2                  | j                  | j                              g| _        |ro|D ]i  }t7        ||j"                        st!        dt9        |             |j:                  |k(  s|j:                  |	k(  rO| j4                  j=                  |       k y y # t        $ r t        d      w xY w)Nr   \Could not import tablestore python package. Please install it with `pip install tablestore`.)retry_policyr   	euclideandot_productz)Unsupported vector_metric_type operator: TF)indexenable_sort_and_aggstoreanalyzer)	data_type	dimensionmetric_type)vector_optionszKmeta_data mapping should be an instance of tablestore.FieldSchema, bug got )
tablestoreImportError!_TablestoreVectorStore__embedding	OTSClientWriteRetryPolicy)_TablestoreVectorStore__tablestore_client"_TablestoreVectorStore__table_name"_TablestoreVectorStore__index_name(_TablestoreVectorStore__vector_dimension$_TablestoreVectorStore__vector_field"_TablestoreVectorStore__text_fieldVectorMetricType	VM_COSINE*_TablestoreVectorStore__vector_metric_typeVM_EUCLIDEANVM_DOT_PRODUCT
ValueErrorFieldSchema	FieldTypeTEXTAnalyzerTypeMAXWORDVECTORVectorOptionsVectorDataTypeVD_FLOAT_32)_TablestoreVectorStore__metadata_mappings
isinstancetype
field_nameappend)selfr   r   r   r   r   r   r   r   r   r   r   r   r,   mappings                  i/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/vectorstores/tablestore.py__init__zTablestoreVectorStore.__init__<   s.    	 %#-#7#7#446 $8 $
  '&"2*&)(2(C(C(M(MD%;.(2(C(C(P(PD%=0(2(C(C(R(RD%;<N;OP 
 ""!!$$))$)#0088 #  ""##$$++)77(77CC"55 $ 9 9  8   # $
 ( , 9!':+A+AB$##'=/3  &&*4))\9((//89 c  	C 	s   G6 6Hreturnc           
         	 ddl }| j                  j                         }| j                  |v r!t
        j                  d| j                         yt
        j                  d| j                         dg}|j                  | j                  |      }|j                         }|j                  |j                  dd            }	 | j                  j                  |||       t
        j                  d| j                         y# t        $ r t        d      w xY w# |j                  $ rH}t
        j                  d| j                  |j                         |j                                Y d}~yd}~w|j                   $ rf}t
        j                  d	| j                  |j                         |j#                         |j                         |j%                                Y d}~yd}~ww xY w)
zCreate table if not exist.r   Nr    z*Tablestore system table[%s] already existszDTablestore system table[%s] does not exist, try to create the table.)idSTRINGz)Tablestore create table[%s] successfully.z]Tablestore create system table[%s] failed with client error, http_status:%d, error_message:%sz{Tablestore create system table[%s] failed with client error, http_status:%d, error_code:%s, error_message:%s, request_id:%s)r,   r-   r1   
list_tabler2   loggerinfo	TableMetaTableOptionsReservedThroughputCapacityUnitcreate_tableOTSClientError	exceptionget_http_statusget_error_messageOTSServiceErrorget_error_codeget_request_id)rK   r,   
table_listschema_of_primary_key
table_metatable_optionsreserved_throughputes           rM   create_table_if_not_existz/TablestoreVectorStore.create_table_if_not_exist   s   	 --88:

*KKDdFWFWXR	

 "2 2))$*;*;=RS
"//1(;;##Aq)
	$$11M+> KKCTEVEVW1  	C 	2 (( 	3!!!!###%  )) 		Q!!!!#  "##%  " 		s0   C>  =D >DG %>E((G :AGG c                    	 ddl }| j                  j                  | j                        }| j
                  |D cg c]  }|d   	 c}v r!t        j                  d| j
                         y|j                  | j                        }| j                  j                  | j                  | j
                  |       t        j                  d| j
                         y# t        $ r t        d      w xY wc c}w )z!Create search index if not exist.r   Nr    r      z*Tablestore system index[%s] already existsz0Tablestore create system index[%s] successfully.)r,   r-   r1   list_search_indexr2   r3   rT   rU   SearchIndexMetarF   create_search_index)rK   r,   search_index_listt
index_metas        rM    create_search_index_if_not_existz6TablestoreVectorStore.create_search_index_if_not_exist   s    	 !44FF(( G 
 /@ A!1 AAKKDdFWFWX//0H0HI
  44t00*	
 	>@Q@Q	
  	C 	 !Bs   C C1C.c                     | j                   j                  | j                        }|D ]$  }| j                   j                  |d   |d          & | j                   j	                  | j                         y)zDelete table if exists.rj   r   rk   N)r1   rl   r2   delete_search_indexdelete_table)rK   ro   
resp_tuples      rM   delete_table_if_existsz,TablestoreVectorStore.delete_table_if_exists   sv     !44FF(( G 
 , 	WJ$$88A
STV	W  --d.?.?@    c                 <    | j                   j                  ||       y)zDelete search index.N)r1   rt   )rK   r   r   s      rM   rt   z)TablestoreVectorStore.delete_search_index   s     	  44ZLrx   row_idembedding_vector	meta_datac                    	 dd l }d|fg}| j                  |f| j                  t	        j
                  |      fg}|j                         D ]  \  }}	||	f}
|j                  |
        |j                  ||      }	 | j                  j                  | j                  |       t        j                  d|||       y # t        $ r t        d      w xY w# |j                  $ r#}t        j                  d||||       Y d }~y d }~w|j                   $ r_}t        j                  d|||||j#                         |j%                         |j'                         |j)                         	       Y d }~y d }~ww xY w)Nr   r    rQ   z@Tablestore put row successfully. id:%s, content:%s, meta_data:%szOTablestore put row failed with client error:%s, id:%s, content:%s, meta_data:%szTablestore put row failed with client error:%s, id:%s, content:%s, meta_data:%s, http_status:%d, error_code:%s, error_message:%s, request_id:%s)r,   r-   r6   r5   jsondumpsitemsrJ   Rowr1   put_rowr2   rT   debugr[   r\   r_   r]   r`   r^   ra   )rK   rz   r   r{   r|   r,   primary_keyattribute_columnskvitemrowrg   s                rM   __write_rowz!TablestoreVectorStore.__write_row   s   	 f~&(  $**-=">?
 OO% 	+DAqq6D$$T*	+ nn[*;<	$$,,T->->DLLR	#  	C 	. (( 	2  )) 	A !!#  "##%  " 	s0   B9 :>C 9CE/ C>>E/AE**E/c                    	 dd l }d|fg}	 | j                  j                  | j                  |d        t
        j                  d|       y # t        $ r t        d      w xY w# |j                  $ r!}t
        j                  d||       Y d }~y d }~w|j                  $ r]}t
        j                  d|||j                         |j                         |j                         |j                                Y d }~y d }~ww xY w)Nr   r    rQ   z)Tablestore delete row successfully. id:%sz8Tablestore delete row failed with client error:%s, id:%szxTablestore delete row failed with client error:%s, id:%s, http_status:%d, error_code:%s, error_message:%s, request_id:%s)r,   r-   r1   
delete_rowr2   rT   rU   r[   r\   r_   r]   r`   r^   ra   )rK   rz   r,   r   rg   s        rM   __delete_rowz"TablestoreVectorStore.__delete_row  s    	 f~&	$$//0A0A;PTUKKCVL  	C 	 (( 	JAv  )) 
	X!!#  "##%  "	 	
	s/   A
 =A" 
A"C<1BC<AC77C<c                    	 dd l }d|fg}	 | j                  j                  | j                  |d d d      \  }}}t
        j                  d|       |t        d|z        |j                  d   d   }i }d}|j                  D ]#  }	|	d   }
|	d   }|
| j                  k(  r|}|||
<   % t        |||      S # t        $ r t        d      w xY w# |j                  $ r}t
        j                  d	||       |d }~w|j                  $ rZ}t
        j                  d
|||j                         |j!                         |j#                         |j%                                |d }~ww xY w)Nr   r    rQ   rk   z&Tablestore get row successfully. id:%sz'Can't not find row_id:%s in tablestore. rQ   page_contentmetadataz5Tablestore get row failed with client error:%s, id:%szuTablestore get row failed with client error:%s, id:%s, http_status:%d, error_code:%s, error_message:%s, request_id:%s)r,   r-   r1   get_rowr2   rT   r   r<   r   r   r6   r	   r[   r\   r_   r]   r`   r^   ra   )rK   rz   r,   r   _r   document_idr|   textcolkeyvalrg   s                rM   	__get_rowzTablestoreVectorStore.__get_row6  s   	 f~&&	0088!!;dAIAsA LLA6J{ !JV!STT//!,Q/KID,, %!f!f$+++D!$	#% !" /  	C 	8 (( 	GF G)) 	X!!#  "##%  "	 G	s0   B5 B(C 5C
E!C55E!AEE!query_embeddingr   tablestore_filter_querykwargsc           
         	 dd l }|r-t        ||j                        st	        dt        |             d|v r|d   }n|}|j                  | j                  |||      }|j                  |j                  |j                  j                        g      }|j                  ||d|	      }		 | j                  j                  | j                  | j                   |	|j#                  |j$                  j&                  
            }
t(        j+                  d|
j,                         g }|
j.                  D ]  }|j0                  }|j2                  }|d   d   d   }i }d}|d   D ]G  }|d   }|d   }|| j4                  k(  r|}|| j                  k(  rt7        j8                  |      }|||<   I t;        |||      }|j=                  ||f        |S # t        $ r t        d      w xY w# |j>                  $ r}t(        jA                  d|       |d }~w|jB                  $ rY}t(        jA                  d||jE                         |jG                         |jI                         |jK                                |d }~ww xY w)Nr   r    zLtable_store_filter_query should be an instance of tablestore.Query, bug got 	knn_top_k)rI   top_kfloat32_query_vectorfilter)
sort_order)sortersF)limitget_total_countsort)return_type)r   r   search_querycolumns_to_getz-Tablestore search successfully. request_id:%srk   r   r   z-Tablestore search failed with client error:%szmTablestore search failed with client error:%s, http_status:%d, error_code:%s, error_message:%s, request_id:%s)&r,   r-   rG   Queryr<   rH   KnnVectorQueryr5   Sort	ScoreSort	SortOrderDESCSearchQueryr1   searchr2   r3   ColumnsToGetColumnReturnTypeALLrT   rU   
request_idsearch_hitsr   scorer6   r~   loadsr	   rJ   r[   r\   r_   r]   r`   r^   ra   )rK   r   r   r   r   r,   r   	ots_queryr   r   search_response
tuple_listhitr   r   r   r|   r   r   r   r   docrg   s                          rM   _tablestore_searchz(TablestoreVectorStore._tablestore_searchg  s   	 #5z7G7GH #$;<=? 
 & {+II--**!0*	 . 
	 ))Z5I5I5N5N)OP  
 "--QD . 
1	"66==,,,,))66 * ; ; ? ?  7  	 > O KK?** J&22 0gg		!!fQil	q6 )Ca&Ca&Cd///" d111"jjo%(IcN) "!%&
 !!3,/)0*   	C 	@ (( 	LaPG)) 
	Q!!#  "##%  " G
	s1   G -D"G( G%(I:7HI:!AI55I:texts	metadatasidsc                    |xs5 |D cg c])  }t        t        j                         j                        + c}}t	        |      }| j
                  j                  |      }t        t        |            D ]w  }||   }	||   }
||   }t        |      | j                  k7  r#t        dt        |      | j                  fz        t               }|r
||   r||   }| j                  |	|
||       y |S c c}w )Nz?embedding vector size:%d is not the same as vector store dim:%d)rz   r   r{   r|   )struuiduuid4hexlistr.   embed_documentsrangelenr4   RuntimeErrordict!_TablestoreVectorStore__write_row)rK   r   r   r   r   r   	text_list
embeddingsirz   r   r{   r   s                rM   	add_textszTablestoreVectorStore.add_texts  s     ;U;c$**,**+;K	%%55i@
s3x 	AVFQ<D)!}#$(?(??"U+,d.E.EFG  vHYq\$Q<!1"	  	$ 
+ <s   .C0c                 8    |r|D ]  }| j                  |        y)NT)"_TablestoreVectorStore__delete_row)rK   r   r   rz   s       rM   deletezTablestoreVectorStore.delete  s%     *!!&)*rx   c                J    |D cg c]  }| j                  |       c}S c c}w N)_TablestoreVectorStore__get_row)rK   r   rz   s      rM   
get_by_idsz TablestoreVectorStore.get_by_ids  s    5896v&999s    queryc                 `     | j                   |f||d|D cg c]  \  }}|	 c}}S c c}}w N)r   r   )similarity_search_with_score)rK   r   r   r   r   r   r   s          rM   similarity_searchz'TablestoreVectorStore.similarity_search  sL     !B A A!4K!OU!
e 
 	
 
   *argsc                 d    | j                   j                  |      } | j                  |f||d|S r   )r.   embed_queryr   )rK   r   r   r   r   r   r   s          rM   r   z2TablestoreVectorStore.similarity_search_with_score  sG     **66u=&t&&
$;
 	
 	
rx   c                 `     | j                   |f||d|D cg c]  \  }}|	 c}}S c c}}w r   )r   )rK   r   r   r   r   r   r   s          rM   similarity_search_by_vectorz1TablestoreVectorStore.similarity_search_by_vector  sM     !8 7 7!(?! 	!
e 
 	
 
r   c                      | |||||||	|
||||      }|j                          |j                          |j                  ||       |S )N)r   r   r   r   r   r   r   r   r   r   r   r   )rh   rr   r   )clsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r&   s                    rM   
from_textsz TablestoreVectorStore.from_texts  s_    & ''/!!!%-1/
 	'')..0y)rx   )rO   N)   N)NNr   )   N)NNNNNr   r   r   r   r   r   N)"__name__
__module____qualname____doc__r
   r   r   intr   r   rN   rh   rr   rw   rt   floatr   r   r   r	   r   r   r   r   r   boolr   r   r   r   r   r   classmethodr    rx   rM   r   r      sV   %V #''+'++/$C$I$-&1 #,415P9P9 3-	P9
  }P9  }P9 $C=P9 SMP9 SMP9 SMP9 smP9 P9 %SMP9 $DI.P9d/b
0AMc Ms Mt M
22$'2;?;2SW2	2h3 4 8/ / /h 15	VeV V "*#	V
 V 
eHeO$	%Vv +/#'	} DJ' d3i 	
  
c<(49-  QU :hsm :4> : 15	

 
 "*#	

 
 
h
" 15	

 
 "*#	

 
 
 
eHeO$	%
& 15	
;
 
 "*#	

 
 
h
" 
 +/"&'+'++/$C$I$-&1 #,415#Cy# # DJ'	#
 3-#  }#  }# $C=# SM# SM# SM# sm# # %SM# $DI.#  !#" 
!## #rx   r   )r~   loggingr   typingr   r   r   r   r   r   langchain_core.documentsr	   langchain_core.embeddingsr
   langchain_core.vectorstoresr   	getLoggerr   rT   r   r   rx   rM   <module>r      sC        . 0 3			8	$eK erx   