
    hR                        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j2                         ZdZ G d	 d
e      Zy)    )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	 	 	 	 	 	 	 	 	 	 	 ddZedd       Z	 	 d	 	 	 	 	 	 	 	 	 ddZ		 	 	 	 	 	 dd	Z
eddf	 	 	 	 	 	 	 	 	 	 	 dd
Zeddf	 	 	 	 	 	 	 	 	 	 	 ddZ	 d 	 	 	 	 	 	 	 d!dZdedddf	 	 	 	 	 	 	 	 	 	 	 	 	 d"dZ	 	 	 	 	 d#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$dZ	 	 	 	 	 d#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZ	 	 	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZ	 d(	 	 	 	 	 d)dZ	 d(	 	 	 	 	 	 	 	 	 d*dZ	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZ	 	 	 	 d+dZ	 	 	 	 d,dZeddeddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-d       Zededdf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.d       Zy)/AwaDBz`AwaDB` vector store.langchain_awadbstr_DEFAULT_TABLE_NAMENc                   	 ddl }||| _        n0| |j                  |      | _        n |j                         | _        || j                  k(  r7|dz  }|t        t        j                               j                  d      d   z  }| j                  j                  |       i | _
        ||| j                  |<   || _        y# t        $ r t        d      w xY w)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)self
table_name	embeddinglog_and_data_dirclientkwargsr   s          d/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/awadb.py__init__zAwaDB.__init__   s    .	  &D+$0ELL1A$B!$0ELLN!111#J#djjl+11#6r::J  ,79 09D!!*- *-  	> 	s   C Cc                f    | j                   | j                  v r| j                  | j                      S y N)r$   r#   )r%   s    r+   
embeddingszAwaDB.embeddingsK   s0      D$9$99(()>)>??    c                   | j                   t        d      d}| j                  | j                  v r1| j                  | j                     j	                  t        |            }| j                   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%   texts	metadatasis_duplicate_textsr*   r/   s         r+   	add_textszAwaDB.add_textsQ   s    " $677
  D$9$99..t/D/DEUUUJ   ))
 	
r0   c                f    | j                   t        d      | j                   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
        r2   )r   r5   Load)r%   r&   r*   s      r+   
load_localzAwaDB.load_localt   s3     $677  %%j11r0   c                "   | j                   t        d      d}| j                  | j                  v r)| j                  | j                     j	                  |      }nddlm}  |       j                  |      }h d}| j                  |||||      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.
        Nr2   r   AwaEmbedding>   _idscorer4   text_in_page_contentmeta_filternot_include_fields_in_metadata)	r   r5   r$   r#   embed_queryr   rB   	Embeddingsimilarity_search_by_vector)	r%   querykrF   rG   r*   r'   rB   not_include_fieldss	            r+   similarity_searchzAwaDB.similarity_search   s    4 $677	  D$9$99--d.C.CDPPQVWI*$007I'I//!5#+= 0 
 	
r0   c                   | j                   t        d      d}| j                  | j                  v r)| j                  | j                     j	                  |      }nddlm}  |       j                  |      }g }ddh}	| j                  |||||	      }
|
D ]3  }|j                  d   }|j                  d= ||f}|j                  |       5 |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.
        Nr2   r   rA   r4   rC   rE   rD   )r   r5   r$   r#   rI   r   rB   rJ   rK   metadataappend)r%   rL   rM   rF   rG   r*   r'   rB   resultsrN   retrieval_docsdocrD   	doc_tuples                 r+   similarity_search_with_scorez"AwaDB.similarity_search_with_score   s    , $677	  D$9$99--d.C.CDPPQVWI*$007I02(8%'@99!5#+= : 
 " 	&CLL)EW%eINN9%		& r0   c                *     | j                   ||fi |S r.   )rW   )r%   rL   rM   r*   s       r+   (_similarity_search_with_relevance_scoresz.AwaDB._similarity_search_with_relevance_scores   s     1t00DVDDr0   c                F   | j                   t        d      g }||S | j                   j                  |||||      }|j                         dk(  r|S |d   d   D ]C  }	d}
