
    dhWL                        S SK Jr  S SKrS SK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rS SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S	rS
rSrSrSrSrSr " S S\5      r g)    )annotationsN)AnyDictListOptionalSequenceTupleType)Document)
Embeddings)UpsertResponse)VectorStore)overrideHNSWCS	langchaini  lc_textuniqueidc                  b  ^  \ rS rSr\\SSS\R                  S4                 SU 4S jjj5       rSS jr	\SSS jj5       r
\SS j5       r\ S         SS jj5       r\        SS j5       rSS	 jr S       SS
 jjr\ S       S S jj5       r\   S!           S"S jj5       r\   S!           S#S jj5       r\\ S           S$S jj5       5       r\\          S%S j5       5       r\S&S j5       r\SS j5       rS'S jrS(S jrSrU =r$ ))
ApertureDB   Nc                  >  SSK Jn	Jn
  [        TU ]  " S0 UD6  [        R                  " [        5      U l	        U R                  R                  U5        X l        Xl        X0l        X@l        XPl        Xpl        Uc&  U R                  R#                  S5        [%        S5      e SSK Jn	Jn
  U
" 5       U l        U	" U R(                  5      U l         U R*                  R-                  5         U R1                  5         g! [         a    [        S5      ef = f! [         a    U R                  R'                  S5        e f = f! [.         a    U R                  R'                  S5        e f = f)a&  Create a vectorstore backed by ApertureDB

A single ApertureDB instance can support many vectorstores,
distinguished by 'descriptor_set' name.  The descriptor set is created
if it does not exist.  Different descriptor sets can use different
engines and metrics, be supplied by different embedding models, and have
different dimensions.

See ApertureDB documentation on `AddDescriptorSet`
https://docs.aperturedata.io/query_language/Reference/descriptor_commands/desc_set_commands/AddDescriptorSet
for more information on the engine and metric options.

Args:
    embeddings (Embeddings): Embeddings object
    descriptor_set (str, optional): Descriptor set name. Defaults to
        "langchain".
    dimensions (Optional[int], optional): Number of dimensions of the
        embeddings. Defaults to None.
    engine (str, optional): Engine to use. Defaults to "HNSW" for new
        descriptorsets.
    metric (str, optional): Metric to use. Defaults to "CS" for new
        descriptorsets.
    log_level (int, optional): Logging level. Defaults to logging.WARN.
r   Utilscreate_connectorzMApertureDB is not installed. Please install it using 'pip install aperturedb'NzNo embedding function provided.zFailed to connect to ApertureDB )aperturedb.Utilsr   r   ImportErrorsuper__init__logging	getLogger__name__loggersetLeveldescriptor_setembedding_function
dimensionsenginemetric
propertiesfatal
ValueError	exception
connectionutilsstatus	Exception_find_or_add_descriptor_set)self
embeddingsr'   r)   r*   r+   	log_levelr,   kwargsr   r   	__class__s              c/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/aperturedb.pyr!   ApertureDB.__init__   sD   J	@ 	"6"''1Y',",$$KK?@>??	@ +,4??+
	JJ
 	((*K  	+ 	,  	KK!!+ 	  	KK!!"CD	s#   C: #D D= :D'D:='E$c                |   U R                   n SUSSSSS0S.0/nU R                  R                  U5      u  p4U R                  R                  5       (       d   U5       eSUS   S   ;   a  [	        US   S   S   5      OSnUS::  d   S5       eUS:X  Ga  US   S   S   S   nU R
                  R                  S	U S
35        US   n[	        U5      S:X  d   S5       eU R                  c  US   U l        OAU R                  US   :w  a.  U R
                  R                  SU R                   SUS    35        US   n[	        U5      S:X  d   S5       eU R                  c  US   U l	        OAU R                  US   :w  a.  U R
                  R                  SU R                   SUS    35        US   n	U R                  c  Xl
        O;U R                  U	:w  a+  U R
                  R                  SU R                   SU	 35        UR                  5        V
Vs0 sH4  u  pU
R                  [        5      (       d  M!  U
[	        [        5      S  U_M6     snn
U l        g U R
                  R                  S	U S35        U R                  c  [        U l        U R                  c  [         U l	        U R                  c)  [	        U R"                  R%                  S5      5      U l
        U R                  b6  U R                  R                  5        V
Vs0 sH  u  p[        U
-   U_M     snn
OS nU R&                  R)                  UU R                  U R                  U R                  US9  U R&                  R+                  SS5        U R&                  R+                  SS5        U R&                  R+                  S[,        5        g s  snn
f s  snn
f )NFindDescriptorSetTall_properties)	with_nameenginesmetricsr)   resultsentitiesr      z+Multiple descriptor sets with the same namezDescriptor set z already exists_engineszOnly one engine is supportedzEngine mismatch: z != _metricszOnly one metric is supportedzMetric mismatch: _dimensionszDimensions mismatch: z does not exist. Creating ittest)namedimr*   r+   r,   _Descriptor_create_txn_DescriptorSet_name)r'   r0   querylast_query_oklenr%   infor*   errorr+   r)   items
startswithPROPERTY_PREFIXr,   ENGINEMETRICr(   embed_queryr1   add_descriptorsetcreate_entity_indexUNIQUEID_PROPERTY)r5   r'   find_ds_queryrb
n_entitieser@   rA   r)   kvr,   s                r:   r4   &ApertureDB._find_or_add_descriptor_setl   s   ,,E $!/##"& 0$7&

 $$]3,,..11. QqT"566 !()*56 	
 QM MM?!()*5a8AKK~.>oNO
mGw<1$D&DD${{"%aj
*!!$5dkk]$wqzl"ST
mGw<1$D&DD${{"%aj
*!!$5dkk]$wqzl"ST=)J&",J.!!+DOO+<DM GGI%DA<<0 -#o&()1,%DO KK!.!11MN {{"${{"$&"%d&=&=&I&I&&Q"R ??. 59OO4I4I4KL4KDA1$a'4KL  JJ((#OO{{{{% )  JJ**=-HJJ**+;WEJJ**=:KLE$ Ms   N23N2N8c                    Uc   S5       eSU R                   [        SU/0S.0/nU R                  R                  U5      u  pEU$ )zDelete documents from the vectorstore by id.

Args:
    ids: List of ids to delete from the vectorstore.

Returns:
    True if the deletion was successful, False otherwise
zids must be providedDeleteDescriptorin)setconstraints)r'   r\   r1   execute)r5   idsr8   rO   result_s         r:   deleteApertureDB.delete   s\     6 66 #..$5c{#C%
 JJ&&u-	    c                   SU R                   [        SU/0SS0S.0/nU R                  R                  U5      u  p4US   S   R	                  S/ 5       Vs/ sH  nU R                  U5      PM     nnU$ s  snf )zFind documents in the vectorstore by id.

Args:
    ids: List of ids to find in the vectorstore.

Returns:
    documents: List of Document objects found in the vectorstore.
FindDescriptorrg   r>   T)rh   ri   rB   r   rC   )r'   r\   r1   rj   get_descriptor_to_document)r5   rk   rO   rB   rm   ddocss          r:   
get_by_idsApertureDB.get_by_ids   s     !..$5c{#C 0$7#
 ZZ''.
 QZ 0155j"E
E ((+E 	 
 	
s   A3c                    U R                   c   S5       eU R                   R                  U5      nU R                  " XR/UQ70 UD6$ )zSearch for documents similar to the query using the vectorstore

Args:
    query: Query string to search for.
    k: Number of results to return.

Returns:
    List of Document objects ordered by decreasing similarity to the query.
zEmbedding function is not set)r(   rY   similarity_search_by_vector)r5   rO   rb   argsr8   	embeddings         r:   similarity_searchApertureDB.similarity_search   sM     &&2S4SS2++77>	//	NtNvNNrp   c                d    U R                   R                  U5      nU R                  " U/UQ70 UD6$ N)r(   rY   '_similarity_search_with_score_by_vector)r5   rO   r{   r8   r|   s        r:   similarity_search_with_score'ApertureDB.similarity_search_with_score  s6     ++77>	;;IWWPVWWrp   c                    0 nUR                  5        H5  u  p4UR                  [        5      (       d  M!  XBU[        [        5      S  '   M7     U[           nU[
           n[        XRUS9nU$ )Npage_contentmetadataid)rT   rU   rV   rQ   TEXT_PROPERTYr\   r   )r5   ru   r   rb   rc   r   r   docs           r:   rt   "ApertureDB._descriptor_to_document  se    GGIDA||O,,673/123  &'DI
rp   c                P   SSK Jn  U" U R                  5      n[        R                  " 5       nUR	                  U R
                  XSS9  U R                  R                  S[        R                  " 5       U-
   S35        U Vs/ sH  opR                  U5      US   4PM     sn$ s  snf )Nr   DescriptorsT)rh   vectork_neighbors	distances"ApertureDB similarity search took  seconds	_distance	aperturedb.Descriptorsr   r0   timefind_similarr'   r%   rR   rt   )r5   r|   rb   vectorsr   descriptors
start_timeru   s           r:   r   2ApertureDB._similarity_search_with_score_by_vector  s     	7!$//2YY[
  ##IPT 	! 	
 	0z1I0J(S	
 LWW;a--a0!K.A;WWWs   B#c                D   SSK Jn  U" U R                  5      n[        R                  " 5       nUR	                  U R
                  XS9  U R                  R                  S[        R                  " 5       U-
   S35        U Vs/ sH  opR                  U5      PM     sn$ s  snf )a  Returns the k most similar documents to the given embedding vector

Args:
    embedding: The embedding vector to search for
    k: The number of similar documents to return

Returns:
    List of Document objects ordered by decreasing similarity to the query.
r   r   )rh   r   r   r   r   r   )r5   r|   rb   r8   r   r   r   ru   s           r:   rz   &ApertureDB.similarity_search_by_vector%  s     	7!$//2YY[
  ##I 	! 	
 	0z1I0J(S	
 :EEA,,Q/EEEs   Bc                    U R                   R                  SU 35        U R                  R                  U5      nU R                  " XbX440 UD6$ )a  Returns similar documents to the query that also have diversity

This algorithm balances relevance and diversity in the search results.

Args:
    query: Query string to search for.
    k: Number of results to return.
    fetch_k: Number of results to fetch.
    lambda_mult: Lambda multiplier for MMR.

Returns:
    List of Document objects ordered by decreasing similarity/diversty.
z)Max Marginal Relevance search for query: )r%   rR   r(   rY   'max_marginal_relevance_search_by_vector)r5   rO   rb   fetch_klambda_multr8   r|   s          r:   max_marginal_relevance_search(ApertureDB.max_marginal_relevance_search>  sT    , 	DUGLM++77>	;;'
28
 	
rp   c                J   SSK Jn  U" U R                  5      n[        R                  " 5       nUR	                  U R
                  UUUUS9  U R                  R                  S[        R                  " 5       U-
   S35        U V	s/ sH  oR                  U	5      PM     sn	$ s  sn	f )a  Returns similar documents to the vector that also have diversity

This algorithm balances relevance and diversity in the search results.

Args:
    embedding: Embedding vector to search for.
    k: Number of results to return.
    fetch_k: Number of results to fetch.
    lambda_mult: Lambda multiplier for MMR.

Returns:
    List of Document objects ordered by decreasing similarity/diversty.
r   r   )rh   r   r   r   r   z&ApertureDB similarity search mmr took r   )	r   r   r0   r   find_similar_mmrr'   r%   rR   rt   )
r5   r|   rb   r   r   r8   r   r   r   ru   s
             r:   r   2ApertureDB.max_marginal_relevance_search_by_vectorZ  s    , 	7!$//2YY[
