
    dh}                       S SK Jr  S SKrS SK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Jr  S SKrS SKrS SKJrJr  S SKJrJr  S SKJr  S SKJr  S SKJr  S S	KJ r    S S
KJ!r!  S SK$J%r%  S SK&J'r'  S SK(J)r)  S SK*J+r+  Sr,Sr-SS jr. " S S5      r/      SS jr0 " S S5      r1 " S S\2\Rf                  5      r4\4Rj                  r6 " S S\+5      r7g! \" a	    S S
K#J!r!   N|f = f)    )annotationsN)
AnyCallableDict	GeneratorIterableListOptionalTupleTypeUnion)deletefunc)JSONUUID)ProgrammingError)Session)quoted_name)maximal_marginal_relevance)declarative_base)Document)
Embeddings)get_from_dict_or_env)VectorStorei   	langchainc                :    U  VVs/ sH  u  pUPM	     snn$ s  snnf )z!Return docs from docs and scores. )docs_and_scoresdoc_s      `/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/lantern.py_results_to_docsr"   ,   s    -.oFCCo...s   c                      \ rS rSrSrSrg)BaseEmbeddingStore1   z+Base class for the Lantern embedding store.r   N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       r!   r$   r$   1   s    5r,   r$   DistanceStrategyc                   ^^ SmU [         R                  :X  a  [        R                  mO[        R                  m[        [        5       S9n " UU4S jSU[        5      nU$ )zGet the embedding store class.N)class_registryc                  8  > \ rS rSr Y r\R                  " \" SS9S\R                  S9rSS0r
\R                  " \R                  SS9r\R                  " \SS9r\R                  " \R                  SS9r\R                  " \R                   "  Y5      5      rSrg)	+get_embedding_store.<locals>.EmbeddingStoreC   T)as_uuid)primary_keydefaultextend_existing)nullabler   N)r&   r'   r(   r)   __tablename__
sqlalchemyColumnr   uuiduuid4__table_args__Stringdocumentr   	cmetadata	custom_idARRAY	embeddingr+   )collection_nameembedding_types   r!   EmbeddingStorer1   C   s    '  D$**
 ,T2$$Z%6%6F%%dT:	%%j&7&7$G	%%j&6&6~&FG	r,   rF   )r-   HAMMINGr9   INTEGERREALr   dictr$   )distance_strategyrD   DynamicBaserF   rE   s    `  @r!   get_embedding_storerM   5   sX    
 N,444#++#"$&9K
H 
H&8 
H r,   c                  .    \ rS rSr% SrS\S'   S\S'   Srg)	QueryResultR   zResult from a query.r$   rF   floatdistancer   N)r&   r'   r(   r)   r*   __annotations__r+   r   r,   r!   rO   rO   R   s    &&Or,   rO   c                  $    \ rS rSrSrSrSrSrSrg)r-   Y   z&Enumerator of the Distance strategies.l2sqcosinehammingr   N)	r&   r'   r(   r)   r*   	EUCLIDEANCOSINErG   r+   r   r,   r!   r-   r-   Y   s    0IFGr,   c                     \ rS rSrSr\\SSSS4                 S1S jjr  S2S jr\	S3S j5       r
\	S4S j5       r\              S5S	 j5       rS6S
 jr\	S7S j5       rS2S jrS2S jrS2S jrS2S jrS8S jrS9S jrS:S jrS:S jr    S;S jr\	S:S j5       r\SSS4           S<S jjrS2S jrS2S jrS2S jr\R@                  S=S j5       r! S>     S?S jjr"\SS\\S4                   S@S jj5       r#            SAS jr$  SB         SCS  jjr%SDS! jr&  SE         SFS" jjr'  SE       SGS# jjr(  SE       SHS$ jjr)  SE       SIS% jjr*  SE         SJS& jjr+\S\\SS4                   SKS' jj5       r,\S\SS\4                 SLS( jj5       r-\\S\4             SMS) jj5       r.\SNS* j5       r/\\\SS4                 SOS+ jj5       r0    SP             SQS, jjr1    SP             SRS- jjr2    SP             SSS. jjr3    SP             STS/ jjr4S0r5g)ULanternd   a  `Postgres` with the `lantern` extension as a vector store.

lantern 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 the name of the table in which embedding data will be stored
        The table 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.
    - `COSINE` is the cosine distance.
    - `HAMMING` is the hamming distance.
- `pre_delete_collection` if True, will delete the collection if it exists.
    (default: False)
    - Useful for testing.
