
    hF                    Z   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 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&y# e$ r	 d dlmZ Y gw xY w)    )annotationsN)AnyDictIterableListOptionalTupleType)func)JSONUUID)Sessionrelationship)declarative_base)Document)
Embeddings)get_from_dict_or_env)VectorStorei   	langchainc                  `    e Zd ZdZdZ ej                   ed      dej                        Zy)	BaseModelzBase model for all SQL stores.Tas_uuid)primary_keydefaultN)
__name__
__module____qualname____doc____abstract__
sqlalchemyColumnr   uuiduuid4     j/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/pgembedding.pyr   r      s)    (L:T$/T4::VDr&   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y)CollectionStorezCollection store.langchain_pg_collectionEmbeddingStore
collectionT)back_populatespassive_deletesc                x    |j                  |       j                  | j                  |k(        j                         S N)queryfilternamefirst)clssessionr3   s      r'   get_by_namezCollectionStore.get_by_name2   s.    }}S!((T)9:@@BBr&   Nc                    d}| j                  ||      }|r||fS  | ||      }|j                  |       |j                          d}||fS )z
        Get or create a collection.
        Returns [Collection, bool] where the bool is True if the collection was created.
        F)r3   	cmetadataT)r7   addcommit)r5   r6   r3   r9   createdr,   s         r'   get_or_createzCollectionStore.get_or_create6   sY     __Wd3
w&&di8
J7""r&   )r6   r   r3   strreturnOptional['CollectionStore']r0   )r6   r   r3   r>   r9   Optional[dict]r?   zTuple['CollectionStore', bool])r   r   r   r   __tablename__r!   r"   Stringr3   r   r9   r   
embeddingsclassmethodr7   r=   r%   r&   r'   r)   r)   $   s    -M:Z../D!
!!$'I#J 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y)r+   zEmbedding store.langchain_pg_embeddingTr   z.uuidCASCADE)ondeleterD   )r-   )nullableN)r   r   r   r   rB   r!   r"   r   
ForeignKeyr)   collection_idr   r,   ARRAYREAL	embeddingrC   documentr   r9   	custom_idr%   r&   r'   r+   r+   M   s    ,M%J%%T
,,-U3	
M olKJ!
!!"2*"2"2:??"CDI z  !2!2TBH!
!!$6I "
!!*"3"3dCIr&   r+   c                  &    e Zd ZU dZded<   ded<   y)QueryResultzResult from a query.r+   floatdistanceN)r   r   r   r   __annotations__r%   r&   r'   rS   rS   c   s    ""Or&   rS   c                     e Zd ZdZedddf	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 ddZed d       Zd!dZ	ddZ
dd	Zdd
ZddZdedddf	 	 	 	 	 	 	 	 	 	 	 d"dZddZd#dZeddedf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$d       Z	 	 	 	 	 	 	 	 	 	 	 	 d%dZ	 	 d&	 	 	 	 	 	 	 	 	 d'dZ	 	 d(	 	 	 	 	 	 	 	 	 d)dZ	 	 d(	 	 	 	 	 	 	 d*dZ	 	 d(	 	 	 	 	 	 	 d+dZ	 	 d(	 	 	 	 	 	 	 	 	 d,dZededdf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-d       Zededdf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.d       Zeedf	 	 	 	 	 	 	 	 	 	 	 d/d       Zed0d       Zeeddf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d1d       Zy)2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.
    NFc                    || _         || _        || _        || _        || _        |xs t        j                  t              | _        | j                          y r0   )
connection_stringembedding_functioncollection_namecollection_metadatapre_delete_collectionlogging	getLoggerr   logger__post_init__)selfrZ   r[   r\   r]   r^   ra   s          r'   __init__zPGEmbedding.__init__}   sO     "3"4.#6 %:"; 1 1( ;r&   c                    | j                         | _        | j                          | j                          | j	                          y r0   )connect_conncreate_hnsw_extensioncreate_tables_if_not_existscreate_collectionrc   s    r'   rb   zPGEmbedding.__post_init__   s5     \\^
""$((* r&   c                    | j                   S r0   )r[   rk   s    r'   rD   zPGEmbedding.embeddings   s    &&&r&   c                d    t        j                  | j                        }|j                         }|S r0   )r!   create_enginerZ   rf   )rc   engineconns      r'   rf   zPGEmbedding.connect   s)    ))$*@*@A~~r&   c                (   	 t        | j                        5 }t        j                  d      }|j	                  |       |j                          d d d        y # 1 sw Y   y xY w# t        $ r%}| j                  j                  |       Y d }~y d }~ww xY w)Nz(CREATE EXTENSION IF NOT EXISTS embedding)	r   rg   r!   textexecuter;   	Exceptionra   	exception)rc   r6   	statementes       r'   rh   z!PGEmbedding.create_hnsw_extension   su    	%$ !&OO,VW		* ! ! !  	%KK!!!$$	%s4   A# 7AA# A A#  A# #	B,BBc                    | j                   j                         5  t        j                  j	                  | j                          d d d        y # 1 sw Y   y xY wr0   )rg   beginBasemetadata
create_allrk   s    r'   ri   z'PGEmbedding.create_tables_if_not_exists   s=    ZZ 	1MM$$TZZ0	1 	1 	1   *AAc                    | j                   j                         5  t        j                  j	                  | j                          d d d        y # 1 sw Y   y xY wr0   )rg   ry   rz   r{   drop_allrk   s    r'   drop_tableszPGEmbedding.drop_tables   s=    ZZ 	/MM""4::.	/ 	/ 	/r}   c                    | j                   r| j                          t        | j                        5 }t        j                  || j                  | j                         d d d        y # 1 sw Y   y xY w)N)r9   )r^   delete_collectionr   rg   r)   r=   r\   r]   rc   r6   s     r'   rj   zPGEmbedding.create_collection   s`    %%""$TZZ  	G))--9Q9Q * 	 	 	s   -A((A1i'        c           	     J   t        j                  dj                  |||||            }	 t        | j                        5 }|j                  |       |j                          d d d        t        d       y # 1 sw Y   xY w# t        $ r}t        d|        Y d }~y 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!   rr   formatr   rg   rs   r;   printrt   )	rc   max_elementsdimsmef_construction	ef_searchcreate_index_queryr6   rw   s	            r'   create_hnsw_indexzPGEmbedding.create_hnsw_index   s     (__ dA	J

	D$ ! 23 ! BC	! !
  	D>qcBCC	Ds.   B "A5!B 5A>:B 	B"
BB"c                @   | j                   j                  d       t        | j                        5 }| j	                  |      }|s%| j                   j                  d       	 d d d        y |j                  |       |j                          d d d        y # 1 sw Y   y xY w)NzTrying to delete collectionCollection not found)ra   debugr   rg   get_collectionwarningdeleter;   )rc   r6   r,   s      r'   r   zPGEmbedding.delete_collection   s    78TZZ  	G,,W5J##$:;		 	
 NN:&NN	 	 	s   0B*!BBc                B    t         j                  || j                        S r0   )r)   r7   r\   r   s     r'   r   zPGEmbedding.get_collection   s    **7D4H4HIIr&   c                    |*|D 	cg c]  }	t        t        j                               ! }}	|s|D 	cg c]  }	i  }}	| j                  |      }
 | |
|||      } |j                  d||||d| |S c c}	w c c}	w )NrZ   r\   r[   r^   )textsrD   	metadatasidsr%   )r>   r#   r$   get_connection_stringadd_embeddings)r5   r   rD   rO   r   r   r\   r^   kwargs_rZ   stores               r'   _initialize_from_embeddingsz'PGEmbedding._initialize_from_embeddings   s     ;.343tzz|$4C4%*++I+55f=/+("7	
 	 	
