a
    bgR                     @  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rd 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                   @  s  e Zd ZU dZdZded< edddfddddd	d
dddZeddddZdTdddd	ddddZ	dd	ddddZ
eddfddddd	ddd d!Zeddfddddd	d"dd#d$ZdUddd	d"d&d'd(Zdedddfd)dddd*d	dd+d,d-ZdVdddd0ddd	dd1d2d3ZdWd4ddd0ddd	dd5d6d7ZdXd8ddd*d9d	d:d;d<d=ZdYd8d	dd>d?d@ZdZdddd	ddAdBdCZdd	dddDdEZdd	dddFdGZd	ddHdIdJZd	ddHdKdLZeddeddfdMddddddd	d dN	dOdPZededdfdMdddddd	d dQdRdSZdS )[AwaDBz`AwaDB` vector store.Zlangchain_awadbstr_DEFAULT_TABLE_NAMENzOptional[Embeddings]zOptional[str]zOptional[awadb.Client]r   None)
table_name	embeddinglog_and_data_dirclientkwargsreturnc                 K  s   zddl }W n ty&   tdY n0 |dur8|| _n |durN||| _n
| | _|| jkr|d7 }|tt dd 7 }| j	| i | _
|dur|| 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(   t/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/awadb.py__init__   s&    



zAwaDB.__init__)r   c                 C  s   | j | jv r| j| j  S d S N)r&   r%   )r'   r(   r(   r)   
embeddingsK   s    zAwaDB.embeddingszIterable[str]zOptional[List[dict]]zOptional[bool]z	List[str])texts	metadatasis_duplicate_textsr   r   c                 K  sN   | j du rtdd}| j| jv r8| 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'   r-   r.   r/   r   r,   r(   r(   r)   	add_textsQ   s    
zAwaDB.add_textsbool)r   r   r   c                 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
        Nr0   )r    r3   Load)r'   r   r   r(   r(   r)   
load_localt   s    
zAwaDB.load_localintzOptional[dict]zList[Document])queryktext_in_page_contentmeta_filterr   r   c           	      K  sj   | j du rtdd}| j| jv r6| 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.
        Nr0   r   AwaEmbedding>   scorer2   _idr<   r=   not_include_fields_in_metadata)	r    r3   r&   r%   embed_queryr   r?   	Embeddingsimilarity_search_by_vector)	r'   r:   r;   r<   r=   r   r   r?   not_include_fieldsr(   r(   r)   similarity_search   s    
zAwaDB.similarity_searchzList[Tuple[Document, float]]c                 K  s   | j du rtdd}| j| jv r6| j| j |}nddlm} | |}g }ddh}	| j|||||	d}
|
D ](}|j	d }|j	d= ||f}|
| qr|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.
        Nr0   r   r>   r2   rA   rB   r@   )r    r3   r&   r%   rD   r   r?   rE   rF   metadataappend)r'   r:   r;   r<   r=   r   r   r?   resultsrG   Zretrieval_docsdocr@   Z	doc_tupler(   r(   r)   similarity_search_with_score   s,    

z"AwaDB.similarity_search_with_scorer   )r:   r;   r   r   c                 K  s   | j ||fi |S r+   )rM   )r'   r:   r;   r   r(   r(   r)   (_similarity_search_with_relevance_scores   s    z.AwaDB._similarity_search_with_relevance_scoreszOptional[List[float]]zOptional[Set[str]])r   r;   r<   r=   rC   r   r   c                 K  s   | j du rtdg }|du r"|S | j j|||||d}| dkrH|S |d d D ]Z}	d}
i }|	D ]6}|dkr||	| }
qdn|dur||v rqd|	| ||< qd|t|
|d qT|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.
        Nr0   )r<   r=   rG   r   ZResultItems r1   page_contentrI   )r    r3   ZSearch__len__rJ   r   )r'   r   r;   r<   r=   rC   r   rK   Zshow_resultsZitem_detailcontentZ	meta_dataZitem_keyr(   r(   r)   rF      s6    
z!AwaDB.similarity_search_by_vector         ?float)r:   r;   fetch_klambda_multr<   r=   r   r   c                 K  sx   | j du rtdg }| j| jv r6| 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.
        Nr0   r   r>   )rX   r<   r=   )
