
    3fi^M                         d dl Z d dlZd dlZd dlm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$                         Z G d d      Zd	eeef   d
eeef   fdZ G d de      Zy)    N)sha1)AnyDictIterableListOptionalTuple)Document)
Embeddings)VectorStorec                   t    e Zd ZdZ	 	 	 	 ddedededededeeef   d	ed
ededeee      ddfdZdede	fdZ
y)AlibabaCloudOpenSearchSettingsu  Alibaba Cloud Opensearch` client configuration.

    Attribute:
        endpoint (str) : The endpoint of opensearch instance, You can find it
         from the console of Alibaba Cloud OpenSearch.
        instance_id (str) : The identify of opensearch instance, You can find
         it from the console of Alibaba Cloud OpenSearch.
        username (str) : The username specified when purchasing the instance.
        password (str) : The password specified when purchasing the instance，
          After the instance is created, you can modify it on the console.
        tablename (str): The table name specified during instance configuration.
        field_name_mapping (Dict) : Using field name mapping between opensearch
          vector store and opensearch instance configuration table field names:
        {
            'id': 'The id field name map of index document.',
            'document': 'The text field name map of index document.',
            'embedding': 'In the embedding field of the opensearch instance,
              the values must be in float type and separated by separator,
              default is comma.',
            'metadata_field_x': 'Metadata field mapping includes the mapped
             field name and operator in the mapping value, separated by a comma
             between the mapped field name and the operator.',
        }
        protocol (str): Communication Protocol between SDK and Server, default is http.
        namespace (str) : The instance data will be partitioned based on the "namespace"
         field,If the namespace is enabled, you need to specify the namespace field
         name during initialization, Otherwise, the queries cannot be executed
         correctly.
        embedding_field_separator(str): Delimiter specified for writing vector
         field data, default is comma.
        output_fields: Specify the field list returned when invoking OpenSearch,
         by default it is the value list of the field mapping field.
    Nendpointinstance_idusernamepassword
table_namefield_name_mappingprotocol	namespaceembedding_field_separatoroutput_fieldsreturnc                    || _         || _        || _        || _        || _        || _        || _        dj                  | j                  | j                  g      | _        || _	        |	| _
        |
?| j                  j                         D cg c]  }|j                  d      d    c}| _        i | _        | j                  j                         D ]&  \  }}|| j                  |j                  d      d   <   ( y c c}w )N_,r   )r   r   r   r   r   r   r   joinopt_table_namer   r   valuessplitr   inverse_field_name_mappingitems)selfr   r   r   r   r   r   r   r   r   r   fieldkeyvalues                 v/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/vectorstores/alibabacloud_opensearch.py__init__z'AlibabaCloudOpenSearchSettings.__init__1   s     !&   "$!hh(8(8$//'JK"4)B& 151H1H1O1O1Q"(-C #"D ;='11779 	GJCCFD++EKK,<Q,?@	G	"s   
C8itemc                     t        | |      S N)getattr)r#   r)   s     r'   __getitem__z*AlibabaCloudOpenSearchSettings.__getitem__P   s    tT""    )http r   N)__name__
__module____qualname____doc__strr   r   r   r(   r   r-    r.   r'   r   r      s     T ),-1GG G 	G
 G G !cNG G G $'G  S	*G 
G># # #r.   r   fieldsr   c                 d    i }| j                         D ]  \  }}|dk(  s
|dk(  s|dk(  r|||<    |S )zCreate metadata from fields.

    Args:
        fields: The fields of the document. The fields must be a dict.

    Returns:
        metadata: The metadata of the document. The metadata must be a dict.
    iddocument	embedding)r"   )r7   metadatar%   r&   s       r'   create_metadatar=   T   sJ      "Hlln 
U$;#+sk/A Or.   c                   V   e Zd ZdZdedededdfdZ	 ddee	   d	e
ee      dedee	   fd
Z	 	 dde	dede
ee	ef      dedee   f
dZ	 	 dde	dede
e   dedeeeef      f
dZ	 	 ddee   dede
e   dedee   f
dZ	 	 ddee   de
ee	ef      dedee	ef   fdZdee	ef   dee   fdZdee	ef   dee	ef   fdZdee	ef   deeeef      fdZdee	   defdZdee	   defdZe	 	 d dee	   ded	e
ee      de
e   dedd fd       Ze	 ddee   dede
e   dedd f
d       Zy)!AlibabaCloudOpenSearchz(`Alibaba Cloud OpenSearch` vector store.r;   configkwargsr   Nc           	      j   	 ddl m}m} ddlm} || _        || _        |j                  ddddd	      | _        |j                  |j                  |j                  |j                  |j                  |j                  |j                  
            | _        i | _        y # t        $ r t	        d      w xY w)Nr   )clientmodelsrD   z}Could not import alibaba cloud opensearch python package. Please install it with `pip install alibabacloud-ha3engine-vector`.i  i'  F2   )connect_timeoutread_timeout	autoretry
ignore_sslmax_idle_conns)r   r   r   access_user_nameaccess_pass_word)alibabacloud_ha3engine_vectorrC   rD   alibabacloud_tea_utilImportErrorr@   r;   RuntimeOptionsruntimeClientConfigr   r   r   r   r   ha3_engine_clientoptions_headers)r#   r;   r@   rA   rC   rD   util_modelss          r'   r(   zAlibabaCloudOpenSearch.__init__h   s    	DC ""11  2 
 "(MM"..!'!'  "
 025  	V 	s   B B2texts	metadatasc                 .    dt         t           dt         t           f fd}ddlm |D cg c]*  }t        |j                  d            j                         , }} j                  j                  t        |            }|xs |D cg c]  }i  c}} j                  j                  g }	t        |      }
t        |      D ]:  \  }}|t        |      k  r||   nd}|t        |      k  r||   nd}|t        |
      k  r|
|   nd}t               }t               }|j!                  d   |       |j!                  d	   |       |?|j!                  d
    j                  j"                  j%                  d |D                     |?|j'                         D ],  \  }}|j!                  |   j)                  d      d   |       . |j!                  d|       |j!                  dd       |	j+                  |       =  ||	      S c c}w c c}w )a  Insert documents into the instance..
        Args:
            texts: The text segments to be inserted into the vector storage,
             should not be empty.
            metadatas: Metadata information.
        Returns:
            id_list: List of document IDs.
        push_doc_listr   c                    | t        |       dk(  rg S 	 j                  j                  |       }j                  j	                  j
                  j                  d   |      }t        j                  |j                        }|d   dk(  r| D cg c]  }|d   d       c}S g S c c}w # t        $ rL}t        j                  dj
                  j                   dj
                  j                   d|       |d }~ww xY w)	Nr   r9   statusOKr7   zadd doc to endpoint: instance_id: failed.)lenPushDocumentsRequestrV   rU   push_documentsr@   r   jsonloadsbody	Exceptionloggererrorr   r   )	r[   push_requestpush_responsejson_responsepush_docefield_name_maprD   r#   s	         r'   _upsertz1AlibabaCloudOpenSearch.add_texts.<locals>._upsert   s   $M(:a(?	%::((-  !% 6 6 E EKK..t0Dl! !%

=+=+= > *d2 )6$ !*>$+?@  		
  *4;;+?+?*@ A##';;#:#:";8E
 s1   A;B, B'"B, %B, 'B, ,	D5AC<<Dr   rE   utf-8Nr9   r:   r;   c              3   2   K   | ]  }t        |        y wr+   )r5   ).0units     r'   	<genexpr>z3AlibabaCloudOpenSearch.add_texts.<locals>.<genexpr>   s      ?&*D	?s   r   r7   cmdadd)r   r   r5   rN   rD   r   encode	hexdigestr;   embed_documentslistr@   r   	enumeratera   dict__setitem__r   r   r"   r    append)r#   rX   rY   rA   rp   tid_list
embeddingsr   add_doc_list	text_listidxdoc_idr;   r<   textadd_docadd_doc_fieldsmd_keymd_valuero   rD   s   `                   @@r'   	add_textsz AlibabaCloudOpenSearch.add_texts   s   	4: 	$s) 	2 	9@EF14)*446FF^^33DK@
4e!4"!4	77K	$W- 	)KC+.Z+@
3dI),s9~)=y~4H%(3y>%99S>tD&*fG-1VN&&~d';VD&&~j'A4H$**";/KK99>> ?.7?  #(0(8 $FH"..&v.44S9!<h .9u-(-	). |$$; G!4s   /H	Hqueryksearch_filterc                 ~    | j                   j                  |      }| j                  | j                  |||            S )u  Perform similarity retrieval based on text.
        Args:
            query: Vectorize text for retrieval.，should not be empty.
            k: top n.
            search_filter: Additional filtering conditions.
        Returns:
            document_list: List of documents.
        r;   r   r   )r;   embed_querycreate_resultsinner_embedding_queryr#   r   r   r   rA   r;   s         r'   similarity_searchz(AlibabaCloudOpenSearch.similarity_search   sE     NN..u5	""&&#=A ' 
 	