J)	
PV	
 % 5 ,s
   $A4	A9c                Z   t        | j                        5 }| j                  |      }|st        d      t	        ||||      D ]B  \  }}	}
}t        |
||	|      }|j                  j                  |       |j                  |       D |j                          d d d        y # 1 sw Y   y xY wNr   )rO   rP   r9   rQ   )
r   rg   r   
ValueErrorzipr+   rD   appendr:   r;   )rc   r   rD   r   r   r   r6   r,   rr   r{   rO   idembedding_stores                r'   r   zPGEmbedding.add_embeddings  s     TZZ  	G,,W5J !78814UIzSV1W --h	2"0'!& 	# %%,,_=O,- NN	 	 	s   BB!!B*c                4   |*|D cg c]  }t        t        j                               ! }}| j                  j	                  t        |            }|s|D cg c]  }i  }}t        | j                        5 }| j                  |      }|st        d      t        ||||      D ]B  \  }	}
}}t        ||	|
|      }|j                  j                  |       |j                  |       D |j                          d d d        |S c c}w c c}w # 1 sw Y   |S xY wr   )r>   r#   r$   r[   embed_documentslistr   rg   r   r   r   r+   rD   r   r:   r;   )rc   r   r   r   r   r   rD   r6   r,   rr   r{   rO   r   r   s                 r'   	add_textszPGEmbedding.add_texts  s    ;.343tzz|$4C4,,<<T%[I
%*++I+TZZ  	G,,W5J !78814UIzSV1W --h	2"0'!& 	# %%,,_=O,- NN	 
- 5
 ,	 
