a
    •˜ëjŽ  ã                   @  sª   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d d	lmZmZ d d
lmZ edddd�G dd„ deƒƒZdS )é    )Úannotations)ÚSequence)Údeepcopy)ÚAnyÚOptionalÚUnion)Ú
deprecated)Ú	Callbacks)ÚBaseDocumentCompressorÚDocument)Úget_from_dict_or_env)Ú
ConfigDictÚmodel_validator)Úoverridez0.0.30z1.0zlangchain_cohere.CohereRerank)ZsinceZremovalZalternative_importc                   @  s´   e Zd ZU dZdZded< dZded< dZd	ed
< dZded< dZ	d	ed< e
ddd�Zedd�edddœdd„ƒƒZddddœdd	dddddœdd„Zed$dd	d dd!œd"d#„ƒZdS )%ÚCohereRerankz2Document compressor that uses `Cohere Rerank API`.Nr   Úclienté   zOptional[int]Útop_nzrerank-english-v2.0ÚstrÚmodelzOptional[str]Úcohere_api_keyÚ	langchainÚ
user_agentTZforbid)Zarbitrary_types_allowedÚextraÚbefore)ÚmodeÚdict)ÚvaluesÚreturnc              
   C  sx   |  d¡stzddl}W n2 tyH } zd}t|ƒ|‚W Y d}~n
d}~0 0 t|ddƒ}|  dd¡}|j||d	�|d< |S )
z?Validate that api key and python package exists in environment.r   r   NzTCould not import cohere python package. Please install it with `pip install cohere`.r   ZCOHERE_API_KEYr   r   )Úclient_name)ÚgetÚcohereÚImportErrorr   ZClient)Úclsr   r!   ÚeÚmsgr   r   © r&   úv/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/langchain/retrievers/document_compressors/cohere_rerank.pyÚvalidate_environment(   s    
ÿ ýz!CohereRerank.validate_environmentéÿÿÿÿ)r   r   Úmax_chunks_per_docz$Sequence[Union[str, Document, dict]]zlist[dict[str, Any]])Ú	documentsÚqueryr   r   r*   r   c                C  sv   t |ƒdkrg S dd„ |D ƒ}|p&| j}|du s8|dkr<|n| j}| jj|||||d�}t|dƒrh|j}dd„ |D ƒS )a÷  Returns an ordered list of documents ordered by their relevance to the provided query.

        Args:
            query: The query to use for reranking.
            documents: A sequence of documents to rerank.
            model: The model to use for re-ranking. Default to self.model.
            top_n : The number of results to return. If None returns all results.
                Defaults to self.top_n.
            max_chunks_per_doc : The maximum number of chunks derived from a document.
        r   c                 S  s    g | ]}t |tƒr|jn|‘qS r&   )Ú
isinstancer   Úpage_content)Ú.0Údocr&   r&   r'   Ú
<listcomp>S   s   z'CohereRerank.rerank.<locals>.<listcomp>N)r,   r+   r   r   r*   Úresultsc                 S  s   g | ]}|j |jd œ‘qS )©ÚindexÚrelevance_scorer3   )r/   Úresr&   r&   r'   r1   a   s   ÿ)Úlenr   r   r   ÚrerankÚhasattrr2   )Úselfr+   r,   r   r   r*   Zdocsr2   r&   r&   r'   r8   >   s&    ÿ
û
þzCohereRerank.rerankzSequence[Document]zOptional[Callbacks])r+   r,   Ú	callbacksr   c                 C  sR   g }|   ||¡D ]<}||d  }t|jt|jƒd�}|d |jd< | |¡ q|S )aN  Compress documents using Cohere's rerank API.

        Args:
            documents: A sequence of documents to compress.
            query: The query to use for compressing the documents.
            callbacks: Callbacks to run during the compression process.

        Returns:
            A sequence of compressed documents.
        r4   )Úmetadatar5   )r8   r   r.   r   r<   Úappend)r:   r+   r,   r;   Ú
compressedr6   r0   Zdoc_copyr&   r&   r'   Úcompress_documentsf   s    zCohereRerank.compress_documents)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r   r   r   Zmodel_configr   Úclassmethodr(   r8   r   r?   r&   r&   r&   r'   r      s(   
þù( ür   N)Ú
__future__r   Úcollections.abcr   Úcopyr   Útypingr   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr	   Zlangchain_core.documentsr
   r   Zlangchain_core.utilsr   Zpydanticr   r   Ztyping_extensionsr   r   r&   r&   r&   r'   Ú<module>   s   ý