a
    bg;/                     @  s   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eZG dd	 d	eZdS )
    )annotationsN)ThreadPoolExecutor)AnyIterableListOptionalTuple)Document)
Embeddings)VectorStorec                   @  s   e Zd ZdZd$d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dZd'dddddddZed(dddddddddd d
dd Z	ed)ddddddddd d!	d"d#Z
dS )*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.comzOptional[str]zOptional[int]None)user_idapp_idnumber_of_docspattokenapi_basereturnc              
   C  s   |pt jd}|pt jd}|du s0|du r8td|| _zddlm}	 W n. ty| }
 ztd|
W Y d}
~
n
d}
~
0 0 |	||||||d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.
        ZCLARIFAI_USER_IDZ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   ImportErrorZauth_helper_auth)selfr   r   r   r   r   r   Z_user_idZ_app_idr   e r&   w/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/vectorstores/clarifai.py__init__#   s.    zClarifai.__init__zIterable[str]zOptional[List[dict]]zOptional[List[str]]r   z	List[str])texts	metadatasidskwargsr   c                   s  zddl m} ddlm} W n. tyJ } ztd|W Y d}~n
d}~0 0 t|}t|}	|	dkslJ d|dur|	t|ksJ d|durt|t|ksJ d|j| jd	d
}
g }t	d|	|
D ]}z||||
  }|r||||
  nd|du rdd t	t|D  n||||
   dur`g D ] }| }|
| | q> fddt|D }j|d}|| td W q ty } z$td|  t  W Y d}~qd}~0 0 q|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                 S  s   g | ]}t  jqS r&   )uuiduuid4hex).0_r&   r&   r'   
<listcomp>       z&Clarifai.add_texts.<locals>.<listcomp>c                   s0   g | ](\}}j  | |r$| nd dqS )N)Zinput_idZraw_textmetadata)Zget_text_input)r4   itextZ	batch_idsZbatch_metadatasZ	input_objZ	meta_listr&   r'   r6      s   )inputszInput posted successfully.zPost inputs failed: )Zclarifai.client.inputr-   Zgoogle.protobuf.struct_pb2r.   r"   listlenfrom_auth_helperr#   rangeupdateappend	enumerateZupload_inputsextendloggerdebug	Exceptionwarning	traceback	print_exc)r$   r)   r*   r+   r,   r-   r.   r%   ZltextslengthZ
batch_sizeZinput_job_idsidxZbatch_textsmetaZmeta_structZinput_batchZ	result_iderrorr&   r;   r'   	add_textsU   sf    



 zClarifai.add_textsstrzOptional[dict]zList[Tuple[Document, float]])querykfiltersr,   r   c              
     s   z(ddl m} ddlm} ddlm W n. tyV } ztd|W Y d}~n
d}~0 0 |p`j}|jj	|d}	d|ig}
|durd	|i}|	j
|
|gd
}n|	j
|
d}dd |D }tdd dddfdd fdd|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   Ztext_rawr8   )ranksrS   )rV   c                 S  s   g | ]}|j D ]}|qqS r&   )hits)r4   datahitr&   r&   r'   r6      r7   z9Clarifai.similarity_search_with_score.<locals>.<listcomp>
   )max_workerszresources_pb2.HitzTuple[Document, float])rY   r   c                   s     | jjj}tjj}tj| jjjj	|d}|j
|_|j}td| jdd| jj d| jj d|d d   t||d| jfS )	N)headersz	Score z.2fz for annotation: z                off input: z, text: }   )page_contentr8   )ZMessageToDictinputrX   r8   dictr#   requestsr   r:   urlapparent_encodingencodingrE   rF   Zscore
annotationidr	   )rY   r8   hrequestZrequested_text)rU   r$   r&   r'   hit_to_document   s    
z>Clarifai.similarity_search_with_score.<locals>.hit_to_documentc                   s   g | ]}  |qS r&   )submit)r4   rY   )executorri   r&   r'   r6      r7   c                 S  s   g | ]}|  qS r&   )result)r4   futurer&   r&   r'   r6      r7   )r!   r   Zclarifai_grpc.grpc.apirT   Zgoogle.protobufrU   r"   r    r?   r#   rQ   r   )r$   rQ   rR   rS   r,   r   rT   r%   r   Z
search_objZrankZsearch_metadataZsearch_responserW   futuresdocs_and_scoresr&   )rk   ri   rU   r$   r'   similarity_search_with_score   s.    


z%Clarifai.similarity_search_with_scorezList[Document])rQ   rR   r,   r   c                 K  s$   | j |fd|i|}d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
        rR   c                 S  s   g | ]\}}|qS r&   r&   )r4   docr5   r&   r&   r'   r6      r7   z.Clarifai.similarity_search.<locals>.<listcomp>)rp   )r$   rQ   rR   r,   ro   r&   r&   r'   similarity_search   s    zClarifai.similarity_searchzOptional[Embeddings])
r)   	embeddingr*   r   r   r   r   r   r,   r   c	                 K  s,   | f |||||d|	}
|
j ||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   )r)   r*   )rO   )clsr)   rs   r*   r   r   r   r   r   r,   Zclarifai_vector_dbr&   r&   r'   
from_texts   s    zClarifai.from_texts)		documentsrs   r   r   r   r   r   r,   r   c              
   K  s<   dd |D }	dd |D }
| j f |||	|||
|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                 S  s   g | ]
}|j qS r&   )r^   r4   rq   r&   r&   r'   r6   >  r7   z+Clarifai.from_documents.<locals>.<listcomp>c                 S  s   g | ]
}|j qS r&   )r8   rw   r&   r&   r'   r6   ?  r7   )r   r   r)   r   r   r*   r   )ru   )rt   rv   rs   r   r   r   r   r   r,   r)   r*   r&   r&   r'   from_documents#  s    zClarifai.from_documents)NNr   NNr   )NN)NN)N)NNNNNNN)NNNNNN)__name__
__module____qualname____doc__r(   rO   rp   rr   classmethodru   rx   r&   r&   r&   r'   r      sB         5  R  E        $)      r   )
__future__r   loggingr   rI   r1   concurrent.futuresr   typingr   r   r   r   r   ra   Zlangchain_core.documentsr	   Zlangchain_core.embeddingsr
   Zlangchain_core.vectorstoresr   	getLoggerry   rE   r   r&   r&   r&   r'   <module>   s   
