
    Ngi                       d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlZd dl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! 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+ dZ,dZ-d"dZ. G d d          Z/d#dZ0 G d d          Z1 G d de2ej3                  Z4e4j5        Z6 G d  d!e+          Z7dS )$    )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   	langchaindocs_and_scoresr   returnList[Document]c                    d | D             S )z!Return docs from docs and scores.c                    g | ]\  }}|S  r!   ).0doc_s      d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/lantern.py
<listcomp>z$_results_to_docs.<locals>.<listcomp>.   s    ...FCC...    r!   )r   s    r%   _results_to_docsr(   ,   s    ..o....r'   c                      e Zd ZdZdS )BaseEmbeddingStorez+Base class for the Lantern embedding store.N)__name__
__module____qualname____doc__r!   r'   r%   r*   r*   1   s        5555r'   r*   distance_strategyDistanceStrategycollection_namestrc                    d| t           j        k    rt          j        nt          j        t          t                                } G fdd|t                    }|S )zGet the embedding store class.N)class_registryc                     e Zd Z Z ej         ed          dej                  ZddiZ	 ej        ej
        d          Z ej        ed          Z ej        ej
        d          Z ej         ej                            ZdS )+get_embedding_store.<locals>.EmbeddingStoreT)as_uuid)primary_keydefaultextend_existing)nullableN)r+   r,   r-   __tablename__
sqlalchemyColumnr   uuiduuid4__table_args__Stringdocumentr   	cmetadata	custom_idARRAY	embedding)r1   embedding_types   r%   EmbeddingStorer6   C   s        ' z DD$*
 
 
 ,T2$:$Z%6FFF%J%dT:::	%J%j&7$GGG	%J%&6j&6~&F&FGG			r'   rI   )r0   HAMMINGr=   INTEGERREALr   dictr*   )r/   r1   DynamicBaserI   rH   s    `  @r%   get_embedding_storerO   5   s    
 N,444#+#"$&&999K
H 
H 
H 
H 
H 
H 
H 
H&8 
H 
H 
H r'   c                  (    e Zd ZU dZded<   ded<   dS )QueryResultzResult from a query.r*   rI   floatdistanceN)r+   r,   r-   r.   __annotations__r!   r'   r%   rQ   rQ   R   s+         &&&&OOOOOr'   rQ   c                      e Zd ZdZdZdZdZdS )r0   z&Enumerator of the Distance strategies.l2sqcosinehammingN)r+   r,   r-   r.   	EUCLIDEANCOSINErJ   r!   r'   r%   r0   r0   Y   s#        00IFGGGr'   c                     e Zd ZdZeeddddfdsdZdtdZedud            Z	edvd            Z
edwd             Zdxd"Zedyd$            Zdtd%Zdtd&Zdtd'Zdtd(Zdzd+Zd{d-Zd|d.Zd|d/Zd}d2Zed|d3            Zed4d5d5fd~d;Zdtd<Zdtd=Zdtd>Zej        dd@            Z 	 dddDZ!eddeedfddK            Z"ddMZ#	 	 dddOZ$ddRZ%	 	 dddXZ&	 	 dddYZ'	 	 ddd[Z(	 	 ddd]Z)	 	 ddd^Z*edeeddfdda            Z+ededdefddd            Z,eedefdde            Z-eddg            Z.eeeddfddi            Z/	 	 	 	 dddoZ0	 	 	 	 dddpZ1	 	 	 	 dddqZ2	 	 	 	 dddrZ3dS )Lanterna  `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.
    NFconnection_stringr2   embedding_functionr   r/   r0   r1   collection_metadataOptional[dict]pre_delete_collectionboolloggerOptional[logging.Logger]relevance_score_fn"Optional[Callable[[float], float]]r   Nonec	                   || _         || _        || _        || _        || _        || _        |pt          j        t                    | _	        || _
        t          | j        |          | _        |                                  d S N)r]   r^   r1   r_   _distance_strategyra   logging	getLoggerr+   rc   override_relevance_score_fnrO   r/   rI   __post_init__)	selfr]   r^   r/   r1   r_   ra   rc   re   s	            r%   __init__zLantern.__init__y   s     "3"4.#6 "3%:"; 1( ; ;+=(1"O
 
 	r'   c                    |                                  | _        |                                  |                                  d S ri   )connect_conncreate_hnsw_extensioncreate_collectionro   s    r%   rn   zLantern.__post_init__   s=     \\^^
