a
    bg?                     @  s   d dl m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rd dlmZ d dlmZ d dlmZ d dlmZ G d	d
 d
eZdS )    )annotationsN)TYPE_CHECKINGAnyIterableListOptionalTuple)Document)
Embeddings)VectorStore)TigrisClient)Filterc                   @  s   e Zd ZdZddddddZeddd	d
ZeddddZd+ddddddddZd,ddddddddZ	d-dddddd d!Z
ed.ddddd"d#dd d$d%d&Zdddd'd(d)d*ZdS )/Tigrisz`Tigris` vector store.r   r
   str)client
embeddings
index_namec                 C  sB   zddl }W n ty&   tdY n0 || _t| || _dS )zInitialize Tigris vector store.r   NzWCould not import tigrisdb python package. Please install it with `pip install tigrisdb`)tigrisdbImportError	_embed_fnTigrisVectorStoreZ
get_search_vector_store)selfr   r   r   r    r   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/tigris.py__init__   s    
zTigris.__init__)returnc                 C  s   | j S N)r   r   r   r   r   r   !   s    zTigris.embeddingsr   c                 C  s   | j S r   )r   r   r   r   r   search_index%   s    zTigris.search_indexNzIterable[str]zOptional[List[dict]]zOptional[List[str]]r   z	List[str])texts	metadatasidskwargsr   c                 K  s(   |  |||}| j|}dd |D S )a  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            ids: Optional list of ids for documents.
                Ids will be autogenerated if not provided.
            kwargs: vectorstore specific parameters

        Returns:
            List of ids from adding the texts into the vectorstore.
        c                 S  s   g | ]
}|j qS r   )id).0rr   r   r   
<listcomp>>       z$Tigris.add_texts.<locals>.<listcomp>)
_prep_docsr   Zadd_documents)r   r    r!   r"   r#   docsresultr   r   r   	add_texts)   s    zTigris.add_texts   intzOptional[TigrisFilter]zList[Document])querykfilterr#   r   c                 K  s   |  |||}dd |D S )z"Return docs most similar to query.c                 S  s   g | ]\}}|qS r   r   )r%   doc_r   r   r   r'   I   r(   z,Tigris.similarity_search.<locals>.<listcomp>)similarity_search_with_score)r   r/   r0   r1   r#   Zdocs_with_scoresr   r   r   similarity_search@   s    zTigris.similarity_searchzList[Tuple[Document, float]])r/   r0   r1   r   c                 C  sV   | j |}| jj|||d}g }|D ]*}|t|jd |jdd|jf q&|S )a  Run similarity search with Chroma with distance.

        Args:
            query (str): Query text to search for.
            k (int): Number of results to return. Defaults to 4.
            filter (Optional[TigrisFilter]): Filter by metadata. Defaults to None.

        Returns:
            List[Tuple[Document, float]]: List of documents most similar to the query
                text with distance in float.
        )vectorr0   Z	filter_bytextmetadata)Zpage_contentr8   )	r   Zembed_queryr   r5   appendr	   r2   getZscore)r   r/   r0   r1   r6   r+   r*   r&   r   r   r   r4   K   s    z#Tigris.similarity_search_with_scorezOptional[TigrisClient]zOptional[str])r    	embeddingr!   r"   r   r   r#   r   c           	      K  s6   |st d|st }| |||}|j|||d |S )z9Return VectorStore initialized from texts and embeddings.z`index_name` is required)r    r!   r"   )
ValueErrorr   r,   )	clsr    r;   r!   r"   r   r   r#   storer   r   r   
from_textsl   s    zTigris.from_textszList[TigrisDocument])r    r!   r"   r   c                 C  sl   | j t|}g }t||p g |p&g |p,g D ]6\}}}}	||pDg |pJi d}
|	r\|	|
d< ||
 q0|S )N)r7   r   r8   r$   )r   Zembed_documentslist	itertoolszip_longestr9   )r   r    r!   r"   r   r*   tmeZ_idr2   r   r   r   r)      s    zTigris._prep_docs)NN)r-   N)r-   N)NNNN)__name__
__module____qualname____doc__r   propertyr   r   r,   r5   r4   classmethodr?   r)   r   r   r   r   r      s,         !     r   )
__future__r   rA   typingr   r   r   r   r   r   Zlangchain_core.documentsr	   Zlangchain_core.embeddingsr
   Zlangchain_core.vectorstoresr   r   r   r   Ztigrisdb.types.filtersr   ZTigrisFilterZtigrisdb.types.vectorZTigrisDocumentr   r   r   r   r   <module>   s    