
    h+                     L    d dl Z d dlmZmZmZmZmZ ddlmZm	Z	  G d d      Z
y)    N)ListDictAnyOptionalUnion   )UnauthorizedErrorNotFoundErrorc                      e Zd Zddedee   fdZdeeef   fdZddede	e
eeef      ej                  f   fdZ	 	 dd	ed
eeef   deeeef      dede	eeef   ej                  f   f
dZddedede	eeef   ej                  f   fdZ	 ddee   d	ee   dede	eeef   ej                  f   fdZddede	e
eeef      ej                  f   fdZ	 	 dded
eeef   deeeef      dede	eeef   ej                  f   f
dZy)ModelsManagementClientNbase_urlapi_keyc                 >    |j                  d      | _        || _        y)a  
        Initialize the ModelsManagementClient.

        Args:
            base_url (str): The base URL of the LiteLLM proxy server (e.g., "http://localhost:8000")
            api_key (Optional[str]): API key for authentication. If provided, it will be sent as a Bearer token.
        /N)rstrip	_base_url_api_key)selfr   r   s      W/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/proxy/client/models.py__init__zModelsManagementClient.__init__   s     "-    returnc                 F    i }| j                   rd| j                    |d<   |S )z
        Get the headers for API requests, including authorization if api_key is set.

        Returns:
            Dict[str, str]: Headers to use for API requests
        zBearer Authorization)r   )r   headerss     r   _get_headersz#ModelsManagementClient._get_headers   s*     ==)0'@GO$r   return_requestc                    | j                    d}t        j                  d|| j                               }|r|S t        j                         }	 |j                  |j                               }|j                          |j                         d   S # t        j                  j                  $ r*}|j                  j                  dk(  rt        |       d}~ww xY w)a  
        Get the list of models supported by the server.

        Args:
            return_request (bool): If True, returns the prepared request object instead of executing it.
                                 Useful for inspection or modification before sending.

        Returns:
            Union[List[Dict[str, Any]], requests.Request]: Either a list of model information dictionaries
            or a prepared request object if return_request is True.

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z/modelsGETr   data  Nr   requestsRequestr   Sessionsendprepareraise_for_statusjson
exceptions	HTTPErrorresponsestatus_coder	   r   r   urlrequestsessionr-   es          r   listzModelsManagementClient.list   s       (""5#t7H7H7JKN ""$	||GOO$56H%%'==?6**"",, 	zz%%,'**	   AB C.%CC
model_namemodel_params
model_infoc                    | j                    d}||d}|r||d<   t        j                  d|| j                         |      }|r|S t        j                         }	 |j                  |j                               }	|	j                          |	j                         S # t        j                  j                  $ r*}
|
j                  j                  dk(  rt        |
       d}
~
ww xY w)a  
        Add a new model to the proxy.

        Args:
            model_name (str): Name of the model to add
            model_params (Dict[str, Any]): Parameters for the model (e.g., model type, api_base, api_key)
            model_info (Optional[Dict[str, Any]]): Additional information about the model
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z
/model/new)r6   litellm_paramsr8   POSTr   r*   r"   Nr#   )r   r6   r7   r8   r   r0   r!   r1   r2   r-   r3   s              r   newzModelsManagementClient.new?   s    0  
+ %*
 !+D""638I8I8KRVWN ""$	||GOO$56H%%'==?""",, 	zz%%,'**	s   >B C"8%CC"model_idc                 N   | j                    d}d|i}t        j                  d|| j                         |      }|r|S t        j                         }	 |j                  |j                               }|j                          |j                         S # t        j                  j                  $ rt}|j                  j                  dk(  rt        |      |j                  j                  dk(  s&d|j                  j                  j                         v rt!        |       d}~ww xY w)	a  
        Delete a model from the proxy.

        Args:
            model_id (str): ID of the model to delete (e.g., "2f23364f-4579-4d79-a43a-2d48dd551c2e")
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            NotFoundError: If the request fails with a 404 status code or indicates the model was not found
            requests.exceptions.RequestException: If the request fails with any other error
        z/model/deleteidr;   r<   r"     	not foundNr   r$   r%   r   r&   r'   r(   r)   r*   r+   r,   r-   r.   r	   textlowerr
   )	r   r>   r   r0   r!   r1   r2   r-   r3   s	            r   deletezModelsManagementClient.deletep   s    "  .h""638I8I8KRVWN ""$		||GOO$56H%%'==?""",, 	zz%%,'**zz%%,qzz?T?T?V0V#A&&	s   >B D$0A/DD$c                     ||||t        d      |r0| j                  d      }t        |t        j                        sJ |S | j                         }t        |t
              sJ |D ]C  }|r$|j                  di       j                  d      |k(  s|s,|j                  d      |k(  sA|c S  |rd| d}n|rd	| d}nd
}t        t        j                  j                  |t        j                                     )aN  
        Get information about a specific model by its ID or name.

        Args:
            model_id (Optional[str]): ID of the model to retrieve
            model_name (Optional[str]): Name of the model to retrieve
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the model information from the server or
            a prepared request object if return_request is True

        Raises:
            ValueError: If neither model_id nor model_name is provided, or if both are provided
            UnauthorizedError: If the request fails with a 401 status code
            NotFoundError: If the model is not found
            requests.exceptions.RequestException: If the request fails with any other error
        z6Exactly one of model_id or model_name must be providedT)r   r8   r@   r6   zModel with id=z
 not foundzModel with model_name=z"Unknown error trying to find model)r-   )
