Ë
    uPêj  ã                   óÈ   — d dl mZmZmZmZ d dlmZ d dlmZ d dl	m
Z
 d dlZd dlmZ erd dlmZ neZdej                   d	eee      fd
„Z G d„ de«      Z G d„ dee
«      Zy)é    )ÚTYPE_CHECKINGÚAnyÚFinalÚOptional)Ú
ConfigDict)ÚDenseEncoder)ÚAsymmetricDenseMixinN)Úverbose_router_logger)ÚRouterÚembedsÚreturnc                 ó¬   — | r&t        | t        j                  «      r| j                  st	        d«      ‚| j                  D �cg c]  }|d   ‘Œ	 c}S c c}w )z™Convert a LiteLLM embedding response to a list of embeddings.

    :param embeds: The LiteLLM embedding response.
    :return: A list of embeddings.
    z2No embeddings found in LiteLLM embedding response.Ú	embedding)Ú
isinstanceÚlitellmÚEmbeddingResponseÚdataÚ
ValueError)r   Úxs     úx/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/router_strategy/auto_router/litellm_encoder.pyÚlitellm_to_listr      sD   € ñ œ F¬G×,EÑ,EÔFÈfÏkÊkÜÐMÓNÐNØ$*§K¡KÖ0˜qˆAˆk‹NÒ0Ð0ùÒ0s   ÁAc                   ó>   ‡ — e Zd Z ed¬«      Zdded   fˆ fd„Zˆ xZS )ÚCustomDenseEncoderÚallow)ÚextraÚlitellm_router_instancer   c                 ó\   •— d|v r|j                  d«      }t        ‰| �  di |¤Ž || _        y )Nr   © )ÚpopÚsuperÚ__init__r   )Úselfr   ÚkwargsÚ	__class__s      €r   r!   zCustomDenseEncoder.__init__   s3   ø€ à$¨Ñ.Ø&,§j¡jÐ1JÓ&KÐ#ä‰ÑÑ"˜6Ò"Ø'>ˆÕ$ó    )N)Ú__name__Ú
__module__Ú__qualname__r   Úmodel_configr   r!   Ú__classcell__©r$   s   @r   r   r      s#   ø„ Ù GÔ,€Lñ?°¸Ñ0B÷ ?ñ ?r%   r   c            	       ó8  ‡ — e Zd ZU dZdZeed<   	 	 ddddededz  d	efˆ fd
„Z	de
e   de
e   fd„Zde
e   de
e
e      fd„Zde
e   de
e
e      fd„Zde
e   de
e
e      fd„Zde
e   de
e
e      fd„Zde
e   de
e
e      fd„Zde
e   de
e
e      fd„Zˆ xZS )ÚLiteLLMRouterEncoderai  LiteLLM encoder class for generating embeddings using LiteLLM.

    The LiteLLMRouterEncoder class is a subclass of DenseEncoder and utilizes the LiteLLM Router SDK
    to generate embeddings for given documents. It supports all encoders supported by LiteLLM
    and supports customization of the score threshold for filtering or processing the embeddings.
    Úinternal_litellm_routerÚtypeNr   r   Ú
model_nameÚscore_thresholdÚmax_input_charsc                 óZ   •— t         ‰| �  ||�|nd¬«       || _        || _        || _        y)a2  Initialize the LiteLLMEncoder.

        :param litellm_router_instance: The instance of the LiteLLM Router.
        :type litellm_router_instance: Router
        :param model_name: The name of the embedding model to use. Must use LiteLLM naming
            convention (e.g. "openai/text-embedding-3-small" or "mistral/mistral-embed").
        :type model_name: str
        :param score_threshold: The score threshold for the embeddings.
        :type score_threshold: float
        :param max_input_chars: Longest doc, in characters, sent to the embedding model; anything
            longer is cut to this length. Opt-in, defaulting to 0 (send docs whole), because a
            caller that has to see the entire text to be correct must not be cut silently: a
            semantic guard that inspects only a prefix can be walked past with a benign opener.
            Callers that only rank text against short utterances, such as the auto-router, pass a
            limit so the encoder's context window cannot fail their request.
        :type max_input_chars: int
        Ng333333Ó?)Únamer1   )r    r!   r0   r   r2   )r"   r   r0   r1   r2   r$   s        €r   r!   zLiteLLMRouterEncoder.__init__1   s>   ø€ ô0 	‰ÑØØ/>Ð/J™OÐPSð 	ô 	