$$### 	% 	
 	4TYY[:5M4NhW	
 :EEA,,Q/EEEs   B c                <    U " SSU0UD6nUR                  X5        U$ )a  Creates a new vectorstore from a list of texts

Args:
    texts: List of text strings
    embedding: Embeddings object as for constructing the vectorstore
    metadatas: Optional list of metadatas associated with the texts.
    kwargs: Additional arguments to pass to the constructor
r6   r   )	add_texts)clstextsr|   	metadatasr8   stores         r:   
from_textsApertureDB.from_texts  s'    " 3y3F3)rp   c                <    U " SSU0UD6nUR                  U5        U$ )a(  Creates a new vectorstore from a list of documents

Args:
    documents: List of Document objects
    embedding: Embeddings object as for constructing the vectorstore
    metadatas: Optional list of metadatas associated with the texts.
    kwargs: Additional arguments to pass to the constructor
r6   r   )add_documents)r   	documentsr|   r8   r   s        r:   from_documentsApertureDB.from_documents  s)      3y3F3I&rp   c                T    SSK JnJn  U" 5       nU" U5      nUR                  U5        g)z}Deletes a vectorstore and all its data from the database

Args:
    descriptor_set: The name of the descriptor set to delete
r   r   N)r   r   r   remove_descriptorset)class_r'   r   r   dbr1   s         r:   delete_vectorstoreApertureDB.delete_vectorstore  s&     	=b	"">2rp   c                    SSK Jn  U" 5       nSSS0SSSS.0/nUR                  U5      u  pEUR                  5       (       d   U5       eUS   S   S   $ )ziReturns a list of all vectorstores in the database