NFc	                    Xl         X l        X@l        XPl        X0l        X`l        U=(       d    [        R                  " [        5      U l	        Xl
        [        U R                  U5      U l        U R                  5         g N)connection_stringembedding_functionrD   collection_metadata_distance_strategypre_delete_collectionlogging	getLoggerr&   loggeroverride_relevance_score_fnrM   rK   rF   __post_init__)	selfr`   ra   rK   rD   rb   rd   rg   relevance_score_fns	            r!   __init__Lantern.__init__y   sk     "3"4.#6 "3%:"; 1 1( ;+=(1""O
 	r,   c                n    U R                  5       U l        U R                  5         U R                  5         g r_   )connect_conncreate_hnsw_extensioncreate_collectionrj   s    r!   ri   Lantern.__post_init__   s)     \\^
""$ r,   c                B   [        U R                  [        5      (       a  U R                  $ U R                  [        R                  R                  :X  a  [        R                  $ U R                  [        R
                  R                  :X  a  [        R
                  $ U R                  [        R                  R                  :X  a  [        R                  $ [        SU R                   SSR                  [         Vs/ sH  oR                  PM     sn5       S35      es  snf )Nz#Got unexpected value for distance: z. Should be one of z, .)	
isinstancerc   r-   rY   valuerZ   rG   
ValueErrorjoin)rj   dss     r!   rK   Lantern.distance_strategy   s    d--/?@@***""&6&@&@&F&FF#---$$(8(?(?(E(EE#***$$(8(@(@(F(FF#+++5d6M6M5N O$$(IIBR.SBRBxxBR.S$T#UUVX .Ss   :Dc                    U R                   $ r_   )ra   rs   s    r!   
embeddingsLantern.embeddings   s    &&&r,   c                *    SU SU SU SU SU SU 3$ )z2Return connection string from database parameters.zpostgresql+z://:@/r   )clsdriverhostportdatabaseuserpasswords          r!    connection_string_from_db_params(Lantern.connection_string_from_db_params   s/     VHCvQxj$qazRRr,   c                f    [         R                  " U R                  5      nUR                  5       nU$ r_   )r9   create_enginer`   ro   )rj   engineconns      r!   ro   Lantern.connect   s)    ))$*@*@A~~r,   c                    U R                   [        R                  :X  a  gU R                   [        R                  :X  a  gU R                   [        R                  :X  a  gg )N	l2sq_distcos_disthamming_dist)rK   r-   rY   rZ   rG   rs   s    r!   distance_functionLantern.distance_function   sP    !!%5%?%??##'7'>'>>##'7'?'??! @r,   c                6    [        U R                  5       n[        R                  " S5      nUR	                  U5        UR                  5         S S S 5        g ! , (       d  f       g = f! [         a%  nU R                  R                  U5         S nAg S nAff = f)Nz&CREATE EXTENSION IF NOT EXISTS lantern)	r   rp   r9   textexecutecommit	Exceptionrg   	exception)rj   session	statementes       r!   rq   Lantern.create_hnsw_extension   sl    	%$&OO,TU		*  %$$  	%KK!!!$$	%s4   A) 8AA) 
A&"A) &A) )
B3BBc                F     U R                  5         g ! [         a     g f = fr_   )rr   r   rs   s    r!   create_tables_if_not_exists#Lantern.create_tables_if_not_exists   s%    	""$ 		s    
  c                     U R                   R                  R                  U R                  R                  5        g ! [
         a     g f = fr_   )rF   	__table__droprp   r   r   rs   s    r!   
drop_tableLantern.drop_table   s=    	))..tzz/@/@A 		s   9< 
A	A	c                $    U R                  5         g r_   )r   rs   s    r!   drop_tablesLantern.drop_tables   s    r,   c                    U$ r_   r   )rj   rR   s     r!   _hamming_relevance_score_fn#Lantern._hamming_relevance_score_fn   s    r,   c                b   U R                   b  U R                   $ U R                  [        R                  :X  a  U R                  $ U R                  [        R
                  :X  a  U R                  $ U R                  [        R                  :X  a  U R                  $ [        SU R                   S35      e)a   
The 'correct' relevance function
may differ depending on a few things, including:
- the distance / similarity metric used by the VectorStore
- the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
- embedding dimensionality
- etc.
z=No supported normalization function for distance_strategy of z>.Consider providing relevance_score_fn to Lantern constructor.)rh   rK   r-   rZ   _cosine_relevance_score_fnrY   _euclidean_relevance_score_fnrG   r   ry   rc   rs   s    r!   _select_relevance_score_fn"Lantern._select_relevance_score_fn   s     ++7333 !!%5%<%<<222##'7'A'AA555##'7'?'??333--1-D-D,E FPP r,   c                    U R                   [        R                  :X  a  gU R                   [        R                  :X  a  gU R                   [        R                  :X  a  g[        SU R                   S35      e)Ndist_cos_opsdist_l2sq_opsdist_hamming_opsz5No supported operator class for distance_strategy of rv   rK   r-   rZ   rY   rG   ry   rc   rs   s    r!   _get_op_classLantern._get_op_class   sp    !!%5%<%<<!##'7'A'AA"##'7'?'??%--1-D-D,EQH r,   c                    U R                   [        R                  :X  a  gU R                   [        R                  :X  a  gU R                   [        R                  :X  a  g[        SU R                   S35      e)Nz<=>z<->z<+>z/No supported operator for distance_strategy of rv   r   rs   s    r!   _get_operatorLantern._get_operator  sp    !!%5%<%<<##'7'A'AA##'7'?'??--1-D-D,EQH r,   c                n    U R                   [        R                  :X  a  [        [	        S U5      5      $ U$ )Nc                    [        U 5      $ r_   )int)xs    r!   <lambda>1Lantern._typed_arg_for_distance.<locals>.<lambda>  s    c!fr,   )rK   r-   rG   listmap)rj   rC   s     r!   _typed_arg_for_distanceLantern._typed_arg_for_distance  s2     !!%5%=%==,i899r,   c                "    SU R                    S3$ )N
langchain__idx)rD   rs   s    r!   _index_nameLantern._index_name  s    D00166r,      @   c           	        [         R                  " SR                  [        U R                  S5      [        U R
                  S5      U R                  5       5      5      n[        U R                  5       nUR                  UUUUUS.5        UR                  5         SSS5        U R                  R                  S5        g! , (       d  f       N*= f)a  Create HNSW index on collection.

Optional Keyword Args for HNSW Index:
    engine: "nmslib", "faiss", "lucene"; default: "nmslib"

    ef: Size of the dynamic list used during k-NN searches. Higher values
    lead to more accurate but slower searches; default: 64

    ef_construction: Size of the dynamic list used during k-NN graph creation.
    Higher values lead to more accurate graph but slower indexing speed;
    default: 64

    m: Number of bidirectional links created for each new element. Large impact
    on memory consumption. Between 2 and 100; default: 16

    dims: Dimensions of the vectors in collection. default: 1536
zCREATE INDEX IF NOT EXISTS {} ON {} USING hnsw (embedding {}) WITH (dim = :dim, m = :m, ef_construction = :ef_construction, ef = :ef);T)dimmef_constructionefNz.HNSW extension and index created successfully.)r9   r   formatr   r   rD   r   r   rp   r   r   rg   info)rj   dimsr   r   	ef_search_kwargscreate_index_queryr   s           r!   create_hnsw_indexLantern.create_hnsw_index#  s    2 (__ D,,d3D00$7""$
 TZZ GOO"'6#	 NN ! 	IJ ! s   3(B??
Cc                   [        U R                  5       nUR                  [        R                  " SR                  [        U R                  S5      5      5      5        UR                  5         S S S 5        g ! , (       d  f       g = f)NzDROP INDEX IF EXISTS {}T)	r   rp   r   r9   r   r   r   r   r   )rj   r   s     r!   
drop_indexLantern.drop_indexY  s^    TZZ GOO-44#D$4$4d; NN !  s   AA88
Bc                   U R                   (       a   U R                  5         U R                  5         U R                  R	                  5           U R
                  R                  R                  U R                  R                  5        S S S 5        g ! [         a  nUR                  S:X  a   S nAN(UeS nAff = f! , (       d  f       g = f)Nf405)rd   delete_collectionr   rp   beginrF   r   creater   r   code)rj   r   s     r!   rr   Lantern.create_collectione  s    %%""$OOZZ##--44TZZ5F5FG   $ 66V#G  s6   B99B
B6B1*B9/B11B66B99
Cc                Z    U R                   R                  S5        U R                  5         g )NzTrying to delete collection)rg   debugr   rs   s    r!   r   Lantern.delete_collectiont  s    78r,   c              #  :   #    [        U R                  5      v   g7f)z?Create a context manager for the session, bind to _conn string.N)r   rp   rs   s    r!   _make_sessionLantern._make_sessionx  s      djj!!s   c                n   [        U R                  5       nUbs  U R                  R                  S5        [	        U R
                  5      R                  U R
                  R                  R                  U5      5      nUR                  U5        UR                  5         SSS5        g! , (       d  f       g= f)zGDelete vectors by ids or uuids.

Args:
    ids: List of ids to delete.
NzUTrying to delete vectors by ids (represented by the model using the custom ids field))r   rp   rg   r   r   rF   whererA   in_r   r   )rj   idskwargsr   stmts        r!   r   Lantern.delete}  s     TZZ G!!2 d11288''1155c: %NN !  s   BB&&
B4c	                .   Uc-  U V
s/ sH!  n
[        [        R                  " 5       5      PM#     nn
U(       d  U V
s/ sH  n
0 PM     nn
U R                  U	5      nU " UUUUUS9nUR                  " SXXES.U	D6  UR
                  " S0 U	D6  U$ s  sn
f s  sn
f )a  
Order of elements for lists `ids`, `embeddings`, `texts`, `metadatas`
should match, so each row will be associated with correct values.

Postgres connection string is required
"Either pass it as `connection_string` parameter
or set the LANTERN_CONNECTION_STRING environment variable.

- `texts` texts to insert into collection.
- `embeddings` an Embeddings to insert into collection
- `embedding` is :class:`Embeddings` that will be used for
        embedding the text sent. If none is sent, then the
        multilingual Tensorflow Universal Sentence Encoder will be used.
- `metadatas` row metadata to insert into collection.
- `ids` row ids to insert into collection.
- `collection_name` is the name of the collection to use. (default: langchain)
    - NOTE: This is the name of the table in which embedding data will be stored
        The table 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.
    - `COSINE` is the cosine distance.
    - `HAMMING` is the hamming distance.
- `pre_delete_collection` if True, will delete the collection if it exists.
    (default: False)
    - Useful for testing.
r`   rD   ra   rd   rK   )textsr~   	metadatasr   r   )strr;   r<   _Lantern__get_connection_stringadd_embeddingsr   )r   r   r~   rC   r   r   rD   rK   rd   r   r    r`   stores                r!   _initialize_from_embeddings#Lantern._initialize_from_embeddings  s    P ;.34e3tzz|$eC4%*+UUI+77?/+("7/
 	 	
)	
PV	
 	)&)+ 5 ,s
   'BBc           	         [        U R                  5       n[        XX$5       H)  u  pxpU R                  U	UUU
