§
    šŠtjÒ7  ã                  ó´   — d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZ erddlmZ  ej        ¦   «         Z G d	„ d
e¦  «        ZdS )z'Wrapper around Epsilla vector database.é    )ÚannotationsN)ÚTYPE_CHECKINGÚAnyÚIterableÚListÚOptionalÚType©ÚDocument)Ú
Embeddings)ÚVectorStore)Úvectordbc                  óô   — e Zd ZU dZdZded<   dZded<   dZded<   eefd8d„Ze	d9d„¦   «         Z
d:d„Zd;d:d„Z	 d<d=d„Z	 d>d?d"„Z	 	 	 d@dAd+„Z	 dBdCd1„Zeddeeed#fdDd5„¦   «         Zedeeed#fdEd7„¦   «         ZdS )FÚEpsillaar  
    Wrapper around Epsilla vector database.

    As a prerequisite, you need to install ``pyepsilla`` package
    and have a running Epsilla vector database (for example, through our docker image)
    See the following documentation for how to run an Epsilla vector database:
    https://epsilla-inc.gitbook.io/epsilladb/quick-start

    Args:
        client (Any): Epsilla client to connect to.
        embeddings (Embeddings): Function used to embed the texts.
        db_path (Optional[str]): The path where the database will be persisted.
                                 Defaults to "/tmp/langchain-epsilla".
        db_name (Optional[str]): Give a name to the loaded database.
                                 Defaults to "langchain_store".
    Example:
        .. code-block:: python

            from langchain_community.vectorstores import Epsilla
            from pyepsilla import vectordb

            client = vectordb.Client()
            embeddings = OpenAIEmbeddings()
            db_path = "/tmp/vectorstore"
            db_name = "langchain_store"
            epsilla = Epsilla(client, embeddings, db_path, db_name)
    Úlangchain_storeÚstrÚ_LANGCHAIN_DEFAULT_DB_NAMEz/tmp/langchain-epsillaÚ_LANGCHAIN_DEFAULT_DB_PATHÚlangchain_collectionÚ_LANGCHAIN_DEFAULT_TABLE_NAMEÚclientr   Ú
embeddingsr   Údb_pathúOptional[str]Údb_namec                ó¤  — 	 ddl }n"# t          $ r}t          d¦  «        |‚d}~ww xY wt          ||j        j        |j        j        j        f¦  «        st          dt          |¦  «        › �¦  «        ‚|| _
        || _        || _        t          j        | _        | j
                             ||¬¦  «         | j
                             |¬¦  «         dS )z%Initialize with necessary components.r   NziCould not import pyepsilla python package. Please install pyepsilla package with `pip install pyepsilla`.zbclient should be an instance of pyepsilla.vectordb.Client or pyepsilla.cloud.client.Vectordb, got )r   r   ©r   )Ú	pyepsillaÚImportErrorÚ
isinstancer   ÚClientÚcloudr   ÚVectordbÚ	TypeErrorÚtypeÚ_clientÚ_db_nameÚ_embeddingsr   r   Ú_collection_nameÚload_dbÚuse_db)Úselfr   r   r   r   r   Úes          úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/vectorstores/epsilla.pyÚ__init__zEpsilla.__init__4   s
  € ð	ØÐÐÐÐøÝð 	ð 	ð 	ÝðQñô ð ðøøøøð	øøøõ Ø�YÔ'Ô.°	´Ô0FÔ0OÐPñ
ô 
ð 	õ ðGÝ8<¸V¹¼ðGð Gñô ð ð
 )/ˆŒØˆŒØ%ˆÔÝ 'Ô EˆÔØŒ×Ò W°gÐÑ>Ô>Ð>ØŒ×Ò GÐÑ,Ô,Ð,Ð,Ð,s   ‚ ‡
&‘!¡&ÚreturnúOptional[Embeddings]c                ó   — | j         S ©N)r(   )r,   s    r.   r   zEpsilla.embeddingsS   s   € àÔÐó    Úcollection_nameÚNonec                ó   — || _         dS )z~
        Set default collection to use.

        Args:
            collection_name (str): The name of the collection.
        N)r)   ©r,   r5   s     r.   Úuse_collectionzEpsilla.use_collectionW   s   € ð !0ˆÔÐÐr4   Ú c                óL   — |s| j         }| j                             |¦  «         dS )zË
        Clear data in a collection.

        Args:
            collection_name (Optional[str]): The name of the collection.
                If not provided, the default collection will be used.
        N)r)   r&   Ú