ð %ˆŒØ'>ˆÔ$Ø.ˆÕr%   Údocsr   c                 ó¦   — | j                   }|dk  r|S |D �cg c]  }|d| ‘Œ	 }}||k7  r!t        j                  d|| j                  «       |S c c}w )zùCut every doc to `max_input_chars`, keeping the head, or return them whole when unset.

        The head carries the ask often enough to place it against a route, and a cut prompt still
        routes where an over-long one just errors out.
        r   NzBLiteLLMRouterEncoder: cut input to %s chars for embedding model %s)r2   r
   Údebugr0   )r"   r5   ÚlimitÚdocÚclampeds        r   Ú_clampzLiteLLMRouterEncoder._clampQ   sd   € ð ×+Ñ+ˆØ�AŠ:ØˆKØ15Ö6¨#˜#˜f˜uš+Ð6ˆÐ6Ø�dŠ?Ü!×'Ñ'ØTÐV[Ð]a×]lÑ]lôð ˆùò 7s   ˜Ac                 ó(   —  | j                   |fi |¤ŽS )z­Encode a list of text documents into embeddings using LiteLLM.

        :param docs: List of text documents to encode.
        :return: List of embeddings for each document.)Úencode_queries©r"   r5   r#   s      r   Ú__call__zLiteLLMRouterEncoder.__call__a   s   € ð
 #ˆt×"Ñ" 4Ñ2¨6Ñ2Ð2r%   c              ‹   óD   K  —  | j                   |fi |¤Žƒ d{  –—† S 7 Œ­w)z²Encode a list of documents into embeddings using LiteLLM asynchronously.

        :param docs: List of documents to encode.
        :return: List of embeddings for each document.N)Úaencode_queriesr>   s      r   ÚacallzLiteLLMRouterEncoder.acallh   s&   è ø€ ð
 *�T×)Ñ)¨$Ñ9°&Ñ9×9Ð9Ð9ús   ‚ ™š c                 ó,  — | j                   €t        d«      ‚	  | j                   j                  d| j                  |«      | j                  dœ|¤Ž}t        |«      S # t        $ r.}t        | j                  j                  «       › d|› �«      |‚d }~ww xY w©Nz"litellm_router_instance is not set)ÚinputÚmodelz API call failed. Error: r   ©	r   r   r   r;   r0   r   Ú	Exceptionr/   Ú
capitalize©r"   r5   r#   r   Úes        r   r=   z#LiteLLMRouterEncoder.encode_querieso   ó    € Ø×'Ñ'Ð/ÜÐAÓBÐBð	]ØB˜D×8Ñ8×BÑBð Ø—k‘k $Ó'¨t¯©ñØBHñˆFô # 6Ó*Ð*øÜò 	]Ü §	¡	× 4Ñ 4Ó 6Ð7Ð7PÐQRÐPSÐTÓUÐ[\Ð\ûð	]úó   ™AA Á	BÁ%)BÂBc                 ó,  — | j                   €t        d«      ‚	  | j                   j                  d| j                  |«      | j                  dœ|¤Ž}t        |«      S # t        $ r.}t        | j                  j                  «       › d|› �«      |‚d }~ww xY wrD   rG   rJ   s        r   Úencode_documentsz%LiteLLMRouterEncoder.encode_documentsz   rL   rM   c              ‹   óH  K  — | j                   €t        d«      ‚	  | j                   j                  d| j                  |«      | j                  dœ|¤Žƒ d {  –—† }t        |«      S 7 Œ# t        $ r.}t        | j                  j                  «       › d|› �«      |‚d }~ww xY w­wrD   ©	r   r   Ú
aembeddingr;   r0   r   rH   r/   rI   rJ   s        r   rA   z$LiteLLMRouterEncoder.aencode_queries…   ó¯   è ø€ Ø×'Ñ'Ð/ÜÐAÓBÐBð	]Ø"I $×">Ñ">×"IÑ"Ið #Ø—k‘k $Ó'¨t¯©ñ#ØBHñ#÷ ˆFô # 6Ó*Ð*ðùô ò 	]Ü §	¡	× 4Ñ 4Ó 6Ð7Ð7PÐQRÐPSÐTÓUÐ[\Ð\ûð	]üó:   ‚B"›;A( ÁA&ÁA( Á%B"Á&A( Á(	BÁ1)BÂBÂB"c              ‹   óH  K  — | j                   €t        d«      ‚	  | j                   j                  d| j                  |«      | j                  dœ|¤Žƒ d {  –—† }t        |«      S 7 Œ# t        $ r.}t        | j                  j                  «       › d|› �«      |‚d }~ww xY w­wrD   rQ   rJ   s        r   Úaencode_documentsz&LiteLLMRouterEncoder.aencode_documents�   rS   rT   )Nr   )r&   r'   r(   Ú__doc__r/   ÚstrÚ__annotations__ÚfloatÚintr!   Úlistr;   r   r?   rB   r=   rO   rA   rV   r*   r+   s   @r   r-   r-   '   s3  ø… ñð *€Dˆ#Ó)ð )-Ø ñ/à!)ð/ð ð/ð  ™ð	/ð
 õ/ð@˜4 ™9ð ¨¨c©ó ð 3˜T #™Yð 3°T¸$¸u¹+Ñ5Fó 3ð:  S¡	ð :¸¸TÀ%¹[Ñ8Ió :ð	] 4¨¡9ð 	]¸4ÀÀUÁÑ;Ló 	]ð	] T¨#¡Yð 	]¸TÀ$ÀuÁ+Ñ=Nó 	]ð	]¨$¨s©)ð 	]À$ÀtÈEÁ{ÑBSó 	]ð	]¨D°©Ið 	]ÀDÈÈeÉÑDU÷ 	]r%   r-   )Útypingr   r   r   r   Úpydanticr   Úsemantic_router.encodersr   Úsemantic_router.encoders.baser	   r   Úlitellm._loggingr
   Úlitellm.routerr   r   r\   rZ   r   r   r-   r   r%   r   ú<module>rc      sg   ðß 6Ó 6å Ý 1Ý >ã Ý 2áÞ%à€Fð1˜G×5Ñ5ð 1¸$¸tÀE¹{Ñ:Kó 1ô	?˜ô 	?ôr]Ð-Ð/Cõ r]r%   