
    h?                        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y)    )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	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd	       Z	 	 	 	 	 	 	 	 dd
Zy)Tigrisz`Tigris` vector store.c                    	 ddl }|| _        t        |j	                         |      | _        y# t        $ r t        d      w xY w)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)selfclient
embeddings
index_namer   s        e/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/vectorstores/tigris.py__init__zTigris.__init__   sO    	 $.v/@/@/BJO  	@ 	s	   - Ac                    | j                   S N)r   r   s    r   r   zTigris.embeddings!   s    ~~    c                    | j                   S r   )r   r    s    r   search_indexzTigris.search_index%   s    !!!r!   Nc                    | j                  |||      }| j                  j                  |      }|D cg c]  }|j                   c}S c c}w )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.
        )
_prep_docsr#   add_documentsid)r   texts	metadatasidskwargsdocsresultrs           r   	add_textszTigris.add_texts)   sD    & ui5""006$%%%%s   A	c                ^    | j                  |||      }|D cg c]  \  }}|	 c}}S c c}}w )z"Return docs most similar to query.)similarity_search_with_score)r   querykfilterr+   docs_with_scoresdoc_s           r   similarity_searchzTigris.similarity_search@   s1      <<UAvN"23Q333s   )c           	     &   | j                   j                  |      }| j                  j                  |||      }g }|D ]P  }|j	                  t        |j                  d   |j                  j                  d            |j                  f       R |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.
        )vectorr3   	filter_bytextmetadata)page_contentr=   )	r   embed_queryr#   r8   appendr
   r6   getscore)r   r2   r3   r4   r:   r-   r,   r.   s           r   r1   z#Tigris.similarity_search_with_scoreK   s    " ++E2""44Q& 5 
 .0 	AKK%&UU6]QUUYYz=R GG		 r!   c                t    |st        d      |s
t               } | |||      }|j                  |||       |S )z9Return VectorStore initialized from texts and embeddings.z`index_name` is required)r(   r)   r*   )
ValueErrorr   r/   )	clsr(   	embeddingr)   r*   r   r   r+   stores	            r   
from_textszTigris.from_textsl   sB     788!^FFIz2eycBr!   c                    | j                   j                  t        |            }g }t        j                  ||xs g |xs g |xs g       D ]-  \  }}}}	||xs g |xs i d}
|	r|	|
d<   |j                  |
       / |S )N)r<   r   r=   r'   )r   embed_documentslist	itertoolszip_longestr@   )r   r(   r)   r*   r   r,   tme_idr6   s              r   r%   zTigris._prep_docs   s     )-(F(FtE{(S
%'%119?J$4"ciR
 
	LAq!S g2G#C
 D	KK
	 r!   )r   r   r   r   r   str)returnr   )rS   r   )NN)
r(   Iterable[str]r)   Optional[List[dict]]r*   Optional[List[str]]r+   r   rS   	List[str])   N)
r2   rR   r3   intr4   Optional[TigrisFilter]r+   r   rS   zList[Document])r2   rR   r3   rY   r4   rZ   rS   zList[Tuple[Document, float]])NNNN)r(   rW   rF   r   r)   rU   r*   rV   r   zOptional[TigrisClient]r   zOptional[str]r+   r   rS   r   )r(   rT   r)   rU   r*   rV   rS   zList[TigrisDocument])__name__
__module____qualname____doc__r   propertyr   r#   r/   r8   r1   classmethodrH   r%    r!   r   r   r      s|    P   " " +/#'	&& (& !	&
 & 
&4 )-		4	4 	4 '		4
 	4 
	4 )-	  '	
 
&B 
 +/#')-$(  (	
 ! ' "  
 ( ( !	
 
r!   r   )
__future__r   rL   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   ra   r!   r   <module>rk      s7    "  F F - 0 3%9=@C[ Cr!   