o
    ZhR                     @  s   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rFd dlZe ZdZG d	d
 d
eZdS )    )annotationsN)	TYPE_CHECKINGAnyDictIterableListOptionalSetTupleType)Document)
Embeddings)VectorStore)maximal_marginal_relevance   c                   @  sZ  e Zd ZU dZdZded< edddfdZddZed[ddZ		d\d]ddZ	d^dd Z
eddfd_d(d)Zeddfd`d+d,Z	-dadbd.d/Zdedddfdcd3d4Z	-	5	6		ddded:d;Z	-	5	6		dddfd=d>Z					dgdhdEdFZ	didjdGdHZ	didkdIdJZd^dKdLZd^dMdNZdldOdPZdmdQdRZeddeddfdndUdVZededdfdodXdYZdS )pAwaDBz`AwaDB` vector store.Zlangchain_awadbstr_DEFAULT_TABLE_NAMEN
table_name	embeddingOptional[Embeddings]log_and_data_dirOptional[str]clientOptional[awadb.Client]kwargsr   returnNonec                 K  s   zddl }W n ty   tdw |dur|| _n|dur%||| _n| | _|| jkr@|d7 }|tt dd 7 }| j	| i | _
|durR|| 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_clientZClientr   r   uuiduuid4splitCreatetable2embeddingsusing_table_name)selfr   r   r   r   r   r!    r+   ]/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/vectorstores/awadb.py__init__   s(   



zAwaDB.__init__c                 C  s   | j | jv r| j| j  S d S N)r)   r(   )r*   r+   r+   r,   
embeddingsK   s   zAwaDB.embeddingstextsIterable[str]	metadatasOptional[List[dict]]is_duplicate_textsOptional[bool]	List[str]c                 K  sN   | j du r	tdd}| j| jv r| 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(   Zembed_documentslistZAddTexts)r*   r0   r2   r4   r   r/   r+   r+   r,   	add_textsQ   s   
zAwaDB.add_textsboolc                 K  s   | j du r	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
        Nr7   )r#   r:   Load)r*   r   r   r+   r+   r,   
load_localt   s   
zAwaDB.load_localquerykinttext_in_page_contentmeta_filterOptional[dict]List[Document]c           	      K  sj   | j du r	tdd}| j| jv r| j| j |}nddlm} | |}h d}| j|||||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.
        Nr7   r   AwaEmbedding>   scorer9   _idrC   rD   not_include_fields_in_metadata)	r#   r:   r)   r(   embed_queryr!   rH   	Embeddingsimilarity_search_by_vector)	r*   r@   rA   rC   rD   r   r   rH   not_include_fieldsr+   r+   r,   similarity_search   s   
zAwaDB.similarity_searchList[Tuple[Document, float]]c                 K  s   | j du r	tdd}| j| jv r| j| j |}nddlm} | |}g }ddh}	| j|||||	d}
|
D ]}|j	d }|j	d= ||f}|
| q9|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.
        Nr7   r   rG   r9   rJ   rK   rI   )r#   r:   r)   r(   rM   r!   rH   rN   rO   metadataappend)r*   r@   rA   rC   rD   r   r   rH   resultsrP   Zretrieval_docsdocrI   Z	doc_tupler+   r+   r,   similarity_search_with_score   s,   

z"AwaDB.similarity_search_with_scorer   c                 K  s   | j ||fi |S r.   )rW   )r*   r@   rA   r   r+   r+   r,   (_similarity_search_with_relevance_scores   s   z.AwaDB._similarity_search_with_relevance_scoresOptional[List[float]]rL   Optional[Set[str]]c                 K  s   | j du r	tdg }|du r|S | j j|||||d}| dkr$|S |d d D ],}	d}
i }|	D ]}|dkr=|	| }
q2|durF||v rFq2|	| ||< q2|t|
|d 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.
        Nr7   )rC   rD   rP   r   ZResultItems r8   page_contentrS   )r#   r:   ZSearch__len__rT   r   )r*   r   rA   rC   rD   rL   r   rU   Zshow_resultsZitem_detailcontentZ	meta_dataZitem_keyr+   r+   r,   rO      s6   
z!AwaDB.similarity_search_by_vector         ?fetch_klambda_multfloatc                 K  sx   | j du r	tdg }| j| jv r| j| j |}nddlm}	 |	 |}| dkr/g S | 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:
            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.
        Nr7   r   rG   )rc   rC   rD   )
r#   r:   r)   r(   rM   r!   rH   rN   r^   'max_marginal_relevance_search_by_vector)r*   r@   rA   rb   rc   rC   rD   r   r   rH   rU   r+   r+   r,   max_marginal_relevance_search)  s$   
z#AwaDB.max_marginal_relevance_searchList[float]c                 K  s   | j du r	tdg }|du r|S ddh}	| j|||||	d}
g }|
D ]
}||jd  q#ttj|tjd|d}|D ]}d|
| jv rR|
| jd= ||
|  q<|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.
        Nr7   rJ   rI   rK   r9   )Zdtype)Zembedding_list)	r#   r:   rO   rT   rS   r   nparrayZfloat32)r*   r   rA   rb   rc   rC   rD   r   rU   rP   Zretrieved_docsZtop_embeddingsrV   Zselected_docsZs_idr+   r+   r,   re   ^  s2   
z-AwaDB.max_marginal_relevance_search_by_vectoridsOptional[List[str]]rP   limitOptional[int]Dict[str, Document]c                 K  s   | j du r	td| j j|||||d}i }|D ]/}	d}
i }|	D ]}|dkr+|	| }
q |dks3|dkr4q |	| ||< q t|
|d}|||	d < q|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.
        Nr7   )rj   rC   rD   rP   rl   r[   r8   r9   rJ   r\   )r#   r:   ZGetr   )r*   rj   rC   rD   rP   rl   r   Zdocs_detailrU   Z
doc_detailr_   Z	meta_infofieldrV   r+   r+   r,   get  s.   
z	AwaDB.getc                 K  s>   | j du r	tdd}|du s| 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.
        Nr7   r   )r#   r:   r^   Delete)r*   rj   r   retr+   r+   r,   delete  s   
zAwaDB.deletec                 K  s&   | j du r	td| j j|d||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.
        Nr7   r8   )rj   Ztext_field_namer0   r2   )r#   r:   ZUpdateTexts)r*   rj   r0   r2   r   r+   r+   r,   update  s
   
zAwaDB.updatec                 K  (   | j du rdS | j |}|r|| _|S )zCreate a new table.NF)r#   r'   r)   r*   r   r   rr   r+   r+   r,   create_table  s   
zAwaDB.create_tablec                 K  ru   )zJUse the specified table. Don't know the tables, please invoke list_tables.NF)r#   ZUser)   rv   r+   r+   r,   use  s   
z	AwaDB.usec                 K  s   | j du rg S | j  S )z*List all the tables created by the client.N)r#   ZListAllTablesr*   r   r+   r+   r,   list_tables  s   

zAwaDB.list_tablesc                 K  s   | j S )zGet the current table.)r)   ry   r+   r+   r,   get_current_table(  s   zAwaDB.get_current_tableclsType[AwaDB]c           	      K  s"   | ||||d}|j ||d |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   )r0   r2   )r<   )	r|   r0   r   r2   r   r   r   r   r#   r+   r+   r,   
from_texts0  s   zAwaDB.from_texts	documentsc           	      K  s2   dd |D }dd |D }| j ||||||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                 S     g | ]}|j qS r+   )r]   .0rV   r+   r+   r,   
<listcomp>j      z(AwaDB.from_documents.<locals>.<listcomp>c                 S  r   r+   )rS   r   r+   r+   r,   r   k  r   )r0   r   r2   r   r   r   )r~   )	r|   r   r   r   r   r   r   r0   r2   r+   r+   r,   from_documentsQ  s   zAwaDB.from_documents)r   r   r   r   r   r   r   r   r   r   r   r   )r   r   )NN)
r0   r1   r2   r3   r4   r5   r   r   r   r6   )r   r   r   r   r   r=   )r@   r   rA   rB   rC   r   rD   rE   r   r   r   rF   )r@   r   rA   rB   rC   r   rD   rE   r   r   r   rR   )r   )r@   r   rA   rB   r   r   r   rR   )r   rY   rA   rB   rC   r   rD   rE   rL   rZ   r   r   r   rF   )r   r`   ra   NN)r@   r   rA   rB   rb   rB   rc   rd   rC   r   rD   rE   r   r   r   rF   )r   rg   rA   rB   rb   rB   rc   rd   rC   r   rD   rE   r   r   r   rF   )NNNNN)rj   rk   rC   r   rD   rE   rP   rZ   rl   rm   r   r   r   rn   r.   )rj   rk   r   r   r   r5   )
rj   r6   r0   r1   r2   r3   r   r   r   r6   )r   r   r   r6   )r   r   r   r   )r|   r}   r0   r6   r   r   r2   r3   r   r   r   r   r   r   r   r   r   r   )r|   r}   r   rF   r   r   r   r   r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r-   propertyr/   r<   r?   DEFAULT_TOPNrQ   rW   rX   rO   rf   re   rp   rs   rt   rw   rx   rz   r{   classmethodr~   r   r+   r+   r+   r,   r      s   
 1
#17
:8@4



 r   )
__future__r   loggingr$   typingr   r   r   r   r   r   r	   r
   r   numpyrh   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.vectorstoresr   Z&langchain_community.vectorstores.utilsr   r!   	getLoggerloggerr   r   r+   r+   r+   r,   <module>   s    ,