drop_tabler8   s     r.   Ú
clear_datazEpsilla.clear_data`   s0   € ð ð 	4Ø"Ô3ˆOØŒ×Ò Ñ0Ô0Ð0Ð0Ð0r4   NÚresponse_fieldsúOptional[List[str]]ú
List[dict]c                ó  — |s| j         }| j                             ||¬¦  «        \  }}|dk    rKt                               d|d         › �¦  «         t          d                     |d         ¦  «        ¦  «        ‚|d         S )a¼  Get the collection.

        Args:
            collection_name (Optional[str]): The name of the collection
                to retrieve data from.
                If not provided, the default collection will be used.
            response_fields (Optional[List[str]]): List of field names in the result.
                If not specified, all available fields will be responded.

        Returns:
            A list of the retrieved data.
        )Ú
table_namer>   éÈ   zFailed to get records: Úmessageú
Error: {}.Úresult)r)   r&   ÚgetÚloggerÚerrorÚ	ExceptionÚformat)r,   r5   r>   Ústatus_codeÚresponses        r.   rG   zEpsilla.getl   s“   € ð ð 	4Ø"Ô3ˆOØ $¤× 0Ò 0Ø&¸ð !1ñ !
ô !
Ñˆ�Xð ˜#ÒÐÝ�LŠLÐH°8¸IÔ3FÐHÐHÑIÔIÐIÝ˜L×/Ò/°¸Ô0CÑDÔDÑEÔEÐEØ˜Ô!Ð!r4   rB   ÚlistÚ	metadatasúOptional[list[dict]]c                óF  — |st          d¦  «        ‚t          |d         ¦  «        }dddœdddœdd	|d
œg}|�Ðd„ |D ¦   «         }|D ]Á}|                     ¦   «         D ]ª\  }}	||v rŒ
t          |	t          ¦  «        rd}
n[t          |	t
          ¦  «        rd}
nCt          |	t          ¦  «        rd}
n+t          |	t          ¦  «        rd}
nt          d|› d�¦  «        ‚|                     ||
dœ¦  «         |                     |¦  «         Œ«ŒÂ| j	         
                    ||¬¦  «        \  }}|dk    rt|dk    r t                               d|› d�¦  «         d S t                               d|› d|d         › �¦  «         t          d                     |d         ¦  «        ¦  «        ‚d S )NzEmbeddings list is empty.r   ÚidÚINT)ÚnameÚdataTypeÚtextÚSTRINGr   ÚVECTOR_FLOAT)rT   rU   Ú
dimensionsc                ó   — g | ]
}|d          ‘ŒS )rT   © )Ú.0Úfields     r.   ú
<listcomp>z.Epsilla._create_collection.<locals>.<listcomp>’   s   € Ð=Ð=Ð=¨U˜5 œ=Ð=Ð=Ð=r4   ÚFLOATÚBOOLzUnsupported data type for ú.)Útable_fieldsrC   i™  z#Continuing with the existing table zFailed to create collection ú: rD   rE   )Ú
ValueErrorÚlenÚitemsr    r   ÚintÚfloatÚboolÚappendr&   Úcreate_tablerH   ÚinforI   rJ   rK   )r,   rB   r   rO   ÚdimÚfieldsÚfield_namesÚmetadataÚkeyÚvalueÚd_typerL   rM   s                r.   Ú_create_collectionzEpsilla._create_collection…   s   € ð ð 	:ÝÐ8Ñ9Ô9Ð9å�*˜Q”-Ñ Ô ˆà uÐ-Ð-Ø¨Ð2Ð2Ø!¨~ÈSÐQÐQð
ˆð
 Ð Ø=Ð=°fÐ=Ñ=Ô=ˆKØ%ð ,ð ,�Ø"*§.¢.Ñ"2Ô"2ð ,ð ,‘J�C˜Ø˜kÐ)Ð)Ø å! %­Ñ-Ô-ð 	NØ!)˜˜Ý# E­3Ñ/Ô/ð NØ!&˜˜Ý# E­5Ñ1Ô1ð NØ!(˜˜Ý# E­4Ñ0Ô0ð NØ!'˜˜å(Ð)LÀcÐ)LÐ)LÐ)LÑMÔMÐMØ—M’M¨3¸FÐ"CÐ"CÑDÔDÐDØ×&Ò& sÑ+Ô+Ð+Ð+ð,ð" !%¤× 9Ò 9Ø Vð !:ñ !
ô !
Ñˆ�Xð ˜#ÒÐØ˜cÒ!Ð!Ý—’ÐOÀ*ÐOÐOÐOÑPÔPÐPÐPÐPå—’ØV°:ÐVÐVÀÈ)ÔATÐVÐVñô ð õ   × 3Ò 3°H¸YÔ4GÑ HÔ HÑIÔIÐIð Ðr4   FÚtextsúIterable[str]úOptional[List[dict]]Údrop_oldúOptional[bool]Úkwargsú	List[str]c                ód  ‡ — |s‰ j         }n|‰ _         |r‰ j                             |¬¦  «         t          |¦  «        }	 ‰ j                             |¦  «        }n# t          $ r ˆ fd„|D ¦   «         }Y nw xY wt          |¦  «        dk    rt           	                    d¦  «         g S ‰  
                    |||¬¦  «         d„ |D ¦   «         }g }t          |¦  «        D ]U\  }	}
|
||	         ||	         dœ}|�'||	                              ¦   «         }|D ]
\  }}|||<   Œ|                     |¦  «         ŒV‰ j                             ||¬	¦  «        \  }}|d
k    rNt                               d|› d|d         › �¦  «         t!          d                     |d         ¦  «        ¦  «        ‚d„ |D ¦   «         S )a©  
        Embed texts and add them to the database.

        Args:
            texts (Iterable[str]): The texts to embed.
            metadatas (Optional[List[dict]]): Metadata dicts
                        attached to each of the texts. Defaults to None.
            collection_name (Optional[str]): Which collection to use.
                        Defaults to "langchain_collection".
                        If provided, default collection name will be set as well.
            drop_old (Optional[bool]): Whether to drop the previous collection
                        and create a new one. Defaults to False.

        Returns:
            List of ids of the added texts.
        r   c                óD   •— g | ]}‰j                              |¦  «        ‘ŒS r[   )r(   Úembed_query)r\   Úxr,   s     €r.   r^   z%Epsilla.add_texts.<locals>.<listcomp>Õ   s*   ø€ ÐIÐIÐI¸a˜$Ô*×6Ò6°qÑ9Ô9ÐIÐIÐIr4   r   zNothing to insert, skipping.)rB   r   rO   c                óN   — g | ]"}t          t          j        ¦   «         ¦  «        ‘Œ#S r[   )ÚhashÚuuidÚuuid4)r\   Ú_s     r.   r^   z%Epsilla.add_texts.<locals>.<listcomp>ß   s&   € Ð1Ð1Ð1 a�t•D”J‘L”LÑ!Ô!Ð1Ð1Ð1r4   ©rR   rV   r   N)rB   ÚrecordsrC   zFailed to add records to rc   rD   rE   c                ó,   — g | ]}t          |¦  «        ‘ŒS r[   )r   )r\   rR   s     r.   r^   z%Epsilla.add_texts.<locals>.<listcomp>õ   s   € Ð&Ð&Ð&˜B•�B‘”Ð&Ð&Ð&r4   )r)   r&   Údrop_dbrN   r(   Úembed_documentsÚNotImplementedErrorre   rH   Údebugrt   Ú	enumeraterf   rj   ÚinsertrI   rJ   rK   )r,   ru   rO   r5   rx   rz   r   Úidsr†   ÚindexrR   Úrecordrp   rq   rr   rL   rM   s   `                r.   Ú	add_textszEpsilla.add_texts±   s.  ø€ ð0 ð 	4Ø"Ô3ˆOˆOà$3ˆDÔ!àð 	:ØŒL× Ò ¨Ð Ñ9Ô9Ð9å�U‘”ˆð	JØÔ)×9Ò9¸%Ñ@Ô@ˆJˆJøÝ"ð 	Jð 	Jð 	JØIÐIÐIÐIÀ5ÐIÑIÔIˆJˆJˆJð	Jøøøõ ˆz‰?Œ?˜aÒÐÝ�LŠLÐ7Ñ8Ô8Ð8ØˆIà×ÒØ&°:Èð 	 ñ 	
