Ë
    uPêjÎ  ã                   ó~   — d 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
 erddlmZ ddlmZ ddlmZ  G d	„ d
e«      Zy)z¼
Cohere Rerank Handler for Unified Guardrails

This module provides guardrail translation support for the rerank endpoint.
The handler processes only the 'query' parameter for guardrails.
é    )ÚTYPE_CHECKINGÚAnyÚFinal)Úverbose_proxy_logger)ÚBaseTranslation)ÚGenericGuardrailAPIInputs)ÚCustomGuardrail)ÚLogging)ÚRerankResponsec                   ód   — e Zd ZdZdZ	 ddeddddd	efd
„Z	 	 	 ddddddddedz  dedz  d	efd„Zy)ÚCohereRerankHandleraÔ  
    Handler for processing rerank requests with guardrails.

    This class provides methods to:
    1. Process input query (pre-call hook)
    2. Process output response (post-call hook) - not applicable for rerank

    The handler specifically processes:
    - The 'query' parameter (string)
    - The 'instruction' parameter (string), when present

    Note: Documents are not processed by guardrails as they are the corpus
    being searched, not user input.
    )ÚqueryÚinstructionNÚdataÚguardrail_to_applyr	   Úlitellm_logging_objzLiteLLMLoggingObj | NoneÚreturnc           
   ƒ   óV  K  — | j                   D �cg c])  }t        |j                  |«      t        «      sŒ#|||   f‘Œ+ }}|st	        j
                  d«       |S t        |D ��cg c]  \  }}|‘Œ	 c}}¬«      }|j                  d«      }	|	r|	|d<   |j                  ||d|¬«      ƒ d{  –—† }
|
j                  dg «      }t        |«      D ]L  \  }\  }}|t        |«      k  sŒ||   ||<   t	        j
                  d|t        |«      t        ||   «      «       ŒN |S c c}w c c}}w 7 Œ}­w)	a¤  
        Process input text fields ('query' and 'instruction') by applying
        guardrails and writing the sanitized values back.

        Args:
            data: Request data dictionary containing 'query' and optionally
                'instruction'
            guardrail_to_apply: The guardrail instance to apply

        Returns:
            Modified data with guardrails applied to query/instruction only
        z6Rerank: No query/instruction to process or not strings)ÚtextsÚmodelÚrequest)ÚinputsÚrequest_dataÚ
input_typeÚlogging_objNr   zDRerank: Applied guardrail to %s. Original length: %d, New length: %d)
Ú_SCANNED_FIELDSÚ
isinstanceÚgetÚstrr   Údebugr   Úapply_guardrailÚ	enumerateÚlen)Úselfr   r   r   ÚkeyÚfields_to_scanÚ_Úvaluer   r   Úguardrailed_inputsÚguardrailed_textsÚidxÚoriginals                 ú}/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/cohere/rerank/guardrail_translation/handler.pyÚprocess_input_messagesz*CohereRerankHandler.process_input_messages*   sC  è ø€ ð( >B×=QÑ=QÖ t°cÔU_Ð`d×`hÑ`hÐilÓ`mÔorÕUs # t¨C¡yÒ!1Ð tˆÐ tÙÜ ×&Ñ&Ð'_Ô`ØˆKä1È~×8^Á8À1ÀeºÓ8^Ô_ˆà—x‘x Ó(ˆÙØ#ˆF�7‰OØ*<×*LÑ*LØØØ Ø+ð	 +Mó +
÷ %
Ðð $6×#9Ñ#9¸'À2Ó#FÐä$-¨nÓ$=ò 
	Ñ ˆC‘�#�xð ”SÐ*Ó+Ó+Ø-¨cÑ2��S‘	Ü$×*Ñ*ØZØÜ˜“MÜ˜˜S™	“Nõ	ð
	ð ˆùò= !uùó
 9_ð
%
ús3   ‚D)‘$D¶	D¿%D)Á$D!
Á09D)Â)D'Â*8D)Ã#AD)Úresponser   Úuser_api_key_dictr   c              ƒ   ó8   K  — t        j                  d«       |S ­w)aY  
        Process output response - not applicable for rerank.

        Rerank responses contain relevance scores and indices, not text,
        so there's nothing to apply guardrails to. This method returns
        the response unchanged.

        Args:
            response: Rerank response object with rankings
            guardrail_to_apply: The guardrail instance (unused)
            litellm_logging_obj: Optional logging object (unused)
            user_api_key_dict: User API key metadata (unused)

        Returns:
            Unmodified response (rankings don't need text guardrails)
        zURerank: Output processing not applicable (output contains relevance scores, not text))r   r    )r$   r/   r   r   r0   r   s         r-   Úprocess_output_responsez+CohereRerankHandler.process_output_response^   s    è ø€ ô0 	×"Ñ"Øcô	
ð ˆùs   ‚)N)NNN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Údictr   r.   r2   © ó    r-   r   r      s‘   „ ñð& /€Oð ;?ñ	2àð2ð .ð2ð 8ð	2ð
 
ó2ðp ;?Ø(,Ø$(ñà"ðð .ðð 8ð	ð
  ™:ðð ˜T‘kðð 
ôr9   r   N)r6   Útypingr   r   r   Úlitellm._loggingr   Ú<litellm.llms.base_llm.guardrail_translation.base_translationr   Úlitellm.types.utilsr   Ú%litellm.integrations.custom_guardrailr	   Ú*litellm.litellm_core_utils.litellm_loggingr
   ÚLiteLLMLoggingObjÚlitellm.types.rerankr   r   r8   r9   r-   ú<module>rB      s5   ðñ÷ -Ñ ,å 1Ý XÝ 9áÝEÝWÝ3ôe˜/õ er9   