i }|	D ]  }|dk(  r|	|   }
|||v r|	|   ||<    |j	                  t        |
|             E |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.
        r2   )rF   rG   rN   r   ResultItems r3   page_contentrQ   )r   r5   Search__len__rR   r   )r%   r'   rM   rF   rG   rH   r*   rS   show_resultsitem_detailcontent	meta_dataitem_keys                r+   rK   z!AwaDB.similarity_search_by_vector   s    , $677"$N((//!5#= 0 
 !Q&N'?=9 	OKGI' <//)(3G3?#AA &1(&;	(#< NN89MN	O r0   c                J   | j                   t        d      g }| j                  | j                  v r)| j                  | j                     j	                  |      }nddlm}	  |	       j                  |      }|j                         dk(  rg S | j                  ||||||      }
|
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.
        r2   r   rA   )lambda_multrF   rG   )
r   r5   r$   r#   rI   r   rB   rJ   r`   'max_marginal_relevance_search_by_vector)r%   rL   rM   fetch_krg   rF   rG   r*   r'   rB   rS   s              r+   max_marginal_relevance_searchz#AwaDB.max_marginal_relevance_search)  s    : $677!#	  D$9$99--d.C.CDPPQVWI*$007I!#I>>#!5# ? 
 r0   c                   | j                   t        d      g }||S ddh}	| j                  |||||	      }
g }|
D ]   }|j                  |j                  d          " t        t        j                  |t        j                        |      }|D ]8  }d|
|   j                  v s|
|   j                  d= |j                  |
|          : |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.
        r2   rC   rD   rE   r4   )dtype)embedding_list)	r   r5   rK   rR   rQ   r   nparrayfloat32)r%   r'   rM   ri   rg   rF   rG   r*   rS   rN   retrieved_docstop_embeddingsrU   selected_docss_ids                  r+   rh   z-AwaDB.max_marginal_relevance_search_by_vector^  s    < $677"$N#('"299!5#+= : 
 ! 	BC!!#,,/?"@A	B 3HHYbjj1.
 " 	5D>$#7#@#@@"4(112BC~d34	5 r0   c                   | j                   t        d      | j                   j                  |||||      }i }|D ]@  }	d}
i }|	D ]   }|dk(  r|	|   }
|dk(  s|dk(  r|	|   ||<   " t        |
|      }|||	d   <   B |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.
        r2   )idsrF   rG   rN   limitr\   r3   r4   rC   r]   )r   r5   Getr   )r%   rv   rF   rG   rN   rw   r*   docs_detailrS   
