
    NgF                       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 d dlZd dlmZ d dlmZmZ d dlmZmZ 	 d dlmZ n# e$ r	 d dlmZ Y nw xY wd dlmZ d d	lmZ d d
lmZ d dlmZ  e            ZdZ dZ! G d de          Z" G d de"          Z# G d de"          Z$ G d d          Z% G d de          Z&dS )    )annotationsN)AnyDictIterableListOptionalTupleType)func)JSONUUID)Sessionrelationship)declarative_base)Document)
Embeddings)get_from_dict_or_env)VectorStorei   	langchainc                  Z    e Zd ZdZdZ ej         ed          dej	                  ZdS )	BaseModelzBase model for all SQL stores.Tas_uuid)primary_keydefaultN)
__name__
__module____qualname____doc____abstract__
sqlalchemyColumnr   uuiduuid4     h/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/pgembedding.pyr   r      sA        ((L:TT$///T4:VVVDDDr&   r   c                      e Zd ZdZdZ ej        ej                  Z ej        e	          Z
 eddd          Zedd            Ze	 ddd            ZdS )CollectionStorezCollection store.langchain_pg_collectionEmbeddingStore
collectionT)back_populatespassive_deletessessionr   namestrreturnOptional['CollectionStore']c                    |                     |                               | j        |k                                              S N)queryfilterr0   first)clsr/   r0   s      r'   get_by_namezCollectionStore.get_by_name2   s5    }}S!!((T)9::@@BBBr&   N	cmetadataOptional[dict]Tuple['CollectionStore', bool]c                    d}|                      ||          }|r||fS  | ||          }|                    |           |                                 d}||fS )z
        Get or create a collection.
        Returns [Collection, bool] where the bool is True if the collection was created.
        F)r0   r;   T)r:   addcommit)r9   r/   r0   r;   createdr,   s         r'   get_or_createzCollectionStore.get_or_create6   su     __Wd33
 	'w&&Sdi888
J7""r&   )r/   r   r0   r1   r2   r3   r5   )r/   r   r0   r1   r;   r<   r2   r=   )r   r   r   r   __tablename__r!   r"   Stringr0   r   r;   r   
embeddingsclassmethodr:   rB   r%   r&   r'   r)   r)   $   s        -M:Z.//D!
!$''I#  J C C C [C 
 %)	# # # # [# # #r&   r)   c                  \   e Zd ZdZdZ ej         ed           ej        e	j         dd                    Z
 ee	d	          Z ej         ej        ej                            Z ej        ej        d
          Z ej        ed
          Z ej        ej        d
          ZdS )r+   zEmbedding store.langchain_pg_embeddingTr   z.uuidCASCADE)ondeleterE   )r-   )nullableN)r   r   r   r   rC   r!   r"   r   
ForeignKeyr)   collection_idr   r,   ARRAYREAL	embeddingrD   documentr   r;   	custom_idr%   r&   r'   r+   r+   M   s        ,M%J%T
,333	
 	
 	
 M olKKKJ!
