U
    hkK                  	   @  s  d dl mZ d dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZ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m Z m!Z!m"Z" erd dl#m$Z$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z, d dl-m.Z. dZ/dZ0dddddZ1dddddZ2d0ddddddddddZ3d1dddd ddd!d"d#Z4G d$d% d%eZ5G d&d' d'e5Z6ed(d)d*d+G d,d de6eZ7ed(d)d-d+G d.d/ d/e5eZ8dS )2    )annotations)ExecutorThreadPoolExecutor)TYPE_CHECKINGAnyClassVarDictIteratorListOptionalUnion)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)BaseLLM)
GenerationGenerationChunk	LLMResult)	BaseModelField)pre_init)create_retry_decoratorget_client_infoinit_vertexairaise_vertex_import_errorPredictionServiceAsyncClientPredictionServiceClient)
PredictionValue)TextGenerationResponse_LanguageModel)ImageNstrbool)
model_namereturnc                 C  s   d| kS )z/Return True if the model name is a Codey model.code r&   r)   r)   E/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/vertexai.pyis_codey_model*   s    r,   c                 C  s   | dk	od| kS )z0Return True if the model name is a Gemini model.NZgeminir)   r*   r)   r)   r+   is_gemini_model/   s    r-   FVertexAIList[Union[str, 'Image']]"Optional[CallbackManagerForLLMRun]r   )llmpromptstream	is_geminirun_managerkwargsr'   c                   s:   t  |d}|d	ddddd fdd}|||f|S )
*Use tenacity to retry the completion call.r5   Fr/   r%   r   r2   r4   r6   r'   c                   sF   |r j j| |dS r. j j| d f|S  j j| d f|S d S )N)r3   generation_configr   )clientZgenerate_contentZpredict_streamingpredictr2   r4   r6   r1   r3   r)   r+   _completion_with_retry?   s      z5completion_with_retry.<locals>._completion_with_retry)Fr   )r1   r2   r3   r4   r5   r6   retry_decoratorr?   r)   r>   r+   completion_with_retry4   s
    	 rB   'Optional[AsyncCallbackManagerForLLMRun])r1   r2   r4   r5   r6   r'   c                   s>   t  |d}|d
ddddd fdd}|||f|I d	H S )r7   r8   Fr$   r%   r   r9   c                   s0   |r j j| |dI d H S  j j| f|I d H S )N)r:   )r;   Zgenerate_content_asyncZpredict_asyncr=   r1   r)   r+   _acompletion_with_retryY   s     z7acompletion_with_retry.<locals>._acompletion_with_retryN)Fr@   )r1   r2   r4   r5   r6   rA   rE   r)   rD   r+   acompletion_with_retryO   s
     	rF   c                   @  s   e Zd ZU dZded< dZded< dZded	< d
