o
    ưiI                     @   s   d dl mZmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ dd	lmZ erCd d
lmZ eZneZG dd deZdS )    )TYPE_CHECKINGAnyListOptionalTupleUnionNget_secret_str)ModelResponse)AllMessageValuesOpenAIError)BaseLLMException   )OpenAIGPTConfig)Loggingc                       s2  e Zd ZdZdedefddZed#dee dee fdd	Z	ed#d
ee dee fddZ
ed#dee dee fddZd
ee dee deee ee f fddZ fddZ		d$dedejdedededee dedededee dee defddZdeded eeejf defd!d"Z  ZS )%ClarifaiConfigz
    Configuration class for Clarifai chat completions.
    Since Clarifai is OpenAI-compatible, we extend OpenAIGPTConfig.
    modelreturnc                 C   s   g dS )zE
        Get the supported OpenAI params for the given model
        )Z
max_tokensZmax_completion_tokensZresponse_formatstreamZtemperatureZtop_pZtool_choiceZtoolsZpresence_penaltyZfrequency_penaltyZstream_options )selfr   r   r   `/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/clarifai/chat/transformation.pyget_supported_openai_params   s   z*ClarifaiConfig.get_supported_openai_paramsNapi_keyc                 C   s   | pt dS )NCLARIFAI_API_KEYr   )r   r   r   r   get_api_key.   s   zClarifaiConfig.get_api_keyapi_basec                 C   s   | pdS )N)https://api.clarifai.com/v2/ext/openai/v1r   )r   r   r   r   get_api_base5   s   zClarifaiConfig.get_api_basec                 C   s.   | r|  d\}}}d| d| d| S d S )N.zhttps://clarifai.com//z/models/)split)r   Zuser_idZapp_idZmodel_idr   r   r   get_base_model9   s   zClarifaiConfig.get_base_modelc                 C   s    |pd}|pt dpd}||fS )z=
        Get API base and key for Clarifai provider.
        r   r    r   )r   r   r   Zdynamic_api_keyr   r   r   $_get_openai_compatible_provider_info@   s   z3ClarifaiConfig._get_openai_compatible_provider_infoc                    s"   |  |p|}t |||||S N)r#   supertransform_request)r   r   messagesoptional_paramslitellm_paramsheaders	__class__r   r   r(   L   s   z ClarifaiConfig.transform_requestraw_responsemodel_responselogging_objrequest_datar)   r*   r+   encoding	json_modec              
   C   s   |j ||
|jd|id z| }W n ty- } zt|jdt| |jd|d}~ww tdi |}|j	dur?d| |_	|S )z
        Transform the Clarifai response to a standard ModelResponse.
        Since Clarifai is OpenAI-compatible, we use OpenAI response transformation.
        Zcomplete_input_dict)inputr   original_responseZadditional_argsz#Failed to parse Clarifai response: status_codemessager,   Nz	clarifai/r   )
Z	post_calltextjson	Exceptionr   r8   strr,   r
   r   )r   r   r/   r0   r1   r2   r)   r*   r+   r3   r   r4   Zcompletion_responseeresponser   r   r   transform_responseP   s,   

z!ClarifaiConfig.transform_responseerror_messager8   r,   c                 C   s   t |||dS )z
        Get the appropriate error class for Clarifai errors.
        Since Clarifai is OpenAI-compatible, we use OpenAI error handling.
        r7   r   )r   rA   r8   r,   r   r   r   get_error_classz   s
   zClarifaiConfig.get_error_classr&   )NN)__name__
__module____qualname____doc__r=   listr   staticmethodr   r   r   r#   r   r%   r(   httpxResponser
   LiteLLMLoggingObjdictr   r   r   boolr@   intr   ZHeadersr   rB   __classcell__r   r   r-   r   r      sj    
	

*r   )typingr   r   r   r   r   r   rI   Zlitellm.secret_managers.mainr	   Zlitellm.types.utilsr
   Zlitellm.types.llms.openair   Z litellm.llms.openai.common_utilsr   Z)litellm.llms.base_llm.chat.transformationr   Zopenai.chat.gpt_transformationr   Z*litellm.litellm_core_utils.litellm_loggingr   Z_LiteLLMLoggingObjrK   r   r   r   r   r   <module>   s     