doc_detailrc   	meta_infofieldrU   s                 r+   getz	AwaDB.get  s    , $677''++!5#1 , 
 (*% 	-JGI# 5,,(/G..%5.#-e#4	% 5 )DC),GJu%&	- r0   c                    | j                   t        d      d}||j                         dk(  r|S | j                   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.
        Nr2   r   )r   r5   r`   Delete)r%   rv   r*   rets       r+   deletezAwaDB.delete  sS     $677";#++-1,J&&s+
r0   c                n    | j                   t        d      | j                   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.
        r2   r3   )rv   text_field_namer9   r:   )r   r5   UpdateTexts)r%   rv   r9   r:   r*   s        r+   updatezAwaDB.update  sD    " $677  ,,%5Ui - 
 	
r0   c                h    | j                   y| j                   j                  |      }|r|| _        |S )zCreate a new table.F)r   r"   r$   r%   r&   r*   r   s       r+   create_tablezAwaDB.create_table  s9     $&&z2$.D!
r0   c                h    | j                   y| j                   j                  |      }|r|| _        |S )zJUse the specified table. Don't know the tables, please invoke list_tables.F)r   User$   r   s       r+   usez	AwaDB.use  s9     $##J/$.D!
r0   c                R    | j                   g S | j                   j                         S )z*List all the tables created by the client.)r   ListAllTablesr%   r*   s     r+   list_tableszAwaDB.list_tables  s*     $I  ..00r0   c                    | j                   S )zGet the current table.)r$   r   s     r+   get_current_tablezAwaDB.get_current_table(  s     $$$r0   c                D     | ||||      }|j                  ||       |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)   )r9   r:   )r<   )	clsr9   r'   r:   r&   r(   r)   r*   r   s	            r+   
from_textszAwaDB.from_texts0  s5    0 !-	
 	Ui@r0   c                    |D cg c]  }|j                    }}|D cg c]  }|j                   }	}| j                  |||	|||      S c c}w c c}w )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.
        )r9   r'   r:   r&   r(   r)   )r^   rQ   r   )
r   	documentsr'   r&   r(   r)   r*   rU   r9   r:   s
             r+   from_documentszAwaDB.from_documentsQ  sd    2 .77c!!77-67cS\\7	7~~!-  
 	
 87s
   A
A)r&   r   r'   Optional[Embeddings]r(   Optional[str]r)   Optional[awadb.Client]r*   r   returnNone)r   r   )NN)
r9   Iterable[str]r:   Optional[List[dict]]r;   Optional[bool]r*   r   r   	List[str])r&   r   r*   r   r   bool)rL   r   rM   intrF   r   rG   Optional[dict]r*   r   r   List[Document])rL   r   rM   r   rF   r   rG   r   r*   r   r   List[Tuple[Document, float]])r   )rL   r   rM   r   r*   r   r   r   )r'   zOptional[List[float]]rM   r   rF   r   rG   r   rH   Optional[Set[str]]r*   r   r   r   )r      g      ?NN)rL   r   rM   r   ri   r   rg   floatrF   r   rG   r   r*   r   r   r   )r'   zList[float]rM   r   ri   r   rg   r   rF   r   rG   r   r*   r   r   r   )NNNNN)rv   Optional[List[str]]rF   r   rG   r   rN   r   rw   zOptional[int]r*   r   r   zDict[str, Document]r.   )rv   r   r*   r   r   r   )
rv   r   r9   r   r:   r   r*   r   r   r   )r*   r   r   r   )r*   r   r   r   )r   Type[AwaDB]r9   r   r'   r   r:   r   r&   r   r(   r   r)   r   r*   r   r   r   )r   r   r   r   r'   r   r&   r   r(   r   r)   r   r*   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r,   propertyr/   r<   r?   DEFAULT_TOPNrO   rW   rY   rK   rj   rh   r}   r   r   r   r   r   r   classmethodr   r    r0   r+   r   r      s   00 .*.*.)-/+/+ (/+ (	/+
 '/+ /+ 
/+b   +/-1	!
!
 (!
 +	!

 !
 
!
F22 2 
	2. .2&*,
,
 ,
 ,	,

 $,
 ,
 
,
b .2&*22 2 ,	2
 $2 2 
&2n EE E 	E
 
&E ,0.2&*=A5(5 5 ,	5
 $5 );5 5 
5t  .2&*33 3 	3
 3 ,3 $3 3 
3p  .2&*<< < 	<
 < ,< $< < 
<@ $(.2&*15#0 0 ,0 $	0
 /0 0 0 
0h $(   
	6 +/	

 
 (	

 
 

0  
	   
	 	1	1 
	1%% 
%  +/*.-*.)- ( (	
  ( '  
 @  +/-*.)-!
!
!!
 (!
 	!

 (!
 '!
 !
 
!
 !
r0   r   )
__future__r   loggingr   typingr   r   r   r   r   r	   r
   r   r   numpyrn   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   r   	getLoggerloggerr   r   r   r0   r+   <module>r      sO    "   W W W  - 0 3 M				^	
K ^	
r0   