!"2*"2:?"C"CDDI z !2TBBBH!
!$666I "
!*"3dCCCIIIr&   r+   c                  (    e Zd ZU dZded<   ded<   dS )QueryResultzResult from a query.r+   floatdistanceN)r   r   r   r   __annotations__r%   r&   r'   rT   rT   c   s+         """"OOOOOr&   rT   c                     e Zd ZdZedddfdOdZdPdZedQd            ZdRdZ	dPdZ
dPdZdPdZdPdZdedddfdSd#ZdPd$ZdTd(ZeddedfdUd4            ZdVd6Z	 	 dWdXd8Z	 	 dYdZd>Z	 	 dYd[d@Z	 	 dYd\dBZ	 	 dYd]dCZededdfd^dF            Zededdfd_dI            Zeedfd`dJ            ZedadL            ZeeddfdbdN            ZdS )cPGEmbeddinga  `Postgres` with the `pg_embedding` extension as a vector store.

    pg_embedding uses sequential scan by default. but you can create a HNSW index
    using the create_hnsw_index method.
    - `connection_string` is a postgres connection string.
    - `embedding_function` any embedding function implementing
        `langchain.embeddings.base.Embeddings` interface.
    - `collection_name` is the name of the collection to use. (default: langchain)
        - NOTE: This is not the name of the table, but the name of the collection.
            The tables will be created when initializing the store (if not exists)
            So, make sure the user has the right permissions to create tables.
    - `distance_strategy` is the distance strategy to use. (default: EUCLIDEAN)
        - `EUCLIDEAN` is the euclidean distance.
    - `pre_delete_collection` if True, will delete the collection if it exists.
        (default: False)
        - Useful for testing.
    NFconnection_stringr1   embedding_functionr   collection_namecollection_metadatar<   pre_delete_collectionboolloggerOptional[logging.Logger]r2   Nonec                    || _         || _        || _        || _        || _        |pt          j        t                    | _        | 	                                 d S r5   )
rZ   r[   r\   r]   r^   logging	getLoggerr   r`   __post_init__)selfrZ   r[   r\   r]   r^   r`   s          r'   __init__zPGEmbedding.__init__}   s[     "3"4.#6 %:"; 1( ; ;r&   c                    |                                  | _        |                                  |                                  |                                  d S r5   )connect_conncreate_hnsw_extensioncreate_tables_if_not_existscreate_collectionrg   s    r'   rf   zPGEmbedding.__post_init__   sO     \\^^
""$$$((***     r&   c                    | j         S r5   )r[   ro   s    r'   rE   zPGEmbedding.embeddings   s    &&r&   sqlalchemy.engine.Connectionc                `    t          j        | j                  }|                                }|S r5   )r!   create_enginerZ   rj   )rg   engineconns      r'   rj   zPGEmbedding.connect   s)    )$*@AA~~r&   c                @   	 t          | j                  5 }t          j        d          }|                    |           |                                 d d d            d S # 1 swxY w Y   d S # t          $ r%}| j                            |           Y d }~d S d }~ww xY w)Nz(CREATE EXTENSION IF NOT EXISTS embedding)	r   rk   r!   textexecuter@   	Exceptionr`   	exception)rg   r/   	statementes       r'   rl   z!PGEmbedding.create_hnsw_extension   s    	%$$ !&O,VWW		***   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !  	% 	% 	%K!!!$$$$$$$$$	%s:   A. >A!A. !A%%A. (A%)A. .
B8BBc                    | j                                         5  t          j                            | j                    d d d            d S # 1 swxY w Y   d S r5   )rk   beginBasemetadata
create_allro   s    r'   rm   z'PGEmbedding.create_tables_if_not_exists   s    Z 	1 	1M$$TZ000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1   %AAAc                    | j                                         5  t          j                            | j                    d d d            d S # 1 swxY w Y   d S r5   )rk   r~   r   r   drop_allro   s    r'   drop_tableszPGEmbedding.drop_tables   s    Z 	/ 	/M""4:...	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/r   c                    | j         r|                                  t          | j                  5 }t                              || j        | j                   d d d            d S # 1 swxY w Y   d S )N)r;   )r^   delete_collectionr   rk   r)   rB   r\   r]   rg   r/   s     r'   rn   zPGEmbedding.create_collection   s    % 	%""$$$TZ   	G))-9Q *   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   (A%%A),A)i'        max_elementsintdimsmef_construction	ef_searchc           	     z   t          j        d                    |||||                    }	 t          | j                  5 }|                    |           |                                 d d d            n# 1 swxY w Y   t          d           d S # t          $ r}t          d|            Y d }~d S d }~ww xY w)NzCREATE INDEX IF NOT EXISTS langchain_pg_embedding_idx ON langchain_pg_embedding USING hnsw (embedding) WITH (maxelements = {}, dims = {}, m = {}, efconstruction = {}, efsearch = {});z.HNSW extension and index created successfully.z*Failed to create HNSW extension or index: )	r!   rw   formatr   rk   rx   r@   printry   )	rg   r   r   r   r   r   create_index_queryr/   r|   s	            r'   create_hnsw_indexzPGEmbedding.create_hnsw_index   s     (_ dA	JJ

 

	D$$ ! 2333   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! BCCCCC 	D 	D 	DBqBBCCCCCCCCC	Ds;   B *A7+B 7A;;B >A;?B 
B:B55B:c                d   | j                             d           t          | j                  5 }|                     |          }|s(| j                             d           	 d d d            d S |                    |           |                                 d d d            d S # 1 swxY w Y   d S )NzTrying to delete collectionCollection not found)r`   debugr   rk   get_collectionwarningdeleter@   )rg   r/   r,   s      r'   r   zPGEmbedding.delete_collection   s   7888TZ   	G,,W55J ##$:;;;		 	 	 	 	 	 	 	
 NN:&&&NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   3B%/)B%%B),B)r/   r   r3   c                B    t                               || j                  S r5   )r)   r:   r\   r   s     r'   r   zPGEmbedding.get_collection   s    **7D4HIIIr&   texts	List[str]rE   List[List[float]]rP   	metadatasOptional[List[dict]]idsOptional[List[str]]kwargsr   c                    |d |D             }|sd |D             }|                      |          }	 | |	|||          }
 |
j        d||||d| |
S )Nc                N    g | ]"}t          t          j                              #S r%   r1   r#   r$   .0_s     r'   
<listcomp>z;PGEmbedding._initialize_from_embeddings.<locals>.<listcomp>   &    4443tz||$$444r&   c                    g | ]}i S r%   r%   r   s     r'   r   z;PGEmbedding._initialize_from_embeddings.<locals>.<listcomp>       ++++++r&   rZ   r\   r[   r^   )r   rE   r   r   r%   )get_connection_stringadd_embeddings)r9   r   rE   rP   r   r   r\   r^   r   rZ   stores              r'   _initialize_from_embeddingsz'PGEmbedding._initialize_from_embeddings   s     ;44e444C 	,++U+++I55f==/+("7	
 
 
 	 	
J)	
 	
PV	
 	
 	
 r&   
List[dict]c                   t          | j                  5 }|                     |          }|st          d          t	          ||||          D ]I\  }}	}
}t          |
||	|          }|j                            |           |                    |           J|	                                 d d d            d S # 1 swxY w Y   d S )Nr   rP   rQ   r;   rR   )
r   rk   r   
ValueErrorzipr+   rE   appendr?   r@   )rg   r   rE   r   r   r   r/   r,   rw   r   rP   idembedding_stores                r'   r   zPGEmbedding.add_embeddings  s$    TZ   	G,,W55J 9 !788814UIzSV1W1W - --h	2"0'!& 	# # # %,,_===O,,,,NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BB99B= B=Iterable[str]c                   |d |D             }| j                             t          |                    }|sd |D             }t          | j                  5 }|                     |          }|st          d          t          ||||          D ]I\  }}	}
}t          |
||	|          }|j	        
                    |           |                    |           J|                                 d d d            n# 1 swxY w Y   |S )Nc                N    g | ]"}t          t          j                              #S r%   r   r   s     r'   r   z)PGEmbedding.add_texts.<locals>.<listcomp>#  r   r&   c                    g | ]}i S r%   r%   r   s     r'   r   z)PGEmbedding.add_texts.<locals>.<listcomp>(  r   r&   r   r   )r[   embed_documentslistr   rk   r   r   r   r+   rE   r   r?   r@   )rg   r   r   r   r   rE   r/   r,   rw   r   rP   r   r   s                r'   	add_textszPGEmbedding.add_texts  sk    ;44e444C,<<T%[[II
 	,++U+++ITZ   	G,,W55J 9 !788814UIzSV1W1W - --h	2"0'!& 	# # # %,,_===O,,,,NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 
s   BC;;C?C?   r6   kr7   List[Document]c                h    | j                             |          }|                     |||          S )N)rw   rP   r   r7   )r[   embed_querysimilarity_search_by_vector)rg   r6   r   r7   r   rP   s         r'   similarity_searchzPGEmbedding.similarity_search;  sC     +77U7CC	// 0 
 
 	
r&   List[Tuple[Document, float]]c                j    | j                             |          }|                     |||          }|S )Nr   )r[   r   &similarity_search_with_score_by_vector)rg   r6   r   r7   rP   docss         r'   similarity_search_with_scorez(PGEmbedding.similarity_search_with_scoreI  sB     +77>>	::1V ; 
 
 r&   List[float]c           
     <    t           j                  5 }                     |          }t          j        d          }|                    |           |st          d          t          j        |j	        k    }|g }|
                                D ]V\  }	}
d}t          |
t                    r|t          t          j        |
          v rdd |

                                D             }t          j        |	         j                            ||                   }|                    |           t          |
t                    rgdt          t          j        |
          v rKt          j        |	         j                            d|
d          d          }|                    |           t          j        |	         j        t          |
          k    }|                    |           Xt          j        |g|R  }|                    t          t/          j        t          j                            d          |                                        d                                        |                              t/          j        t          j                            d          |                                                                                  |                                           }d d d            n# 1 swxY w Y    fd	|D             }|S )
NzSET enable_seqscan = offr   inc                >    i | ]\  }}|                                 |S r%   )lower)r   r   vs      r'   
<dictcomp>zFPGEmbedding.similarity_search_with_score_by_vector.<locals>.<dictcomp>i  s3     2 2 2-1QAGGIIq2 2 2r&   	substring%z<->rV   c                z    g | ]7}t          |j        j        |j        j                   j        |j        ndf8S ))page_contentr   Ng        )r   r+   rQ   r;   r[   rV   )r   resultrg   s     r'   r   zFPGEmbedding.similarity_search_with_score_by_vector.<locals>.<listcomp>  sd     	
 	
 	
  !'!6!?#2<   $(#:#FC	
 	
 	
r&   )!r   rk   r   r!   rw   rx   r   r+   rM   r#   items
isinstancedictmapr1   r   r;   astextin_r   ilikeand_r6   r   absrP   oplabelr7   order_byasclimitall)rg   rP   r   r7   r/   r,   set_enable_seqscan_stmt	filter_byfilter_clauseskeyvalueINvalue_case_insensitivefilter_by_metadataresultsr   s   `               r'   r   z2PGEmbedding.similarity_search_with_score_by_vectorU  sI    TZ   1	G,,W55J&0o6P&Q&Q#OO3444 9 !7888&4
GI!!#"(,,.. B BJCB!%.. B2SY9N9N3N3N2 25:[[]]2 2 2. .<-Ec-J-Q-U-U226. .* '--.@AAAA#E400 B[C	5E E 6 6 .<-Ec-J-Q-W-W5k 2555. .* '--.@AAAA-;-E. CJJ./* '--.@AAAA&OIGGGG	 "H^588??	JJKKQQ"   	""H^588??	JJKKOOQQ  q I1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	 1	f	
 	
 	
 	
 "	
 	
 	
 s   K!LL
Lc                J    |                      |||          }d |D             S )Nr   c                    g | ]\  }}|S r%   r%   )r   docr   s      r'   r   z;PGEmbedding.similarity_search_by_vector.<locals>.<listcomp>  s    222Q222r&   )r   )rg   rP   r   r7   r   docs_and_scoress         r'   r   z'PGEmbedding.similarity_search_by_vector  s<     EE1V F 
 
 32/2222r&   r9   Type[PGEmbedding]c                p    |                     t          |                    } | j        |||f||||d|S )Nr   r   r\   r^   )r   r   r   )	r9   r   rP   r   r\   r   r^   r   rE   s	            r'   
from_textszPGEmbedding.from_texts  s_     ..tE{{;;
.s.	
  +"7	
 	
 	
 	
 		
r&   text_embeddingsList[Tuple[str, List[float]]]c                \    d |D             }d |D             }	 | j         ||	|f||||d|S )Nc                    g | ]
}|d          S )r   r%   r   ts     r'   r   z/PGEmbedding.from_embeddings.<locals>.<listcomp>  s    ///!1///r&   c                    g | ]
}|d          S )   r%   r   s     r'   r   z/PGEmbedding.from_embeddings.<locals>.<listcomp>  s    444qad444r&   r   )r   )
r9   r   rP   r   r\   r   r^   r   r   rE   s
             r'   from_embeddingszPGEmbedding.from_embeddings  sl     0////44O444
.s.	
  +"7	
 	
 	
 	
 		
r&   c                N    |                      |          } | ||||          }|S )Nr   )r   )r9   rP   r\   r^   r   rZ   r   s          r'   from_existing_indexzPGEmbedding.from_existing_index  sA      55f==/+("7	
 
 
 r&   Dict[str, Any]c                L    t          |dd          }|st          d          |S )NrZ   POSTGRES_CONNECTION_STRING)datar   env_keyz~Postgres connection string is requiredEither pass it as a parameteror set the POSTGRES_CONNECTION_STRING environment variable.)r   r   )r9   r   rZ   s      r'   r   z!PGEmbedding.get_connection_string  sJ    !5#0"
 "
 "
 ! 	N   ! r&   	documentsc           
         d |D             }d |D             }|                      |          }	|	|d<    | j        d||||||d|S )Nc                    g | ]	}|j         
S r%   )r   r   ds     r'   r   z.PGEmbedding.from_documents.<locals>.<listcomp>  s    333A333r&   c                    g | ]	}|j         
S r%   )r   r  s     r'   r   z.PGEmbedding.from_documents.<locals>.<listcomp>  s    333AQZ333r&   rZ   )r   r^   rP   r   r   r\   r%   )r   r   )
r9   r  rP   r\   r   r^   r   r   r   rZ   s
             r'   from_documentszPGEmbedding.from_documents  s     4333333333	55f==&7"#s~ 
"7+
 
 
 
 	
r&   )rZ   r1   r[   r   r\   r1   r]   r<   r^   r_   r`   ra   r2   rb   )r2   rb   )r2   r   )r2   rq   )r   r   r   r   r   r   r   r   r   r   r2   rb   )r/   r   r2   r3   )r   r   rE   r   rP   r   r   r   r   r   r\   r1   r^   r_   r   r   r2   rY   )r   r   rE   r   r   r   r   r   r   r   r2   rb   )NN)
r   r   r   r   r   r   r   r   r2   r   )r   N)
r6   r1   r   r   r7   r<   r   r   r2   r   )r6   r1   r   r   r7   r<   r2   r   )rP   r   r   r   r7   r<   r2   r   )
rP   r   r   r   r7   r<   r   r   r2   r   )r9   r   r   r   rP   r   r   r   r\   r1   r   r   r^   r_   r   r   r2   rY   )r   r   rP   r   r   r   r\   r1   r   r   r^   r_   r   r   r2   rY   )r9   r   rP   r   r\   r1   r^   r_   r   r   r2   rY   )r   r  r2   r1   )r9   r   r  r   rP   r   r\   r1   r   r   r^   r_   r   r   r2   rY   )r   r   r   r   "_LANGCHAIN_DEFAULT_COLLECTION_NAMErh   rf   propertyrE   rj   rl   rm   r   rn   ADA_TOKEN_COUNTr   r   r   rF   r   r   r   r   r   r   r   r   r  r  r   r  r%   r&   r'   rY   rY   j   s        ,  B.2&++/    "! ! ! ! ' ' ' X'   
% % % %1 1 1 1/ / / /    "#!D D D D D<   J J J J  +/#'A&+    [@   4 +/#'	    F !%	
 
 
 
 
" !%	
 
 
 
 
 !%	C C C C CP !%	
3 
3 
3 
3 
3 
 +/A#'&+
 
 
 
 [
. 
 +/A#'&+
 
 
 
 [
0   B&+	    [$ ! ! ! [!  
  B#'&+
 
 
 
 [
 
 
r&   rY   )'
__future__r   rd   r#   typingr   r   r   r   r   r	   r
   r!   r   sqlalchemy.dialects.postgresqlr   r   sqlalchemy.ormr   r   r   ImportErrorsqlalchemy.ext.declarativelangchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   r   r  r  r   r)   r+   rT   rY   r%   r&   r'   <module>r      sT   " " " " " "   C C C C C C C C C C C C C C C C C C           5 5 5 5 5 5 5 5 0 0 0 0 0 0 0 0</////// < < <;;;;;;;;< . - - - - - 0 0 0 0 0 0 5 5 5 5 5 5 3 3 3 3 3 3 %0 "W W W W W W W W&# &# &# &# &#i &# &# &#RD D D D DY D D D,       i
 i
 i
 i
 i
+ i
 i
 i
 i
 i
s   A AA