S9nUR	                  U5        M+     UR                  5         S S S 5        g ! , (       d  f       g = fN)rC   r?   r@   rA   )r   rp   ziprF   addr   )rj   r   r~   r   r   r   r   r   metadatarC   idembedding_stores               r!   r   Lantern.add_embeddings  sp     TZZ G14Uz1W-	"&"5"5'!& 	 #6 # O, 2X NN !  s   A
A))
A7c           	        Uc-  U Vs/ sH!  n[        [        R                  " 5       5      PM#     nnU R                  R	                  [        U5      5      nU(       d  U Vs/ sH  n0 PM     nn[        U R                  5       n[        XXc5       H)  u  ppU R                  U
UU	US9nUR                  U5        M+     UR                  5         S S S 5        U$ s  snf s  snf ! , (       d  f       U$ = fr   )r   r;   r<   ra   embed_documentsr   r   rp   r   rF   r  r   )rj   r   r   r   r   r    r~   r   r   r  rC   r  r  s                r!   	add_textsLantern.add_texts  s     ;.34e3tzz|$eC4,,<<T%[I
%*+UUI+TZZ G14Uz1W-	"&"5"5'!& 	 #6 # O, 2X NN ! 
% 5
 ,   
s   'C CA
C  
C/c                    U Vs/ sHP  n[        UR                  R                  UR                  R                  S9U R                  b  UR
                  OS4PMR     nnU$ s  snf )z$Return docs and scores from results.)page_contentr  N)r   rF   r?   r@   ra   rR   )rj   resultsresultdocss       r!   _results_to_docs_and_scores#Lantern._results_to_docs_and_scores  sr     "	
 " !'!6!6!?!?#22<< $(#:#:#FD " 	 	
 	
