
    Ng;/                        d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	m
Z
mZmZmZ d dlZd dlmZ d dlmZ d dlmZ  ej        e          Z G d d	e          ZdS )
    )annotationsN)ThreadPoolExecutor)AnyIterableListOptionalTuple)Document)
Embeddings)VectorStorec                      e Zd ZdZ	 	 	 	 	 	 d(d)dZ	 	 d*d+dZ	 	 d*d,d Z	 d-d.d"Ze	 	 	 	 	 	 	 d/d0d%            Z	e	 	 	 	 	 	 d1d2d'            Z
dS )3Clarifaia  `Clarifai AI` vector store.

    To use, you should have the ``clarifai`` python SDK package installed.

    Example:
        .. code-block:: python

                from langchain_community.vectorstores import Clarifai

                clarifai_vector_db = Clarifai(
                        user_id=USER_ID,
                        app_id=APP_ID,
                        number_of_docs=NUMBER_OF_DOCS,
                        )
    N   https://api.clarifai.comuser_idOptional[str]app_idnumber_of_docsOptional[int]pattokenapi_basereturnNonec                H   |pt           j                            d          }|pt           j                            d          }||t          d          || _        	 ddlm}	 n"# t          $ r}
t          d          |
d}
~
ww xY w |	||||||          j        | _	        dS )	a  Initialize with Clarifai client.

        Args:
            user_id (Optional[str], optional): User ID. Defaults to None.
            app_id (Optional[str], optional): App ID. Defaults to None.
            pat (Optional[str], optional): Personal access token. Defaults to None.
            token (Optional[str], optional): Session token. Defaults to None.
            number_of_docs (Optional[int], optional): Number of documents to return
            during vector search. Defaults to None.
            api_base (Optional[str], optional): API base. Defaults to None.

        Raises:
            ValueError: If user ID, app ID or personal access token is not provided.
        CLARIFAI_USER_IDCLARIFAI_APP_IDNzCould not find CLARIFAI_USER_ID or CLARIFAI_APP_ID in your environment. Please set those env variables with a valid user ID, app IDr   SearchXCould not import clarifai python package. Please install it with `pip install clarifai`.)r   r   top_kr   r   base_url)
osenvironget
ValueError_number_of_docsclarifai.client.searchr   ImportErrorauth_helper_auth)selfr   r   r   r   r   r   _user_id_app_idr   es              e/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/clarifai.py__init__zClarifai.__init__#   s    . @bjnn-?@@=BJNN+<==wN  
  .	5555555 	 	 	A  	 V 
 
 
  	


s   A% %
B/A??BtextsIterable[str]	metadatasOptional[List[dict]]idsOptional[List[str]]kwargsr   	List[str]c                B   	 ddl m} ddlm} n"# t          $ r}t	          d          |d}~ww xY wt          |          }t          |          }	|	dk    s
J d            ||	t          |          k    s
J d            |*t          |          t          |          k    s
J d            |                    | j        	          d
}
g }t          d|	|
          D ]?}	 ||||
z            }|r||||
z            nd|'d t          t          |                    D             n||||
z            ;g D ]6} |            }|
                    |                               |           7fdt          |          D             }                    |          }|                    |           t                              d           # t"          $ r;}t                              d|            t'          j                     Y d}~9d}~ww xY w|S )a'  Add texts to the Clarifai vectorstore. This will push the text
        to a Clarifai application.
        Application use a base workflow that create and store embedding for each text.
        Make sure you are using a base workflow that is compatible with text
        (such as Language Understanding).

        Args:
            texts (Iterable[str]): Texts to add to the vectorstore.
            metadatas (Optional[List[dict]], optional): Optional list of metadatas.
            ids (Optional[List[str]], optional): Optional list of IDs.

        r   )Inputs)Structr    Nz,No texts provided to add to the vectorstore.z1Number of texts and metadatas should be the same.z7Number of text inputs and input ids should be the same.)auth    c                >    g | ]}t          j                    j        S  )uuiduuid4hex).0_s     r0   
