
    h              	           d dl Z d dlZd dlmZmZmZmZ d dl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  G d d	e      Z G d
 de      Z	 ddeeef   dedeeef   fdZy)    N)DictListOptionalUnion)DEFAULT_MAX_RECURSE_DEPTH)BaseLLMModelInfo)BaseLLMExceptionget_secret_str)AllMessageValuesc                       e Zd Zy)GeminiErrorN)__name__
__module____qualname__     \/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/llms/gemini/common_utils.pyr   r      s    r   r   c                   X   e Zd Z	 	 ddededee   dededee   dee   d	efd
Ze	d	efd       Z
eddee   d	ee   fd       Zeddee   d	ee   fd       Zeded	ee   fd       Zdeeeef      d	ee   fdZ	 ddee   dee   d	ee   fdZdededeeej*                  f   d	efdZy)GeminiModelInfoNheadersmodelmessagesoptional_paramslitellm_paramsapi_keyapi_basereturnc                     |S )z.Google AI Studio sends api key in query paramsr   )selfr   r   r   r   r   r   r   s           r   validate_environmentz$GeminiModelInfo.validate_environment   s	     r   c                      y)Nv1betar   )r    s    r   api_versionzGeminiModelInfo.api_version!   s    r   c                 (    | xs t        d      xs dS )NGEMINI_API_BASEz)https://generativelanguage.googleapis.comr
   )r   s    r   get_api_basezGeminiModelInfo.get_api_base%   s"      ;/0;:	
r   c                      | xs t        d      S )NGEMINI_API_KEYr
   )r   s    r   get_api_keyzGeminiModelInfo.get_api_key-   s    <>*:;<r   c                 &    | j                  dd      S )Ngemini/ )replace)r   s    r   get_base_modelzGeminiModelInfo.get_base_model1   s    }}Y++r   modelsc                 n    g }|D ]-  }|d   j                  dd      }d|z   }|j                  |       / |S )Nnamezmodels/r-   r,   )r.   append)r    r0   litellm_model_namesr   stripped_model_namelitellm_model_names         r   process_model_namez"GeminiModelInfo.process_model_name5   sL     E"'-"7"7	2"F!*-@!@&&'9:  #"r   c                    t         j                  |      }t         j                  |      }d| j                   d}||t	        d      t
        j                  j                  | | d|       }|j                  dk7  r)t	        d|j                   d|j                                |j                         d	   }| j                  |      }|S )
N/z/modelszxGEMINI_API_BASE or GEMINI_API_KEY is not set. Please set the environment variable, to query Gemini's `/models` endpoint.z?key=)url   z1Failed to fetch models from Gemini. Status code: z, Response: r0   )r   r'   r*   r$   
ValueErrorlitellmmodule_level_clientgetstatus_codejsonr7   )r    r   r   endpointresponser0   r4   s          r   
get_modelszGeminiModelInfo.get_models=   s     #//9!--g6t''(0w K  ..22*XJeG95 3 
 3&CHDXDXCYYefnfsfsfuevw  *"55f=""r   error_messager@   c                     t        |||      S )N)r@   messager   )r   )r    rE   r@   r   s       r   get_error_classzGeminiModelInfo.get_error_classV   s     #]G
 	
r   )NNN)r   r   r   dictstrr   r   r   r!   propertyr$   staticmethodr'   r*   r/   r   r7   rD   intr   httpxHeadersr	   rH   r   r   r   r   r      s    "&"&  '(	
   # 3- 
 S   
x} 
 
 
 =Xc] =hsm = = ,c ,hsm , ,#d38n)= #$s) # HL#}#7?}#	c#2
 
/2
=B4CV=W
	
r   r   datadepthr   c           	         |t         kD  r| S i }t        | t              s| S | j                         D ]B  \  }}t        |t              r(t        j                  |      j                  d      ||<   ?t        |t        j                        r|j                         ||<   mt        |t              rt        ||dz         ||<   t        |t              rt        d |D              r5|D cg c]&  }t        j                  |      j                  d      ( c}||<   t        d |D              r#|D cg c]  }|j                          c}||<   |D cg c]  }t        ||dz          c}||<   >|||<   E |S c c}w c c}w c c}w )a  Converts unserializable types in dict to json.dumps() compatible types.

    This function is called in models.py after calling convert_to_dict(). The
    convert_to_dict() can convert pydantic object to dict. However, the input to
    convert_to_dict() is dict mixed of pydantic object and nested dict(the output
    of converters). So they may be bytes in the dict and they are out of
    `ser_json_bytes` control in model_dump(mode='json') called in
    `convert_to_dict`, as well as datetime deserialization in Pydantic json mode.

    Returns:
      A dictionary with json.dumps() incompatible type (e.g. bytes datetime)
      to compatible type (e.g. base64 encoded string, isoformat date string).
    ascii   c              3   <   K   | ]  }t        |t                y wrI   )
isinstancebytes.0vs     r   	<genexpr>z.encode_unserializable_types.<locals>.<genexpr>{   s     7A:a'7s   c              3   P   K   | ]  }t        |t        j                           y wrI   )rW   datetimerY   s     r   r\   z.encode_unserializable_types.<locals>.<genexpr>   s     C:a!2!23Cs   $&)r   rW   rJ   itemsrX   base64urlsafe_b64encodedecoder^   	isoformatencode_unserializable_typeslistall)rQ   rR   processed_datakeyvaluer[   s         r   rd   rd   ^   se     (((*NdD!jjl
UeU#"(":":5"A"H"H"QN3x001"'//"3N3t$"=eUQY"ON3t$777IN'DEF,,Q/66w?'s# CUCC>C&Dq{{}&Ds# HM'BC/519='s# #(N3' #( ' 'E's   &+E8-E=F)r   )r`   r^   typingr   r   r   r   rO   r=   litellm.constantsr    litellm.llms.base_llm.base_utilsr   )litellm.llms.base_llm.chat.transformationr	   litellm.secret_managers.mainr   litellm.types.llms.openair   r   r   rK   objectrN   rd   r   r   r   <module>rq      sw      . .   7 = F 7 6	" 	H
& H
X +,)
sF{
)$')	#v+)r   