ValueErrorinfo
isinstancer$   r%   r   getr
   r+   r,   Response)r   r>   r6   r   resultmodelsmodelmsgs           r   rK   zModelsManagementClient.get   s   * !39MR\RhUVV YYdY3Ffh&6&6777M &$''' EUYY|R8<<TBhNuyy6*D	  "8*J7C*:,jAC6C))!**, * 
 	
r   c                    | j                    d}t        j                  d|| j                               }|r|S t        j                         }	 |j                  |j                               }|j                          |j                         d   S # t        j                  j                  $ r*}|j                  j                  dk(  rt        |       d}~ww xY w)a;  
        Get detailed information about all models from the server.

        Args:
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[List[Dict[str, Any]], requests.Request]: Either a list of model information dictionaries
            or a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            requests.exceptions.RequestException: If the request fails with any other error
        z/v1/model/infor   r    r!   r"   Nr#   r/   s          r   rI   zModelsManagementClient.info   s      /""5#t7H7H7JKN ""$	||GOO$56H%%'==?6**"",, 	zz%%,'**	r5   c                 ^   | j                    d}||d}|r||d<   t        j                  d|| j                         |      }|r|S t        j                         }	 |j                  |j                               }	|	j                          |	j                         S # t        j                  j                  $ rt}
|
j                  j                  dk(  rt        |
      |
j                  j                  dk(  s&d|
j                  j                  j                         v rt!        |
       d	}
~
ww xY w)
aK  
        Update an existing model's configuration.

        Args:
            model_id (str): ID of the model to update
            model_params (Dict[str, Any]): New parameters for the model (e.g., model type, api_base, api_key)
            model_info (Optional[Dict[str, Any]]): Additional information about the model
            return_request (bool): If True, returns the prepared request object instead of executing it

        Returns:
            Union[Dict[str, Any], requests.Request]: Either the response from the server or
            a prepared request object if return_request is True

        Raises:
            UnauthorizedError: If the request fails with a 401 status code
            NotFoundError: If the model is not found
            requests.exceptions.RequestException: If the request fails with any other error
        z/model/update)r@   r:   r8   r;   r<   r"   rA   rB   NrC   )r   r>   r7   r8   r   r0   r!   r1   r2   r-   r3   s              r   updatezModelsManagementClient.update   s   2  . *
 !+D""638I8I8KRVWN ""$		||GOO$56H%%'==?""",, 	zz%%,'**zz%%,qzz?T?T?V0V#A&&	s   >B D,8A/D''D,)N)F)NF)NNF)__name__
__module____qualname__strr   r   r   r   boolr   r   r   r$   r%   r4   r=   rF   rK   rI   rS    r   r   r   r      s   	  	 x} 	 
d38n 
4 E$tCH~:NPXP`P`:`4a J 04$// 38n/ T#s(^,	/
 / 
tCH~x///	0/b$s $D $U4PSUXPX>[c[k[kKkEl $N hm5
 5
:B3-5
`d5
	tCH~x///	05
n4 E$tCH~:NPXP`P`:`4a H 04$22 38n2 T#s(^,	2
 2 
tCH~x///	02r   r   )r$   typingr   r   r   r   r   r+   r	   r
   r   rY   r   r   <module>r[      s     3 3 8Y Yr   