Zded< edddZ	ded< dZ
ded< dZded< eddddddZdS )_VertexAIBaseNOptional[str]projectzus-central1r$   location   intrequest_parallelism   max_retriesTdefaultexcludezClassVar[Optional[Executor]]task_executorOptional[List[str]]stopr&   r   )rM   r'   c                 C  s   | j d krt|d| _ | j S )N)max_workers)rS   r   )clsrM   r)   r)   r+   _get_task_executorv   s    
z _VertexAIBase._get_task_executor)rK   )__name__
__module____qualname__rI   __annotations__rJ   rM   rO   r   rS   rU   r&   classmethodrX   r)   r)   r)   r+   rG   f   s   
rG   c                   @  s  e Zd ZU dZded< dZded< ded< dZded	< d
Zded< dZded< dZ	ded< e
dddZded< dZded< dZded< eddddZeddddZedddd Zed!dd"d#Zed!dd$d%Zed&d'd(d)d*Zd0d+ddd,d-d.d/ZdS )1_VertexAICommonNz'_LanguageModel'r;   client_previewr$   r&   g        floattemperature   rL   max_output_tokensgffffff?top_p(   top_kTrP   r   credentials   nFr%   	streamingr'   c                 C  s   dS )Nvertexair)   selfr)   r)   r+   	_llm_type   s    z_VertexAICommon._llm_typec                 C  s
   t | jS N)r,   r&   rm   r)   r)   r+   r,      s    z_VertexAICommon.is_codey_modelc                 C  s
   t | jS rp   )r-   r&   rm   r)   r)   r+   _is_gemini_model   s    z _VertexAICommon._is_gemini_modelzDict[str, Any]c                 C  s   d| j i| jS )z Gets the identifying parameters.r&   )r&   _default_paramsrm   r)   r)   r+   _identifying_params   s    z#_VertexAICommon._identifying_paramsc                 C  s0   | j | j| jd}| js,|| j| jd |S )N)ra   rc   candidate_count)rf   rd   )ra   rc   ri   r,   updaterf   rd   )rn   paramsr)   r)   r+   rr      s    z_VertexAICommon._default_paramsr   Nonevaluesr'   c                   s.   dddg  fdd|  D }tf | d S )NrI   rJ   rg   c                   s   i | ]\}}| kr||qS r)   r)   .0kvZallowed_paramsr)   r+   
<dictcomp>   s       z6_VertexAICommon._try_init_vertexai.<locals>.<dictcomp>)itemsr   )rW   ry   rv   r)   r~   r+   _try_init_vertexai   s    

z"_VertexAICommon._try_init_vertexairT   dict)rU   r3   r6   r'   c                   sP   |p| j }ddi  fdd| D }| jd|i|}|sB| jrL|d |S )Nri   rt   c                   s   i | ]\}}  |||qS r)   )getrz   Zparams_mappingr)   r+   r      s     
 z3_VertexAICommon._prepare_params.<locals>.<dictcomp>stop_sequences)rU   r   rr   rj   pop)rn   rU   r3   r6   r   rv   r)   r   r+   _prepare_params   s    


z_VertexAICommon._prepare_params)NF)rY   rZ   r[   r;   r\   r_   ra   rc   rd   rf   r   rg   ri   rj   propertyro   r,   rq   rs   rr   r]   r   r   r)   r)   r)   r+   r^   }   s2   
  r^   z0.0.12z1.0z"langchain_google_vertexai.VertexAI)ZsinceZremovalZalternative_importc                   @  s   e Zd ZU dZdZded< dZded< edd	d
dZedd	ddZ	e
dddddZdddddZdddddZd,dddddd d!d"d#Zd-ddd$dd d%d&d'Zd.ddddd(d)d*d+ZdS )/r.   z'Google Vertex AI large language models.z
text-bisonr$   r&   NrH   tuned_model_namer%   rk   c                 C  s   dS )NTr)   rm   r)   r)   r+   is_lc_serializable   s    zVertexAI.is_lc_serializable	List[str]c                 C  s
   dddgS )z*Get the namespace of the langchain object.Z	langchainZllmsrl   r)   )rW   r)   r)   r+   get_lc_namespace   s    zVertexAI.get_lc_namespacer   rx   c                 C  s.  | d}|d }t|d }| | zddlm}m} ddlm} ddlm} |rbddlm}	 t	|rt|}
|}n|r|	}
|	}n|}
|}|r|

||d< |
||d	< n>|r|
|d
|d< ||d
|d	< n|
||d< |||d	< W n tk
r   t  Y nX |d r*|d dkr*td|S )7Validate that the python package exists in environment.r   r&   r   )CodeGenerationModelTextGenerationModel)r   )r   )GenerativeModelr;   r_   r*   rj   ri   rh   z3Only one candidate can be generated with streaming!)r   r-   r   Zvertexai.language_modelsr   r   Z vertexai.preview.language_models"vertexai.preview.generative_modelsr   r,   Zget_tuned_modelZfrom_pretrainedImportErrorr   
ValueError)rW   ry   r   r&   r4   r   r   ZPreviewCodeGenerationModelZPreviewTextGenerationModelr   Z	model_clsZpreview_model_clsr)   r)   r+   validate_environment   sF    