<listcomp>z&Clarifai.add_texts.<locals>.<listcomp>   s!     S S Sa!1 S S S    c                f    g | ]-\  }}                     |         |r|         nd           .S )N)input_idraw_textmetadata)get_text_input)rD   itext	batch_idsbatch_metadatas	input_obj	meta_lists      r0   rF   z&Clarifai.add_texts.<locals>.<listcomp>   s^         4 ,,!*1!%1@!J1d -    rG   )inputszInput posted successfully.zPost inputs failed: )clarifai.client.inputr;   google.protobuf.struct_pb2r<   r)   listlenfrom_auth_helperr+   rangeupdateappend	enumerateupload_inputsextendloggerdebug	Exceptionwarning	traceback	print_exc)r,   r2   r4   r6   r8   r;   r<   r/   ltextslength
batch_sizeinput_job_idsidxbatch_textsmetameta_structinput_batch	result_iderrorrO   rP   rQ   rR   s                      @@@@r0   	add_textszClarifai.add_textsU   s   &	4444449999999 	 	 	A  	 eVzzzIzzz S    B   ?v;;## #   H   +++<<	
FJ// 	& 	&C&$S3+;%;<9BLIcC*$4455   ; S S5[AQAQ;R;R S S SII #C#
*:$: ;I". "I / 6 6&,fhh#**4000!((5555       $-[#9#9   &33;3GG	$$Y///9:::: & & &=e==>>>#%%%%%%%%& s)    
2-2"C4G
H!0HHquerystrkfiltersOptional[dict]List[Tuple[Document, float]]c                    	 ddl m} ddlm} ddlm n"# t          $ r}t          d          |d}~ww xY w|p j        }|                     j	        |          }	d|ig}
|d	|i}|	
                    |
|g
          }n|	
                    |
          }d |D             }t          d          d fdfd|D             }d |D             }|S )a  Run similarity search with score using Clarifai.

        Args:
            query (str): Query text to search for.
            k (Optional[int]): Number of results to return. If not set,
            it'll take _number_of_docs. Defaults to None.
            filter (Optional[Dict[str, str]]): Filter by metadata.
            Defaults to None.

        Returns:
            List[Document]: List of documents most similar to the query text.
        r   r   )resources_pb2)json_formatr    N)r=   r!   text_rawrK   )ranksrt   )r{   c                &    g | ]}|j         D ]}|S r@   )hits)rD   datahits      r0   rF   z9Clarifai.similarity_search_with_score.<locals>.<listcomp>   s'    EEE49EECEEEErG   
   )max_workersr   resources_pb2.Hitr   Tuple[Document, float]c                                        | j        j        j                  }t	          j        j                  }t          j        | j        j        j        j	        |          }|j
        |_        |j        }t                              d| j        dd| j        j         d| j        j         d|d d                     t#          ||          | j        fS )	N)headersz	Score z.2fz for annotation: z                off input: z, text: }   )page_contentrK   )MessageToDictinputr~   rK   dictr+   requestsr%   rN   urlapparent_encodingencodingr_   r`   score
annotationidr
   )r   rK   hrequestrequested_textry   r,   s        r0   hit_to_documentz>Clarifai.similarity_search_with_score.<locals>.hit_to_document   s    "001HIIHTZ())Al39>#6#:AFFFG  '8G$\NLLI39 I I I3>;L I IILI I2@#2FI I   .8LLLciXXrG   c                <    g | ]}                     |          S r@   )submit)rD   r   executorr   s     r0   rF   z9Clarifai.similarity_search_with_score.<locals>.<listcomp>   s'    IIIS8???C88IIIrG   c                6    g | ]}|                                 S r@   )result)rD   futures     r0   rF   z9Clarifai.similarity_search_with_score.<locals>.<listcomp>   s     AAAv6==??AAArG   )r   r   r   r   )r(   r   clarifai_grpc.grpc.apirx   google.protobufry   r)   r'   rX   r+   rq   r   )r,   rq   rs   rt   r8   r   rx   r/   r!   