""$$$     r'   c           	        t          | j        t                    r| j        S | j        t          j        j        k    rt          j        S | j        t          j        j        k    rt          j        S | j        t          j        j        k    rt          j        S t          d| j         dd                    d t          D                        d          )Nz#Got unexpected value for distance: z. Should be one of z, c                    g | ]	}|j         
S r!   )value)r"   dss     r%   r&   z-Lantern.distance_strategy.<locals>.<listcomp>   s    .S.S.SBrx.S.S.Sr'   .)	
isinstancerj   r0   rY   ry   rZ   rJ   
ValueErrorjoinrv   s    r%   r/   zLantern.distance_strategy   s    d-/?@@ 	+**"&6&@&FFF#--$(8(?(EEE#**$(8(@(FFF#++Xd6M X X$(II.S.SBR.S.S.S$T$TX X X  r'   c                    | j         S ri   )r^   rv   s    r%   
embeddingszLantern.embeddings   s    &&r'   driverhostportintdatabaseuserpasswordc                *    d| d| d| d| d| d| S )z2Return connection string from database parameters.zpostgresql+z://:@/r!   )clsr   r   r   r   r   r   s          r%    connection_string_from_db_paramsz(Lantern.connection_string_from_db_params   s;     SVRRRRxRR$RRRRRRRr'   sqlalchemy.engine.Connectionc                `    t          j        | j                  }|                                }|S ri   )r=   create_enginer]   rr   )ro   engineconns      r%   rr   zLantern.connect   s)    )$*@AA~~r'   r   c                    | j         t          j        k    rdS | j         t          j        k    rdS | j         t          j        k    rdS d S )N	l2sq_distcos_disthamming_dist)r/   r0   rY   rZ   rJ   rv   s    r%   distance_functionzLantern.distance_function   sP    !%5%???;#'7'>>>:#'7'???!> @?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 lantern)	r   rs   r=   textexecutecommit	Exceptionrc   	exception)ro   session	statementes       r%   rt   zLantern.create_hnsw_extension   s    	%$$ !&O,TUU		***   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !  	% 	% 	%K!!!$$$$$$$$$	%s:   A. >A!A. !A%%A. (A%)A. .
B8BBc                R    	 |                                   d S # t          $ r Y d S w xY wri   )ru   r   rv   s    r%   create_tables_if_not_existsz#Lantern.create_tables_if_not_exists   sA    	""$$$$$ 	 	 	DD	s    
&&c                |    	 | j         j                            | j        j                   d S # t
          $ r Y d S w xY wri   )rI   	__table__droprs   r   r   rv   s    r%   
drop_tablezLantern.drop_table   sN    	)..tz/@AAAAA 	 	 	DD	s   )- 
;;c                .    |                                   d S ri   )r   rv   s    r%   drop_tableszLantern.drop_tables   s    r'   rS   rR   c                    |S ri   r!   )ro   rS   s     r%   _hamming_relevance_score_fnz#Lantern._hamming_relevance_score_fn   s    r'   Callable[[float], float]c                    | j         | j         S | j        t          j        k    r| j        S | j        t          j        k    r| j        S | j        t          j        k    r| j        S t          d| j
         d          )a8  
        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.
        Nz=No supported normalization function for distance_strategy of z>.Consider providing relevance_score_fn to Lantern constructor.)rm   r/   r0   rZ   _cosine_relevance_score_fnrY   _euclidean_relevance_score_fnrJ   r   r}   rj   rv   s    r%   _select_relevance_score_fnz"Lantern._select_relevance_score_fn   s     +733 !%5%<<<22#'7'AAA55#'7'???33P-1-DP P P  r'   c                    | j         t          j        k    rdS | j         t          j        k    rdS | j         t          j        k    rdS t          d| j         d          )Ndist_cos_opsdist_l2sq_opsdist_hamming_opsz5No supported operator class for distance_strategy of r{   r/   r0   rZ   rY   rJ   r}   rj   rv   s    r%   _get_op_classzLantern._get_op_class   sx    !%5%<<<!>#'7'AAA"?#'7'???%%H-1-DH H H  r'   c                    | j         t          j        k    rdS | j         t          j        k    rdS | j         t          j        k    rdS t          d| j         d          )Nz<=>z<->z<+>z/No supported operator for distance_strategy of r{   r   rv   s    r%   _get_operatorzLantern._get_operator  sw    !%5%<<<5#'7'AAA5#'7'???5H-1-DH H H  r'   rG   List[Union[float, int]]c                l    | j         t          j        k    rt          t	          d |                    S |S )Nc                     t          |           S ri   )r   )xs    r%   <lambda>z1Lantern._typed_arg_for_distance.<locals>.<lambda>  s    c!ff r'   )r/   r0   rJ   listmap)ro   rG   s     r%   _typed_arg_for_distancezLantern._typed_arg_for_distance  s8     !%5%===,,i88999r'   c                    d| j          dS )N
langchain__idx)r1   rv   s    r%   _index_namezLantern._index_name  s    6D06666r'      @   dimsmef_construction	ef_search_kwargsc           	        t          j        d                    t          | j        d          t          | j        d          |                                                     }t          | j                  5 }|	                    |||||d           |
                                 ddd           n# 1 swxY w Y   | j                            d           dS )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)dimr   r   efNz.HNSW extension and index created successfully.)r=   r   formatr   r   r1   r   r   rs   r   r   rc   info)ro   r   r   r   r   r   create_index_queryr   s           r%   create_hnsw_indexzLantern.create_hnsw_index#  s   2 (_ D,d33D0$77""$$ 
 
 TZ   	GOO"'6#	    NN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	IJJJJJs   60B22B69B6c                "   t          | j                  5 }|                    t          j        d                    t          | j        d                                         |                                 d d d            d S # 1 swxY w Y   d S )NzDROP INDEX IF EXISTS {}T)	r   rs   r   r=   r   r   r   r   r   )ro   r   s     r%   
drop_indexzLantern.drop_indexY  s    TZ   		GOO-44#D$4d;;     NN		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   A"BBBc                h   | j         r(|                                  |                                  | j                                        5  	 | j        j                            | j        j                   n%# t          $ r}|j
        dk    rn|Y d }~nd }~ww xY wd d d            d S # 1 swxY w Y   d S )Nf405)ra   delete_collectionr   rs   beginrI   r   creater   r   code)ro   r   s     r%   ru   zLantern.create_collectione  s   % 	""$$$OOZ 	 	#-44TZ5FGGGG#   6V##G DDDD	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s<   	B')A54B'5
B?BB'BB''B+.B+c                b    | j                             d           |                                  d S )NzTrying to delete collection)rc   debugr   rv   s    r%   r   zLantern.delete_collectiont  s/    7888r'   Generator[Session, None, None]c              #  6   K   t          | j                  V  dS )z?Create a context manager for the session, bind to _conn string.N)r   rs   rv   s    r%   _make_sessionzLantern._make_sessionx  s$       dj!!!!!!!r'   idsOptional[List[str]]kwargsc                r   t          | j                  5 }|s| j                            d           t	          | j                                      | j        j                            |                    }|	                    |           |
                                 ddd           dS # 1 swxY w Y   dS )z_Delete 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   rs   rc   r   r   rI   whererE   in_r   r   )ro   r   r   r   stmts        r%   r   zLantern.delete}  s     TZ   
	G!!2   d12288'155c::  %%%NN
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	s   B
B,,B03B0texts	List[str]r   List[List[float]]	metadatasOptional[List[dict]]c	                    |d |D             }|sd |D             }|                      |	          }
 | |
||||          } |j        d||||d|	  |j        di |	 |S )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.
        Nc                N    g | ]"}t          t          j                              #S r!   r2   r?   r@   r"   r$   s     r%   r&   z7Lantern._initialize_from_embeddings.<locals>.<listcomp>  &    4443tz||$$444r'   c                    g | ]}i S r!   r!   r   s     r%   r&   z7Lantern._initialize_from_embeddings.<locals>.<listcomp>      ++++++r'   r]   r1   r^   ra   r/   )r   r   r   r   r!   )_Lantern__get_connection_stringadd_embeddingsr   )r   r   r   rG   r   r   r1   r/   ra   r   r]   stores               r%   _initialize_from_embeddingsz#Lantern._initialize_from_embeddings  s    P ;44e444C 	,++U+++I77??/+("7/
 
 
 	 	
J)	
 	
PV	
 	
 	
 	 ))&)))r'   
List[dict]c                   t          | j                  5 }t          ||||          D ]5\  }}}	}
|                     |	|||
          }|                    |           6|                                 d d d            d S # 1 swxY w Y   d S )NrG   rC   rD   rE   )r   rs   ziprI   addr   )ro   r   r   r   r   r   r   r   metadatarG   idembedding_stores               r%   r   zLantern.add_embeddings  s     TZ   		G14UIzSV1W1W - --h	2"&"5"5'!& 	 #6 # # O,,,,NN		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 		s   AA??BBIterable[str]c                   |d |D             }| j                             t          |                    }|sd |D             }t          | j                  5 }t          ||||          D ]5\  }}}	}
|                     |	|||
          }|                    |           6|                                 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%Lantern.add_texts.<locals>.<listcomp>  r   r'   c                    g | ]}i S r!   r!   r   s     r%   r&   z%Lantern.add_texts.<locals>.<listcomp>  r   r'   r   )	r^   embed_documentsr   r   rs   r   rI   r   r   )ro   r   r   r   r   r   r   r   r   rG   r   r   s               r%   	add_textszLantern.add_texts  s0    ;44e444C,<<T%[[II
 	,++U+++ITZ   		G14UIzSV1W1W - --h	2"&"5"5'!& 	 #6 # # O,,,,NN		 		 		 		 		 		 		 		 		 		 		 		 		 		 		 
s   ACCCresultsList[Tuple[Document, float]]c                $      fd|D             }|S )z$Return docs and scores from results.c                z    g | ]7}t          |j        j        |j        j                   j        |j        ndf8S ))page_contentr   N)r   rI   rC   rD   r^   rS   )r"   resultro   s     r%   r&   z7Lantern._results_to_docs_and_scores.<locals>.<listcomp>  sd     	
 	
 	
  !'!6!?#2<   $(#:#FD	
 	
 	
r'   r!   )ro   r  docss   `  r%   _results_to_docs_and_scoresz#Lantern._results_to_docs_and_scores  s4    	
 	
 	
 	
 "	
 	
 	
 r'      querykfilterr   c                h    | j                             |          }|                     |||          S )N)r   rG   r  r  )r^   embed_querysimilarity_search_by_vector)ro   r  r  r  r   rG   s         r%   similarity_searchzLantern.similarity_search  sC     +77U7CC	// 0 
 
 	
