
    dhR                        S SK Jr  S SKrS SKrS SKJrJrJrJrJ	r	J
r
JrJrJr  S SKrS SKJr  S SKJr  S SKJr  S SKJr  \(       a  S SKr\R2                  " 5       rSr " S	 S
\5      rg)    )annotationsN)	TYPE_CHECKINGAnyDictIterableListOptionalSetTupleType)Document)
Embeddings)VectorStore)maximal_marginal_relevance   c                     \ rS rSr% SrSrS\S'   \SSS4           SS jjr\SS j5       r	  S         SS	 jjr
      SS
 jr\SS4           S S jjr\SS4           S!S jjr S"       S#S jjrS\SSS4             S$S jjr     S%               S&S jjr     S%               S'S jjr     S(             S)S jjr S*     S+S jjr S*         S,S jjr      SS jr      SS jr    S-S jr    S.S jr\SS\SS4                 S/S jj5       r\S\SS4               S0S jj5       rSrg)1AwaDB   z`AwaDB` vector store.langchain_awadbstr_DEFAULT_TABLE_NAMENc                    SSK nUb  X@l        O1Ub  UR                  " U5      U l        OUR                  " 5       U l        XR                  :X  a8  US-  nU[        [        R                  " 5       5      R                  S5      S   -  nU R                  R                  U5        0 U l
        Ub  X R                  U'   Xl        g! [         a    [        S5      ef = f)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          ^/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/awadb.py__init__AwaDB.__init__   s    .	  &+$)LL1A$B!$)LLN!111#J#djjl+11#6r::J  ,79 09!!*- *-  	> 	s   C Cc                j    U R                   U R                  ;   a  U R                  U R                      $ g N)r%   r$   )r&   s    r,   
embeddingsAwaDB.embeddingsK   s0      D$9$99(()>)>??    c                   U R                   c  [        S5      eSnU R                  U R                  ;   a1  U R                  U R                     R	                  [        U5      5      nU R                   R                  SSUUUU5      $ )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+   r1   s         r,   	add_textsAwaDB.add_textsQ   s    " $677
  D$9$99..t/D/DEUUUJ   ))
 	
r3   c                h    U R                   c  [        S5      eU R                   R                  U5      $ )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
r5   )r   r8   Load)r&   r'   r+   s      r,   
load_localAwaDB.load_localt   s3     $677  %%j11r3   c                "   U R                   c  [        S5      eSnU R                  U R                  ;   a)  U R                  U R                     R	                  U5      nOSSKJn  U" 5       R                  U5      n1 SknU R                  UUUUUS9$ )a  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.
Nr5   r   AwaEmbedding>   _idscorer7   text_in_page_contentmeta_filternot_include_fields_in_metadata)	r   r8   r%   r$   embed_queryr   rG   	Embeddingsimilarity_search_by_vector)	r&   querykrK   rL   r+   r(   rG   not_include_fieldss	            r,   similarity_search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 
 	
r3   c                   U R                   c  [        S5      eSnU R                  U R                  ;   a)  U R                  U R                     R	                  U5      nOSSKJn  U" 5       R                  U5      n/ nSS1n	U R                  UUUUU	S9n
U
 H3  nUR                  S   nUR                  S	 X4nUR                  U5        M5     U$ )	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.
