a
    bg7                     @  s   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rtddlmZ e ZG d	d
 d
eZdS )z'Wrapper around Epsilla vector database.    )annotationsN)TYPE_CHECKINGAnyIterableListOptionalTypeDocument)
Embeddings)VectorStore)vectordbc                   @  sD  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d	d
dddddZe	ddddZ
dddddZd:dddddZd;ddddddZd<dd d!dd"d#d$Zd=d&d'dd(d	d)d*d+d,Zd>dd.dd	d/d0d1d2Zeddeeed%fd3d)d
d'd	dddd(d	d d4d5d6Zedeeed%fd3d/d
d	dddd(d	d d7
d8d9ZdS )?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)
    Zlangchain_storestr_LANGCHAIN_DEFAULT_DB_NAMEz/tmp/langchain-epsilla_LANGCHAIN_DEFAULT_DB_PATHZlangchain_collection_LANGCHAIN_DEFAULT_TABLE_NAMEr   r   zOptional[str])client
embeddingsdb_pathdb_namec              
   C  s   zddl }W n. ty: } ztd|W Y d}~n
d}~0 0 t||jj|jjjfsftdt	| || _
|| _|| _tj| _| j
j||d | j
j|d 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   ZClientcloudr   ZVectordb	TypeErrortype_clientZ_db_name_embeddingsr   r   _collection_nameZload_dbZuse_db)selfr   r   r   r   r   e r#   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/epsilla.py__init__4   s,    zEpsilla.__init__zOptional[Embeddings])returnc                 C  s   | j S )N)r   r!   r#   r#   r$   r   S   s    zEpsilla.embeddingsNone)collection_namer&   c                 C  s
   || _ dS )z~
        Set default collection to use.

        Args:
            collection_name (str): The name of the collection.
        N)r    r!   r)   r#   r#   r$   use_collectionW   s    zEpsilla.use_collection c                 C  s   |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   Z
drop_tabler*   r#   r#   r$   
clear_data`   s    zEpsilla.clear_dataNzOptional[List[str]]z
List[dict])r)   response_fieldsr&   c                 C  sT   |s
| j }| jj||d\}}|dkrLt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!   r)   r.   status_coderesponser#   r#   r$   r4   l   s    
zEpsilla.getlistzOptional[list[dict]])r/   r   	metadatasr&   c                 C  sH  |st dt|d }dddddddd	|d
g}|d urdd |D }|D ]}| D ]|\}}	||v rnq\t|	tr~d}
n@t|	trd}
n0t|	trd}
n t|	trd}
nt d| d|||
d || q\qP| j	j
||d\}}|dkrD|dkrtd| d n,td| d|d   td|d d S )NzEmbeddings list is empty.r   idINT)namedataTypetextSTRINGr   ZVECTOR_FLOAT)r?   r@   
dimensionsc                 S  s   g | ]}|d  qS )r?   r#   ).0fieldr#   r#   r$   
<listcomp>       z.Epsilla._create_collection.<locals>.<listcomp>FLOATZBOOLzUnsupported data type for .)Ztable_fieldsr0   i  z#Continuing with the existing table zFailed to create collection : r1   r2   )
ValueErrorlenitemsr   r   intfloatboolappendr   Zcreate_tabler5   infor6   r7   r8   )r!   r/   r   r<   dimfieldsfield_namesmetadatakeyvalueZd_typer9   r:   r#   r#   r$   _create_collection   sD    







zEpsilla._create_collectionFzIterable[str]zOptional[List[dict]]zOptional[bool]z	List[str])textsr<   r)   drop_oldkwargsr&   c                   sP  |s j }n| _ |r$ jj|d t|}z j|}W n$ ty`    fdd|D }Y n0 t|dkr|t	d g S  j
|||d dd |D }g }t|D ]P\}	}
|
||	 ||	 d}|d	ur||	  }|D ]\}}|||< q|| q jj||d
\}}|dkrBtd| d|d   td|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                   s   g | ]} j |qS r#   )r   embed_query)rD   xr'   r#   r$   rF      rG   z%Epsilla.add_texts.<locals>.<listcomp>r   zNothing to insert, skipping.)r/   r   r<   c                 S  s   g | ]}t t qS r#   )hashuuiduuid4)rD   _r#   r#   r$   rF      rG   r=   rA   r   N)r/   recordsr0   zFailed to add records to rJ   r1   r2   c                 S  s   g | ]}t |qS r#   )r   )rD   r=   r#   r#   r$   rF      rG   )r    r   Zdrop_dbr;   r   Zembed_documentsNotImplementedErrorrL   r5   debugrY   	enumeraterM   rQ   insertr6   r7   r8   )r!   rZ   r<   r)   r[   r\   r   Zidsrd   indexr=   recordrV   rW   rX   r9   r:   r#   r'   r$   	add_texts   sJ    



zEpsilla.add_texts   rN   zList[Document])querykr)   r\   r&   c                   s   |s
| j }| j|}| jj|d||d\}}|dkr^td|d  d td|d g d t	t
 fd	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   )r/   Zquery_fieldquery_vectorlimitr0   zSearch failed: r1   rI   r2   rc   c                   s    t  d  fdd D dS )NrA   c                   s   i | ]}| vr|| qS r#   r#   )rD   rW   )exclude_keysitemr#   r$   
<dictcomp>  s   z?Epsilla.similarity_search.<locals>.<lambda>.<locals>.<dictcomp>)page_contentrV   r	   rr   rq   ru   r$   <lambda>  s
   z+Epsilla.similarity_search.<locals>.<lambda>r3   )r    r   r]   r   rm   r5   r6   r7   r8   r;   map)r!   rm   rn   r)   r\   ro   r9   r:   r#   rv   r$   similarity_search   s&    

zEpsilla.similarity_searchzType[Epsilla])clsrZ   	embeddingr<   r   r   r   r)   r[   r\   r&   c	                 K  s.   t ||||d}
|
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   )r<   r)   r[   )r   rk   )rz   rZ   r{   r<   r   r   r   r)   r[   r\   instancer#   r#   r$   
from_texts  s    "zEpsilla.from_texts)
rz   	documentsr{   r   r   r   r)   r[   r\   r&   c              	   K  s>   dd |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                 S  s   g | ]
}|j qS r#   )rt   rD   docr#   r#   r$   rF   m  rG   z*Epsilla.from_documents.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r#   )rV   r   r#   r#   r$   rF   n  rG   )r<   r   r   r   r)   r[   )r}   )rz   r~   r{   r   r   r   r)   r[   r\   rZ   r<   r#   r#   r$   from_documentsL  s    !	zEpsilla.from_documents)r,   )r,   N)N)Nr,   F)rl   r,   )__name__
__module____qualname____doc__r   __annotations__r   r   r%   propertyr   r+   r-   r4   rY   rk   ry   classmethodr}   r   r#   r#   r#   r$   r      sH   
	  /   G (&,r   )r   
__future__r   loggingr`   typingr   r   r   r   r   r   Zlangchain_core.documentsr
   Zlangchain_core.embeddingsr   Zlangchain_core.vectorstoresr   r   r   	getLoggerr5   r   r#   r#   r#   r$   <module>   s    