s   AAc                V    U R                   R                  US9nU R                  UUUS9$ )N)r   rC   kfilter)ra   embed_querysimilarity_search_by_vector)rj   queryr  r  r   rC   s         r!   similarity_searchLantern.similarity_search  s>     ++77U7C	// 0 
 	
r,   c                \    U R                   R                  U5      nU R                  XBUS9nU$ Nr  )ra   r  &similarity_search_with_score_by_vector)rj   r  r  r  rC   r  s         r!   similarity_search_with_score$Lantern.similarity_search_with_score  s;     ++77>	::V ; 
 r,   c                D    U R                  XUS9nU R                  U5      $ r  )_Lantern__query_collectionr  )rj   rC   r  r  r  s        r!   r  .Lantern.similarity_search_with_score_by_vector*  s*     ))I6)R//88r,   c                (   [        U R                  5       n[        R                  " S5      n[        R                  " S5      nUR	                  U5        UR	                  USU05        S nUGb  / nUR                  5        H  u  pSn[        U
[        5      (       a  U[        [        R                  U
5      ;   ay  U
R                  5        VVs0 sH  u  p,UR                  5       U_M     nnnU R                  R                  U	   R                  R                  X   5      nUR                  U5        M  U R                  R                  U	   R                  [        U
5      :H  nUR                  U5        M     [        R                   " U6 nU R#                  U5      nUR%                  U R                  ['        [(        U R*                  5      " U R                  R,                  U5      R/                  S5      5      nUb  UR1                  U5      nUR3                  U R                  R,                  R5                  U R7                  5       5      " U5      5      R9                  W5      R;                  5       nS S S 5        U$ s  snnf ! , (       d  f       W$ = f)NzSET enable_seqscan = offzSET hnsw.init_k = :kr  inrR   )r   rp   r9   r   r   itemsrw   rJ   r   r   lowerrF   r@   astextr   appendand_r   r  getattrr   r   rC   labelr  order_byopr   limitall)rj   rC   r  r  r   set_enable_seqscan_stmt
set_init_k	filter_byfilter_clauseskeyrx   INvvalue_case_insensitivefilter_by_metadatar  r  s                    r!   __query_collectionLantern.__query_collection4  s/    TZZ G&0oo6P&Q##)?@JOO34OOJa1I!!#"(,,.JCB!%..2SYY9N3N5:[[]25BTQAGGIqL] / 2 .2-@-@-J-J. &%;%?!@ + '--.@A-1-@-@-J-J. &CJ./* '--.@A #1  'OO^<	44Y?IMM##d445''119%
#	E $Y/ ''1144T5G5G5IJ9U q M !\ E2 ! \ s   B7JI<
)F	J<J
Jc                8    U R                  XUS9n[        U5      $ r  )r  r"   )rj   rC   r  r  r   r   s         r!   r  #Lantern.similarity_search_by_vectorj  s,     EEV F 
  00r,   c           	     l    UR                  [        U5      5      n	U R                  " UU	U4UUUUUS.UD6$ )a  
Initialize Lantern vectorstore from list of texts.
The embeddings will be generated using `embedding` class provided.

Order of elements for lists `ids`, `texts`, `metadatas` should match,
so each row will be associated with correct values.

Postgres connection string is required
"Either pass it as `connection_string` parameter
or set the LANTERN_CONNECTION_STRING environment variable.

- `connection_string` is fully populated connection string for postgres database
- `texts` texts to insert into collection.
- `embedding` is :class:`Embeddings` that will be used for
        embedding the text sent. If none is sent, then the
        multilingual Tensorflow Universal Sentence Encoder will be used.
- `metadatas` row metadata to insert into collection.
- `collection_name` is the name of the collection to use. (default: langchain)
    - NOTE: This is the name of the table in which embedding data will be stored
        The table 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.
    - `COSINE` is the cosine distance.
    - `HAMMING` is the hamming distance.
- `ids` row ids to insert into collection.
- `pre_delete_collection` if True, will delete the collection if it exists.
    (default: False)
    - Useful for testing.
r   r   rD   rd   rK   )r  r   r   )
r   r   rC   r   rD   rK   r   rd   r   r~   s
             r!   
from_textsLantern.from_textsv  sU    T ..tE{;
..

  +"7/

 

 
	
r,   c           	         U V	s/ sH  oS   PM	     n
n	U V	s/ sH  oS   PM	     nn	U R                   " U
UU4UUUUUS.UD6$ s  sn	f s  sn	f )a  Construct Lantern wrapper from raw documents and pre-
generated embeddings.

Postgres connection string is required
"Either pass it as `connection_string` parameter
or set the LANTERN_CONNECTION_STRING environment variable.

Order of elements for lists `ids`, `text_embeddings`, `metadatas` should match,
so each row will be associated with correct values.

- `connection_string` is fully populated connection string for postgres database
- `text_embeddings` is array with tuples (text, embedding)
        to insert into collection.
- `embedding` is :class:`Embeddings` that will be used for
        embedding the text sent. If none is sent, then the
        multilingual Tensorflow Universal Sentence Encoder will be used.
- `metadatas` row metadata to insert into collection.
- `collection_name` is the name of the collection to use. (default: langchain)
    - NOTE: This is the name of the table in which embedding data will be stored
        The table will be created when initializing the store (if not exists)
        So, make sure the user has the right permissions to create tables.
- `ids` row ids to insert into collection.
- `pre_delete_collection` if True, will delete the collection if it exists.
    (default: False)
    - Useful for testing.
- `distance_strategy` is the distance strategy to use. (default: EUCLIDEAN)
    - `EUCLIDEAN` is the euclidean distance.
    - `COSINE` is the cosine distance.
    - `HAMMING` is the hamming distance.
r      r=  )r   )r   text_embeddingsrC   r   rD   r   rd   rK   r   tr   r~   s               r!   from_embeddingsLantern.from_embeddings  su    T  //!1/$34OqdO
4..

  +"7/

 

 
	
 04s
   AAc                <    U R                  U5      nU " UUUUUS9nU$ )a  
Get instance of an existing Lantern store.This method will
return the instance of the store without inserting any new
embeddings

Postgres connection string is required
"Either pass it as `connection_string` parameter
or set the LANTERN_CONNECTION_STRING environment variable.

- `connection_string` is a postgres connection string.
- `embedding` is :class:`Embeddings` that will be used for
        embedding the text sent. If none is sent, then the
        multilingual Tensorflow Universal Sentence Encoder will be used.
- `collection_name` is the name of the collection to use. (default: langchain)
    - NOTE: This is the name of the table in which embedding data will be stored
        The table will be created when initializing the store (if not exists)
        So, make sure the user has the right permissions to create tables.
- `ids` row ids to insert into collection.
- `pre_delete_collection` if True, will delete the collection if it exists.
    (default: False)
    - Useful for testing.
- `distance_strategy` is the distance strategy to use. (default: EUCLIDEAN)
    - `EUCLIDEAN` is the euclidean distance.
    - `COSINE` is the cosine distance.
    - `HAMMING` is the hamming distance.
r   )r   )r   rC   rD   rd   rK   r   r`   r   s           r!   from_existing_indexLantern.from_existing_index  s6    F  77?/+("7/
 r,   c                @    [        USSS9nU(       d  [        S5      eU$ )Nr`   LANTERN_CONNECTION_STRING)datar3  env_keyzPostgres connection string is requiredEither pass it as `connection_string` parameteror set the LANTERN_CONNECTION_STRING variable.)r   ry   )r   r   r`   s      r!   __get_connection_stringLantern.__get_connection_string  s6    !5#/"
 !A  ! r,   c                    U Vs/ sH  oR                   PM     n	nU Vs/ sH  oR                  PM     n
nU R                  U5      nXS'   U R                  " SU	UUU
UUUS.UD6$ s  snf s  snf )a  
Initialize a vector store with a set of documents.

Postgres connection string is required
"Either pass it as `connection_string` parameter
or set the LANTERN_CONNECTION_STRING environment variable.

- `connection_string` is a postgres connection string.
- `documents` is list of :class:`Document` to initialize the vector store with
- `embedding` is :class:`Embeddings` that will be used for
        embedding the text sent. If none is sent, then the
        multilingual Tensorflow Universal Sentence Encoder will be used.
- `collection_name` is the name of the collection to use. (default: langchain)
    - NOTE: This is the name of the table in which embedding data will be stored
        The table 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.
    - `COSINE` is the cosine distance.
    - `HAMMING` is the hamming distance.
- `ids` row ids to insert into collection.
- `pre_delete_collection` if True, will delete the collection if it exists.
    (default: False)
    - Useful for testing.
r`   )r   rd   rC   r   r   rD   rK   r   )r  r  r   r>  )r   	documentsrC   rD   rK   r   rd   r   dr   r   r`   s               r!   from_documentsLantern.from_documents'  s    H *33A3)23AZZ	377?&7"#~~ 	
"7+/	
 	
 		
 43s
   A$A)c                H   U R                  XUS9nU Vs/ sH  oR                  R                  PM     n	n[        [        R
                  " U[        R                  S9U	UUS9n
U R                  U5      n[        U5       VVs/ sH  u  pX;   d  M  UPM     snn$ s  snf s  snnf )aZ  Return docs selected using the maximal marginal relevance with score
    to embedding vector.

Maximal marginal relevance optimizes for similarity to query AND diversity
    among selected documents.

Args:
    embedding: Embedding to look up documents similar to.
    k (int): Number of Documents to return. Defaults to 4.
    fetch_k (int): Number of Documents to fetch to pass to MMR algorithm.
        Defaults to 20.
    lambda_mult (float): 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.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List[Tuple[Document, float]]: List of Documents selected by maximal marginal
        relevance to the query and score for each.
r  )dtype)r  lambda_mult)	r   rF   rC   r   nparrayfloat32r  	enumerate)rj   rC   r  fetch_krV  r  r   r  r  embedding_listmmr_selected
candidatesirs                 r!   2max_marginal_relevance_search_with_score_by_vector:Lantern.max_marginal_relevance_search_with_score_by_vector\  s    < ))IQW)XHOPf//99P1HHYbjj1#	
 55g>
'
3I3daq7H3II Q Js   B
BBc                h    U R                   R                  U5      nU R                  " U4UUUUS.UD6$ )a  Return docs selected using the maximal marginal relevance.

Maximal marginal relevance optimizes for similarity to query AND diversity
    among selected documents.

Args:
    query (str): Text to look up documents similar to.
    k (int): Number of Documents to return. Defaults to 4.
    fetch_k (int): Number of Documents to fetch to pass to MMR algorithm.
        Defaults to 20.
    lambda_mult (float): 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.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List[Document]: List of Documents selected by maximal marginal relevance.
r  r[  rV  r  )ra   r  'max_marginal_relevance_search_by_vector)rj   r  r  r[  rV  r  r   rC   s           r!   max_marginal_relevance_search%Lantern.max_marginal_relevance_search  sK    8 ++77>	;;
#
 
 	
r,   c           	     l    U R                   R                  U5      nU R                  " SUUUUUS.UD6nU$ )a?  Return docs selected using the maximal marginal relevance with score.

Maximal marginal relevance optimizes for similarity to query AND diversity
    among selected documents.

Args:
    query (str): Text to look up documents similar to.
    k (int): Number of Documents to return. Defaults to 4.
    fetch_k (int): Number of Documents to fetch to pass to MMR algorithm.
        Defaults to 20.
    lambda_mult (float): 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.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List[Tuple[Document, float]]: List of Documents selected by maximal marginal
        relevance to the query and score for each.
)rC   r  r[  rV  r  r   )ra   r  ra  )	rj   r  r  r[  rV  r  r   rC   r  s	            r!   (max_marginal_relevance_search_with_score0Lantern.max_marginal_relevance_search_with_score  sO    : ++77>	FF 
#
 
 r,   c                H    U R                   " U4UUUUS.UD6n[        U5      $ )a  Return docs selected using the maximal marginal relevance
    to embedding vector.

Maximal marginal relevance optimizes for similarity to query AND diversity
    among selected documents.

Args:
    embedding (str): Text to look up documents similar to.
    k (int): Number of Documents to return. Defaults to 4.
    fetch_k (int): Number of Documents to fetch to pass to MMR algorithm.
        Defaults to 20.
    lambda_mult (float): 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.
    filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None.

Returns:
    List[Document]: List of Documents selected by maximal marginal relevance.
rd  )ra  r"   )rj   rC   r  r[  rV  r  r   r   s           r!   re  /Lantern.max_marginal_relevance_search_by_vector  s?    : QQ
#
 
  00r,   )
rF   rp   rc   rb   rD   r`   ra   rg   rh   rd   )r`   r   ra   r   rK   r-   rD   r   rb   Optional[dict]rd   boolrg   zOptional[logging.Logger]rk   z"Optional[Callable[[float], float]]returnNone)ro  rp  )ro  r-   )ro  r   )r   r   r   r   r   r   r   r   r   r   r   r   ro  r   )ro  zsqlalchemy.engine.Connection)ro  r   )rR   rQ   ro  rQ   )ro  zCallable[[float], float])ro  r   )rC   List[Union[float, int]]ro  rq  )r   r   r   r   r   r   r   r   r   r   ro  rp  )ro  zGenerator[Session, None, None]r_   )r   Optional[List[str]]r   r   ro  rp  )r   	List[str]r~   List[List[float]]rC   r   r   Optional[List[dict]]r   rr  rD   r   rK   r-   rd   rn  r   r   ro  r\   )r   rs  r~   rt  r   z
List[dict]r   rs  r   r   ro  rp  )NN)
r   zIterable[str]r   ru  r   rr  r   r   ro  rs  )r  r   ro  List[Tuple[Document, float]])   N)
r  r   r  r   r  rm  r   r   ro  List[Document])r  r   r  r   r  rm  ro  rv  )rC   List[float]r  r   r  rm  ro  rv  )rC   ry  r  r   r  rm  ro  z	List[Any])
rC   ry  r  r   r  rm  r   r   ro  rx  )r   Type[Lantern]r   rs  rC   r   r   ru  rD   r   rK   r-   r   rr  rd   rn  r   r   ro  r\   )rB  zList[Tuple[str, List[float]]]rC   r   r   ru  rD   r   r   rr  rd   rn  rK   r-   r   r   ro  r\   )r   rz  rC   r   rD   r   rd   rn  rK   r-   r   r   ro  r\   )r   zDict[str, Any]ro  r   )r   rz  rP  rx  rC   r   rD   r   rK   r-   r   rr  rd   rn  r   r   ro  r\   )rw     g      ?N)rC   ry  r  r   r[  r   rV  rQ   r  Optional[Dict[str, str]]r   r   ro  rv  )r  r   r  r   r[  r   rV  rQ   r  r|  r   r   ro  rx  )r  r   r  r   r[  r   rV  rQ   r  rm  r   r   ro  rv  )rC   ry  r  r   r[  r   rV  rQ   r  r|  r   r   ro  rx  )6r&   r'   r(   r)   r*   DEFAULT_DISTANCE_STRATEGY"_LANGCHAIN_DEFAULT_COLLECTION_NAMErl   ri   propertyrK   r~   classmethodr   ro   r   rq   r   r   r   r   r   r   r   r   r   ADA_TOKEN_COUNTr   r   rr   r   
contextlibcontextmanagerr   r   r   r   r  r  r  r  r  r   r  r>  rD  rG  r   rR  ra  rf  ri  re  r+   r   r,   r!   r\   r\   d   s   0 /HA.2&++/AE ' ,	
  ,  $ ) ? 
0!	!    ' ' 
S
S 
S 	
S
 
S 
S 
S 

S 
S
 " "%60	  7 7
 $!4K4K 4K 	4K
 4K 4K 
4Kl
 " " $(   
	,  +/#'A.G&+== &= 	=
 (= != = ,=  $= = 
= =~ & 	
   
, +/#'	 ( !	
  
8" !%	

 
 	

 
 

" !%	

 
 	

 
&
 !%	99 9 	9
 
&9 !%	44 4 	4
 
4r !%	
1
1 
1 	
1
 
1 

1 
 +/A.G#'&+5
5
5
 5
 (	5

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

 6
 !6
  $6
 ,6
 6
 
6
 6
p   B&+.G,,, ,  $	,
 ,, , 
, ,\ ! !  
  B.G#'&+2
2
!2
 2
 	2

 ,2
 !2
  $2
 2
 
2
 2
n  +/*J*J *J 	*J
 *J )*J *J 
&*J^  +/$
$
 $
 	$

 $
 )$
 $
 
$
R  !%&& & 	&
 & & & 
&&V  +/&1&1 &1 	&1
 &1 )&1 &1 
&1 &1r,   r\   )r   r   ro  rx  )rK   r-   rD   r   ro  r   )8
__future__r   r  enumre   r;   typingr   r   r   r   r   r	   r
   r   r   r   numpyrW  r9   r   r   sqlalchemy.dialects.postgresqlr   r   sqlalchemy.excr   sqlalchemy.ormr   sqlalchemy.sqlr   &langchain_community.vectorstores.utilsr   r   ImportErrorsqlalchemy.ext.declarativelangchain_core.documentsr   langchain_core.embeddingsr   langchain_core.utilsr   langchain_core.vectorstoresr   r  r~  r"   r$   rM   rO   r   Enumr-   rZ   r}  r\   r   r,   r!   <module>r     s    "         # 5 + " & M</ . 0 5 3%0 "/
6 6':=: sDII  -33 X1k X1I  <;<s    C C#"C#