r'   c                j    | j                             |          }|                     |||          }|S Nr  )r^   r  &similarity_search_with_score_by_vector)ro   r  r  r  rG   r  s         r%   similarity_search_with_scorez$Lantern.similarity_search_with_score  sB     +77>>	::1V ; 
 
 r'   List[float]c                \    |                      |||          }|                     |          S r  )_Lantern__query_collectionr  )ro   rG   r  r  r  s        r%   r  z.Lantern.similarity_search_with_score_by_vector*  s2     ))I6)RR//888r'   	List[Any]c           	        t          | j                  5 }t          j        d          }t          j        d          }|                    |           |                    |d|i           d }|g }|                                D ]\  }	}
d}t          |
t                    r|t          t          j
        |
          v rdd |
                                D             }| j        j        |	         j                            ||                   }|                    |           | j        j        |	         j        t          |
          k    }|                    |           t          j        | }|                     |          }|                    | j         t'          t(          | j                  | j        j        |                              d                    }||                    |          }|                     | j        j                            |                                           |                                        |                                          }d d d            n# 1 swxY w Y   |S )NzSET enable_seqscan = offzSET hnsw.init_k = :kr  inc                >    i | ]\  }}|                                 |S r!   )lower)r"   r  vs      r%   
<dictcomp>z.Lantern.__query_collection.<locals>.<dictcomp>F  s3     2 2 2-1QAGGIIq2 2 2r'   rS   )r   rs   r=   r   r   itemsr|   rM   r   r2   r!  rI   rD   astextr   appendand_r   r  getattrr   r   rG   labelr  order_byopr   limitall)ro   rG   r  r  r   set_enable_seqscan_stmt
set_init_k	filter_byfilter_clauseskeyry   INvalue_case_insensitivefilter_by_metadatar  r  s                   r%   __query_collectionzLantern.__query_collection4  s    TZ   ,	G&0o6P&Q&Q##)?@@JOO3444OOJa111I!!#"(,,.. B BJCB!%.. B2SY9N9N3N3N2 25:[[]]2 2 2. .2-@-J. %;B%?!@!@ + '--.@AAAA-1-@-J. CJJ./* '--.@AAAA&O^<	44Y??IMM#5d455'19 %
##	 E $Y// JD'144T5G5G5I5IJJ9UU  q M,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	 ,	\ s   II11I58I5c                P    |                      |||          }t          |          S r  )r  r(   )ro   rG   r  r  r   r   s         r%   r  z#Lantern.similarity_search_by_vectorj  s6     EE1V F 
 
  000r'   r   Type[Lantern]c           	     r    |                     t          |                    }	 | j        ||	|f|||||d|S )ap  
        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   r1   ra   r/   )r  r   r   )