Nr5   r   rF   r7   rH   rJ   rI   )r   r8   r%   r$   rN   r   rG   rO   rP   metadataappend)r&   rQ   rR   rK   rL   r+   r(   rG   resultsrS   retrieval_docsdocrI   	doc_tuples                 r,   similarity_search_with_score"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%INN9%	 " r3   c                (    U R                   " X40 UD6$ r0   )r]   )r&   rQ   rR   r+   s       r,   (_similarity_search_with_relevance_scores.AwaDB._similarity_search_with_relevance_scores   s     00DVDDr3   c                J   U R                   c  [        S5      e/ nUc  U$ U R                   R                  UUUUUS9nUR                  5       S:X  a  U$ US   S    HD  n	Sn
0 nU	 H  nUS:X  a  X   n
M  Ub  X;   a  M  X   X'   M!     UR	                  [        XS95        MF     U$ )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.
r5   )rK   rL   rS   r   ResultItems r6   page_contentrW   )r   r8   Search__len__rX   r   )r&   r(   rR   rK   rL   rM   r+   rY   show_resultsitem_detailcontent	meta_dataitem_keys                r,   rP   !AwaDB.similarity_search_by_vector   s    , $677"$N((//!5#= 0 
 !Q&N'?=9KGI'//)3G3?A &1&;	# ( NN8MN : r3   c           	     L   U R                   c  [        S5      e/ nU R                  U R                  ;   a)  U R                  U R                     R	                  U5      nOSSKJn	  U	" 5       R                  U5      nUR                  5       S:X  a  / $ U R                  UUUUUUS9n
U
$ )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.
r5   r   rF   )lambda_multrK   rL   )
r   r8   r%   r$   rN   r   rG   rO   rh   'max_marginal_relevance_search_by_vector)r&   rQ   rR   fetch_krp   rK   rL   r+   r(   rG   rY   s              r,   max_marginal_relevance_search#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# ? 
 r3   c                   U R                   c  [        S5      e/ nUc  U$ SS1n	U R                  UUUUU	S9n
/ nU
 H!  nUR                  UR                  S   5        M#     [        [        R                  " U[        R                  S9US9nU H9  nSX   R                  ;   d  M  X   R                  S	 UR                  X   5        M;     U$ )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.
r5   rH   rI   rJ   r7   )dtype)embedding_list)	r   r8   rP   rX   rW   r   nparrayfloat32)r&   r(   rR   rr   rp   rK   rL   r+   rY   rS   retrieved_docstop_embeddingsr[   selected_docss_ids                  r,   rq   -AwaDB.max_marginal_relevance_search_by_vector^  s    < $677"$N#('"299!5#+= : 
 !C!!#,,/?"@A " 3HHYbjj1.
 "D>#7#@#@@"(112BC~34 " r3   c                    U R                   c  [        S5      eU R                   R                  UUUUUS9n0 nU H@  n	Sn
0 nU	 H#  nUS:X  a  X   n
M  US:X  d  US:X  a  M  X   X'   M%     [        XS9nXU	S   '   MB     U$ )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.
r5   )idsrK   rL   rS   limitrd   r6   r7   rH   re   )r   r8   Getr   )r&   r   rK   rL   rS   r   r+   docs_detailrY   
doc_detailrk   	meta_infofieldr[   s                 r,   get	AwaDB.get  s    , $677''++!5#1 , 
 (*%JGI#,,(/G..%5.#-#4	  $ DC),Ju%& & r3   c                    U R                   c  [        S5      eSnUb  UR                  5       S:X  a  U$ U R                   R                  U5      nU$ )a  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.
Nr5   r   )r   r8   rh   Delete)r&   r   r+   rets       r,   deleteAwaDB.delete  sS     $677";#++-1,J&&s+
r3   c                h    U R                   c  [        S5      eU R                   R                  USX#S9$ )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.
r5   r6   )r   text_field_namer<   r=   )r   r8   UpdateTexts)r&   r   r<   r=   r+   s        r,   updateAwaDB.update  sB    " $677  ,,%5U - 
 	
r3   c                r    U R                   c  gU R                   R                  U5      nU(       a  Xl        U$ )zCreate a new table.F)r   r#   r%   r&   r'   r+   r   s       r,   create_tableAwaDB.create_table  s7     $&&z2$.!
r3   c                r    U R                   c  gU R                   R                  U5      nU(       a  Xl        U$ )zJUse the specified table. Don't know the tables, please invoke list_tables.F)r   User%   r   s       r,   use	AwaDB.use  s7     $##J/$.!
r3   c                T    U R                   c  / $ U R                   R                  5       $ )z*List all the tables created by the client.)r   ListAllTablesr&   r+   s     r,   list_tablesAwaDB.list_tables  s*     $I  ..00r3   c                    U R                   $ )zGet the current table.)r%   r   s     r,   get_current_tableAwaDB.get_current_table(  s     $$$r3   c                6    U " UUUUS9nUR                  XS9  U$ )a  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=   )r?   )	clsr<   r(   r=   r'   r)   r*   r+   r   s	            r,   
from_textsAwaDB.from_texts0  s3    0 !-	
 	U@r3   c           	         U Vs/ sH  owR                   PM     nnU Vs/ sH  owR                  PM     n	nU R                  UUU	UUUS9$ s  snf s  snf )a  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.
)r<   r(   r=   r'   r)   r*   )rf   rW   r   )
r   	documentsr(   r'   r)   r*   r+   r[   r<   r=   s
             r,   from_documentsAwaDB.from_documentsQ  sb    2 .77Yc!!Y7-67Yc\\Y	7~~!-  
 	
 87s
   A	A)r   r$   r%   )r'   r   r(   Optional[Embeddings]r)   Optional[str]r*   Optional[awadb.Client]r+   r   returnNone)r   r   )NN)
r<   Iterable[str]r=   Optional[List[dict]]r>   Optional[bool]r+   r   r   	List[str])r'   r   r+   r   r   bool)rQ   r   rR   intrK   r   rL   Optional[dict]r+   r   r   List[Document])rQ   r   rR   r   rK   r   rL   r   r+   r   r   List[Tuple[Document, float]])r   )rQ   r   rR   r   r+   r   r   r   )r(   zOptional[List[float]]rR   r   rK   r   rL   r   rM   Optional[Set[str]]r+   r   r   r   )r      g      ?NN)rQ   r   rR   r   rr   r   rp   floatrK   r   rL   r   r+   r   r   r   )r(   zList[float]rR   r   rr   r   rp   r   rK   r   rL   r   r+   r   r   r   )NNNNN)r   Optional[List[str]]rK   r   rL   r   rS   r   r   zOptional[int]r+   r   r   zDict[str, Document]r0   )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   Type[AwaDB]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   r   )__name__
__module____qualname____firstlineno____doc__r   __annotations__r-   propertyr1   r?   rC   DEFAULT_TOPNrT   r]   r`   rP   rs   rq   r   r   r   r   r   r   r   classmethodr   r   __static_attributes__ r3   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%% 
%  +/*.-*.)- ( (	
  ( '  
 @  +/-*.)-!
!
!!
 (!
 	!

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