
    Ng?                        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rd dlmZ d dlmZ d dlmZ d dlmZ  G d	 d
e          ZdS )    )annotationsN)TYPE_CHECKINGAnyIterableListOptionalTuple)Document)
Embeddings)VectorStore)TigrisClient)Filterc                      e Zd ZdZd(dZed)d
            Zed*d            Z	 	 d+d,dZ	 	 d-d.dZ		 	 d-d/d!Z
e	 	 	 	 d0d1d%            Zd2d'ZdS )3Tigrisz`Tigris` vector store.clientr   
embeddingsr   
index_namestrc                    	 ddl }n# t          $ r t          d          w xY 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TigrisVectorStore
get_search_vector_store)selfr   r   r   r   s        c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/tigris.py__init__zTigris.__init__   sr    	OOOO 	 	 	@  	 $.v/@/@/B/BJOOs    !returnc                    | j         S N)r   r   s    r   r   zTigris.embeddings!   s
    ~    r   c                    | j         S r!   )r   r"   s    r   search_indexzTigris.search_index%   s    !!r#   NtextsIterable[str]	metadatasOptional[List[dict]]idsOptional[List[str]]kwargsr   	List[str]c                |    |                      |||          }| j                            |          }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                    g | ]	}|j         
S  )id).0rs     r   
<listcomp>z$Tigris.add_texts.<locals>.<listcomp>>   s    %%%%%%r#   )
_prep_docsr%   add_documents)r   r&   r(   r*   r,   docsresults          r   	add_textszTigris.add_texts)   sC    & ui55"0066%%f%%%%r#      querykintfilterOptional[TigrisFilter]List[Document]c                H    |                      |||          }d |D             S )z"Return docs most similar to query.c                    g | ]\  }}|S r0   r0   )r2   doc_s      r   r4   z,Tigris.similarity_search.<locals>.<listcomp>I   s    333Q333r#   )similarity_search_with_score)r   r;   r<   r>   r,   docs_with_scoress         r   similarity_searchzTigris.similarity_search@   s1      <<UAvNN33"23333r#   List[Tuple[Document, float]]c           	        | j                             |          }| j                            |||          }g }|D ]P}|                    t          |j        d         |j                            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.
        )vectorr<   	filter_bytextmetadata)page_contentrM   )	r   embed_queryr%   rG   appendr
   rC   getscore)r   r;   r<   r>   rJ   r8   r7   r3   s           r   rE   z#Tigris.similarity_search_with_scoreK   s    " ++E22"44Q& 5 
 
 .0 	 	AKK%&U6]QUYYz=R=R   G	    r#   	embeddingOptional[TigrisClient]Optional[str]c                    |st          d          |st                      } | |||          }|                    |||           |S )z9Return VectorStore initialized from texts and embeddings.z`index_name` is required)r&   r(   r*   )
ValueErrorr   r9   )	clsr&   rS   r(   r*   r   r   r,   stores	            r   
from_textszTigris.from_textsl   s\      	97888 	$!^^FFIz22eycBBBr#   List[TigrisDocument]c                    | j                             t          |                    }g }t          j        ||pg |pg |pg           D ]-\  }}}}	||pg |pi d}
|	r|	|
d<   |                    |
           .|S )N)rL   r   rM   r1   )r   embed_documentslist	itertoolszip_longestrP   )r   r&   r(   r*   r   r7   tme_idrC   s              r   r5   zTigris._prep_docs   s     )-(F(FtE{{(S(S
%'%19?J$4"ciR
 
 
	 
	LAq!S g2G# #C
   D	KKr#   )r   r   r   r   r   r   )r   r   )r   r   )NN)
r&   r'   r(   r)   r*   r+   r,   r   r   r-   )r:   N)
r;   r   r<   r=   r>   r?   r,   r   r   r@   )r;   r   r<   r=   r>   r?   r   rH   )NNNN)r&   r-   rS   r   r(   r)   r*   r+   r   rT   r   rU   r,   r   r   r   )r&   r'   r(   r)   r*   r+   r   r[   )__name__
__module____qualname____doc__r   propertyr   r%   r9   rG   rE   classmethodrZ   r5   r0   r#   r   r   r      s         P P P P    X " " " X" +/#'	& & & & &4 )-		4 	4 	4 	4 	4 )-	    B 
 +/#')-$(    [(     r#   r   )
__future__r   r_   typingr   r   r   r   r   r	   langchain_core.documentsr
   langchain_core.embeddingsr   langchain_core.vectorstoresr   r   r   r   tigrisdb.types.filtersr   TigrisFiltertigrisdb.types.vectorTigrisDocumentr   r0   r#   r   <module>rt      s   " " " " " "     F F F F F F F F F F F F F F F F - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3 A%%%%%%999999======@@@@@@C C C C C[ C C C C Cr#   