a
    bgWL                     @  s   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 deZ dS )    )annotationsN)AnyDictListOptionalSequenceTupleType)Document)
Embeddings)UpsertResponse)VectorStore)overrideZHNSWCSZ	langchaini  Zlc_textuniqueidc                      s  e Zd Zeedddejdfddddddddd	d
	 fddZd	dddZedOddddddZ	edddddZ
edPddddddddZedddddd d!Zd"d#d$d%d&ZdQd(dd)dd*d+d,ZedRd(dddd-d.d/ZedSdddd2ddd3d4d5ZedTd(ddd2ddd6d7d8ZeedUd9d:dd;dd d<d=d>Zeed9dddd d?d@dAZedd	dBdCdDZed	ddEdFZddd:dGdHdIZdJddKdLdMdNZ  ZS )V
ApertureDBNr   strzOptional[int]zOptional[str]intzOptional[Dict]r   None)	
embeddingsdescriptor_set
dimensionsenginemetric	log_level
propertieskwargsreturnc                   s"  zddl m}	m}
 W n ty.   tdY n0 t jf i | tt| _	| j	
| || _|| _|| _|| _|| _|| _|du r| j	d tdzddl m}	m}
 W n  ty   | j	d  Y n0 |
 | _|	| j| _z| j  W n" ty   | j	d  Y n0 |   dS )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setLevelr   embedding_functionr   r   r   r   fatal
ValueError	exception
connectionutilsstatus	Exception_find_or_add_descriptor_set)selfr   r   r   r   r   r   r   r   r    r!   	__class__ y/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/aperturedb.pyr%      sB    %
zApertureDB.__init__)r   c                 C  s  | j }d|dddddidig}| j|\}}| j sBJ |d|d d v rft|d d d nd}|dkszJ d|dkr|d d d d }| jd	| d
 |d }t|dksJ d| jd u r|d | _n.| j|d kr| jd| j d|d   |d }t|dks*J d| j	d u rB|d | _	n.| j	|d krp| jd| j	 d|d   |d }	| j
d u r|	| _
n&| j
|	kr| jd| j
 d|	  dd | D | _n| jd	| d | jd u rt| _| j	d u r t| _	| j
d u rt| jd| _
| jd ur>dd | j D nd }
| jj|| j
| j| j	|
d | jdd | jdd | jdt d S )NFindDescriptorSetTall_properties)	with_nameenginesmetricsr   resultsentitiesr      z+Multiple descriptor sets with the same namezDescriptor set z already existsZ_engineszOnly one engine is supportedzEngine mismatch: z != Z_metricszOnly one metric is supportedzMetric mismatch: Z_dimensionszDimensions mismatch: c                 S  s,   i | ]$\}}| tr|ttd  |qS N)
startswithPROPERTY_PREFIXlen.0kvr7   r7   r8   
<dictcomp>   s   
z:ApertureDB._find_or_add_descriptor_set.<locals>.<dictcomp>z does not exist. Creating ittestc                 S  s   i | ]\}}t | |qS r7   rC   rE   r7   r7   r8   rI          )namedimr   r   r   Z_DescriptorZ_create_txnZ_DescriptorSet_name)r   r/   querylast_query_okrD   r)   infor   errorr   r   itemsr   ENGINEMETRICr+   embed_queryr0   Zadd_descriptorsetZcreate_entity_indexUNIQUEID_PROPERTY)r4   r   Zfind_ds_queryrbZ
n_entitieser<   r=   r   r   r7   r7   r8   r3   l   s    



	z&ApertureDB._find_or_add_descriptor_setzOptional[List[str]]zOptional[bool])idsr   r   c                 K  s>   |dusJ dd| j td|gidig}| 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
        Nzids must be providedZDeleteDescriptorin)setconstraints)r   rX   r0   execute)r4   r\   r   rP   result_r7   r7   r8   delete   s    

	zApertureDB.deletezSequence[str]zList[Document])r\   r   c                  sV   d j td|giddidig} j|\}} fdd|d d d	g D }|S )
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.
        ZFindDescriptorr]   r:   T)r^   r_   r>   c                   s   g | ]}  |qS r7   _descriptor_to_documentrF   dr4   r7   r8   
<listcomp>   s   z)ApertureDB.get_by_ids.<locals>.<listcomp>r   r?   )r   rX   r0   r`   get)r4   r\   rP   r>   rb   docsr7   rh   r8   
get_by_ids   s    


zApertureDB.get_by_ids   )rP   rG   argsr   r   c                 O  s8   | j dusJ d| j |}| j||g|R 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.
        NzEmbedding function is not set)r+   rW   similarity_search_by_vector)r4   rP   rG   rn   r   	embeddingr7   r7   r8   similarity_search   s    zApertureDB.similarity_searchzList[Tuple[Document, float]])rP   rn   r   r   c                 O  s$   | j |}| j|g|R i |S rA   )r+   rW   '_similarity_search_with_score_by_vector)r4   rP   rn   r   rp   r7   r7   r8   similarity_search_with_score  s    z'ApertureDB.similarity_search_with_scoredictr
   )rg   r   c                 C  sV   i }|  D ]&\}}|tr|||ttd  < q|t }|t }t|||d}|S )Npage_contentmetadataid)rT   rB   rC   rD   TEXT_PROPERTYrX   r
   )r4   rg   rw   rG   rH   r   r   docr7   r7   r8   re     s    
z"ApertureDB._descriptor_to_documentFzList[float]bool)rp   rG   vectorsr   c                   s`   ddl m} | j}t }|j j||dd  jdt |  d  fdd|D S )	Nr   DescriptorsT)r^   vectork_neighborsZ	distances"ApertureDB similarity search took  secondsc                   s   g | ]}  ||d  fqS )Z	_distancerd   rf   rh   r7   r8   ri   #  rL   zFApertureDB._similarity_search_with_score_by_vector.<locals>.<listcomp>aperturedb.Descriptorsr~   r/   timeZfind_similarr   r)   rR   )r4   rp   rG   r|   r~   descriptors
start_timer7   rh   r8   rr     s    

