o
    Zh?                     @  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rFd 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d0dd	Zed1ddZed2ddZ		d3d4ddZ		d5d6d#d$Z			d5d7d&d'Z
e				d8d9d+d,Zd:d.d/ZdS );Tigrisz`Tigris` vector store.clientr   
embeddingsr
   
index_namestrc                 C  s>   zddl }W n ty   tdw || _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   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/vectorstores/tigris.py__init__   s   zTigris.__init__returnc                 C     | j S N)r   r   r   r   r   r   !      zTigris.embeddingsr   c                 C  r   r   )r   r   r   r   r   search_index%   r    zTigris.search_indexNtextsIterable[str]	metadatasOptional[List[dict]]idsOptional[List[str]]kwargsr   	List[str]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>>   s    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   querykintfilterOptional[TigrisFilter]List[Document]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   s    z,Tigris.similarity_search.<locals>.<listcomp>)similarity_search_with_score)r   r3   r4   r6   r(   Zdocs_with_scoresr   r   r   similarity_search@   s   zTigris.similarity_searchList[Tuple[Document, float]]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.
        )vectorr4   Z	filter_bytextmetadata)Zpage_contentr@   )	r   Zembed_queryr!   r<   appendr	   r9   getZscore)r   r3   r4   r6   r>   r0   r/   r,   r   r   r   r;   K   s   z#Tigris.similarity_search_with_score	embeddingOptional[TigrisClient]Optional[str]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   r1   )	clsr"   rC   r$   r&   r   r   r(   storer   r   r   
from_textsl   s   zTigris.from_textsList[TigrisDocument]c                 C  sl   | j t|}g }t||pg |pg |pg D ]\}}}}	||p"g |p%i d}
|	r.|	|
d< ||
 q|S )N)r?   r   r@   r*   )r   Zembed_documentslist	itertoolszip_longestrA   )r   r"   r$   r&   r   r/   tmeZ_idr9   r   r   r   r.      s   zTigris._prep_docs)r   r   r   r
   r   r   )r   r
   )r   r   )NN)
r"   r#   r$   r%   r&   r'   r(   r   r   r)   )r2   N)
r3   r   r4   r5   r6   r7   r(   r   r   r8   )r3   r   r4   r5   r6   r7   r   r=   )NNNN)r"   r)   rC   r
   r$   r%   r&   r'   r   rD   r   rE   r(   r   r   r   )r"   r#   r$   r%   r&   r'   r   rJ   )__name__
__module____qualname____doc__r   propertyr   r!   r1   r<   r;   classmethodrI   r.   r   r   r   r   r      s.    
!r   )
__future__r   rL   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     