Ë
    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Ä
OpenAI Embeddings Handler for Unified Guardrails

This module provides guardrail translation support for OpenAI's embeddings endpoint.
The handler processes the 'input' parameter for guardrails.
é    )ÚTYPE_CHECKINGÚAnyÚFinal)Úverbose_proxy_logger)ÚBaseTranslation)ÚGenericGuardrailAPIInputs)ÚCustomGuardrail)ÚLogging)ÚEmbeddingResponsec                   ó°   — e Zd ZdZ	 ddedddddefd	„Zded
edddddef
d„Zded
e	ee
z  e	e
   z     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)ÚOpenAIEmbeddingsHandlera%  
    Handler for processing OpenAI embeddings requests with guardrails.

    This class provides methods to:
    1. Process input text (pre-call hook)
    2. Process output response (post-call hook) - embeddings don't typically need output guardrails

    The handler specifically processes the 'input' parameter which can be:
    - A single string
    - A list of strings (for batch embeddings)
    - A list of integers (token IDs - not processed by guardrails)
    - A list of lists of integers (batch token IDs - not processed by guardrails)
    NÚdataÚguardrail_to_applyr	   Úlitellm_logging_objzLiteLLMLoggingObj | NoneÚreturnc              ƒ   ó`  K  — |j                  d«      }|€t        j                  d«       |S t        |t        «      r| j                  ||||«      ƒ d{  –—† }|S t        |t        «      r| j                  ||||«      ƒ d{  –—† }|S t        j                  dt        |«      «       |S 7 ŒU7 Œ)­w)ah  
        Process input text by applying guardrails to text content.

        Args:
            data: Request data dictionary containing 'input' parameter
            guardrail_to_apply: The guardrail instance to apply
            litellm_logging_obj: Optional logging object

        Returns:
            Modified data with guardrails applied to input
        ÚinputNz1OpenAI Embeddings: No input found in request datazFOpenAI Embeddings: Unexpected input type: %s. Expected string or list.)
Úgetr   ÚdebugÚ
isinstanceÚstrÚ_process_string_inputÚlistÚ_process_list_inputÚwarningÚtype)Úselfr   r   r   Ú
input_datas        ú�/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/openai/embeddings/guardrail_translation/handler.pyÚprocess_input_messagesz.OpenAIEmbeddingsHandler.process_input_messages#   s¶   è ø€ ð" !ŸH™H WÓ-ˆ
ØÐÜ ×&Ñ&Ð'ZÔ[ØˆKä�j¤#Ô&Ø×3Ñ3°D¸*ÐFXÐZmÓn×nˆDð ˆô ˜
¤DÔ)Ø×1Ñ1°$¸
ÐDVÐXkÓl×lˆDð ˆô !×(Ñ(ØXÜ�ZÓ ôð
 ˆð oøàlús$   ‚AB.ÁB*Á-B.ÂB,Â(B.Â,B.r   c              ƒ   ó$  K  — t        |g¬«      }|j                  d«      x}r||d<   |j                  ||d|¬«      ƒ d{  –—† }|j                  d«      x}r4|d   |d<   t        j                  d	t        |«      t        |d   «      «       |S 7 ŒM­w)
z4Process a single string input through the guardrail.©ÚtextsÚmodelÚrequest©ÚinputsÚrequest_dataÚ
input_typeÚlogging_objNr#   r   r   zYOpenAI Embeddings: Applied guardrail to string input. Original length: %d, New length: %d)r   r   Úapply_guardrailr   r   Úlen)	r   r   r   r   r   r'   r$   Úguardrailed_inputsÚguardrailed_textss	            r   r   z-OpenAIEmbeddingsHandler._process_string_inputE   s°   è ø€ ô 2¸¸ÔEˆØ—H‘H˜WÓ%Ð%ˆ5Ð%Ø#ˆF�7‰Oà*<×*LÑ*LØØØ Ø+ð	 +Mó +
÷ %
Ðð !3× 6Ñ 6°wÓ ?Ð?ÐÐ?Ø-¨aÑ0ˆD�‰MÜ ×&Ñ&ØkÜ�J“Ü�D˜‘MÓ"ôð ˆð%
ús   ‚>BÁ BÁABc              ƒ   óè  K  — t        |«      dk(  r|S |d   }t        |t        t        f«      rt	        j
                  d«       |S t        |t        «      s!t	        j                  dt        |«      «       |S t        |¬«      }|j                  d«      x}r||d<   |j                  ||d|¬«      ƒ d{  –—† }|j                  d	«      x}	r$|	|d
<   t	        j
                  dt        |	«      «       |S 7 Œ=­w)zDProcess a list input through the guardrail (if it contains strings).r   zDOpenAI Embeddings: Input is token IDs, skipping guardrail processingz6OpenAI Embeddings: Unexpected input list item type: %sr"   r$   r%   r&   Nr#   r   z1OpenAI Embeddings: Applied guardrail to %d inputs)r,   r   Úintr   r   r   r   r   r   r   r   r+   )
r   r   r   r   r   Ú
first_itemr'   r$   r-   r.   s
             r   r   z+OpenAIEmbeddingsHandler._process_list_inputb   s	  è ø€ ô ˆz‹?˜aÒØˆKà& q™Mˆ
ô �j¤3¬ +Ô.Ü ×&Ñ&Ð'mÔnØˆKä˜*¤cÔ*Ü ×(Ñ(ØHÜ�ZÓ ôð ˆKô 2¸
ÔCˆØ—H‘H˜WÓ%Ð%ˆ5Ð%Ø#ˆF�7‰Oà*<×*LÑ*LØØØ Ø+ð	 +Mó +
÷ %
Ðð !3× 6Ñ 6°wÓ ?Ð?ÐÐ?Ø-ˆD�‰MÜ ×&Ñ&ØCÜÐ%Ó&ôð
 ˆð%
ús   ‚B0C2Â2C0Â3>C2Úresponser   Úuser_api_key_dictr(   c              ƒ   ó8   K  — t        j                  d«       |S ­w)ap  
        Process output response - embeddings responses contain vectors, not text.

        For embeddings, the output is numerical vectors, so there's typically
        no text content to apply guardrails to. This method is a no-op but
        is included for interface consistency.

        Args:
            response: Embedding response object
            guardrail_to_apply: The guardrail instance to apply
            litellm_logging_obj: Optional logging object
            user_api_key_dict: User API key metadata

        Returns:
            Unmodified response (embeddings don't have text output to guard)
        z\OpenAI Embeddings: Output response processing skipped - embeddings contain vectors, not text)r   r   )r   r2   r   r   r3   r(   s         r   Úprocess_output_responsez/OpenAIEmbeddingsHandler.process_output_response�   s    è ø€ ô0 	×"Ñ"Øjô	
ð ˆùs   ‚)N)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údictr   r    r   r   r   r0   r   r5   © ó    r   r   r      s  „ ñð$ ;?ñ	 àð ð .ð ð 8ð	 ð
 
ó ðDàðð ðð .ð	ð
 8ðð 
óð:,àð,ð ˜˜s™ T¨#¡YÑ.Ñ/ð,ð .ð	,ð
 8ð,ð 
ó,ðd ;?Ø(,Ø$(ñà%ðð .ðð 8ð	ð
  ™:ðð ˜T‘kðð 
ôr<   r   N)r9   Ú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
   ÚLiteLLMLoggingObjr   r   r;   r<   r   ú<module>rD      s5   ðñ÷ -Ñ ,å 1Ý XÝ 9áÝEÝWÝ5ôW˜oõ Wr<   