Ë
    uPêjÛ  ã                   óP   — d Z ddlmZ ddlmZmZ ddlmZmZm	Z	m
Z
  G d„ d«      Zy)	aÇ  
Vertex AI BGE (BAAI General Embedding) Configuration

BGE models deployed on Vertex AI require different input/output format:
- Request: Use "prompt" instead of "content" as the input field
- Response: Embeddings are returned directly as arrays, not wrapped in objects

Model name handling:
- Model names like "bge/endpoint_id" are automatically transformed in common_utils._get_vertex_url()
- This module focuses on request/response transformation only
é    )ÚFinal)ÚEmbeddingResponseÚUsageé   )ÚEmbeddingParametersÚTaskTypeÚTextEmbeddingBGEInputÚVertexEmbeddingRequestc            
       ó¨   — e Zd ZdZededefd„«       Zedeez  de	dede
fd„«       Ze	 	 dd	ed
edz  dedz  defd„«       Zede	dededefd„«       Zy)ÚVertexBGEConfiga"  
    Configuration and transformation logic for BGE models on Vertex AI.

    BGE (BAAI General Embedding) models use a different request format
    where the input field is named "prompt" instead of "content".

    Supported model patterns (after provider split in main.py):
    - "bge-small-en-v1.5" (model name)
    - "bge/204379420394258432" (endpoint ID pattern)

    Note: Model name transformation (bge/ -> numeric ID) is handled automatically
    in common_utils._get_vertex_url(). This class focuses on request/response format only.
    ÚmodelÚreturnc                 óP   — | j                  «       }|j                  d«      xs d|v S )ap  
        Check if the model is a BGE (BAAI General Embedding) model.

        After provider split in main.py, supports:
        - "bge-small-en-v1.5" (model name)
        - "bge/204379420394258432" (endpoint ID pattern)

        Args:
            model: The model name after provider split

        Returns:
            bool: True if the model is a BGE model
        zbge/Úbge)ÚlowerÚ
startswith)r   Úmodel_lowers     úq/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/llms/vertex_ai/vertex_embeddings/bge.pyÚis_bge_modelzVertexBGEConfig.is_bge_model(   s*   € ð #Ÿ[™[›]ˆà×%Ñ% fÓ-ÒE°¸+Ð1EÐEó    ÚinputÚoptional_paramsc                 ó  — t        «       }g }|j                  d«      }|j                  d«      }t        | t        «      r| g} | D ]+  }t        j                  |||¬«      }|j                  |«       Œ- ||d<   t        di |¤Ž|d<   |S )a‰  
        Transforms an OpenAI request to a Vertex BGE embedding request.

        BGE models use "prompt" instead of "content" as the input field.

        Args:
            input: The input text(s) to embed
            optional_params: Optional parameters for the request
            model: The model name

        Returns:
            VertexEmbeddingRequest: The transformed request
        Ú	task_typeÚtitle)Úpromptr   r   Ú	instancesÚ
parameters© )r
   ÚgetÚ
isinstanceÚstrr   Ú_create_embedding_inputÚappendr   )	r   r   r   Úvertex_requestÚ vertex_text_embedding_input_listr   r   ÚtextÚembedding_inputs	            r   Útransform_requestz!VertexBGEConfig.transform_request;   s¡   € ô 9OÓ8PˆØOQÐ(Ø,;×,?Ñ,?ÀÓ,Lˆ	Ø&×*Ñ*¨7Ó3ˆä�eœSÔ!Ø�GˆEàò 	EˆDÜ-×EÑEÈTÐ]fÐnsÐEÓtˆOØ,×3Ñ3°OÕDð	Eð 'Gˆ�{Ñ#Ü':Ñ'M¸_Ñ'Mˆ�|Ñ$àÐr   Nr   r   r   c                 ó:   — t        | ¬«      }|�||d<   |�||d<   |S )a—  
        Creates a TextEmbeddingBGEInput object for BGE models.

        BGE models use "prompt" instead of "content" as the input field.

        Args:
            prompt: The prompt to be embedded
            task_type: The type of task to be performed
            title: The title of the document to be embedded

        Returns:
            TextEmbeddingBGEInput: A TextEmbeddingBGEInput object
        )r   r   r   )r	   )r   r   r   Útext_embedding_inputs       r   r#   z'VertexBGEConfig._create_embedding_input[   s7   € ô& '<À6Ô&JÐØÐ Ø09Ð  Ñ-ØÐØ,1Ð  Ñ)Ø#Ð#r   ÚresponseÚmodel_responsec           	      óˆ  — d| vrt        d«      ‚| d   }t        |t        «      st        dt	        |«      › �«      ‚g }d}t        |«      D ]D  \  }}t        |t        «      st        d|› dt	        |«      › �«      ‚|j                  d||dœ«       ŒF d	|_        ||_        ||_	        t        |d|¬
«      }t        |d|«       |S )a¹  
        Transforms a Vertex BGE embedding response to OpenAI format.

        BGE models return embeddings directly as arrays in predictions:
        {
          "predictions": [
            [0.002, 0.021, ...],
            [0.003, 0.022, ...]
          ]
        }

        Args:
            response: The raw response from Vertex AI
            model: The model name
            model_response: The EmbeddingResponse object to populate

        Returns:
            EmbeddingResponse: The transformed response in OpenAI format

        Raises:
            KeyError: If response doesn't contain 'predictions'
            ValueError: If predictions is not a list or contains invalid data
        Úpredictionsz$Response missing 'predictions' fieldz)Expected 'predictions' to be a list, got r   zExpected embedding at index z to be a list, got Ú	embedding)ÚobjectÚindexr0   Úlist)Úprompt_tokensÚcompletion_tokensÚtotal_tokensÚusage)ÚKeyErrorr!   r3   Ú
ValueErrorÚtypeÚ	enumerater$   r1   Údatar   r   Úsetattr)	r,   r   r-   Ú_predictionsÚembedding_responseÚinput_tokensÚidxÚembedding_valuesr7   s	            r   Útransform_responsez"VertexBGEConfig.transform_responseu   sî   € ð2  Ñ(ÜÐAÓBÐBà& }Ñ5ˆä˜,¬Ô-ÜÐHÌÈlÓI[ÐH\Ð]Ó^Ð^à$&Ðàˆä%.¨|Ó%<ò 
	Ñ!ˆCÐ!ÜÐ.´Ô5Ü Ð#?À¸uÐDWÔX\Ð]mÓXnÐWoÐ!pÓqÐqà×%Ñ%à)Ø Ø!1ñõð	
	ð !'ˆÔØ0ˆÔØ$ˆÔÜ¨<È1Ð[gÔhˆÜ� ¨Ô/ØÐr   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr"   Úboolr   r3   Údictr
   r)   r   r	   r#   r   rC   r   r   r   r   r      sÜ   „ ñð ðF˜Cð F Dò Fó ðFð$ ð ¨¡ð ¸dð È3ð ÐSiò ó ðð> ð &*Ø ñ$Øð$à˜d‘?ð$ð �T‰zð$ð 
ò	$ó ð$ð2 ð5 Tð 5°#ð 5ÐGXð 5Ð]nò 5ó ñ5r   r   N)rG   Útypingr   Úlitellm.types.utilsr   r   Útypesr   r   r	   r
   r   r   r   r   ú<module>rN      s(   ðñ
õ ç 8÷ó ÷Rò Rr   