o
    Zh?/                     @  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Z		d0d1ddZ		d0d2d"d#Z	d3d4d%d&Ze							d5d6d)d*Z	e						d7d8d,d-Z
dS )9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              
   C  s   |pt jd}|pt jd}|du s|du rtd|| _zddlm}	 W n ty8 }
 ztd|
d}
~
ww |	||||||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(   `/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/vectorstores/clarifai.py__init__#   s6   zClarifai.__init__textsIterable[str]	metadatasOptional[List[dict]]idsOptional[List[str]]kwargsr   	List[str]c                   s  zddl m} ddlm} W n ty } ztd|d}~ww t|}t|}	|	dks0J d|dur>|	t|ks>J d|durNt|t|ksNJ d|j| jd	d
}
g }t	d|	|
D ]}zd||||
  }|rt||||
  nd|du rdd t	t|D  n||||
   durg D ]}| }|
| | q fddt|D }j|d}|| td W q_ ty } ztd|  t  W Y d}~q_d}~w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                 S  s   g | ]}t  jqS r(   )uuiduuid4hex).0_r(   r(   r)   
<listcomp>   s    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)r:   itextZ	batch_idsZbatch_metadatasZ	input_objZ	meta_listr(   r)   r<      s    )ZinputszInput posted successfully.zPost inputs failed: )Zclarifai.client.inputr3   Zgoogle.protobuf.struct_pb2r4   r$   listlenfrom_auth_helperr%   rangeupdateappend	enumerateZupload_inputsextendloggerdebug	Exceptionwarning	traceback	print_exc)r&   r+   r-   r/   r1   r3   r4   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querystrkfiltersOptional[dict]List[Tuple[Document, float]]c              
     s   zddl m} ddlm} ddlm W n ty% } ztd|d}~ww |p*j}|jj	|d}	d|ig}
|durId	|i}|	j
|
|gd
}n|	j
|
d}dd |D }t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)r5   r   Ztext_rawr=   )ranksrW   )r\   c                 S  s   g | ]
}|j D ]}|qqS r(   )hits)r:   datahitr(   r(   r)   r<      s    z9Clarifai.similarity_search_with_score.<locals>.<listcomp>
   )max_workersr_   resources_pb2.Hitr   Tuple[Document, float]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_contentr=   )ZMessageToDictinputr^   r=   dictr%   requestsr    r?   urlapparent_encodingencodingrI   rJ   Zscore
annotationidr	   )r_   r=   hrequestZrequested_text)r[   r&   r(   r)   hit_to_document   s   
z>Clarifai.similarity_search_with_score.<locals>.hit_to_documentc                   s   g | ]}  |qS r(   )submit)r:   r_   )executorrq   r(   r)   r<      s    c                 S  s   g | ]}|  qS r(   )result)r:   futurer(   r(   r)   r<          )r_   rb   r   rc   )r#   r   Zclarifai_grpc.grpc.apirZ   Zgoogle.protobufr[   r$   r"   rC   r%   rT   r   )r&   rT   rV   rW   r1   r   rZ   r'   r   Z
search_objZrankZsearch_metadataZsearch_responser]   futuresdocs_and_scoresr(   )rs   rq   r[   r&   r)   similarity_search_with_score   s2   


z%Clarifai.similarity_search_with_scoreList[Document]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
        rV   c                 S  s   g | ]\}}|qS r(   r(   )r:   docr;   r(   r(   r)   r<      rv   z.Clarifai.similarity_search.<locals>.<listcomp>)ry   )r&   rT   rV   r1   rx   r(   r(   r)   similarity_search   s   zClarifai.similarity_search	embeddingOptional[Embeddings]c	                 K  s,   | d|||||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-   Nr(   )rS   )clsr+   r}   r-   r   r   r   r   r   r1   Zclarifai_vector_dbr(   r(   r)   
from_texts   s   zClarifai.from_texts	documentsc              
   K  s<   dd |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                 S     g | ]}|j qS r(   )rf   r:   r{   r(   r(   r)   r<   >      z+Clarifai.from_documents.<locals>.<listcomp>c                 S  r   r(   )r=   r   r(   r(   r)   r<   ?  r   )r   r   r+   r   r   r-   r   Nr(   )r   )r   r   r}   r   r   r   r   r   r1   r+   r-   r(   r(   r)   from_documents#  s   zClarifai.from_documents)NNr   NNr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NN)
r+   r,   r-   r.   r/   r0   r1   r   r   r2   )
rT   rU   rV   r   rW   rX   r1   r   r   rY   )N)rT   rU   rV   r   r1   r   r   rz   )NNNNNNN)r+   r2   r}   r~   r-   r.   r   r   r   r   r   r   r   r   r   r   r1   r   r   r   )NNNNNN)r   rz   r}   r~   r   r   r   r   r   r   r   r   r   r   r1   r   r   r   )__name__
__module____qualname____doc__r*   rS   ry   r|   classmethodr   r   r(   r(   r(   r)   r      sD    5RE)r   )
__future__r   loggingr   rM   r7   concurrent.futuresr   typingr   r   r   r   r   ri   Zlangchain_core.documentsr	   Zlangchain_core.embeddingsr
   Zlangchain_core.vectorstoresr   	getLoggerr   rI   r   r(   r(   r(   r)   <module>   s    
