§
    šŠtj}–  ã                  óÂ  — 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      úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/vectorstores/lantern.pyú
<listcomp>z$_results_to_docs.<locals>.<listcomp>.   s   € Ð.Ð.Ð.‘F�C˜ˆ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Ô% d°TÐ:Ñ:Ô:ˆ	à%�JÔ% jÔ&7À$ÐGÑGÔGˆ	Ø%�JÔ%Ð&6 jÔ&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Ô9€Kð
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+   € € € € € € ØÐà&Ð&Ð&Ñ&Ø€O€O�O€O€Or'   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Ø€G€G€Gr'   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 	 d€d�dD„Z!eddeedfd‚dK„¦   «         Z"dƒdM„Z#	 	 d„d…dO„Z$d†dR„Z%	 	 d‡dˆdX„Z&	 	 d‡d‰dY„Z'	 	 d‡dŠd[„Z(	 	 d‡d‹d]„Z)	 	 d‡dŒd^„Z*edeeddfd�da„¦   «         Z+ededdefdŽdd„¦   «         Z,eedefd�de„¦   «         Z-ed�dg„¦   «         Z.eeeddfd‘di„¦   «         Z/	 	 	 	 d’d“do„Z0	 	 	 	 d’d”dp„Z1	 	 	 	 d’d•dq„Z2	 	 	 	 d’d–dr„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Ð.S¸B¨r¬xÐ.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°dÔ6Mð Xð XØ$(§I¢IÐ.SÐ.SÕBRÐ.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ÐR¨xÐ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   € ð	Ø×"Ò"Ñ$Ô$Ð$Ð$Ð$øÝð 	ð 	ð 	ØˆDˆDð	øøø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   € ð	ØÔÔ)×.Ò.¨t¬zÔ/@ÑAÔAÐAÐAÐAøÝð 	ð 	ð 	ØˆDˆDð	øøø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¨!¡f¤f€ 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   € à6˜DÔ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
        z†CREATE 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Ø×"Ò"Ñ$Ô$ñô ñ
ô 
Ðõ �T”ZÑ Ô ð 	 Gà�OŠOØ"àØØ'6Ø#ð	ð ñô ð ð �NŠNÑÔÐð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	Œ×ÒÐ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Í   € Ý�T”ZÑ Ô ð 		 Gà�OŠOÝ”Ø-×4Ò4Ý# DÔ$4°dÑ;Ô;ñô ñô ñô ð ð �NŠNÑÔÐð		ð 		ð 		ñ 		ô 		ð 		ð 		ð 		ð 		ð 		ð 		ð 		øøøð 		ð 		ð 		ð 		ð 		ð 		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  € ØÔ%ð 	Ø×"Ò"Ñ$Ô$Ð$Ø�OŠOÑÔÐàŒZ×ÒÑÔð 	ð 	ðØÔ#Ô-×4Ò4°T´ZÔ5FÑGÔGÐGÐGøÝ#ð ð ð à”6˜VÒ#Ð#Øà�Gð �D�D�D�Døøøøðøøøð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	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$   è è € õ �d”jÑ!Ô!Ð!Ð!Ð!Ð!Ð!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ö   € õ �T”ZÑ Ô ð 
	 GØˆØ”×!Ò!ð2ñô ð õ ˜dÔ1Ñ2Ô2×8Ò8ØÔ'Ô1×5Ò5°cÑ:Ô:ñô �ð —’ Ñ%Ô%Ð%Ø�NŠNÑÔÐð
	ð 
	ð 
	ñ 
	ô 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	øøøð 
	ð 
	ð 
	ð 
	ð 
	ð 
	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¨•3•t”z‘|”|Ñ$Ô$Ð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Ð4¨eÐ4Ñ4Ô4ˆCàð 	,Ø+Ð+ 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é   € õ �T”ZÑ Ô ð 		 GÝ14°U¸IÀzÐSVÑ1WÔ1Wð -ð -Ñ-��h 	¨2Ø"&×"5Ò"5Ø'Ø!Ø&Ø ð	 #6ñ #ô #�ð —’˜OÑ,Ô,Ð,Ð,Ø�NŠNÑÔÐð		ð 		ð 		ñ 		ô 		ð 		ð 		ð 		ð 		ð 		ð 		ð 		øøøð 		ð 		ð 		ð 		ð 		ð 		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Ð4¨eÐ4Ñ4Ô4ˆCàÔ,×<Ò<½TÀ%¹[¼[ÑIÔIˆ