r.   c                 ~    | j                   j                  |      }| j                  | j                  |||            S )u'  Perform similarity retrieval based on text with scores.
        Args:
            query: Vectorize text for retrieval.，should not be empty.
            k: top n.
            search_filter: Additional filtering conditions.
        Returns:
            document_list: List of documents.
        r   )r;   r   create_results_with_scorer   r   s         r'   'similarity_search_with_relevance_scoresz>AlibabaCloudOpenSearch.similarity_search_with_relevance_scores   sE     "&!;!;E!B	--&&#=A ' 
 	
r.   c                 H    | j                  | j                  |||            S )zPerform retrieval directly using vectors.
        Args:
            embedding: vectors.
            k: top n.
            search_filter: Additional filtering conditions.
        Returns:
            document_list: List of documents.
        r   )r   r   )r#   r;   r   r   rA   s        r'   similarity_search_by_vectorz2AlibabaCloudOpenSearch.similarity_search_by_vector  s1     ""&&#=A ' 
 	
r.   c           	      6   	 dt         ffd	dt         dt        dt         f fddt        t         t        f   f	 fd}ddlm 	  |       }d	|v rad
|v r]t        |d
         dkD  rLt        j                  d j                  j                   d j                  j                   d|d
    d       i S |S # t        $ rP}t        j                  d j                  j                   d j                  j                   d|       Y d }~i S d }~ww xY w)Nr   c            
          ydj                  j                         D  cg c]  \  } } | |       c}}       }|S c c}} w )Nr0   z AND )r   r"   )r   r   filter_clausecreate_filterr   s      r'   generate_filter_queryzKAlibabaCloudOpenSearch.inner_embedding_query.<locals>.generate_filter_query  sS    $#LL -:,?,?,A( "&(3M ! s   A 
r   r   c                 V   j                   j                  |    }|y|j                  d      }t        |      dk7  rt        j                  d| d       y|d   j                         }|d   j                         }t        |t        j                        r
| d| d| S | | d	| d	S )
Nr0   r      zfilter zA express is not correct, must contain mapping field and operator.r       ")
r@   r   r    ra   rh   ri   strip
isinstancenumbersNumber)r   r   md_filter_exprexprmd_filter_keymd_filter_operatorr#   s         r'   r   zCAlibabaCloudOpenSearch.inner_embedding_query.<locals>.create_filter(  s    ![[;;FCN%!'',D4yA~n- .? @  GMMOM!%a(GNN3'*<)=QxjII#_%7$8(1EEr.   c            	          j                   j                  j                   j                  dj                   j                                } j                  j                  |       }t        j                  |j                        S )NT)r   r   vectorinclude_vectorr   filtertop_k)	r@   r   r   r   rU   r   rd   re   rf   )requestquery_resultQueryRequestr;   r   r   r#   s     r'   search_datazAAlibabaCloudOpenSearch.inner_embedding_query.<locals>.search_data9  sp    ";;11++// #"kk77,.G  1177@L::l//00r.   r   )r   	errorCodeerrorMsgzquery r   z failed:.zquery instance endpoint:r_   r`   )r5   r   r   $alibabacloud_ha3engine_vector.modelsr   ra   rh   ri   r@   r   r   rg   )
r#   r;   r   r   r   rl   rn   r   r   r   s
   ````   @@@r'   r   z,AlibabaCloudOpenSearch.inner_embedding_query  s+   		!s 		!	F# 	F 	F 	F"	1T#s(^ 	1 	1 	F	'MM},-/j12Q6T[[112!DKK4K4K3L M+J78; 	 %$ 	LL*4;;+?+?*@ A#{{667xA 
 		s    A*B? =B? ?	DADDjson_resultc           	      .   |d   }g }|D ]  }d|vs| j                   j                  d   |d   vr|j                  t                      ?|d   }|j                  t        || j                   j                  d      | j	                  |                    |S )zAssemble documents.resultr7   r:   page_contentr<   )r@   r   r   r
   create_inverse_metadatar#   r   r"   query_result_listr)   r7   s         r'   r   z%AlibabaCloudOpenSearch.create_results^  s    H%,. 	D$;;11*=T(^S!((4h!((%+DKK,J,J:,V%W!%!=!=f!E	 ! r.   r7   c                     i }|j                         D ]1  \  }}|dk(  s
