Ë
    uPêjC  ã                   ó    — d Z 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 erddlmZ ddlmZ ddlmZ dd	lmZ neZeZeZeZ G d
„ de«      Zy)z@
Auto-Routing Strategy that works with a Semantic Router Config
é    )ÚTYPE_CHECKINGÚAnyÚFinalÚOptional)Úverbose_router_logger)Ú#DEFAULT_AUTO_ROUTER_MAX_INPUT_CHARS)ÚCustomLogger©ÚSemanticRouter©ÚRoute)ÚRouter©ÚPreRoutingHookResponsec                   ó  — e Zd ZdZddefdedededddedz  d	edz  d
efd„Zdee	   fd„Z
dee	   fd„Zedeeeef      defd„«       Z	 	 	 ddededeeeef      dz  deez  dz  dedz  ded   fd„Zdddededz  fd„Zy)Ú
AutoRouterÚlocalNÚ
model_nameÚdefault_modelÚembedding_modelÚlitellm_router_instancer   Úauto_router_config_pathÚauto_router_configÚmax_input_charsc                 ó¾   — ddl m} || _        || _        | j                  | _        | j                  «       | _        d| _        || _	        || _
        || _        || _        y)a¢  
        Auto-Router class that uses a semantic router to route requests to the appropriate model.

        Args:
            model_name: The name of the model to use for the auto-router. eg. if model = "auto-router1" then us this router.
            auto_router_config_path: The path to the router config file.
            auto_router_config: The config to use for the auto-router. You can either use this or auto_router_config_path, not both.
            default_model: The default model to use if no route is found.
            embedding_model: The embedding model to use for the auto-router.
            litellm_router_instance: The instance of the LiteLLM Router.
            max_input_chars: Longest prompt, in characters, handed to the embedding model. Longer
                prompts are cut to this length so an embedding context window far smaller than the
                routed model's never fails the request.
        r   r
   N)Úsemantic_router.routersr   r   r   ÚDEFAULT_AUTO_SYNC_VALUEÚauto_sync_valueÚ_load_semantic_routing_routesÚloaded_routesÚ
routelayerr   r   r   r   )	Úselfr   r   r   r   r   r   r   r   s	            út/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/router_strategy/auto_router/auto_router.pyÚ__init__zAutoRouter.__init__   s]   € õ0 	;à3JˆÔ$Ø.@ˆÔØ#×;Ñ;ˆÔØ*.×*LÑ*LÓ*NˆÔØ15ˆŒØ*ˆÔØ$3ˆÔØ$3ˆÔØ/FˆÕ$ó    Úreturnc                 óÀ   — ddl m} | j                  r& |j                  | j                  «      j                  S | j
                  r| j                  «       S t        d«      ‚)Nr   r
   zNo router config provided)r   r   r   Ú	from_jsonÚroutesr   Ú)_load_auto_router_routes_from_config_jsonÚ
ValueError)r"   r   s     r#   r   z(AutoRouter._load_semantic_routing_routes?   sQ   € Ý:à×'Ò'Ø+�>×+Ñ+¨D×,HÑ,HÓI×PÑPÐPØ×$Ò$Ø×AÑAÓCÐCäÐ8Ó9Ð9r%   c                 ó`  — dd l }ddlm} | j                  €t	        d«      ‚g }|j                  | j                  «      }|j                  dg «      D ]Z  }|j                   ||j                  d«      |j                  d«      |j                  dg «      |j                  d«      ¬	«      «       Œ\ |S )
Nr   r   zNo auto router config providedr)   ÚnameÚdescriptionÚ
utterancesÚscore_threshold)r-   r.   r/   r0   )ÚjsonÚsemantic_router.routers.baser   r   r+   ÚloadsÚgetÚappend)r"   r1   r   Úauto_router_routesÚloaded_configÚroutes         r#   r*   z4AutoRouter._load_auto_router_routes_from_config_jsonI   s§   € Ûå6à×"Ñ"Ð*ÜÐ=Ó>Ð>Ø13ÐØ#Ÿz™z¨$×*AÑ*AÓBˆØ"×&Ñ& x°Ó4ò 	ˆEØ×%Ñ%ÙØŸ™ 6Ó*Ø %§	¡	¨-Ó 8Ø$Ÿy™y¨°rÓ:Ø$)§I¡IÐ.?Ó$@ô	õð	ð "Ð!r%   Úmessagesc                 óä   — t        | «      D ]b  }|j                  d«      dk(  sŒ|j                  d«      }|€ yt        |t        «      rdj	                  d„ |D «       «      c S t        |«      c S  y)a!  
        Extract text content from the last user message for routing.

        Handles tool-call conversations (where the last message may be an
        assistant or tool message with non-string content) and multimodal
        messages (where content is a list of content blocks).
        ÚroleÚuserÚcontentÚ ú c              3   óˆ   K  — | ]:  }t        |t        «      r(|j                  d «      dk(  r|j                  dd«      –— Œ< y­w)ÚtypeÚtextr>   N)Ú
