o
    Zh                     @  s   d dl mZ d dlmZmZmZ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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eZdS )    )annotations)AnyIteratorListOptional)CallbackManagerForLLMRun)LangSmithParams)BaseLLM)HumanMessage)
GenerationGenerationChunk	LLMResult)
ConfigDictmodel_validator)Self)_BaseGoogleGenerativeAI)ChatGoogleGenerativeAIc                      s   e Zd ZU dZdZded< eddZeddd%ddZ		d&d' fddZ
		d(d)ddZ		d(d*ddZed+dd Zd,d#d$Z  ZS )-GoogleGenerativeAIzGoogle GenerativeAI models.

    Example:
        .. code-block:: python

            from langchain_google_genai import GoogleGenerativeAI
            llm = GoogleGenerativeAI(model="gemini-pro")
    Nr   clientT)Zpopulate_by_nameafter)modereturnr   c                 C  s>   t | j| j| j| j| j| j| j| j| j	| j
| j| jd| _| S )z@Validates params and passes them to google-generativeai package.)Zapi_keycredentialstemperaturetop_ptop_kZ
max_tokenstimeoutmodelclient_options	transportadditional_headerssafety_settings)r   Zgoogle_api_keyr   r   r   r   max_output_tokensr   r   r   r   r    r!   r   self r%   R/var/www/html/lang_env/lib/python3.10/site-packages/langchain_google_genai/llms.pyvalidate_environment$   s   z'GoogleGenerativeAI.validate_environmentstopOptional[List[str]]kwargsr   c                   s<   t  jdd|i|}d|d< |d| j }r||d< |S )z Get standard params for tracing.r(   Zgoogle_genaiZls_providerr"   ls_max_tokensNr%   )super_get_ls_paramsgetr"   )r$   r(   r*   Z	ls_paramsr+   	__class__r%   r&   r-   9   s
   z!GoogleGenerativeAI._get_ls_paramsprompts	List[str]run_manager"Optional[CallbackManagerForLLMRun]r   c                 K  sP   g }|D ]}| j jt|dgf||d|}|dd |jD  qt|dS )Ncontentr(   r3   c                 S  s.   g | ]}t |jji |jd |jjidqS )usage_metadata)textgeneration_info)r   messager6   r:   r8   ).0gr%   r%   r&   
<listcomp>S   s    
z0GoogleGenerativeAI._generate.<locals>.<listcomp>)generations)r   	_generater
   appendr?   r   )r$   r1   r(   r3   r*   r?   promptZchat_resultr%   r%   r&   r@   C   s    

zGoogleGenerativeAI._generaterB   strIterator[GenerationChunk]c                 k  sZ    | j jt|dgf||d|D ]}t|jjd}|V  |r*|j|j|| jd qd S )Nr5   r7   )r9   )chunkverbose)	r   _streamr
   r   r;   r6   Zon_llm_new_tokenr9   rF   )r$   rB   r(   r3   r*   Zstream_chunkrE   r%   r%   r&   rG   `   s&   

zGoogleGenerativeAI._streamc                 C  s   dS )zReturn type of llm.Zgoogle_geminir%   r#   r%   r%   r&   	_llm_typev   s   zGoogleGenerativeAI._llm_typer9   intc                 C  s   | j |S )a  Get the number of tokens present in the text.

        Useful for checking if an input will fit in a model's context window.

        Args:
            text: The string input to tokenize.

        Returns:
            The integer number of tokens in the text.
        )r   get_num_tokens)r$   r9   r%   r%   r&   rJ   {   s   z!GoogleGenerativeAI.get_num_tokens)r   r   )N)r(   r)   r*   r   r   r   )NN)
r1   r2   r(   r)   r3   r4   r*   r   r   r   )
rB   rC   r(   r)   r3   r4   r*   r   r   rD   )r   rC   )r9   rC   r   rI   )__name__
__module____qualname____doc__r   __annotations__r   Zmodel_configr   r'   r-   r@   rG   propertyrH   rJ   __classcell__r%   r%   r/   r&   r      s&   
 	 r   N)
__future__r   typingr   r   r   r   Zlangchain_core.callbacksr   Zlangchain_core.language_modelsr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.messagesr
   Zlangchain_core.outputsr   r   r   Zpydanticr   r   Ztyping_extensionsr   Zlangchain_google_genai._commonr   Z"langchain_google_genai.chat_modelsr   r   r%   r%   r%   r&   <module>   s    