r    r3   r&   r%   rD   r   r?   rE   rR   'max_marginal_relevance_search_by_vector)r'   r:   r;   rW   rX   r<   r=   r   r   r?   rK   r(   r(   r)   max_marginal_relevance_search)  s$    
z#AwaDB.max_marginal_relevance_searchzList[float])r   r;   rW   rX   r<   r=   r   r   c                 K  s   | j du rtdg }|du r"|S ddh}	| j|||||	d}
g }|
D ]}||jd  qFttj|tjd|d}|D ],}d|
| jv rx|
| jd= ||
|  qx|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.
        Nr0   rA   r@   rB   r2   )Zdtype)Zembedding_list)	r    r3   rF   rJ   rI   r   nparrayZfloat32)r'   r   r;   rW   rX   r<   r=   r   rK   rG   Zretrieved_docsZtop_embeddingsrL   Zselected_docsZs_idr(   r(   r)   rY   ^  s0    
z-AwaDB.max_marginal_relevance_search_by_vectorzOptional[List[str]]zOptional[int]zDict[str, Document])idsr<   r=   rG   limitr   r   c                 K  s   | j du rtd| j j|||||d}i }|D ]`}	d}
i }|	D ]6}|dkrX|	| }
q@n|dks@|dkrjq@|	| ||< q@t|
|d}|||	d < q0|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.
        Nr0   )r]   r<   r=   rG   r^   rO   r1   r2   rA   rP   )r    r3   ZGetr   )r'   r]   r<   r=   rG   r^   r   Zdocs_detailrK   Z
doc_detailrS   Z	meta_infofieldrL   r(   r(   r)   get  s.    
z	AwaDB.get)r]   r   r   c                 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.
        Nr0   r   )r    r3   rR   Delete)r'   r]   r   retr(   r(   r)   delete  s    
zAwaDB.delete)r]   r-   r.   r   r   c                 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.
        Nr0   r1   )r]   Ztext_field_namer-   r.   )r    r3   ZUpdateTexts)r'   r]   r-   r.   r   r(   r(   r)   update  s
    
zAwaDB.updatec                 K  s(   | j du rdS | j |}|r$|| _|S )zCreate a new table.NF)r    r$   r&   r'   r   r   rb   r(   r(   r)   create_table  s    
zAwaDB.create_tablec                 K  s(   | j du rdS | j |}|r$|| _|S )zJUse the specified table. Don't know the tables, please invoke list_tables.NF)r    ZUser&   re   r(   r(   r)   use  s    
z	AwaDB.use)r   r   c                 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&   rh   r(   r(   r)   get_current_table(  s    zAwaDB.get_current_tablezType[AwaDB])	clsr-   r   r.   r   r   r   r   r   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   )r-   r.   )r5   )	rk   r-   r   r.   r   r   r   r   r    r(   r(   r)   
from_texts0  s    zAwaDB.from_texts)rk   	documentsr   r   r   r   r   r   c           	      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  s   g | ]
}|j qS r(   )rQ   .0rL   r(   r(   r)   
<listcomp>j      z(AwaDB.from_documents.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r(   )rI   rn   r(   r(   r)   rp   k  rq   )r-   r   r.   r   r   r   )rl   )	rk   rm   r   r   r   r   r   r-   r.   r(   r(   r)   from_documentsQ  s    zAwaDB.from_documents)NN)r   )r   rT   rU   NN)r   rT   rU   NN)NNNNN)N)N)__name__
__module____qualname____doc__r   __annotations__r*   propertyr,   r5   r8   DEFAULT_TOPNrH   rM   rN   rF   rZ   rY   r`   rc   rd   rf   rg   ri   rj   classmethodrl   rr   r(   r(   r(   r)   r      s   
1  #17 
:     8     @     4  " r   )
__future__r   loggingr!   typingr   r   r   r   r   r   r	   r
   r   numpyr[   Zlangchain_core.documentsr   Zlangchain_core.embeddingsr   Zlangchain_core.vectorstoresr   Z&langchain_community.vectorstores.utilsr   r   	getLoggerloggerry   r   r(   r(   r(   r)   <module>   s   ,