r   r   rG   r   r1   r/   r   ra   r   r   s
             r%   
from_textszLantern.from_textsv  sc    T ..tE{{;;
.s.

  +"7/

 

 

 

 
	
r'   text_embeddingsList[Tuple[str, List[float]]]c           	     ^    d |D             }	d |D             }
 | j         |	|
|f|||||d|S )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.
        c                    g | ]
}|d          S )r   r!   r"   ts     r%   r&   z+Lantern.from_embeddings.<locals>.<listcomp>  s    ///!1///r'   c                    g | ]
}|d          S )   r!   r@  s     r%   r&   z+Lantern.from_embeddings.<locals>.<listcomp>  s    444qad444r'   r:  )r   )r   r<  rG   r   r1   r   ra   r/   r   r   r   s              r%   from_embeddingszLantern.from_embeddings  sp    T 0////44O444
.s.

  +"7/

 

 

 

 
	
r'   c                P    |                      |          } | |||||          }|S )am  
        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   rG   r1   ra   r/   r   r]   r   s           r%   from_existing_indexzLantern.from_existing_index  sE    F  77??/+("7/
 
 
 r'   Dict[str, Any]c                L    t          |dd          }|st          d          |S )Nr]   LANTERN_CONNECTION_STRING)datar2  env_keyzPostgres connection string is requiredEither pass it as `connection_string` parameteror set the LANTERN_CONNECTION_STRING variable.)r   r}   )r   r   r]   s      r%   __get_connection_stringzLantern.__get_connection_string  sJ    !5#/"
 "
 "
 ! 	A   ! r'   	documentsc                    d |D             }d |D             }	|                      |          }
|
|d<    | j        d||||	|||d|S )af  
        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.
        c                    g | ]	}|j         
