
    hWL                        d dl mZ 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mZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZ d	Zd
ZdZdZdZdZdZ G d de      Z y)    )annotationsN)AnyDictListOptionalSequenceTupleType)Document)
Embeddings)UpsertResponse)VectorStore)overrideHNSWCS	langchaini  lc_textuniqueidc                  &    e Zd Zeedddej                  df	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd       ZddZeddd       Z	edd       Z
e	 d	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 	 	 dd       ZddZ	 d	 	 	 	 	 	 	 dd	Ze	 d	 	 	 	 	 	 	 dd
       Ze	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d d       Ze	 	 	 d	 	 	 	 	 	 	 	 	 	 	 d!d       Zee	 d	 	 	 	 	 	 	 	 	 	 	 d"d              Zee	 	 	 	 	 	 	 	 	 	 d#d              Zed$d       Zedd       Zd%dZd&dZ xZS )'
ApertureDBNc                   	 ddl m}	m}
 t	        |   di | t        j                  t              | _	        | j                  j                  |       || _        || _        || _        || _        || _        || _        |&| j                  j#                  d       t%        d      	 ddl m}	m}
  |
       | _         |	| j(                        | _        	 | j*                  j-                          | j1                          y# t        $ r t        d      w xY w# t        $ r | j                  j'                  d        w xY w# t.        $ r | j                  j'                  d        w xY w)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              i/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/aperturedb.pyr    zApertureDB.__init__   sP   J	@ 	"6"''1Y',",$$KK?@>??	@ +,4??+
	JJ
 	((*K  	+ 	,  	KK!!+ 	  	KK!!"CD	s#   C= &D D> =D&D;>&E$c                J   | j                   }	 d|dddddidig}| j                  j                  |      \  }}| j                  j                         sJ |       d|d   d   v rt	        |d   d   d         nd}|dk  sJ d       |dk(  r|d   d   d   d   }| j
                  j                  d	| d
       |d   }t	        |      dk(  sJ d       | j                  |d   | _        n@| j                  |d   k7  r.| j
                  j                  d| j                   d|d           |d   }t	        |      dk(  sJ d       | j                  |d   | _	        n@| j                  |d   k7  r.| j
                  j                  d| j                   d|d           |d   }	| j                  |	| _
        n:| j                  |	k7  r+| j
                  j                  d| j                   d|	        |j                         D 
ci c]-  \  }
}|
j                  t              r|
t	        t              d  |/ c}}
| _        y | j
                  j                  d	| d       | j                  t        | _        | j                  t         | _	        | j                  )t	        | j"                  j%                  d            | _
        | j                  5| j                  j                         D 
ci c]  \  }
}t        |
z   | c}}
nd }| j&                  j)                  || j                  | j                  | j                  |       | j&                  j+                  dd       | j&                  j+                  dd       | j&                  j+                  dt,               y c c}}
w c c}}
w )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&   r/   querylast_query_oklenr$   infor)   errorr*   r(   items
startswithPROPERTY_PREFIXr+   ENGINEMETRICr'   embed_queryr0   add_descriptorsetcreate_entity_indexUNIQUEID_PROPERTY)r4   r&   find_ds_queryrb
n_entitieser>   r?   r(   kvr+   s                r9   r3   z&ApertureDB._find_or_add_descriptor_setl   s   ,,E $!/##"& 0$7&

 $$]31,,.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Aq<<0 #o&()1,DO KK!.!11MN {{"${{"$&"%d&=&=&I&I&&Q"R ??. 59OO4I4I4KLDAq1$a'L  JJ((#OO{{{{% )  JJ**=-HJJ**+;WEJJ**=:KLE$ Ms   2N(Nc                    |J d       d| j                   t        d|gidig}| j                  j                  |      \  }}|S )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&   rZ   r0   execute)r4   idsr7   rM   result_s         r9   deletezApertureDB.delete   s^     6 66 #..$5c{#C%
 JJ&&u-	    c                   d| j                   t        d|giddidig}| j                  j                  |      \  }}|d   d   j	                  dg       D cg c]  }| j                  |       }}|S c c}w )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.
        FindDescriptorrd   r<   T)re   rf   r@   r   rA   )r&   rZ   r0   rg   get_descriptor_to_document)r4   rh   rM   r@   rj   ddocss          r9   