s   $D	D7BDDc                b    | j                   j                  |      }| j                  |||      S )N)rr   rO   kr2   )r[   embed_querysimilarity_search_by_vector)rc   r1   r   r2   r   rO   s         r'   similarity_searchzPGEmbedding.similarity_search;  s>     ++77U7C	// 0 
 	
r&   c                d    | j                   j                  |      }| j                  |||      }|S Nr   )r[   r   &similarity_search_with_score_by_vector)rc   r1   r   r2   rO   docss         r'   similarity_search_with_scorez(PGEmbedding.similarity_search_with_scoreI  s=     ++77>	::1V ; 
 r&   c           
        t        | j                        5 }| j                  |      }t        j                  d      }|j                  |       |st        d      t        j                  |j                  k(  }|~g }|j                         D ]R  \  }	}
d}t        |
t              r|t        t        j                  |
      v rr|
j                         D ci c]  \  }}|j                         | }}}t        j                   |	   j"                  j%                  ||         }|j'                  |       t        |
t              rbdt        t        j                  |
      v rFt        j                   |	   j"                  j)                  d|
d    d      }|j'                  |       t        j                   |	   j"                  t        |
      k(  }|j'                  |       U t        j*                  |g| }|j-                  t        t/        j0                  t        j2                  j5                  d      |            j7                  d            j9                  |      j;                  t/        j0                  t        j2                  j5                  d      |            j=                               j?                        jA                         }d d d        D cg c]Q  }tC        |j                  jD                  |j                  j                         | jF                  |jH                  nd	fS }}|S c c}}w # 1 sw Y   mxY wc c}w )
NzSET enable_seqscan = offr   in	substring%z<->rU   )page_contentr{   g        )%r   rg   r   r!   rr   rs   r   r+   rL   r#   items
isinstancedictmapr>   lowerr9   astextin_r   ilikeand_r1   r   absrO   oplabelr2   order_byasclimitallr   rP   r[   rU   )rc   rO   r   r2   r6   r,   set_enable_seqscan_stmt	filter_byfilter_clauseskeyvalueINvvalue_case_insensitivefilter_by_metadataresultsresultr   s                     r'   r   z2PGEmbedding.similarity_search_with_score_by_vectorU  s    TZZ  1	G,,W5J&0oo6P&Q#OO34 !788&44
GI!!#"(,,. BJCB!%.2SYY9N3N5:[[]2-1QAGGIqL2. 2 .<-E-Ec-J-Q-Q-U-U226.* '--.@A#E40[C		5E 6 .<-E-Ec-J-Q-Q-W-Wk 2315.* '--.@A-;-E-E. &CJ./* '--.@A+B. 'OOIGG	 "HH^5588?	JKQQ" 	"HH^5588?	JKOOQ q I1	v "	
  !'!6!6!?!?#22<< $(#:#:#FC	
 	
 _21	 1	f	
