
    h7                        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y)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	 	 	 	 	 	 	 dd	Ze	dd
       Z
ddZdddZ	 d	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 	 	 d dZeddeeedf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!d       Zedeeedf	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"d       Zy)#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_NAMEc                   	 ddl }t        ||j                  j                  |j
                  j                  j                  f      st        dt        |             || _
        || _        || _        t        j                  | _        | j                  j!                  ||       | j                  j#                  |       y# t        $ r}t        d      |d}~ww xY w)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          f/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/epsilla.py__init__zEpsilla.__init__4   s    	 Y''..	0F0F0O0OP
 88<V~G 
 )/% ' E EWg>G,'  	Q 	s   C 	C
CCc                    | j                   S N)r&   )r*   s    r-   r+   zEpsilla.embeddingsS   s        c                    || _         y)z~
        Set default collection to use.

        Args:
            collection_name (str): The name of the collection.
        N)r'   r*   collection_names     r-   use_collectionzEpsilla.use_collectionW   s     !0r1   c                V    |s| j                   }| j                  j                  |       y)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_tabler3   s     r-   
clear_datazEpsilla.clear_data`   s$     "33O0r1   Nc                    |s| j                   }| j                  j                  ||      \  }}|dk7  r8t        j	                  d|d           t        dj                  |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_nameresponse_fields   zFailed to get records: message
Error: {}.result)r'   r$   getloggererror	Exceptionformat)r*   r4   r;   status_coderesponses        r-   r@   zEpsilla.getl   s     "33O $ 0 0& !1 !
X #LL28I3F2GHIL//0CDEE!!r1   c                   |st        d      t        |d         }dddddddd	|d
g}||D cg c]  }|d   	 }}|D ]  }|j                         D ]  \  }	}
|	|v rt        |
t              rd}nHt        |
t
              rd}n5t        |
t              rd}n"t        |
t              rd}nt        d|	 d      |j                  |	|d       |j                  |	         | j                  j                  ||      \  }}|dk7  rZ|dk(  rt        j                  d| d       y t        j                  d| d|d           t        dj                  |d               y c c}w )NzEmbeddings list is empty.r   idINT)namedataTypetextSTRINGr+   VECTOR_FLOAT)rJ   rK   
dimensionsrJ   FLOATBOOLzUnsupported data type for .)table_fieldsr<   i  z#Continuing with the existing table zFailed to create collection : r=   r>   )
ValueErrorlenitemsr   r   intfloatboolappendr$   create_tablerA   inforB   rC   rD   )r*   r:   r+   	metadatasdimfieldsfieldfield_namesmetadatakeyvalued_typerE   rF   s                 r-   _create_collectionzEpsilla._create_collection   s    899*Q- u-2!~SQ

  6<=U5==K=% ,"*.."2 ,JCk) !%-!)#E3/!&#E51!(#E40!'(+EcU!)LMMMM3F"CD&&s+,,$ !% 9 9V !: !
X #c!A*QOP2:,b)AT@UV   3 3HY4G HII - >s   E#Fc                   |s| j                   }n|| _         |r| j                  j                  |       t        |      }	 | j                  j                  |      }t        |      dk(  rt        j                  d       g S | j                  |||       |D cg c]  }t        t        j                               ! }	}g }
t        |	      D ]F  \  }}|||   ||   d}|"||   j!                         }|D ]
  \  }}|||<    |
j#                  |       H | j                  j%                  ||
      \  }}|dk7  r;t        j'                  d| d	|d
           t)        dj+                  |d
               |	D cg c]  }t-        |       c}S # t        $ r2 |D cg c]  }| j                  j                  |       nc c}w }}Y lw xY wc c}w c c}w )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   r   zNothing to insert, skipping.)r:   r+   r^   rH   rL   r+   )r:   recordsr<   zFailed to add records to rT   r=   r>   )r'   r$   drop_dblistr&   embed_documentsNotImplementedErrorembed_queryrV   rA   debugrg   hashuuiduuid4	enumeraterW   r[   insertrB   rC   rD   r   )r*   textsr^   r4   drop_oldkwargsr+   x_idsrj   indexrH   recordrc   rd   re   rE   rF   s                      r-   	add_textszEpsilla.add_texts   s   0 "33O$3D!LL   9U	J))99%@J z?aLL78I&: 	  	
 ,11atDJJL!11"3 
	#IE2e(/F
 $$U+113"* (JC"'F3K(NN6"
	# !% 3 3& !4 !
X #LL+O+<Bx	?R>ST L//0CDEE"%&BB&&C # 	JCHIa$**66q9IIJI	J 2, 's*   F $G9GG	"F>=G	G	c                T   |s| j                   }| j                  j                  |      }| j                  j	                  |d||      \  }}|dk7  r9t
        j                  d|d    d       t        dj                  |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+   )r:   query_fieldquery_vectorlimitr<   zSearch failed: r=   rR   r>   ri   c           	     \    t        | d   | D ci c]  }|vs|| |    c}      S c c}w )NrL   )page_contentrc   r
   )itemrd   exclude_keyss     r-   <lambda>z+Epsilla.similarity_search.<locals>.<lambda>  s:    X!%f26+.#\:QT#Y s   	)
)
r?   )r'   r&   ro   r$   queryrA   rB   rC   rD   rl   map)	r*   r   kr4   rx   r   rE   rF   r   s	           @r-   similarity_searchzEpsilla.similarity_search   s     "33O''33E: $ 2 2&$%	 !3 !
X #LL?8I+>*?qABL//0CDEE3 "

 
	
r1   c	                R    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   )r^   r4   rw   )r   r~   )clsrv   	embeddingr^   r    r   r   r4   rw   rx   instances              r-   
from_textszEpsilla.from_texts  sG    D 69gwO	
+		

 	
 r1   c           
         |D 	cg c]  }	|	j                    }
}	|D 	cg c]  }	|	j                   }}	 | j                  |
|f||||||d|S c c}	w c c}	w )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.
        )r^   r    r   r   r4   rw   )r   rc   r   )r   	documentsr   r    r   r   r4   rw   rx   docrv   r^   s               r-   from_documentszEpsilla.from_documentsL  sy    B .77c!!77-67cS\\7	7s~~

  +

 

 
	
 87s
   AA)r    r   r+   r   r   Optional[str]r   r   )returnzOptional[Embeddings])r4   r   r   None) )r   N)r4   r   r;   zOptional[List[str]]r   z
List[dict]r0   )r:   r   r+   rl   r^   zOptional[list[dict]]r   r   )Nr   F)rv   zIterable[str]r^   Optional[List[dict]]r4   r   rw   Optional[bool]rx   r   r   	List[str])   r   )
r   r   r   rX   r4   r   rx   r   r   List[Document])r   Type[Epsilla]rv   r   r   r   r^   r   r    r   r   r   r   r   r4   r   rw   r   rx   r   r   r   )r   r   r   r   r   r   r    r   r   r   r   r   r4   r   rw   r   rx   r   r   r   )__name__
__module____qualname____doc__r   __annotations__r   r   r.   propertyr+   r5   r8   r@   rg   r~   r   classmethodr   r    r1   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#(-
-
!-
 -
 	-

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