zVertexAI.validate_environmentrL   )textr'   c                 C  s4   z| j |g}W n tk
r,   t  Y nX |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_   Zcount_tokensAttributeErrorr   Ztotal_tokens)rn   r   resultr)   r)   r+   get_num_tokens  s
    zVertexAI.get_num_tokensr!   r   )responser'   c                 C  s:   z|j |jd}W n tk
r*   d}Y nX t|j|dS )z1Converts a stream response to a generation chunk.)
is_blockedsafety_attributesN)r   generation_info)r   r   	Exceptionr   r   )rn   r   r   r)   r)   r+   _response_to_generation,  s    

z VertexAI._response_to_generationrT   r0   zOptional[bool]r   r   )promptsrU   r5   r3   r6   r'   c                   s   |d k	r|n j } jf ||d|}g }|D ]~}	|rvtdd}
 j|	f||d|D ]}|
|7 }
qZ||
g q0t |	gf| j|d|}| fdd|jD  q0t|dS )	NrU   r3    r   )rU   r5   r3   r4   r5   c                   s   g | ]}  |qS r)   r   r{   rrm   r)   r+   
<listcomp>V  s     z&VertexAI._generate.<locals>.<listcomp>generations)	rj   r   r   _streamappendrB   rq   
candidatesr   )rn   r   rU   r5   r3   r6   Zshould_streamrv   r   r2   Z
generationchunkresr)   rm   r+   	_generate9  s<    
 

zVertexAI._generaterC   r   rU   r5   r6   r'   c           	        sf    j f d|i|}g }|D ]>}t |f j|d|I d H }| fdd|jD  qt|dS )NrU   )r4   r5   c                   s   g | ]}  |qS r)   r   r   rm   r)   r+   r   l  s     z'VertexAI._agenerate.<locals>.<listcomp>r   )r   rF   rq   r   r   r   )	rn   r   rU   r5   r6   rv   r   r2   r   r)   rm   r+   
_agenerateZ  s     zVertexAI._ageneratezIterator[GenerationChunk])r2   rU   r5   r6   r'   c                 k  sf   | j f |dd|}t| |gfd| j|d|D ],}| |}|rZ|j|j|| jd |V  q4d S )NTr   r   )r   verbose)r   rB   rq   r   Zon_llm_new_tokenr   r   )rn   r2   rU   r5   r6   rv   Zstream_respr   r)   r)   r+   r   p  s&    

zVertexAI._stream)NNN)NN)NN)rY   rZ   r[   __doc__r&   r\   r   r]   r   r   r   r   r   r   r   r   r   r)   r)   r)   r+   r.      s*   
7   $    z-langchain_google_vertexai.VertexAIModelGardenc                   @  s   e Zd ZU dZdZded< dZded< ded< dZd	ed
< dZded< dZ	ded< e
dddddZeddddZeddddZddddddZd.dd	ddd d!d"d#Zd$d d%d&d'Zddd(d)d*Zd/dd	d+dd d!d,d-ZdS )0VertexAIModelGardenz-Vertex AI Model Garden large language models.Nz'PredictionServiceClient'r;   z'PredictionServiceAsyncClient'async_clientr$   endpoint_idrT   allowed_model_argsr2   
prompt_argZgenerated_textrH   
result_argr   rx   c                 C  s   z ddl m} ddlm}m} W n tk
r:   t  Y nX |d sLtd||d  dd}td	d
}|||d|d< |||d|d< |S )r   r   )ClientOptionsr   rI   zBA GCP project should be provided to run inference on Model Garden!rJ   z-aiplatform.googleapis.com)Zapi_endpointzvertex-ai-model-garden)module)client_optionsclient_infor;   r   )	Zgoogle.api_core.client_optionsr   google.cloud.aiplatform.gapicr   r   r   r   r   r   )rW   ry   r   r   r   r   r   r)   r)   r+   r     s,    
 
 
z(VertexAIModelGarden.validate_environmentrk   c                 C  s   | j j| j| j| jdS )N)rI   rJ   endpoint)r;   endpoint_pathrI   rJ   r   rm   r)   r)   r+   r     s
    z!VertexAIModelGarden.endpoint_pathc                 C  s   dS )NZvertexai_model_gardenr)   rm   r)   r)   r+   ro     s    zVertexAIModelGarden._llm_typer   r   zList['Value'])r   r6   r'   c                   s   zddl m ddlm  W n tk
