
    h                         d Z ddlmZmZmZmZmZ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 n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DictListOptionalUnion)verbose_router_logger)CustomLoggerRoute)RouterPreRoutingHookResponsec                       e Zd ZdZ	 	 ddedededddee   d	ee   fd
Zdee   fdZ	dee   fdZ
	 	 	 ddededeeeeef         deeeef      dee   ded   fdZy)
AutoRouterlocalN
model_namedefault_modelembedding_modellitellm_router_instancer   auto_router_config_pathauto_router_configc                     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.
        r   SemanticRouterN)semantic_router.routersr   r   r   DEFAULT_AUTO_SYNC_VALUEauto_sync_value_load_semantic_routing_routesloaded_routes
routelayerr   r   r   )selfr   r   r   r   r   r   r   s           k/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/router_strategy/auto_router/auto_router.py__init__zAutoRouter.__init__   sU    ( 	;6M$1C#;;*.*L*L*N48*$31H$    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_routes5   sO    :''!++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_json?   s    6""*=>>*,

4#:#:;"&&x4E%%6* %		- 8$yyr:$)II.?$@	 5 "!r%   modelrequest_kwargsmessagesinputspecific_deploymentr   c                   K   ddl m} ddlm} ddlm} ddlm}	 |y| j                  @ || j                   || j                  | j                        | j                        | _        |d	   }
|
j                  d
d      }| j                  |      }t        j                  d|        t!        ||      r|j"                  xs | j$                  }n-t!        |t&              r|d   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   )RouteChoice)LiteLLMRouterEncoderr   N)r   r   )r)   encoder	auto_synccontent )textzroute_choice: )r9   r;   )r   r   semantic_router.schemar?   3litellm.router_strategy.auto_router.litellm_encoderr@   litellm.types.routerr   r!   r    r   r   r   r4   r	   debug
isinstancer-   r   list)r"   r9   r:   r;   r<   r=   r   r?   r@   r   user_messagemessage_contentroute_choices                r#   async_pre_routing_hookz!AutoRouter.async_pre_routing_hookT   s     	;6	
 	@??" ---0040L0L#'#7#7 #22DO (0|+//	2>HL]lHm##n\N$CDlK0 %%;););Ed+ O((>D,>,>E%
 	
s   DD	)NN)NNF)__name__
__module____qualname__r   strr   r$   r   r   r   r*   r   r   boolrP    r%   r#   r   r      s    % 26,0II I 	I
 "*I "*#I %SMI>:tE{ :"4; "2 48,0.31
1
 1
 4S#X/0	1

 c4i()1
 &d^1
 
*	+1
r%   r   N)__doc__typingr   r   r   r   r   r   litellm._loggingr	   "litellm.integrations.custom_loggerr
   r2   r   litellm.routerr   rI   r   r   rV   r%   r#   <module>r\      sC    C B 2 ;2%;F Eq
 q
r%   