get_by_idszApertureDB.get_by_ids   s     !..$5c{#C 0$7#
 ZZ''.
 QZ 0155j"E
 ((+
 
 	
s   A2c                    | j                   J d       | j                   j                  |      } | j                  ||g|i |S )a  Search 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'   rW   similarity_search_by_vector)r4   rM   r`   argsr7   	embeddings         r9   similarity_searchzApertureDB.similarity_search   sQ     &&2S4SS2++77>	/t//	1NtNvNNrl   c                d    | j                   j                  |      } | j                  |g|i |S N)r'   rW   '_similarity_search_with_score_by_vector)r4   rM   rv   r7   rw   s        r9   similarity_search_with_scorez'ApertureDB.similarity_search_with_score  s8     ++77>	;t;;IWWPVWWrl   c                    i }|j                         D ]0  \  }}|j                  t              s|||t        t              d  <   2 |t           }|t
           }t        |||      }|S )Npage_contentmetadataid)rR   rS   rT   rO   TEXT_PROPERTYrZ   r   )r4   rq   r   r`   ra   r   r   docs           r9   rp   z"ApertureDB._descriptor_to_document  sm    GGI 	8DAq||O,673/123	8 &'D8I
rl   c                R   ddl m}  || j                        }t        j                         }|j	                  | j
                  ||d       | j                  j                  dt        j                         |z
   d       |D cg c]  }| j                  |      |d   f c}S c c}w )Nr   DescriptorsT)re   vectork_neighbors	distances"ApertureDB similarity search took  seconds	_distance	aperturedb.Descriptorsr   r/   timefind_similarr&   r$   rP   rp   )r4   rw   r`   vectorsr   descriptors
start_timerq   s           r9   r{   z2ApertureDB._similarity_search_with_score_by_vector  s     	7!$//2YY[
  ##I1PT 	! 	
 	0z1I0J(S	
 LWWa--a0!K.AWWWs   B$c                F   ddl m}  || j                        }t        j                         }|j	                  | j
                  ||       | j                  j                  dt        j                         |z
   d       |D cg c]  }| j                  |       c}S c c}w )a3  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   )re   r   r   r   r   r   )r4   rw   r`   r7   r   r   r   rq   s           r9   ru   z&ApertureDB.similarity_search_by_vector%  s     	7!$//2YY[
  ##I1 	! 	
 	0z1I0J(S	
 :EEA,,Q/EEEs   Bc                    | j                   j                  d|        | j                  j                  |      } | j                  ||||fi |S )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$   rP   r'   rW   'max_marginal_relevance_search_by_vector)r4   rM   r`   fetch_klambda_multr7   rw   s          r9   max_marginal_relevance_searchz(ApertureDB.max_marginal_relevance_search>  sZ    , 	DUGLM++77>	;t;;q';
28
 	
rl   c                J   ddl m}  || j                        }t        j                         }|j	                  | j
                  ||||       | j                  j                  dt        j                         |z
   d       |D 	cg c]  }	| j                  |	       c}	S c c}	w )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   )re   r   r   r   r   z&ApertureDB similarity search mmr took r   )	r   r   r/   r   find_similar_mmrr&   r$   rP   rp   )