ô 	
ð 	
ð 2Ð1¨5Ð1Ñ1Ô1ˆØˆÝ" 3™œð 
	#ð 
	#‰IˆE�2àØ˜eœØ(¨Ô/ðð ˆFð
 Ð$Ø$ UÔ+×1Ò1Ñ3Ô3�Ø"*ð (ð (‘J�C˜Ø"'�F˜3‘K�KØ�NŠN˜6Ñ"Ô"Ð"Ð"à $¤× 3Ò 3Ø&°ð !4ñ !
ô !
Ñˆ�Xð ˜#ÒÐÝ�LŠLØT¨OÐTÐT¸xÈ	Ô?RÐTÐTñô ð õ ˜L×/Ò/°¸Ô0CÑDÔDÑEÔEÐEØ&Ð& #Ð&Ñ&Ô&Ð&s   Á A ÁA6Á5A6é   ÚqueryÚkrg   úList[Document]c                ó„  ‡— |s| j         }| j                             |¦  «        }| j                             |d||¬¦  «        \  }}|dk    rLt
                               d|d         › d�¦  «         t          d                     |d         ¦  «        ¦  «        ‚g d¢Št          t          ˆfd	„|d
         ¦  «        ¦  «        S )aÝ  
        Return the documents that are semantically most relevant to the query.

        Args:
            query (str): String to query the vectorstore with.
            k (Optional[int]): Number of documents to return. Defaults to 4.
            collection_name (Optional[str]): Collection to use.
                Defaults to "langchain_store" or the one provided before.
        Returns:
            List of documents that are semantically most relevant to the query
        r   )rB   Úquery_fieldÚquery_vectorÚlimitrC   zSearch failed: rD   ra   rE   r…   c                óN   •‡ — t          ‰ d         ˆˆ fd„‰ D ¦   «         ¬¦  «        S )NrV   c                ó*   •— i | ]}|‰v¯|‰|         “ŒS r[   r[   )r\   rq   Úexclude_keysÚitems     €€r.   ú
<dictcomp>z?Epsilla.similarity_search.<locals>.<lambda>.<locals>.<dictcomp>  s0   ø€ ð ð ð Ø+.¸#À\Ð:QÐ:Q˜˜T #œYÐ:QÐ:QÐ:Qr4   )Úpage_contentrp   r
   )r�   rœ   s   `€r.   ú<lambda>z+Epsilla.similarity_search.<locals>.<lambda>  sI   øø€ �XØ!% f¤ðð ð ð ð Ø26ðñ ô ðñ ô € r4   rF   )r)   r(   r~   r&   r“   rH   rI   rJ   rK   rN   Úmap)	r,   r“   r”   r5   rz   r˜   rL   rM   rœ   s	           @r.   Úsimilarity_searchzEpsilla.similarity_search÷   sñ   ø€ ð ð 	4Ø"Ô3ˆOØÔ'×3Ò3°EÑ:Ô:ˆØ $¤× 2Ò 2Ø&Ø$Ø%Øð	 !3ñ !
ô !
Ñˆ�Xð ˜#ÒÐÝ�LŠLÐA¨8°IÔ+>ÐAÐAÐAÑBÔBÐBÝ˜L×/Ò/°¸Ô0CÑDÔDÑEÔEÐEà3Ð3Ð3ˆÝÝðð ð ð ð ˜Ô"ñô ñ

ô 

ð 
	
r4   ÚclsúType[Epsilla]Ú	embeddingc	                óP   — t          ||||¬¦  «        }
 |
j        |f|||dœ|	¤Ž |
S )a  Create an Epsilla vectorstore from raw documents.

        Args:
            texts (List[str]): List of text data to be inserted.
            embeddings (Embeddings): Embedding function.
            client (pyepsilla.vectordb.Client): Epsilla client to connect to.
            metadatas (Optional[List[dict]]): Metadata for each text.
                    Defaults to None.
            db_path (Optional[str]): The path where the database will be persisted.
                    Defaults to "/tmp/langchain-epsilla".
            db_name (Optional[str]): Give a name to the loaded database.
                    Defaults to "langchain_store".
            collection_name (Optional[str]): Which collection to use.
                    Defaults to "langchain_collection".
                    If provided, default collection name will be set as well.
            drop_old (Optional[bool]): Whether to drop the previous collection
                    and create a new one. Defaults to False.

        Returns:
            Epsilla: Epsilla vector store.
        )r   r   )rO   r5   rx   )r   r‘   )r£   ru   r¥   rO   r   r   r   r5   rx   rz   Úinstances              r.   Ú
from_textszEpsilla.from_texts  sY   € õD ˜6 9°gÀwÐOÑOÔOˆØˆÔØð	
àØ+Øð		
ð 	
ð
 ð	
ð 	
ð 	
ð ˆr4   Ú	documentsc           
     ó^   — d„ |D ¦   «         }	d„ |D ¦   «         }
 | j         |	|f|
|||||dœ|¤ŽS )a"  Create an Epsilla vectorstore from a list of documents.

        Args:
            texts (List[str]): List of text data to be inserted.
            embeddings (Embeddings): Embedding function.
            client (pyepsilla.vectordb.Client): Epsilla client to connect to.
            metadatas (Optional[List[dict]]): Metadata for each text.
                    Defaults to None.
            db_path (Optional[str]): The path where the database will be persisted.
                    Defaults to "/tmp/langchain-epsilla".
            db_name (Optional[str]): Give a name to the loaded database.
                    Defaults to "langchain_store".
            collection_name (Optional[str]): Which collection to use.
                    Defaults to "langchain_collection".
                    If provided, default collection name will be set as well.
            drop_old (Optional[bool]): Whether to drop the previous collection
                    and create a new one. Defaults to False.

        Returns:
            Epsilla: Epsilla vector store.
        c                ó   — g | ]	}|j         ‘Œ
S r[   )rŸ   ©r\   Údocs     r.   r^   z*Epsilla.from_documents.<locals>.<listcomp>m  s   € Ð7Ð7Ð7 c�Ô!Ð7Ð7Ð7r4   c                ó   — g | ]	}|j         ‘Œ
S r[   )rp   r¬   s     r.   r^   z*Epsilla.from_documents.<locals>.<listcomp>n  s   € Ð7Ð7Ð7 c�S”\Ð7Ð7Ð7r4   )rO   r   r   r   r5   rx   )r¨   )r£   r©   r¥   r   r   r   r5   rx   rz   ru   rO   s              r.   Úfrom_documentszEpsilla.from_documentsL  so   € ðB 8Ð7¨YÐ7Ñ7Ô7ˆØ7Ð7¨YÐ7Ñ7Ô7ˆ	àˆsŒ~ØØð

ð  ØØØØ+Øð

ð 

ð ð

ð 

ð 
	
r4   )r   r   r   r   r   r   r   r   )r0   r1   )r5   r   r0   r6   )r:   )r:   N)r5   r   r>   r?   r0   r@   r3   )rB   r   r   rN   rO   rP   r0   r6   )Nr:   F)ru   rv   rO   rw   r5   r   rx   ry   rz   r   r0   r{   )r’   r:   )
r“   r   r”   rg   r5   r   rz   r   r0   r•   )r£   r¤   ru   r{   r¥   r   rO   rw   r   r   r   r   r   r   r5   r   rx   ry   rz   r   r0   r   )r£   r¤   r©   r•   r¥   r   r   r   r   r   r   r   r5   r   rx   ry   rz   r   r0   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r/   Úpropertyr   r9   r=   rG   rt   r‘   r¢   Úclassmethodr¨   r¯   r[   r4   r.   r   r      sÆ  € € € € € € ðð ð8 '8ÐÐ7Ð7Ð7Ñ7Ø&>ÐÐ>Ð>Ð>Ñ>Ø)?Ð!Ð?Ð?Ð?Ñ?ð "<Ø!;ð-ð -ð -ð -ð -ð> ð ð  ð  ñ „Xð ð0ð 0ð 0ð 0ð
1ð 
1ð 
1ð 
1ð 
1ð QUð"ð "ð "ð "ð "ð4 TXð*Jð *Jð *Jð *Jð *Jð^ +/Ø)+Ø#(ðD'ð D'ð D'ð D'ð D'ðN >@ð&
ð &
ð &
ð &
ð &
ðP ð
 +/ØØ!;Ø!;Ø)FØ#(ð*ð *ð *ð *ñ „[ð*ðX ð
 Ø!;Ø!;Ø)FØ#(ð-
ð -
ð -
ð -
ñ „[ð-
ð -
ð -
r4   r   )r³   Ú
__future__r   Úloggingr‚   Útypingr   r   r   r   r   r	   Úlangchain_core.documentsr   Úlangchain_core.embeddingsr   Úlangchain_core.vectorstoresr   r   r   Ú	getLoggerrH   r   r[   r4   r.   ú<module>r¾      sý   ðØ -Ð -à "Ð "Ð "Ð "Ð "Ð "à €€€Ø €€€Ø EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ EÐ Eà -Ð -Ð -Ð -Ð -Ð -Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3àð #Ø"Ð"Ð"Ð"Ð"Ð"à	ˆÔ	Ñ	Ô	€ðg
ð g
ð g
ð g
ð g
ˆkñ g
ô g
ð g
ð g
ð g
r4   