S r!   )r	  r"   ds     r%   r&   z*Lantern.from_documents.<locals>.<listcomp>K  s    333A333r'   c                    g | ]	}|j         
S r!   )r   rP  s     r%   r&   z*Lantern.from_documents.<locals>.<listcomp>L  s    333AQZ333r'   r]   )r   ra   rG   r   r   r1   r/   r!   )r   r;  )r   rM  rG   r1   r/   r   ra   r   r   r   r]   s              r%   from_documentszLantern.from_documents'  s    H 4333333333	77??&7"#s~ 	
"7+/	
 	
 	
 	
 		
r'            ?fetch_klambda_multOptional[Dict[str, str]]c                  
 |                      |||          }d |D             }t          t          j        |t          j                  |||          
|                     |          }	
fdt          |	          D             S )a  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  c                &    g | ]}|j         j        S r!   )rI   rG   )r"   r
  s     r%   r&   zNLantern.max_marginal_relevance_search_with_score_by_vector.<locals>.<listcomp>{  s    PPPf&/9PPPr'   )dtype)r  rW  c                "    g | ]\  }}|v 	|S r!   r!   )r"   irmmr_selecteds      r%   r&   zNLantern.max_marginal_relevance_search_with_score_by_vector.<locals>.<listcomp>  s'    IIIdaqL7H7H7H7H7Hr'   )r  r   nparrayfloat32r  	enumerate)ro   rG   r  rV  rW  r  r   r  embedding_list
candidatesr_  s             @r%   2max_marginal_relevance_search_with_score_by_vectorz:Lantern.max_marginal_relevance_search_with_score_by_vector\  s    < ))IQW)XXPPPPP1HYbj111#	
 
 
 55g>>
