
    NgR                        d dl m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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 erd dlZ ej                    ZdZ G d	 d
e          ZdS )    )annotationsN)	TYPE_CHECKINGAnyDictIterableListOptionalSetTupleType)Document)
Embeddings)VectorStore)maximal_marginal_relevance   c                  \   e Zd ZU dZdZded<   edddfdGdZedHd            Z	 	 dIdJdZ	dKdZ
eddfdLd$ZeddfdMd&Z	 dNdOd(ZdedddfdPd,Z	 	 	 	 	 dQdRd2Z	 	 	 	 	 dQdSd4Z	 	 	 	 	 dTdUd;Z	 dVdWd<Z	 dVdXd=ZdKd>ZdKd?ZdYd@ZdZdAZeddeddfd[dD            Zededdfd\dF            ZdS )]AwaDBz`AwaDB` vector store.langchain_awadbstr_DEFAULT_TABLE_NAMEN
table_name	embeddingOptional[Embeddings]log_and_data_dirOptional[str]clientOptional[awadb.Client]kwargsr   returnNonec                   	 ddl }n# t          $ r t          d          w xY w||| _        n,| |j        |          | _        n |j                    | _        || j        k    rA|dz  }|t          t          j                                                  d          d         z  }| j        	                    |           i | _
        |
|| j
        |<   || _        dS )a@  Initialize with AwaDB client.
           If table_name is not specified,
           a random table name of `_DEFAULT_TABLE_NAME + last segment of uuid`
           would be created automatically.

        Args:
            table_name: Name of the table created, default _DEFAULT_TABLE_NAME.
            embedding: Optional Embeddings initially set.
            log_and_data_dir: Optional the root directory of log and data.
            client: Optional AwaDB client.
            kwargs: Any possible extend parameters in the future.

        Returns:
            None.
        r   NzRCould not import awadb python package. Please install it with `pip install awadb`._-)awadbImportErrorawadb_clientClientr   r   uuiduuid4splitCreatetable2embeddingsusing_table_name)selfr   r   r   r   r   r%   s          b/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/awadb.py__init__zAwaDB.__init__   s   .	LLLL 	 	 	>  	  &D+$0EL1A$B$B!!$0ELNN!111#J#djll++11#66r::J  ,,,79 09D!*- *s    !c                F    | j         | j        v r| j        | j                  S d S N)r.   r-   )r/   s    r0   
embeddingszAwaDB.embeddingsK   s)     D$999()>??t    textsIterable[str]	metadatasOptional[List[dict]]is_duplicate_textsOptional[bool]	List[str]c                    | j         t          d          d}| j        | j        v r2| j        | j                                     t          |                    }| j                             dd||||          S )a  Run more texts through the embeddings and add to the vectorstore.
        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            is_duplicate_texts: Optional whether to duplicate texts. Defaults to True.
            kwargs: any possible extend parameters in the future.

        Returns:
            List of ids from adding the texts into the vectorstore.
        NAwaDB client is None!!!embedding_texttext_embedding)r'   
ValueErrorr.   r-   embed_documentslistAddTexts)r/   r6   r8   r:   r   r4   s         r0   	add_textszAwaDB.add_textsQ   s    " $6777
 D$999.t/DEUUU J  ))
 
 	
r5   boolc                b    | j         t          d          | j                             |          S )zLoad the local specified table.

        Args:
            table_name: Table name
            kwargs: Any possible extend parameters in the future.

        Returns:
            Success or failure of loading the local specified table
        Nr>   )r'   rA   Load)r/   r   r   s      r0   
load_localzAwaDB.load_localt   s3     $6777 %%j111r5   querykinttext_in_page_contentmeta_filterOptional[dict]List[Document]c                   | j         t          d          d}| j        | j        v r&| j        | j                                     |          }n#ddlm}  |                                |          }h d}|                     |||||          S )a8  Return docs most similar to query.

        Args:
            query: Text query.
            k: The maximum number of documents to return.
            text_in_page_content: Filter by the text in page_content of Document.
            meta_filter (Optional[dict]): Filter by metadata. Defaults to None.
            E.g. `{"color" : "red", "price": 4.20}`. Optional.
            E.g. `{"max_price" : 15.66, "min_price": 4.20}`
            `price` is the metadata field, means range filter(4.20<'price'<15.66).
            E.g. `{"maxe_price" : 15.66, "mine_price": 4.20}`
            `price` is the metadata field, means range filter(4.20<='price'<=15.66).
            kwargs: Any possible extend parameters in the future.

        Returns:
            Returns the k most similar documents to the specified text query.
        Nr>   r   AwaEmbedding>   _idscorer@   rM   rN   not_include_fields_in_metadata)	r'   rA   r.   r-   embed_queryr%   rS   	Embeddingsimilarity_search_by_vector)	r/   rJ   rK   rM   rN   r   r   rS   not_include_fieldss	            r0   similarity_searchzAwaDB.similarity_search   s    4 $6777	 D$999-d.CDPPQVWWII******$0077I'I'I'I//!5#+= 0 
 
 	
