
    dh7                        S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
Jr  SSKJr  SSKJr  SSKJr  \(       a  SSKJr  \R(                  " 5       r " S	 S
\5      rg)z'Wrapper around Epsilla vector database.    )annotationsN)TYPE_CHECKINGAnyIterableListOptionalTypeDocument)
Embeddings)VectorStore)vectordbc                     \ rS rSr% SrSrS\S'   SrS\S'   SrS\S	'   \\4       SS
 jjr	\
SS j5       rSS jrSSS jjr S     SS jjr S       SS jjr   S           S S jjr S!         S"S jjr\SS\\\S4                     S#S jj5       r\S\\\S4                   S$S jj5       rSrg)%Epsilla   a  
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_NAMEc                    SSK n[        XR                  R                  UR
                  R                  R                  45      (       d  [        S[        U5       35      eXl
        X@l        X l        [        R                  U l        U R                  R!                  XCS9  U R                  R#                  US9  g! [         a  n[        S5      UeSnAff = f)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 )db_namedb_pathr   )	pyepsillaImportError
isinstancer   ClientcloudclientVectordb	TypeErrortype_client_db_name_embeddingsr   r   _collection_nameload_dbuse_db)selfr!   
embeddingsr   r   r   es          `/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/vectorstores/epsilla.py__init__Epsilla.__init__4   s    	 ''..	0F0F0O0OP
 
 88<V~G 
 )/% ' E EW>G,'  	Q 	s   B; ;
CCCc                    U R                   $ N)r'   )r+   s    r.   r,   Epsilla.embeddingsS   s        c                    Xl         g)z^
Set default collection to use.

Args:
    collection_name (str): The name of the collection.
N)r(   r+   collection_names     r.   use_collectionEpsilla.use_collectionW   s
     !0r4   c                `    U(       d  U R                   nU R                  R                  U5        g)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_tabler6   s     r.   
clear_dataEpsilla.clear_data`   s$     "33O0r4   Nc                    U(       d  U R                   nU R                  R                  XS9u  p4US:w  a8  [        R	                  SUS    35        [        SR                  US   5      5      eUS   $ )at  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_nameresponse_fields   zFailed to get records: message
Error: {}.result)r(   r%   getloggererror	Exceptionformat)r+   r7   r@   status_coderesponses        r.   rE   Epsilla.getl   s{     "33O $ 0 0& !1 !
 #LL28I3F2GHIL//0CDEE!!r4   c                   U(       d  [        S5      e[        US   5      nSSS.SSS.SS	US
./nUb  U Vs/ sH  ofS   PM	     nnU H  nUR                  5        H  u  pX;   a  M  [        U
[        5      (       a  SnOW[        U
[
        5      (       a  SnO?[        U
[        5      (       a  SnO'[        U
[        5      (       a  SnO[        SU	 S35      eUR                  XS.5        UR                  U	5        M     M     U R                  R                  XS9u  pUS:w  a[  US:X  a  [        R                  SU S35        g [        R                  SU SUS    35        [        SR                  US   5      5      eg s  snf )NzEmbeddings list is empty.r   idINT)namedataTypetextSTRINGr,   VECTOR_FLOAT)rP   rQ   
dimensionsrP   FLOATBOOLzUnsupported data type for .)table_fieldsrA   i  z#Continuing with the existing table zFailed to create collection : rB   rC   )
ValueErrorlenitemsr   r   intfloatboolappendr%   create_tablerF   inforG   rH   rI   )r+   r?   r,   	metadatasdimfieldsfieldfield_namesmetadatakeyvalued_typerJ   rK   s                 r.   _create_collectionEpsilla._create_collection   s    899*Q- u-2!~SQ

  6<=fU=fK=%"*.."2JC) !%--!)#E3//!&#E511!(#E400!'(+EcU!)LMMMM3"CD&&s+ #3 &$ !% 9 9 !: !
 #c!A*QOP2:,b)AT@UV   3 3HY4G HII - >s   E?Fc                   U(       d  U R                   nOX0l         U(       a  U R                  R                  US9  [        U5      n U R                  R                  U5      n[        U5      S:X  a  [        R                  S5        / $ U R                  X6US9  U Vs/ sH!  n[        [        R                  " 5       5      PM#     n	n/ n
[        U	5       HE  u  pUX   Xk   S.nUb"  X+   R!                  5       nU H
  u  nnUX'   M     U
R#                  U5        MG     U R                  R%                  X:S9u  nnUS:w  a;  [        R'                  SU S	US
    35        [)        SR+                  US
   5      5      eU	 Vs/ sH  n[-        U5      PM     sn$ ! [         a3    U Vs/ sH  opR                  R                  U5      PM     Os  snf nn GNmf = fs  snf s  snf )aA  
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   r   zNothing to insert, skipping.)r?   r,   rd   rN   rR   r,   )r?   recordsrA   zFailed to add records to rZ   rB   rC   )r(   r%   drop_dblistr'   embed_documentsNotImplementedErrorembed_queryr\   rF   debugrm   hashuuiduuid4	enumerater]   ra   insertrG   rH   rI   r   )r+   textsrd   r7   drop_oldkwargsr,   x_idsrq   indexrN   recordri   rj   rk   rJ   rK   s                      r.   	add_textsEpsilla.add_texts   s   0 "33O$3!LL   9U	J))99%@J z?aLL78I& 	  	
 ,115atDJJL!51"3IE(/F
 $$+113"*JC"'FK #+NN6" ( !% 3 3& !4 !
X #LL+O+<Bx	?R>ST L//0CDEE"%&#BB#&&C # 	JCHI5a**66q95IJIJ	J 2, 's*   F 'G<GG!#GGGc                Z  ^ U(       d  U R                   nU R                  R                  U5      nU R                  R	                  USUUS9u  pgUS:w  a9  [
        R                  SUS    S35        [        SR                  US   5      5      e/ SQm[        [        U4S	 jUS
   5      5      $ )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,   )r?   query_fieldquery_vectorlimitrA   zSearch failed: rB   rX   rC   rp   c           	     \   > [        U S   U  Vs0 sH  oT;  d  M
  XU   _M     snS9$ s  snf )NrR   )page_contentri   r
   )itemrj   exclude_keyss     r.   <lambda>+Epsilla.similarity_search.<locals>.<lambda>  s:    X!%f26263\:Q#Y$s   )
	)
rD   )r(   r'   rv   r%   queryrF   rG   rH   rI   rs   map)	r+   r   kr7   r   r   rJ   rK   r   s	           @r.   similarity_searchEpsilla.similarity_search   s     "33O''33E: $ 2 2&$%	 !3 !
 #LL?8I+>*?qABL//0CDEE3 "

 
	
r4   c	                H    [        XBXVS9n
U
R                  " U4UUUS.U	D6  U
$ )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   )rd   r7   r~   )r   r   )clsr}   	embeddingrd   r!   r   r   r7   r~   r   instances              r.   
from_textsEpsilla.from_texts  sA    D 6gO	
+		

 	
 r4   c           
         U V	s/ sH  oR                   PM     n
n	U V	s/ sH  oR                  PM     nn	U R                  " U
U4UUUUUUS.UD6$ s  sn	f s  sn	f )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.
)rd   r!   r   r   r7   r~   )r   ri   r   )r   	documentsr   r!   r   r   r7   r~   r   docr}   rd   s               r.   from_documentsEpsilla.from_documentsL  su    B .77Yc!!Y7-67Yc\\Y	7~~

  +

 

 
	
 87s
   AA)r%   r(   r&   r'   )r!   r   r,   r   r   Optional[str]r   r   )returnzOptional[Embeddings])r7   r   r   None) )r   N)r7   r   r@   zOptional[List[str]]r   z
List[dict]r2   )r?   r   r,   rs   rd   zOptional[list[dict]]r   r   )Nr   F)r}   zIterable[str]rd   Optional[List[dict]]r7   r   r~   Optional[bool]r   r   r   	List[str])   r   )
r   r   r   r^   r7   r   r   r   r   List[Document])r   Type[Epsilla]r}   r   r   r   rd   r   r!   r   r   r   r   r   r7   r   r~   r   r   r   r   r   )r   r   r   r   r   r   r!   r   r   r   r   r   r7   r   r~   r   r   r   r   r   )__name__
__module____qualname____firstlineno____doc__r   __annotations__r   r   r/   propertyr,   r8   r<   rE   rm   r   r   classmethodr   r   __static_attributes__ r4   r.   r   r      so   8 '87&>>)?!3? "<!;-- - 	-
 ->    0
1 QU""":M"	"4 TX*J*J+/*J<P*J	*J^ +/)+#(D'D' (D' '	D'
 !D' D' 
D'N >@&
&
 &
7:&
KN&
	&
P 
 +/!;!;)F#(*** * (	*
 * * * '* !* * 
* *X 
 !;!;)F#(-
-
!-
 -
 	-

 -
 -
 '-
 !-
 -
 
-
 -
r4   r   )r   
__future__r   loggingry   typingr   r   r   r   r   r	   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   r   r   	getLoggerrF   r   r   r4   r.   <module>r      sB    - "   E E - 0 3"				g
k g
r4   