search_objranksearch_metadatasearch_responser}   futuresdocs_and_scoresr   r   ry   s   `               @@@r0   similarity_search_with_scorez%Clarifai.similarity_search_with_score   s   &	555555<<<<<<3333333 	 	 	A  	 )T),,$*E,JJ
U#$)73O(..TOCT.UUOO(..T.::O FEEEE%"555	Y 	Y 	Y 	Y 	Y 	Y 	Y  JIIIIDIIIAAAAAs    
838List[Document]c                :     | j         |fd|i|}d |D             S )aG  Run similarity search using Clarifai.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return.
            If not set, it'll take _number_of_docs. Defaults to None.

        Returns:
            List of Documents most similar to the query and score for each
        rs   c                    g | ]\  }}|S r@   r@   )rD   docrE   s      r0   rF   z.Clarifai.similarity_search.<locals>.<listcomp>   s    222Q222rG   )r   )r,   rq   rs   r8   r   s        r0   similarity_searchzClarifai.similarity_search   s7      <$;EQQQQ&QQ22/2222rG   	embeddingOptional[Embeddings]c	           	     P     | d|||||d|	}
|
                     ||           |
S )a  Create a Clarifai vectorstore from a list of texts.

        Args:
            user_id (str): User ID.
            app_id (str): App ID.
            texts (List[str]): List of texts to add.
            number_of_docs (Optional[int]): Number of documents
            to return during vector search. Defaults to None.
            pat (Optional[str], optional): Personal access token.
            Defaults to None.
            token (Optional[str], optional): Session token. Defaults to None.
            metadatas (Optional[List[dict]]): Optional list
            of metadatas. Defaults to None.
            kwargs: Additional keyword arguments to be passed to the Search.

        Returns:
            Clarifai: Clarifai vectorstore.
        )r   r   r   r   r   )r2   r4   r@   )rp   )clsr2   r   r4   r   r   r   r   r   r8   clarifai_vector_dbs              r0   
from_textszClarifai.from_texts   sZ    > !S 
)
 
 
 
 	$$5I$FFF!!rG   	documentsc                \    d |D             }	d |D             }
 | j         d|||	|||
|d|S )a  Create a Clarifai vectorstore from a list of documents.

        Args:
            user_id (str): User ID.
            app_id (str): App ID.
            documents (List[Document]): List of documents to add.
            number_of_docs (Optional[int]): Number of documents
            to return during vector search. Defaults to None.
            pat (Optional[str], optional): Personal access token. Defaults to None.
            token (Optional[str], optional): Session token. Defaults to None.
            kwargs: Additional keyword arguments to be passed to the Search.

        Returns:
            Clarifai: Clarifai vectorstore.
        c                    g | ]	}|j         
S r@   )r   rD   r   s     r0   rF   z+Clarifai.from_documents.<locals>.<listcomp>>  s    777c!777rG   c                    g | ]	}|j         
S r@   )rK   r   s     r0   rF   z+Clarifai.from_documents.<locals>.<listcomp>?  s    777cS\777rG   )r   r   r2   r   r   r4   r   r@   )r   )r   r   r   r   r   r   r   r   r8   r2   r4   s              r0   from_documentszClarifai.from_documents#  si    6 87Y77777Y777	s~ 	
)	
 	
 	
 	
 		
rG   )NNr   NNr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NN)
r2   r3   r4   r5   r6   r7   r8   r   r   r9   )
rq   rr   rs   r   rt   ru   r8   r   r   rv   )N)rq   rr   rs   r   r8   r   r   r   )NNNNNNN)r2   r9   r   r   r4   r5   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   )NNNNNN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r8   r   r   r   )__name__
__module____qualname____doc__r1   rp   r   r   classmethodr   r   r@   rG   r0   r   r      s.        $ "& $()!#"<0 0 0 0 0j +/#'	M M M M Md  "&	@ @ @ @ @J  3 3 3 3 3&  +/*.!% $(,!#'" '" '" '" ['"R  +/!% $(,!#%
 %
 %
 %
 [%
 %
 %
rG   r   )
__future__r   loggingr#   rc   rA   concurrent.futuresr   typingr   r   r   r   r	   r   langchain_core.documentsr
   langchain_core.embeddingsr   langchain_core.vectorstoresr   	getLoggerr   r_   r   r@   rG   r0   <module>r      s	   " " " " " "  				      1 1 1 1 1 1 7 7 7 7 7 7 7 7 7 7 7 7 7 7  - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3		8	$	$w
 w
 w
 w
 w
{ w
 w
 w
 w
 w
rG   