s&   B?L=L7
0G!L=AM	7L==Mc                `    | j                  |||      }|D cg c]  \  }}|	 c}}S c c}}w r   )r   )rc   rO   r   r2   r   docs_and_scoresdocr   s           r'   r   z'PGEmbedding.similarity_search_by_vector  s;     EE1V F 
 #22Q222s   *c                j    |j                  t        |            } | j                  |||f||||d|S )Nr   r   r\   r^   )r   r   r   )	r5   r   rO   r   r\   r   r^   r   rD   s	            r'   
from_textszPGEmbedding.from_texts  sS     ..tE{;
.s..	
  +"7	
 	
 		
r&   c                    |D cg c]  }|d   	 }	}|D cg c]  }|d   	 }
} | j                   |	|
|f||||d|S c c}w c c}w )Nr      r   )r   )r5   text_embeddingsrO   r   r\   r   r^   r   tr   rD   s              r'   from_embeddingszPGEmbedding.from_embeddings  su      //!1//$34qad4
4.s..	
  +"7	
 	
 		
 04s	   ?Ac                @    | j                  |      } | ||||      }|S )Nr   )r   )r5   rO   r\   r^   r   rZ   r   s          r'   from_existing_indexzPGEmbedding.from_existing_index  s2      55f=/+("7	
 r&   c                <    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   )r5   r   rZ   s      r'   r   z!PGEmbedding.get_connection_string  s6    !5#0"
 !N  ! r&   c           
         |D cg c]  }|j                    }}|D cg c]  }|j                   }	}| j                  |      }
|
|d<    | j                  d||||	||d|S c c}w c c}w )NrZ   )r   r^   rO   r   r   r\   r%   )r   r{   r   r   )r5   	documentsrO   r\   r   r^   r   dr   r   rZ   s              r'   from_documentszPGEmbedding.from_documents  s     *33A33)23AQZZ3	355f=&7"#s~~ 
"7+
 
 	
 43s
   A"A')rZ   r>   r[   r   r\   r>   r]   rA   r^   boolra   zOptional[logging.Logger]r?   None)r?   r   )r?   r   )r?   zsqlalchemy.engine.Connection)r   intr   r   r   r   r   r   r   r   r?   r   )r6   r   r?   r@   )r   	List[str]rD   List[List[float]]rO   r   r   Optional[List[dict]]r   Optional[List[str]]r\   r>   r^   r   r   r   r?   rX   )r   r   rD   r   r   z
List[dict]r   r   r   r   r?   r   )NN)
r   zIterable[str]r   r   r   r   r   r   r?   r   )   N)
r1   r>   r   r   r2   rA   r   r   r?   List[Document])r1   r>   r   r   r2   rA   r?   List[Tuple[Document, float]])rO   List[float]r   r   r2   rA   r?   r   )
rO   r   r   r   r2   rA   r   r   r?   r   )r5   Type[PGEmbedding]r   r   rO   r   r   r   r\   r>   r   r   r^   r   r   r   r?   rX   )r   zList[Tuple[str, List[float]]]rO   r   r   r   r\   r>   r   r   r^   r   r   r   r?   rX   )r5   r   rO   r   r\   r>   r^   r   r   r   r?   rX   )r   zDict[str, Any]r?   r>   )r5   r   r   r   rO   r   r\   r>   r   r   r^   r   r   r   r?   rX   )r   r   r   r   "_LANGCHAIN_DEFAULT_COLLECTION_NAMErd   rb   propertyrD   rf   rh   ri   r   rj   ADA_TOKEN_COUNTr   r   r   rE   r   r   r   r   r   r   r   r   r   r   r   r   r%   r&   r'   rX   rX   j   s   ,  B.2&++/ ' 	
 ,  $ ) 
"!	! ' '
%1/ "#!DD D 	D
 D D 
D<J  +/#'A&+ & 	
 ( !   $  
 @ & 	
   
4 +/#'	 ( !	
  
F !%	

 
 	

 
 

" !%	

 
 	

 
&
 !%	CC C 	C
 
&CP !%	
3
3 
3 	
3
 
3 

3 
 +/A#'&+


 
 (	

 
 !
  $
 
 

 
. 
 +/A#'&+
6
 
 (	

 
 !
  $
 
 

 
0   B&+	   $	
  
 $ ! !  
  B#'&+

!
 
 	

 !
  $
 
 

 
r&   rX   )'
__future__r   r_   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   rz   r   r   r   r)   r+   rS   rX   r%   r&   r'   <module>r     s    "   C C C   5 0</ . 0 5 3 %0 "W W&#i &#RDY D, i
+ i
y  <;<s   B B*)B*