
    hR              
          d dl Z d dlZd dlZd dlmZmZ d dlZ ej                  e      Z	d dl
mZ g dZddgZd Zdded	ee   fd
ZdedefdZd Zdded	ee   fdZdededefdZ	 	 	 ddedee   dee   d	ee   fdZy)    N)ListOptional)HEALTH_CHECK_TIMEOUT_SECONDS)messagesapi_keypromptinputvertex_credentialsaws_access_key_idaws_secret_access_keymodel
mode_errorc                  <    ddg} dt        j                  |       dgS )z,
    Get a random message from the LLM.
    zHey how's it going?zWhat's 1 + 1?user)rolecontent)randomchoice)r   s    V/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/health_check.py_get_random_llm_messager      s%     &7Hh(?@AA    endpoint_datadetailsc                     | j                  dd       |dur-| j                         D ci c]  \  }}|t        vs|| c}}S | j                         D ci c]  \  }}|t        v s|| c}}S c c}}w c c}}w )z7
    Clean the endpoint data for display to users.
    litellm_logging_objNF)popitemsILLEGAL_DISPLAY_PARAMSMINIMAL_DISPLAY_PARAMS)r   r   kvs       r   _clean_endpoint_datar"   #   s     +T2 % (--/S$!Q1<R3RAS  -224Ttq!=S8SadTSTs   A1A1A7(A7
model_listreturnc                     t               }g }| D ]V  }|j                  d      xs i }|j                  d      xs d }|0||vs5|j                  |       |j                  |       X |S )N
model_infoid)setgetaddappend)r#   seen_idsfiltered_deployments
deployment_model_info_ids         r   filter_deployments_by_idr1   /   ss     uH 
 nn\28bood#+t;hLL ''
3 !  r   c                   K   	 t        j                  | |       d {   S 7 # t         j                  $ r | j                          t        j                         }t        j
                         D ]  }||k7  s	|j                           	 t        j                  | d       d {  7   n.# t         j                  t         j                  t        f$ r Y nw xY wddicY S w xY ww)Ng?errorzTimeout exceeded)asynciowait_forTimeoutErrorcancelcurrent_task	all_tasksCancelledError	Exception)tasktimeoutr8   ts       r   run_with_timeoutr?   B   s     -%%dG4444 -++-""$AL 
 %	""4---$$g&<&<iH 		+,,-sh   C#$ "$ C#$ AC 8C B+$B'%B+*C +(CC CC C#C  C#c           	        K   g }| D ]  }|d   }|j                  di       }|j                  dd      }t        ||      }|j                  d      xs t        }t        t	        j
                  |d   |ddg      |      }|j                  |        t        j                  |dd	i d{   }	g }
g }t        |	|       D ]  \  }}|d   }t        |t              r$d
|vr |
j                  t        i |||             ?t        |t              r |j                  t        i |||             o|j                  t        ||              |
|fS 7 w)z<
    Perform a health check for each model in the list.
    litellm_paramsr&   modeNhealth_check_timeoutztest from litellm)rB   r   r	   return_exceptionsTr3   )r)   '_update_litellm_params_for_health_checkr   r?   litellmahealth_checkr+   r4   gatherzip
isinstancedictr"   )r#   r   tasksr   rA   r&   rB   r=   r<   resultshealthy_endpointsunhealthy_endpoints
is_healthys                r   _perform_health_checkrQ   S   s|    
 E/0YY|R0
~~fd+@
 ..!78X<X!!&'**+	 
 	T' * NNEBTBBG *5
E/0j$'G:,E$$$%E%E*%EwO 
D)&&$%E%E*%EwO  &&';NG'TU 6 111' Cs   B(E*E+B%Er&   rA   c                 R    t               |d<   | j                  dd      }|||d<   |S )z
    Update the litellm params for health check.

    - gets a short `messages` param for health check
    - updates the `model` param with the `health_check_model` if it exists Doc: https://docs.litellm.ai/docs/proxy/health#wildcard-routes
    r   health_check_modelNr   )r   r)   )r&   rA   _health_check_models      r   rE   rE      s9     "9!:N:$..)=tD&"5wr   	cli_modelc                 
  K   | s|r	|d|idg} ng g fS |:| D cg c]  }|d   d   |k(  s| }}|g k(  r| D cg c]  }|d   |k(  s| }}|} t        |       } t        | |       d{   \  }}||fS c c}w c c}w 7 w)z
    Perform a health check on the system.

    Returns:
        (bool): True if the health check passes, False otherwise.
    r   )
model_namerA   NrA   rW   )r#   )r1   rQ   )r#   r   rU   r   x_new_model_listrN   rO   s           r   perform_health_checkrZ      s      (Wi<PQJ r6M!
Q'7%8%AU%JA
 
 b *4QQ,58PqQOQ$
)J 4IG4 .** 111
 R.s1   BA7A7BA<A<B+B,B)T)NNT)r4   loggingr   typingr   r   rF   	getLogger__name__loggerlitellm.constantsr   r   r   r   rK   boolr"   r1   r?   listrQ   rE   strrZ    r   r   <module>re      s       ! 			8	$ :  "<0 B	 	x~ 	  	 &-".2D .28D> .2b&*	$  #"	#2#2C=#2 }#2 d^	#2r   