|dk(  s|dk(  r||| j                  j                  |   <   3 |S )zCreate metadata from fields.

        Args:
            fields: The fields of the document. The fields must be a dict.

        Returns:
            metadata: The metadata of the document. The metadata must be a dict.
        r9   r:   r;   )r"   r@   r!   )r#   r7   r<   r%   r&   s        r'   r   z.AlibabaCloudOpenSearch.create_inverse_metadatar  s\     $& ,,. 	JJCd{cZ/3+3EDIHT[[;;C@A	J r.   c           	          |d   }g }|D ]Z  }|d   }|j                  t        || j                  j                  d      | j	                  |            t        |d         f       \ |S )zParsing the returned results with scores.
        Args:
            json_result: Results from OpenSearch query.
        Returns:
            query_result_list: Results with scores.
        r   r7   r:   r   score)r   r
   r@   r   r   floatr   s         r'   r   z0AlibabaCloudOpenSearch.create_results_with_score  s     H%:< 
	D(^F$$%+DKK,J,J:,V%W!%!=!=f!E $w-(
	 ! r.   c                     |D cg c]*  }t        |j                  d            j                         , }}| j                  |      S c c}w )zDelete documents based on their page content.

        Args:
            texts: List of document page content.
        Returns:
           Whether the deletion was successful or not.
        rq   )r   rx   ry   !delete_documents_with_document_id)r#   rX   r   r   s       r'   delete_documents_with_textsz2AlibabaCloudOpenSearch.delete_documents_with_texts  sG     AFF14)*446FF55g>> Gs   /Ar   c                 d   |t        |      dk(  ryddlm} g }|D ]/  }|j                  | j                  j
                  d   |idd       1 |j                  | j                  |      }	 | j                  j                  | j                  j                  | j                  j
                  d   |      }t        j                  |j                        }|d   d	k(  S # t        $ rL}t        j!                  d
| j                  j"                   d| j                  j$                   d|       |d}~ww xY w)zDelete documents based on their IDs.

        Args:
            id_list: List of document IDs.
        Returns:
            Whether the deletion was successful or not.
        Nr   TrE   r9   delete)r7   rv   r]   r^   zdelete doc from :r_   r`   )ra   rN   rD   r   r@   r   rb   rV   rU   rc   r   rd   re   rf   rg   rh   ri   r   r   )	r#   r   rD   delete_doc_listr   delete_requestdelete_responserl   rn   s	            r'   r   z8AlibabaCloudOpenSearch.delete_documents_with_document_id  s0    ?c'la/8 	F""#{{==dCVL#	  44  /
	"44CC**..t4O
 !JJ';';<M *d22 	LL#DKK$8$8#9 :#{{667xA
 G	s   +A.C 	D/#AD**D/c                     |t        |      dk(  rt        d      |t        d      |t        d       | ||fi |}|j                  ||       |S )a  Create alibaba cloud opensearch vector store instance.

        Args:
            texts: The text segments to be inserted into the vector storage,
             should not be empty.
            embedding: Embedding function, Embedding function.
            config: Alibaba OpenSearch instance configuration.
            metadatas: Metadata information.
        Returns:
            AlibabaCloudOpenSearch: Alibaba cloud opensearch vector store instance.
        r   z0the inserted text segments, should not be empty.#the embeddings should not be empty.zconfig should not be none.)rX   rY   )ra   rg   r   )clsrX   r;   rY   r@   rA   ctxs          r'   
from_textsz!AlibabaCloudOpenSearch.from_texts  sh    ( =CJ!ONOOABB>899)V.v.EY7
r.   	documentsc                    |t        |      dk(  rt        d      |t        d      |t        d      |D cg c]  }|j                   }}|D cg c]  }|j                   }} | j                  d||||d|S c c}w c c}w )a5  Create alibaba cloud opensearch vector store instance.

        Args:
            documents: Documents to be inserted into the vector storage,
             should not be empty.
            embedding: Embedding function, Embedding function.
            config: Alibaba OpenSearch instance configuration.
            ids: Specify the ID for the inserted document. If left empty, the ID will be
             automatically generated based on the text content.
        Returns:
            AlibabaCloudOpenSearch: Alibaba cloud opensearch vector store instance.
        r   z,the inserted documents, should not be empty.r   zconfig can't be none)rX   r;   rY   r@   r6   )ra   rg   r   r<   r   )r   r   r;   r@   rA   drX   rY   s           r'   from_documentsz%AlibabaCloudOpenSearch.from_documents  s    ( I! 3JKKABB>233)23A33)23AQZZ3	3s~~ 
	

 
 	
 43s   A?Br+   )   N)Nr   )NN) r1   r2   r3   r4   r   r   r   r(   r   r5   r   r   r}   r   intr   r
   r   r	   r   r   r   r   r   r   r   boolr   r   classmethodr   r   r6   r.   r'   r?   r?   e   s
   2#2#2 /#2 	#2
 
#2P +/G%}G% DJ'G% 	G%
 
cG%X 26	

 
  S#X/	

 
 
h
2 (,	

 
  ~	

 
 
eHeO$	%
2 (,	
;
 
  ~	

 
 
h
0 37	E;E  S#X/E 	E
 
c3hEN!$sCx. !T(^ !(d38n c3h  !S>!	eHeO$	%!0	?c 	?t 	?'c 't 'R 
 +/;?Cy  DJ'	
 78  
" @ 
 <@	$
>$
 $
 78	$

 $
 
"$
 $
r.   r?   )rd   loggingr   hashlibr   typingr   r   r   r   r   r	   langchain_core.documentsr
   langchain_core.embeddingsr   langchain_core.vectorstoresr   	getLoggerrh   r   r5   r=   r?   r6   r.   r'   <module>r      sn        = = - 0 3				C# C#LDcN tCH~ "o
[ o
r.   