IIIIi
33IIIIr'   c                \    | j                             |          } | j        |f||||d|S )av  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  rV  rW  r  )r^   r  'max_marginal_relevance_search_by_vector)ro   r  r  rV  rW  r  r   rG   s           r%   max_marginal_relevance_searchz%Lantern.max_marginal_relevance_search  sT    8 +77>>	;t;
#
 
 
 
 	
r'   c           	     `    | j                             |          } | j        d|||||d|}|S )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.
        )rG   r  rV  rW  r  r!   )r^   r  rf  )	ro   r  r  rV  rW  r  r   rG   r  s	            r%   (max_marginal_relevance_search_with_scorez0Lantern.max_marginal_relevance_search_with_score  sX    : +77>>	FtF 
#
 
 
 
 r'   c                F     | j         |f||||d|}t          |          S )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.
        rh  )rf  r(   )ro   rG   r  rV  rW  r  r   r   s           r%   ri  z/Lantern.max_marginal_relevance_search_by_vector  sL    : R$Q
#
 
 
 
  000r'   )r]   r2   r^   r   r/   r0   r1   r2   r_   r`   ra   rb   rc   rd   re   rf   r   rg   )r   rg   )r   r0   )r   r   )r   r2   r   r2   r   r   r   r2   r   r2   r   r2   r   r2   )r   r   )r   r   )rS   rR   r   rR   )r   r   )r   r2   )rG   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   rg   )r   r   ri   )r   r   r   r   r   rg   )r   r   r   r   rG   r   r   r   r   r   r1   r2   r/   r0   ra   rb   r   r   r   r\   )r   r   r   r   r   r   r   r   r   r   r   rg   )NN)
r   r   r   r   r   r   r   r   r   r   )r  r   r   r  )r  N)
r  r2   r  r   r  r`   r   r   r   r   )r  r2   r  r   r  r`   r   r  )rG   r  r  r   r  r`   r   r  )rG   r  r  r   r  r`   r   r  )
rG   r  r  r   r  r`   r   r   r   r   )r   r8  r   r   rG   r   r   r   r1   r2   r/   r0   r   r   ra   rb   r   r   r   r\   )r<  r=  rG   r   r   r   r1   r2   r   r   ra   rb   r/   r0   r   r   r   r\   )r   r8  rG   r   r1   r2   ra   rb   r/   r0   r   r   r   r\   )r   rG  r   r2   )r   r8  rM  r   rG   r   r1   r2   r/   r0   r   r   ra   rb   r   r   r   r\   )r  rT  rU  N)rG   r  r  r   rV  r   rW  rR   r  rX  r   r   r   r  )r  r2   r  r   rV  r   rW  rR   r  rX  r   r   r   r   )r  r2   r  r   rV  r   rW  rR   r  r`   r   r   r   r  )rG   r  r  r   rV  r   rW  rR   r  rX  r   r   r   r   )4r+   r,   r-   r.   DEFAULT_DISTANCE_STRATEGY"_LANGCHAIN_DEFAULT_COLLECTION_NAMErp   rn   propertyr/   r   classmethodr   rr   r   rt   r   r   r   r   r   r   r   r   r   ADA_TOKEN_COUNTr   r   ru   r   
contextlibcontextmanagerr   r   r   r   r  r  r  r  r  r  r  r;  rD  rF  r   rS  rf  rj  rl  ri  r!   r'   r%   r\   r\   d   s6        0 /HA.2&++/AE    0! ! ! !    X  ' ' ' X' 
S 
S 
S [
S   
 " " " X"% % % %               6          7 7 7 X7
 $!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 
 +/A.G#'&+5
 5
 5
 5
 [5
n 
 +/A#'&+.G6
 6
 6
 6
 [6
p   B&+.G, , , , [,\ ! ! ! [!  
  B.G#'&+2
 2
 2
 2
 [2
n  +/*J *J *J *J *J^  +/$
 $
 $
 $
 $
R  !%& & & & &V  +/&1 &1 &1 &1 &1 &1 &1r'   r\   )r   r   r   r   )r/   r0   r1   r2   r   r   )8
__future__r   rs  enumrk   r?   typingr   r   r   r   r   r	   r
   r   r   r   numpyr`  r=   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   rr  ro  r(   r*   rO   rQ   r2   Enumr0   rZ   rn  r\   r!   r'   r%   <module>r     s   " " " " " "                                       # # # # # # # # 5 5 5 5 5 5 5 5 + + + + + + " " " " " " & & & & & & M M M M M M</////// < < <;;;;;;;;< . - - - - - 0 0 0 0 0 0 5 5 5 5 5 5 3 3 3 3 3 3%0 "/ / / /
6 6 6 6 6 6 6 6   :           sDI    -3 X1 X1 X1 X1 X1k X1 X1 X1 X1 X1s    A' 'A54A5