àð 	,Ø+Ð+ UÐ+Ñ+Ô+ˆIå�T”ZÑ Ô ð 		 GÝ14°U¸IÀzÐSVÑ1WÔ1Wð -ð -Ñ-��h 	¨2Ø"&×"5Ò"5Ø'Ø!Ø&Ø ð	 #6ñ #ô #�ð —’˜OÑ,Ô,Ð,Ð,Ø�NŠNÑÔÐð		ð 		ð 		ñ 		ô 		ð 		ð 		ð 		ð 		ð 		ð 		øøøð 		ð 		ð 		ð 		ð ˆ
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Ò7¸UÐ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¸Ñ>Ô>ˆ	Ø×:Ò:Ø 1¨Vð ;ñ 
ô 
ˆð ˆ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Ù-1¨Q°˜AŸGšG™IœI qð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¢  € õ �T”ZÑ Ô ð ,	 GÝ&0¤oÐ6PÑ&QÔ&QÐ#Ý#œÐ)?Ñ@Ô@ˆJØ�OŠOÐ3Ñ4Ô4Ð4Ø�OŠO˜J¨¨a¨Ñ1Ô1Ð1àˆIØÐ!Ø!#�Ø"(§,¢,¡.¤.ð Bð B‘J�C˜Ø�BÝ! %­Ñ.Ô.ð B°2½½S¼YÈÑ9NÔ9NÐ3NÐ3Nð2ð 2Ø5:·[²[±]´]ð2ñ 2ô 2Ð.ð .2Ô-@Ô-JØô.ä §¢Ð%;¸BÔ%?Ñ!@Ô!@ð +ð '×-Ò-Ð.@ÑAÔAÐAÐAà-1Ô-@Ô-JØô.ä ¥C¨¡J¤Jò./Ð*ð '×-Ò-Ð.@ÑAÔAÐAÐAå&œO¨^Ð<�	à×4Ò4°YÑ?Ô?ˆIØ—M’MØÔ#Ø5•�˜dÔ4Ñ5Ô5ØÔ'Ô1°9ñô ç’%˜
Ñ#Ô#ñ	ô ˆEð Ð$ØŸš YÑ/Ô/�ð —’ØJ�DÔ'Ô1×4Ò4°T×5GÒ5GÑ5IÔ5IÑJÔJÈ9Ñ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Ø 1¨Vð 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 ×.Ò.­t°E©{¬{Ñ;Ô;ˆ
à.ˆ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Ð4˜q�a˜”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Ð4 OÐ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_keyzƒPostgres 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Ð3 A�”Ð3Ð3Ð3r'   c                ó   — g | ]	}|j         ‘Œ
S r!   )rü   rP  s     r%   r&   z*Lantern.from_documents.<locals>.<listcomp>L  s   € Ð3Ð3Ð3 A�Q”ZÐ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ÐP¸f˜&Ô/Ô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ÐI‘d�a˜°q¸LÐ7HÐ7H�Ð7HÐ7HÐ7Hr'   )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ÝŒH�Y¥b¤jÐ1Ñ1Ô1ØØØ#ð	
ñ 
ô 
ˆð ×5Ò5°gÑ>Ô>ˆ
àIÐIÐIÐI�i¨
Ñ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¸Ñ>Ô>ˆ	ØFˆtÔ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ðð ð ð ð:ð ð ð ð ñ ô ð ðð ð ð ð �s˜DœIñ ô ð ð -Ô3Ð ðX1ð X1ð X1ð X1ð X1ˆkñ X1ô X1ð X1ð X1ð X1s   Á A' Á'A5Á4A5