r5   List[Tuple[Document, float]]c                   | j         t          d          d}| j        | j        v r&| j        | j                                     |          }n#ddlm}  |                                |          }g }ddh}	|                     |||||	          }
|
D ]0}|j	        d         }|j	        d= ||f}|
                    |           1|S )	a  The most k similar documents and scores of the specified query.

        Args:
            query: Text query.
            k: The k most similar documents to the text query.
            text_in_page_content: Filter by the text in page_content of Document.
            meta_filter: Filter by metadata. Defaults to None.
            kwargs: Any possible extend parameters in the future.

        Returns:
            The k most similar documents to the specified text query.
            0 is dissimilar, 1 is the most similar.
        Nr>   r   rR   r@   rT   rV   rU   )r'   rA   r.   r-   rX   r%   rS   rY   rZ   metadataappend)r/   rJ   rK   rM   rN   r   r   rS   resultsr[   retrieval_docsdocrU   	doc_tuples                 r0   similarity_search_with_scorez"AwaDB.similarity_search_with_score   s   , $6777	 D$999-d.CDPPQVWWII******$0077I02(8%'@99!5#+= : 
 
 " 	& 	&CL)EW%eINN9%%%%r5   r   c                      | j         ||fi |S r3   )re   )r/   rJ   rK   r   s       r0   (_similarity_search_with_relevance_scoresz.AwaDB._similarity_search_with_relevance_scores   s!     1t0DDVDDDr5   Optional[List[float]]rW   Optional[Set[str]]c                n   | j         t          d          g }||S | j                             |||||          }|                                dk    r|S |d         d         D ]P}	d}
i }|	D ]#}|dk    r	|	|         }
|||v r|	|         ||<   $|                    t          |
|                     Q|S )	a
  Return docs most similar to embedding vector.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            text_in_page_content: Filter by the text in page_content of Document.
            meta_filter: Filter by metadata. Defaults to None.
            not_incude_fields_in_metadata: Not include meta fields of each document.

        Returns:
            List of Documents which are the most similar to the query vector.
        Nr>   )rM   rN   r[   r   ResultItems r?   page_contentr_   )r'   rA   Search__len__r`   r   )r/   r   rK   rM   rN   rW   r   ra   show_resultsitem_detailcontent	meta_dataitem_keys                r0   rZ   z!AwaDB.similarity_search_by_vector   s   , $6777"$N(//!5#= 0 
 
 !!Q&&N'?=9 	O 	OKGI' < <///)(3G3?#AAA &1(&;	(##NN89MMMNNNNr5            ?fetch_klambda_multfloatc                N   | j         t          d          g }| j        | j        v r&| j        | j                                     |          }n#ddlm}	  |	                                |          }|                                dk    rg S | 	                    ||||||          }
|
S )a  Return docs selected using the maximal marginal relevance.

        Maximal marginal relevance optimizes for similarity to query AND diversity
        among selected documents.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            fetch_k: Number of Documents to fetch to pass to MMR algorithm.
            lambda_mult: Number between 0 and 1 that determines the degree
                        of diversity among the results with 0 corresponding
                        to maximum diversity and 1 to minimum diversity.
                        Defaults to 0.5.
            text_in_page_content: Filter by the text in page_content of Document.
            meta_filter (Optional[dict]): Filter by metadata. Defaults to None.

        Returns:
            List of Documents selected by maximal marginal relevance.
        Nr>   r   rR   )ry   rM   rN   )
r'   rA   r.   r-   rX   r%   rS   rY   rp   'max_marginal_relevance_search_by_vector)r/   rJ   rK   rx   ry   rM   rN   r   r   rS   ra   s              r0   max_marginal_relevance_searchz#AwaDB.max_marginal_relevance_search)  s    : $6777!#	 D$999-d.CDPPQVWWII******$0077I!##I>>#!5# ? 
 
 r5   List[float]c                   | j         t          d          g }||S ddh}	|                     |||||	          }