z2ApertureDB._similarity_search_with_score_by_vector)rp   rG   r   r   c                   s^   ddl m} | j}t }|j j||d  jdt |  d  fdd|D S )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}   )r^   r   r   r   r   c                   s   g | ]}  |qS r7   rd   rf   rh   r7   r8   ri   <  rL   z:ApertureDB.similarity_search_by_vector.<locals>.<listcomp>r   )r4   rp   rG   r   r~   r   r   r7   rh   r8   ro   %  s    
z&ApertureDB.similarity_search_by_vector         ?float)rP   rG   fetch_klambda_multr   r   c                 K  s6   | j d|  | 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)   rR   r+   rW   'max_marginal_relevance_search_by_vector)r4   rP   rG   r   r   r   rp   r7   r7   r8   max_marginal_relevance_search>  s    z(ApertureDB.max_marginal_relevance_search)rp   rG   r   r   r   r   c           	        sb   ddl m} | j}t }|j j||||d  jdt |  d  fdd|D S )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}   )r^   r   r   r   r   z&ApertureDB similarity search mmr took r   c                   s   g | ]}  |qS r7   rd   rf   rh   r7   r8   ri   ~  rL   zFApertureDB.max_marginal_relevance_search_by_vector.<locals>.<listcomp>)r   r~   r/   r   Zfind_similar_mmrr   r)   rR   )	r4   rp   rG   r   r   r   r~   r   r   r7   rh   r8   r   Z  s    
z2ApertureDB.max_marginal_relevance_search_by_vectorzType[ApertureDB]z	List[str]zOptional[List[dict]])clstextsrp   	metadatasr   r   c                 K  s"   | f d|i|}| || |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
        r   )Z	add_texts)r   r   rp   r   r   storer7   r7   r8   
from_texts  s    zApertureDB.from_texts)r   	documentsrp   r   r   c                 K  s    | f d|i|}| | |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
        r   )add_documents)r   r   rp   r   r   r7   r7   r8   from_documents  s    
zApertureDB.from_documents)r   r   c                 C  s,   ddl m}m} | }||}|| dS )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!   Zremove_descriptorset)class_r   r    r!   dbr0   r7   r7   r8   delete_vectorstore  s    zApertureDB.delete_vectorstorec                 C  sX   ddl m} | }dddiddddig}||\}}| sHJ ||d d d S )zReturns a list of all vectorstores in the database

        Returns:
            List of descriptor sets with properties
        r   )r!   r9   r:   T)r>   r<   r=   r   r?   )r"   r!   rP   rQ   )r   r!   r   rP   responserb   r7   r7   r8   list_vectorstores  s    zApertureDB.list_vectorstores)r   r   r   c                 K  s~   d|v rd| d}|r.t|t|kr.tdg }t||D ]$\}}t|j|j|d}|| q<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.
        r\   zhThe number of ids must match the number of documents. Got {len(ids)} ids and {len(documents)} documents.ru   	succeeded)	poprD   r-   zipr
   rv   rw   appendupsert)r4   r   r   r\   Z
documents_Zid_ZdocumentZdoc_with_idr7   r7   r8   r     s     
zApertureDB.add_documentszSequence[Document]r   )rT   r   r   c                K  s   ddl m} dd |D }|r(| | dd |D }dd |D }| j|}dd |D }g }	t||||D ]h\}
}}}dd	 | D }|
|t< ||t< d
| j	|di}|g}t
j|t
jd g}|	||f qp|| j}|j|	td t|g dS )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   )ParallelLoaderc                 S  s&   g | ]}t |d r|jdur|jqS rx   N)hasattrrx   )rF   itemr7   r7   r8   ri     s   z%ApertureDB.upsert.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r7   )rv   rF   rz   r7   r7   r8   ri     rL   c                 S  s&   g | ]}t |d ddur|jni qS )rw   N)getattrrw   r   r7   r7   r8   ri     s   c                 S  s2   g | ]*}t |d r"|jdur"|jn
tt qS r   )r   rx   r   uuiduuid4r   r7   r7   r8   ri     s   c                 S  s   i | ]\}}t | |qS r7   rK   rE   r7   r7   r8   rI     rL   z%ApertureDB.upsert.<locals>.<dictcomp>ZAddDescriptor)r^   r   )Zdtype)Z	batchsize)r   failed)Zaperturedb.ParallelLoaderr   rc   r+   Zembed_documentsr   rT   ry   rX   r   nparrayZfloat32tobytesr   r/   Zingest	BATCHSIZEr   )r4   rT   r   r   Zids_to_deleter   r   r   r\   datar   rp   rw   	unique_idr   commandrP   Zblobsloaderr7   r7   r8   r     s@    

zApertureDB.upsert)N)rm   )rm   F)rm   )rm   r   r   )rm   r   r   )N)r(   
__module____qualname__r   DESCRIPTOR_SETr&   WARNr%   r3   rc   rl   rq   rs   re   rr   ro   r   r   classmethodr   r   r   r   r   r   __classcell__r7   r7   r5   r8   r      s^   &MV         % 'r   )!
__future__r   r&   r   r   typingr   r   r   r   r   r   r	   numpyr   Zlangchain_core.documentsr
   Zlangchain_core.embeddingsr   Zlangchain_core.indexing.baser   Zlangchain_core.vectorstoresr   Ztyping_extensionsr   rU   rV   r   r   rC   ry   rX   r   r7   r7   r7   r8   <module>   s$   $