r8   tdY nX g }|D ]:}jrdfdd| D }ni }||j< || qB fdd|D }|S )	Nr   )json_formatr   zIprotobuf package not found, please install it with `pip install protobuf`c                   s    i | ]\}}| j kr||qS r)   )r   rz   rm   r)   r+   r     s    
  z8VertexAIModelGarden._prepare_request.<locals>.<dictcomp>c                   s   g | ]} |  qS r)   )Z	ParseDict)r{   instance_dict)r    r   r)   r+   r     s    z8VertexAIModelGarden._prepare_request.<locals>.<listcomp>)	Zgoogle.protobufr   google.protobuf.struct_pb2r    r   r   r   r   r   )rn   r   r6   	instancesr2   instanceZpredict_instancesr)   )r    r   rn   r+   _prepare_request  s(    


z$VertexAIModelGarden._prepare_requestr0   r   r   c                 K  s*   | j |f|}| jj| j|d}| |S )*Run the LLM on the given prompt and input.r   r   )r   r;   r<   r   _parse_responsern   r   rU   r5   r6   r   r   r)   r)   r+   r     s    zVertexAIModelGarden._generatez'Prediction')predictionsr'   c                   s2   g }|j D ]}| fdd|D  q
t|dS )Nc                   s   g | ]}t  |d qS )r   )r   _parse_prediction)r{   
predictionrm   r)   r+   r     s   z7VertexAIModelGarden._parse_response.<locals>.<listcomp>r   )r   r   r   )rn   r   r   r   r)   rm   r+   r     s    

z#VertexAIModelGarden._parse_response)r   r'   c                 C  sv   t |tr|S | jrrz|| j W S  tk
rp   t |tr\d| j dt| d}t|nt| j dY nX |S )Nz+Provided non-None `result_arg` (result_arg=z). But got prediction of type zl instead of dict. Most probably, youneed to set `result_arg=None` during VertexAIModelGarden initialization.z key not found in prediction!)
isinstancer$   r   KeyErrortyper   )rn   r   Z
error_descr)   r)   r+   r     s    


z%VertexAIModelGarden._parse_predictionrC   c                   s0   | j |f|}| jj| j|dI dH }| |S )r   r   N)r   r   r<   r   r   r   r)   r)   r+   r     s     zVertexAIModelGarden._agenerate)NN)NN)rY   rZ   r[   r   r;   r\   r   r   r   r   r   r   r   r   ro   r   r   r   r   r   r)   r)   r)   r+   r     s,   
    r   )FFN)FN)9
__future__r   concurrent.futuresr   r   typingr   r   r   r   r	   r
   r   r   Zlangchain_core._api.deprecationr   Z langchain_core.callbacks.managerr   r   Z#langchain_core.language_models.llmsr   Zlangchain_core.outputsr   r   r   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   Z&langchain_community.utilities.vertexair   r   r   r   r   r   r   Zgoogle.cloud.aiplatform.modelsr   r   r    Z)vertexai.language_models._language_modelsr!   r"   r   r#   r   Zstream_completion_with_retryr,   r-   rB   rF   rG   r^   r.   r   r)   r)   r)   r+   <module>   sR   (     O :