g }|
D ]"}|                    |j        d                    #t          t          j        |t          j                  |          }|D ]:}d|
|         j        v r)|
|         j        d= |                    |
|                    ;|S )	a  Return docs selected using the maximal marginal relevance.

        Maximal marginal relevance optimizes for similarity to query AND diversity
        among selected documents.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            fetch_k: Number of Documents to fetch to pass to MMR algorithm.
            lambda_mult: Number between 0 and 1 that determines the degree
                        of diversity among the results with 0 corresponding
                        to maximum diversity and 1 to minimum diversity.
                        Defaults to 0.5.
            text_in_page_content: Filter by the text in page_content of Document.
            meta_filter (Optional[dict]): Filter by metadata. Defaults to None.

        Returns:
            List of Documents selected by maximal marginal relevance.
        Nr>   rT   rU   rV   r@   )dtype)embedding_list)	r'   rA   rZ   r`   r_   r   nparrayfloat32)r/   r   rK   rx   ry   rM   rN   r   ra   r[   retrieved_docstop_embeddingsrc   selected_docss_ids                  r0   r|   z-AwaDB.max_marginal_relevance_search_by_vector^  s   < $6777"$N#('"299!5#+= : 
 
 ! 	B 	BC!!#,/?"@AAAA2HYbj111.
 
 
 " 	5 	5D>$#7#@@@"4(12BC~d3444r5   idsOptional[List[str]]r[   limitOptional[int]Dict[str, Document]c                   | j         t          d          | j                             |||||          }i }|D ]N}	d}
i }|	D ])}|dk    r	|	|         }
|dk    s|dk    r|	|         ||<   *t          |
|          }|||	d         <   O|S )	a  Return docs according ids.

        Args:
            ids: The ids of the embedding vectors.
            text_in_page_content: Filter by the text in page_content of Document.
            meta_filter: Filter by any metadata of the document.
            not_include_fields: Not pack the specified fields of each document.
            limit: The number of documents to return. Defaults to 5. Optional.

        Returns:
            Documents which satisfy the input conditions.
        Nr>   )r   rM   rN   r[   r   rl   r?   r@   rT   rm   )r'   rA   Getr   )r/   r   rM   rN   r[   r   r   docs_detailra   
doc_detailrs   	meta_infofieldrc   s                 r0   getz	AwaDB.get  s    , $6777'++!5#1 , 
 
 (*% 	- 	-JGI# 5 5,,,(/G...%5..#-e#4	%  )DDDC),GJu%&&r5   c                    | j         t          d          d}||                                dk    r|S | j                             |          }|S )aJ  Delete the documents which have the specified ids.

        Args:
            ids: The ids of the embedding vectors.
            **kwargs: Other keyword arguments that subclasses might use.

        Returns:
            Optional[bool]: True if deletion is successful.
            False otherwise, None if not implemented.
        Nr>   r   )r'   rA   rp   Delete)r/   r   r   rets       r0   deletezAwaDB.delete  sX     $6777";#++--1,,J&&s++
r5   c                j    | j         t          d          | j                             |d||          S )a@  Update the documents which have the specified ids.

        Args:
            ids: The id list of the updating embedding vector.
            texts: The texts of the updating documents.
            metadatas: The metadatas of the updating documents.
        Returns:
            the ids of the updated documents.
        Nr>   r?   )r   text_field_namer6   r8   )r'   rA   UpdateTexts)r/   r   r6   r8   r   s        r0   updatezAwaDB.update  sF    " $6777 ,,%5Ui - 
 
 	
r5   c                ^    | j         dS | j                             |          }|r|| _        |S )zCreate a new table.NF)r'   r,   r.   r/   r   r   r   s       r0   create_tablezAwaDB.create_table  s=     $5&&z22 	/$.D!
r5   c                ^    | j         dS | j                             |          }|r|| _        |S )zJUse the specified table. Don't know the tables, please invoke list_tables.NF)r'   User.   r   s       r0   usez	AwaDB.use  s=     $5##J// 	/$.D!
r5   c                F    | j         g S | j                                         S )z*List all the tables created by the client.)r'   ListAllTablesr/   r   s     r0   list_tableszAwaDB.list_tables  s'     $I ..000r5   c                    | j         S )zGet the current table.)r.   r   s     r0   get_current_tablezAwaDB.get_current_table(  s     $$r5   clsType[AwaDB]c                R     | ||||          }|                     ||           |S )a3  Create an AwaDB vectorstore from a raw documents.

        Args:
            texts (List[str]): List of texts to add to the table.
            embedding (Optional[Embeddings]): Embedding function. Defaults to None.
            metadatas (Optional[List[dict]]): List of metadatas. Defaults to None.
            table_name (str): Name of the table to create.
            log_and_data_dir (Optional[str]): Directory of logging and persistence.
            client (Optional[awadb.Client]): AwaDB client

        Returns:
            AwaDB: AwaDB vectorstore.
        )r   r   r   r   )r6   r8   )rE   )	r   r6   r   r8   r   r   r   r   r'   s	            r0   