isinstanceÚdictr4   )Ú.0Úblocks     r#   ú	<genexpr>z9AutoRouter._extract_text_from_messages.<locals>.<genexpr>l   s=   è ø€ ò $à!Ü% e¬TÔ2°u·y±yÀÓ7HÈFÒ7Rð Ÿ	™	 &¨"×-ñ$ùs   ‚A A)Úreversedr4   rC   ÚlistÚjoinÚstr)r9   Úmsgr=   s      r#   Ú_extract_text_from_messagesz&AutoRouter._extract_text_from_messages]   sw   € ô ˜HÓ%ò 	$ˆCØ�w‰w�v‹ &Ó(ØŸ'™' )Ó,�Ø�?ÙÜ˜g¤tÔ,ØŸ8™8ñ $à%,ô$ó ò ô
 ˜7“|Ò#ð	$ð r%   ÚmodelÚrequest_kwargsÚinputÚspecific_deploymentr   c              ƒ   óˆ  K  — ddl m} ddlm} ddlm} ddlm}	 |�|n	 |d|¬«      }
|
€y| j                  }|€M || j                   || j                  | j                  | j                  ¬«      | j                  ¬	«      }|| _        | j                  |
«      }| j                  ||«      } |	|xs | j                   |¬
«      S ­w)z¯
        This hook is called before the routing decision is made.

        Used for the litellm auto-router to modify the request before the routing decision is made.
        r   r
   )Úresolve_structured_messages)ÚLiteLLMRouterEncoderr   N)r9   rO   )r   r   r   )r)   ÚencoderÚ	auto_sync)rN   r9   )r   r   Ú3litellm.litellm_core_utils.prompt_templates.factoryrS   Ú3litellm.router_strategy.auto_router.litellm_encoderrT   Úlitellm.types.routerr   r!   r    r   r   r   r   rM   Ú_matched_route_namer   )r"   rN   rO   r9   rP   rQ   r   rS   rT   r   Úresolved_messagesr!   Úmessage_contentÚ
route_names                 r#   Úasync_pre_routing_hookz!AutoRouter.async_pre_routing_hookt   sÜ   è ø€ õ 	;åcõ	
õ 	@ð Ð#ñ á,°dÈ>ÔZð 	ð
 Ð$Øà—_‘_ˆ
ØÐñ (Ø×)Ñ)Ù,Ø,0×,HÑ,HØ#×3Ñ3Ø$(×$8Ñ$8ôð
 ×.Ñ.ôˆJð )ˆDŒOà!%×!AÑ!AÐBSÓ!TˆØ ×4Ñ4°ZÀÓQˆ
á%ØÒ2 × 2Ñ 2Øô
ð 	
ùs   ‚C Cr!   r   rB   c                 ó0  — ddl m} 	  ||¬«      }t        j                  d|«       t        ||«      r|j                  S t        |t        «      r|r|d   j                  S y# t        $ r+}t        j                  d|| j
                  «       Y d}~yd}~ww xY w)a‚  Name of the route `text` matches, or None when nothing matched or the match failed.

        The route layer embeds `text` to compare it against the routes, and that embedding call can
        fail (context limit, timeout, provider error). Choosing a model is a routing decision, so a
        failure here falls back to the default model rather than failing the user's request.
        r   )ÚRouteChoice)rB   zJAutoRouter: semantic routing failed (%s), falling back to default model %sNzroute_choice: %s)
Úsemantic_router.schemar`   Ú	Exceptionr   Úwarningr   ÚdebugrC   r-   rI   )r"   r!   rB   r`   Úroute_choiceÚes         r#   rZ   zAutoRouter._matched_route_name©   s’   € õ 	7ð	Ù",°$Ô"7ˆLô 	×#Ñ#Ð$6¸ÔEÜ�l KÔ0Ø×$Ñ$Ð$Ü�l¤DÔ)©lØ ‘?×'Ñ'Ð'Øøô ò 	Ü!×)Ñ)Ø\Ð^_Ðae×asÑasôô ûð		ús   ˆ	A! Á!	BÁ*!BÂB)NNF)Ú__name__Ú
__module__Ú__qualname__r   r   rK   Úintr$   rI   r   r   r*   ÚstaticmethodrD   r   rM   Úboolr   r^   rZ   © r%   r#   r   r      sL  „ Ø%Ðð /3Ø)-ØBñ"Gàð"Gð ð"Gð ð	"Gð
 "*ð"Gð "% t¡ð"Gð   $™Jð"Gð ó"GðH:¨t°E©{ó :ð"¸4À¹;ó "ð( ð¨d°4¸¸S¸±>Ñ.Bð Àsò ó ðð4 15Ø#'Ø+0ñ3
àð3
ð ð3
ð �t˜C ˜H‘~Ñ&¨Ñ-ð	3
ð
 �T‰z˜DÑ ð3
ð " D™[ð3
ð 
Ð*Ñ	+ó3
ðjÐ.>ð Àcð ÈcÐTXÉjô r%   r   N)Ú__doc__Útypingr   r   r   r   Úlitellm._loggingr   Úlitellm.constantsr   Ú"litellm.integrations.custom_loggerr	   r   r   r2   r   Úlitellm.routerr   rY   r   r   rm   r%   r#   ú<module>rt      sM   ðñ÷ 7Ó 6å 2Ý AÝ ;áÝ6Ý2å%Þ;à€FØ ÐØ€EØ€Nôf�õ fr%   