r4   rw   r`   r   r   r7   r   r   r   rq   s
             r9   r   z2ApertureDB.max_marginal_relevance_search_by_vectorZ  s    , 	7!$//2YY[
$$### 	% 	
 	4TYY[:5M4NhW	
 :EEA,,Q/EEEs   B c                >     | dd|i|}|j                  ||       |S )aL  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
        r5   r   )	add_texts)clstextsrw   	metadatasr7   stores         r9   
from_textszApertureDB.from_texts  s)    " 3y3F3y)rl   c                <     | dd|i|}|j                  |       |S )aX  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
        r5   r   )add_documents)r   	documentsrw   r7   r   s        r9   from_documentszApertureDB.from_documents  s)      3y3F3I&rl   c                T    ddl m}m}  |       } ||      }|j                  |       y)zDeletes 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   dbr0   s         r9   delete_vectorstorezApertureDB.delete_vectorstore  s&     	=b	"">2rl   c                    ddl m}  |       }dddiddddig}|j                  |      \  }}|j                         sJ |       |d   d   d   S )zReturns a list of all vectorstores in the database

        Returns:
            List of descriptor sets with properties
        r   )r   r;   r<   T)r@   r>   r?   r(   rA   )r   r   rM   rN   )r   r   r   rM   responserj   s         r9   list_vectorstoreszApertureDB.list_vectorstores  su     	6 $ 0$7##"&&

 hhuo!!+8+!{./
;;rl   c                8   d|v r|j                  d      }|r"t        |      t        |      k7  rt        d      g }t        ||      D ]8  \  }}t	        |j
                  |j                  |      }|j                  |       : n|} | j                  |fi |d   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.
        rh   zhThe number of ids must match the number of documents. Got {len(ids)} ids and {len(documents)} documents.r~   	succeeded)	poprO   r-   zipr   r   r   appendupsert)r4   r   r7   rh   
documents_id_documentdoc_with_ids           r9   r   zApertureDB.add_documents  s      F?**U#Cs3x3y>1 I 
 J!$S)!4 /X&!)!6!6%..
 !!+./ #J t{{:00==rl   c                  ddl m} |D cg c](  }t        |d      s|j                  |j                  * }}|r| j	                  |       |D cg c]  }|j
                   }}|D cg c]  }t        |dd      |j                  ni  }}| j                  j                  |      }	|D cg c]C  }t        |d      r|j                  |j                  nt        t        j                               E }
}g }t        ||	||
      D ]  \  }}}}|j                         D ci c]  \  }}t        |z   | }}}||t         <   ||t"        <   d| j$                  |di}|g}t'        j(                  |t&        j*                        j-                         g}|j/                  ||f         || j0                        }|j3                  |t4        	       t7        |
g 
      S c c}w c c}w c c}w c c}w c c}}w )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)re   r+   )dtype)	batchsize)r   failed)aperturedb.ParallelLoaderr   hasattrr   rk   r   getattrr   r'   embed_documentsstruuiduuid4r   rR   rT   r   rZ   r&   nparrayfloat32tobytesr   r/   ingest	BATCHSIZEr   )r4   rR   r7   r   itemids_to_deleter   r   r   r5   rh   datar   rw   r   	unique_idr`   ra   r+   commandrM   blobsloaders                          r9   r   zApertureDB.upsert  s    	= !&$
t)<ATDGG$
 $
 KK&-23c!!33 
 $CT:FCLLBN
	 
 ,,<<UC
 
 c4(SVV-?CFFSEVV
 

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


 Os(   GGGG#*"G(-AG-"G2)r5   r   r&   r   r(   zOptional[int]r)   Optional[str]r*   r   r6   intr+   zOptional[Dict]r7   r   returnNone)r   r   rz   )rh   zOptional[List[str]]r7   r   r   zOptional[bool])rh   zSequence[str]r   List[Document])   )
rM   r   r`   r   rv   r   r7   r   r   r   )rM   r   rv   r   r7   r   r   List[Tuple[Document, float]])rq   dictr   r   )r   F)rw   List[float]r`   r   r   boolr   r   )rw   r   r`   r   r7   r   r   r   )r      g      ?)rM   r   r`   r   r   r   r   floatr7   r   r   r   )rw   r   r`   r   r   r   r   r   r7   r   r   r   )r   Type[ApertureDB]r   	List[str]rw   r   r   zOptional[List[dict]]r7   r   r   r   )
r   r   r   r   rw   r   r7   r   r   r   )r&   r   r   r   )r   r   r7   r   r   r   )rR   zSequence[Document]r7   r   r   r   )r#   
__module____qualname__r   DESCRIPTOR_SETr!   WARNr    r3   rk   rs   rx   r|   rp   r{   ru   r   r   classmethodr   r   r   r   r   r   __classcell__)r8   s   @r9   r   r      sE    -$( $ $ %)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8rl   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   rU   rV   r   r   rT   r   rZ   r   r   rl   r9   <module>r      sd    "    C C C  . 0 7 3 & 
		 M8 M8rl   