o
    ưi)                     @   s   d dl mZmZmZ d dlmZmZ d dlmZ d dl	m
Z
mZ d dlmZmZ d dlmZ ddlmZ dd	lmZ G d
d deZdS )    )ListOptionalcast)/convert_generic_image_chunk_to_openai_image_objconvert_to_anthropic_image_obj)convert_url_to_base64)AllMessageValuesChatCompletionFileObject)ContentTypePartType)supports_reasoning   )%_gemini_convert_messages_with_history)VertexGeminiConfigc                       sT  e Zd ZU dZdZee ed< dZee	 ed< dZ
ee ed< dZee	 ed< dZee ed< dZee ed< dZee	 ed	< dZee ed
< 								ddee dee	 dee dee	 dee dee d	ee	 d
ee ddfddZe fddZdedefddZdedee fddZ	ddee dee dee fddZ  ZS )GoogleAIStudioGeminiConfiga  
    Reference: https://ai.google.dev/api/rest/v1beta/GenerationConfig

    The class `GoogleAIStudioGeminiConfig` provides configuration for the Google AI Studio's Gemini API interface. Below are the parameters:

    - `temperature` (float): This controls the degree of randomness in token selection.

    - `max_output_tokens` (integer): This sets the limitation for the maximum amount of token in the text output. In this case, the default value is 256.

    - `top_p` (float): The tokens are selected from the most probable to the least probable until the sum of their probabilities equals the `top_p` value. Default is 0.95.

    - `top_k` (integer): The value of `top_k` determines how many of the most probable tokens are considered in the selection. For example, a `top_k` of 1 means the selected token is the most probable among all tokens. The default value is 40.

    - `response_mime_type` (str): The MIME type of the response. The default value is 'text/plain'. Other values - `application/json`.

    - `response_schema` (dict): Optional. Output response schema of the generated candidate text when response mime type can have schema. Schema can be objects, primitives or arrays and is a subset of OpenAPI schema. If set, a compatible response_mime_type must also be set. Compatible mimetypes: application/json: Schema for JSON response.

    - `candidate_count` (int): Number of generated responses to return.

    - `stop_sequences` (List[str]): The set of character sequences (up to 5) that will stop output generation. If specified, the API will stop at the first appearance of a stop sequence. The stop sequence will not be included as part of the response.

    Note: Please make sure to modify the default parameters as required for your use case.
    Ntemperaturemax_output_tokenstop_ptop_kresponse_mime_typeresponse_schemacandidate_countstop_sequencesreturnc	                 C   s>   t   }	|	 D ]\}
}|
dkr|d urt| j|
| q	d S )Nself)localscopyitemssetattr	__class__)r   r   r   r   r   r   r   r   r   Zlocals_keyvalue r"   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/gemini/chat/transformation.py__init__4   s   
z#GoogleAIStudioGeminiConfig.__init__c                    s
   t   S N)super
get_config)clsr   r"   r#   r'   D   s   
z%GoogleAIStudioGeminiConfig.get_configmodelc                 C   s   d|v S )NZttsr"   )r   r*   r"   r"   r#   is_model_gemini_audio_modelH   s   z6GoogleAIStudioGeminiConfig.is_model_gemini_audio_modelc                 C   s@   g d}t |ddr|d |d | |r|d |S )N)r   r   Z
max_tokensZmax_completion_tokensstreamZtoolsZtool_choiceZ	functionsZresponse_formatnstopZlogprobsZfrequency_penaltyZpresence_penaltyZ
modalitiesZparallel_tool_callsZweb_search_optionsgemini)Zcustom_llm_providerZreasoning_effortZthinkingZaudio)r   appendr+   )r   r*   Zsupported_paramsr"   r"   r#   get_supported_openai_paramsK   s   



z6GoogleAIStudioGeminiConfig.get_supported_openai_paramsmessagesc              
   C   s\  |D ]}| d}|durt|trg }|D ]}| ddkrk|}d}d}	d}
t| dtrE|d  d}|d  d}	|d  d}
n| d}|rjd|v rjt||	d	}t|}|
durf||
d
|d< q||d< q| ddkrtt|}|d  d}|rd|v sd|v rzt|}||d d< |d 	dd W q t
y   Y qw qqt||dS )z
        Google AI Studio Gemini does not support HTTP/HTTPS URLs for files.
        Convert them to base64 data instead.
        contentNtypeZ	image_urlurlformatdetailzhttps://)r6   )r5   r7   filefile_idzhttp://Z	file_data)r2   r*   )get
isinstancelistdictr   r   r   r	   r   pop	Exceptionr   )r   r2   r*   messageZ_message_content_partselementZimg_elementZ
_image_urlr6   r7   Z	image_objZconverted_image_urlZfile_elementr9   Zbase64_datar"   r"   r#   _transform_messagesf   sR   


z.GoogleAIStudioGeminiConfig._transform_messages)NNNNNNNNr%   )__name__
__module____qualname____doc__r   r   float__annotations__r   intr   r   r   strr   r=   r   r   r<   r$   classmethodr'   boolr+   r   r1   r   r
   rC   __classcell__r"   r"   r)   r#   r      sb   
 	

r   N)typingr   r   r   Z3litellm.litellm_core_utils.prompt_templates.factoryr   r   Z:litellm.litellm_core_utils.prompt_templates.image_handlingr   Zlitellm.types.llms.openair   r	   Zlitellm.types.llms.vertex_air
   r   Zlitellm.utilsr   Zvertex_ai.gemini.transformationr   Z3vertex_ai.gemini.vertex_and_google_ai_studio_geminir   r   r"   r"   r"   r#   <module>   s    