Returns:
    List of descriptor sets with properties
r   )r   r=   r>   T)rB   r@   rA   r)   rC   )r   r   rO   rP   )r   r   r   rO   responserm   s         r:   list_vectorstoresApertureDB.list_vectorstores  sv     	6 $ 0$7##"&&

 hhuo!!+8+!{./
;;rp   c                B   SU;   a  UR                  S5      nU(       a#  [        U5      [        U5      :w  a  [        S5      e/ n[        X15       H5  u  pV[	        UR
                  UR                  US9nUR                  U5        M7     OUnU R                  " U40 UD6S   $ )a  Add or update documents in the vectorstore.

Args:
    documents: Documents to add to the vectorstore.
    kwargs: Additional keyword arguments.
        if kwargs contains ids and documents contain ids,
        the ids in the kwargs will receive precedence.

Returns:
    List of IDs of the added texts.

Raises:
    ValueError: If the number of ids does not match the number of documents.
rk   zhThe number of ids must match the number of documents. Got {len(ids)} ids and {len(documents)} documents.r   	succeeded)	poprQ   r.   zipr   r   r   appendupsert)r5   r   r8   rk   
documents_id_documentdoc_with_ids           r:   r   ApertureDB.add_documents  s      F?**U#Cs3x3y>1 I 
 J!$S!4&!)!6!6%..
 !!+. "5 #J {{:00==rp   c                  SSK Jn  U Vs/ sH1  n[        US5      (       d  M  UR                  c  M%  UR                  PM3     nnU(       a  U R	                  U5        U Vs/ sH  ofR
                  PM     nnU Vs/ sH  n[        USS5      b  UR                  O0 PM!     nnU R                  R                  U5      n	U Vs/ sHK  n[        US5      (       a  UR                  b  UR                  O[        [        R                  " 5       5      PMM     n
n/ n[        XyX5       H  u  ppUR                  5        VVs0 sH  u  nn[        U-   U_M     nnnUU[         '   UU["        '   SU R$                  US.0nU/n[&        R(                  " U[&        R*                  S9R-                  5       /nUR/                  UU45        M     U" U R0                  5      nUR3                  U[4        S	9  [7        U
/ S
9$ s  snf s  snf s  snf s  snf s  snnf )zInsert or update items

Updating documents is dependent on the documents' `id` attribute.

Args:
    items: List of Document objects to upsert

Returns:
    UpsertResponse object with succeeded and failed
r   )ParallelLoaderr   Nr   AddDescriptor)rh   r,   )dtype)	batchsize)r   failed)aperturedb.ParallelLoaderr   hasattrr   rn   r   getattrr   r(   embed_documentsstruuiduuid4r   rT   rV   r   r\   r'   nparrayfloat32tobytesr   r0   ingest	BATCHSIZEr   )r5   rT   r8   r   itemids_to_deleter   r   r   r6   rk   datar   r|   r   	unique_idrb   rc   r,   commandrO   blobsloaders                          r:   r   ApertureDB.upsert  s     	= !&$
 %t)<GGDGG 	 $
 KK&-23Uc!!U3 
 $CT:FCLLBN 	 
 ,,<<UC
 
 c4((SVV-?CFFSEVV 	 

 47y5
0DX >F^^=MN=MTQ/A-q0=MJN(,J}%,5J()..","G IEXXirzz:BBDEEKK'5
  0di0B77G$
 4


 Os(   G2G2G2 G7:%G< AH:H)	r0   r'   r)   r(   r*   r%   r+   r,   r1   )r6   r   r'   r   r)   zOptional[int]r*   Optional[str]r+   r   r7   intr,   zOptional[Dict]r8   r   returnNone)r   r   r   )rk   zOptional[List[str]]r8   r   r   zOptional[bool])rk   zSequence[str]r   List[Document])   )
rO   r   rb   r   r{   r   r8   r   r   r   )rO   r   r{   r   r8   r   r   List[Tuple[Document, float]])ru   dictr   r   )r   F)r|   List[float]rb   r   r   boolr   r   )r|   r   rb   r   r8   r   r   r   )r      g      ?)rO   r   rb   r   r   r   r   floatr8   r   r   r   )r|   r   rb   r   r   r   r   r   r8   r   r   r   )r   Type[ApertureDB]r   	List[str]r|   r   r   zOptional[List[dict]]r8   r   r   r   )
r   r   r   r   r|   r   r8   r   r   r   )r'   r   r   r   )r   r   r8   r   r   r   )rT   zSequence[Document]r8   r   r   r   )r$   
__module____qualname____firstlineno__r   DESCRIPTOR_SETr"   WARNr!   r4   rn   rw   r}   r   rt   r   rz   r   r   classmethodr   r   r   r   r   r   __static_attributes____classcell__)r9   s   @r:   r   r      sJ    -$( $ $ %)K+K+ K+ "	K+
 K+ K+ K+ #K+ K+ 
K+ K+ZTMl  ,  4 #$OO O-0O<?O	O O  XX!$X03X	%X X CHX$X),X;?X	%X /0F$F),F<?F	F F0   

 
 	

 
 
 

 
6   #F#F #F 	#F
 #F #F 
#F #FJ 
 +/	  (	
  
  & !  	
 
  $ 
3 
3 < <0%>N48 48rp   r   )!
__future__r   r"   r   r   typingr   r   r   r   r   r	   r
   numpyr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.indexing.baser   langchain_core.vectorstoresr   typing_extensionsr   rW   rX   r   r   rV   r   r\   r   r   rp   r:   <module>r     sd    "    C C C  . 0 7 3 & 
		 M8 M8rp   