from_textszAwaDB.from_texts0  sG    0 s!-	
 
 
 	Ui@@@r5   	documentsc                h    d |D             }d |D             }|                      ||||||          S )av  Create an AwaDB vectorstore from a list of documents.

        If a log_and_data_dir specified, the table will be persisted there.

        Args:
            documents (List[Document]): List of documents to add to the vectorstore.
            embedding (Optional[Embeddings]): Embedding function. Defaults to None.
            table_name (str): Name of the table to create.
            log_and_data_dir (Optional[str]): Directory to persist the table.
            client (Optional[awadb.Client]): AwaDB client.
            Any: Any possible parameters in the future

        Returns:
            AwaDB: AwaDB vectorstore.
        c                    g | ]	}|j         
S  )rn   .0rc   s     r0   
<listcomp>z(AwaDB.from_documents.<locals>.<listcomp>j  s    777c!777r5   c                    g | ]	}|j         
S r   )r_   r   s     r0   r   z(AwaDB.from_documents.<locals>.<listcomp>k  s    777cS\777r5   )r6   r   r8   r   r   r   )r   )	r   r   r   r   r   r   r   r6   r8   s	            r0   from_documentszAwaDB.from_documentsQ  sW    2 87Y77777Y777	~~!-  
 
 	
r5   )r   r   r   r   r   r   r   r   r   r   r   r    )r   r   )NN)
r6   r7   r8   r9   r:   r;   r   r   r   r<   )r   r   r   r   r   rF   )rJ   r   rK   rL   rM   r   rN   rO   r   r   r   rP   )rJ   r   rK   rL   rM   r   rN   rO   r   r   r   r]   )r   )rJ   r   rK   rL   r   r   r   r]   )r   rh   rK   rL   rM   r   rN   rO   rW   ri   r   r   r   rP   )r   rv   rw   NN)rJ   r   rK   rL   rx   rL   ry   rz   rM   r   rN   rO   r   r   r   rP   )r   r~   rK   rL   rx   rL   ry   rz   rM   r   rN   rO   r   r   r   rP   )NNNNN)r   r   rM   r   rN   rO   r[   ri   r   r   r   r   r   r   r3   )r   r   r   r   r   r;   )
r   r<   r6   r7   r8   r9   r   r   r   r<   )r   r   r   r<   )r   r   r   r   )r   r   r6   r<   r   r   r8   r9   r   r   r   r   r   r   r   r   r   r   )r   r   r   rP   r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r1   propertyr4   rE   rI   DEFAULT_TOPNr\   re   rg   rZ   r}   r|   r   r   r   r   r   r   r   classmethodr   r   r   r5   r0   r   r      s        00000 .*.*.)-/+ /+ /+ /+ /+b    X +/-1	!
 !
 !
 !
 !
F2 2 2 2. .2&*,
 ,
 ,
 ,
 ,
b .2&*2 2 2 2 2n E E E E E ,0.2&*=A5 5 5 5 5t  .2&*3 3 3 3 3p  .2&*< < < < <@ $(.2&*15#0 0 0 0 0h $(    6 +/	
 
 
 
 
0        	1 	1 	1 	1% % % %  +/*.-*.)-    [@  +/-*.)-!
 !
 !
 !
 [!
 !
 !
r5   r   )
__future__r   loggingr)   typingr   r   r   r   r   r	   r
   r   r   numpyr   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   r%   	getLoggerloggerr   r   r   r5   r0   <module>r      s#   " " " " " "   W W W W W W W W W W W W W W W W W W W W W W     - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3 M M M M M M LLL				^	
 ^	
 ^	
 ^	
 ^	
K ^	
 ^	
 ^	
 ^	
 ^	
r5   