o
    ưi+                     @   sF   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 dZ
dS )    N)ListDictAnyOptionalUnion   )UnauthorizedErrorNotFoundErrorc                   @   s  e Zd Zddedee fddZdeeef fddZdd
ede	e
eeef  ejf fd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dZdded
ede	eeef ejf fddZ		ddee dee d
ede	eeef ejf fddZdd
ede	e
eeef  ejf fd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dZdS )ModelsManagementClientNbase_urlapi_keyc                 C   s   | d| _|| _dS )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    r   R/home/app/Keep/.python/lib/python3.10/site-packages/litellm/proxy/client/models.py__init__   s   
zModelsManagementClient.__init__returnc                 C   s   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   headersr   r   r   _get_headers   s   z#ModelsManagementClient._get_headersFreturn_requestc              
   C      | j  d}tjd||  d}|r|S t }z|| }|  | d W S  tj	j
yC } z|jjdkr>t| d}~w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)   er   r   r   list   s   zModelsManagementClient.list
model_namemodel_params
model_infoc              
   C   s   | j  d}||d}|r||d< tjd||  |d}|r |S t }z|| }	|	  |	 W S  tj	j
yM }
 z|
jjdkrHt|
 d}
~
w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)r1   litellm_paramsr3   POSTr   r&   r   Nr   )r   r1   r2   r3   r   r,   r   r-   r.   r)   r/   r   r   r   new?   s(   
zModelsManagementClient.newmodel_idc           	   
   C   s   | j  d}d|i}tjd||  |d}|r|S t }z|| }|  | W S  tj	j
yX } z|jjdkrAt||jjdksOd|jj v rSt| d}~w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idr5   r6   r     	not foundNr   r    r!   r   r"   r#   r$   r%   r&   r'   r(   r)   r*   r   textlowerr	   )	r   r8   r   r,   r   r-   r.   r)   r/   r   r   r   deletep   s$   
zModelsManagementClient.deletec                 C   s   |du r|du s|dur|durt d|r&| jdd}t|tjs$J |S |  }t|ts1J |D ]}|rB|di d|ksK|rO|d|krO|  S q3|rYd| d	}n|rbd
| d	}nd}ttjj	|t
 d)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
        Nz6Exactly one of model_id or model_name must be providedT)r   r3   r9   r1   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   r8   r1   r   resultmodelsmodelmsgr   r   r   rC      s4    zModelsManagementClient.getc              
   C   r   )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+   r   r   r   rA      s   zModelsManagementClient.infoc              
   C   s   | j  d}||d}|r||d< tjd||  |d}|r |S t }z|| }	|	  |	 W S  tj	j
y_ }
 z|
jjdkrHt|
|
jjdksVd|
jj v rZt|
 d	}
~
w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)r9   r4   r3   r5   r6   r   r:   r;   Nr<   )r   r8   r2   r3   r   r,   r   r-   r.   r)   r/   r   r   r   update   s,   
zModelsManagementClient.update)N)F)NF)NNF)__name__
__module____qualname__strr   r   r   r   boolr   r   r   r    r!   r0   r7   r?   rC   rA   rI   r   r   r   r   r
      sT    *%

*1'
*7$
r
   )r    typingr   r   r   r   r   r'   r   r	   r
   r   r   r   r   <module>   s    